Dear caml-list, here you can find another way of doing what Paolo wants to do. Basically the point was to reuse as much (well-tested) code as I could, so I tried to use Camlp4 to parse a type definition, and work from there. You can find in Common the definition of the AST that Paolo provided us with, along with a string_of function and a parse_type function overidden at runtime. In Pa_exp_ext a syntax extension extends the OCaml standard syntax with a rule that parses a type and End Of Input, and in Pa_exp is the driver that loads: - Camlp4OCamlRevisedParser the revised syntax parser, - Camlp4OCamlParser the "normal" syntax parser, - and "pa_exp_ext" our parser. Actually, Pa_exp acts as the standard "camlp4" executable, loading parsers and stuff. Please note that this example is incomplete because Pa_exp_ext.Make.convert_type does not take into account the syntax of applied types, which are tranformed using the function "ident_of_ctyp" from Camlp4Ast.mlast which I did not have time to code. However it is my belief that these files provide a (very interesting) complete example of how camlp4 can be used to parse parts of OCaml syntax without having to write the syntax rules over and over, while using the mysterious quotations :-) Having spent a couple of hours on this, I hope this helps anyone that is having a look at camlp4 to do what they would like to do, Cheers and la multi ani "happy new year", Matei ----- Message d'origine ---- > De : Martin Jambon > À : David Allsopp > Cc : OCaml mailing list ; Paolo Donadeo > Envoyé le : Mardi, 6 Janvier 2009, 23h10mn 05s > Objet : Re: [Caml-list] Parsing simple type expressions > > David Allsopp wrote: > > ocamlyacc - you can get most of it for free out of parsing/parser.mly in the > OCaml sources... the section on type expressions starts at line 1144 for OCaml > 3.11.0. > > Our json-wheel library is a complete example: > > http://martin.jambon.free.fr/json-wheel.html > > > Martin > > >> -----Original Message----- > >> From: caml-list-bounces@yquem.inria.fr [mailto:caml-list- > >> bounces@yquem.inria.fr] On Behalf Of Paolo Donadeo > >> Sent: 06 January 2009 14:04 > >> To: OCaml mailing list > >> Subject: [Caml-list] Parsing simple type expressions > >> > >> For a serializer I'm writing I need to parse simple OCaml type > >> expressions composed by OCaml basic types, tuples, options and lists. > >> Given a string like "(int * string option) list" and this type: > >> > >> type types = > >> | Int > >> | String > >> | Float > >> | Char > >> | Bool > >> | Option of types > >> | List of types > >> | Tuple of types list > >> > >> the function I need should return something like List (Tuple ([Int; > >> Option(String)])) > >> > >> Before starting with low level sscanf functions I looked at the Genlex > >> module, but it wasn't so inspiring. Then I tried with Camlp4 but the > >> documentation doesn't really shine :-) > >> > >> So is there a simple way to write this function using some standard > >> module? > >> > >> TIA, > >> > >> > >> -- > >> Paolo > >> ~ > >> ~ > >> :wq > > -- > http://mjambon.com/ > > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs