Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Caml-list] Parsing simple type expressions

11 views
Skip to first unread message

Paolo Donadeo

unread,
Jan 6, 2009, 9:04:00 AM1/6/09
to OCaml mailing list
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

_______________________________________________
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

David Allsopp

unread,
Jan 6, 2009, 10:12:54 AM1/6/09
to Paolo Donadeo, OCaml mailing list
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.


David

Jake Donham

unread,
Jan 6, 2009, 1:19:11 PM1/6/09
to Paolo Donadeo, OCaml mailing list
On Tue, Jan 6, 2009 at 6:03 AM, Paolo Donadeo <p.do...@gmail.com> wrote:

> For a serializer I'm writing I need to parse simple OCaml type
> expressions composed by OCaml basic types, tuples, options and lists.


This is pretty easy with Camlp4, although as you say there isn't much in the
docs to point the way. You might take a look at how orpc does it--see the
parse_type function in

http://code.google.com/p/orpc2/source/browse/trunk/src/generator/parse.ml

(I'm going to cover using Camlp4 in this way on the blog I posted yesterday,
but not for another week or two.)

You might also look at some other Camlp4-based serialization tools, like
bin-prot/type-conv at

http://www.ocaml.info/home/ocaml_sources.html

Jake

Paolo Donadeo

unread,
Jan 6, 2009, 4:00:22 PM1/6/09
to OCaml mailing list
> This is pretty easy with Camlp4, although as you say there isn't much in the
> docs to point the way. You might take a look at how orpc does it--see the
> parse_type function

Thanks for this pointer!

Martin Jambon

unread,
Jan 6, 2009, 4:11:27 PM1/6/09
to David Allsopp, OCaml mailing list, Paolo Donadeo
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

--
http://mjambon.com/

Paolo Donadeo

unread,
Jan 7, 2009, 5:50:52 PM1/7/09
to OCaml mailing list
Thanks, Matthieu and Jake, this is exactly what I had in mind.

At the end, Camlp4 was the right solution to my simple problem, as I
suspected from the beginning. Camlp4 is an extremely powerful tool and
it's a pity it couldn't be used by everyone for lack of documentation.
What is really needed, in my opinion, is a description of *what* one
can do with Camlp4 without being an expert. For example a list of all
syntax extension available out of the box might spread the use of
Camlp4 among common programmers.

Searching in the source code I can see things named:
Camlp4ExceptionTracer, Camlp4FoldGenerator, Camlp4MetaGenerator, and
so on. The names sounds great but... what is it? The source code is
cryptic to every "regular" OCaml programmer like me. The same
considerations apply to all the (many!) libraries available and
related to Camlp4.

What is needed to spread the use of this tool are some... recipes,
like those available for ocamlbuild in its wiki pages.


--
Paolo
~
~
:wq

_______________________________________________

0 new messages