let gen_code ocaml_file rep_desc =
  try
    let chanout = open_out ocaml_file in
    let fmt = Format.formatter_of_out_channel chanout in
    p fmt ("(** "^Rep_messages.generated_by^" *)\n\n");
    p fmt (rep_desc.rep_header^"\n");
    p fmt "open Report\n\n" ;
    p fmt ("let rec report "^(String.concat " " rep_desc.rep_params)^" = ({\n") ;
    p fmt "  rep_eles = [\n" ;
    List.iter 
      (fun e ->
        generate_structure fmt e ;
        p fmt ";\n"
      )
      rep_desc.rep_eles ;
    p fmt " ]\n} : int report)\n\n" ;
    p fmt
      ("let print_file ?(html=false) file "^(String.concat " " rep_desc.rep_params)^" =\n"^
       "  let r = report "^(String.concat " " rep_desc.rep_params)^" in\n"^
       "  Report.compute_file ~html: html file r\n");
    p fmt
      ("let print ?(html=false) fmt "^(String.concat " " rep_desc.rep_params)^" =\n"^
       "  let r = report "^(String.concat " " rep_desc.rep_params)^" in\n"^
       "  Report.compute ~html: html fmt r\n");
    flush chanout ;
    close_out chanout
  with
    Sys_error s ->
      prerr_endline s ;
      exit 1