I am using Camlp5's extensible grammars to parse a language
of my own (no relation at all with Ocaml's). I have some questions
that the manual doesn't seem to address and I was wondering how
to do:
1. When I load my grammar in a suitable Ocaml toplevel, I get
wonderful error messages for the parser, but when I am running
my compiler stand alone, I only get an exception. Is there a
way to recover the error messages that I get when running under
the toplevel for my use?
2. What is the simplest way of replacing (not extending) Ocaml's
grammar using Camlp5 ? Or equivalently, if I have a parser that
generates Ocaml AST using q_MLast, how do I do to feed that to
the Ocaml compiler?
Thanks!
N
_______________________________________________
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
On Wed, Aug 11, 2010 at 07:45:26PM -0400, Nicolas Ojeda Bar wrote:
> 1. When I load my grammar in a suitable Ocaml toplevel, I get
> wonderful error messages for the parser, but when I am running
> my compiler stand alone, I only get an exception. Is there a
> way to recover the error messages that I get when running under
> the toplevel for my use?
When a Camlp5 grammar entry fails, it raises an exception defined in
the Camlp5 module Ploc. This exception encloses the location and the
real exception. Do:
try Grammar.Entry.parse your_entry with
Ploc (loc, exn) ->
... print the location 'loc' of the error if you want using
the module Ploc
... print the real exception 'exn' using the OCaml module Printexc
or raises it to make the OCaml runtime display it
> 2. What is the simplest way of replacing (not extending) Ocaml's
> grammar using Camlp5 ? Or equivalently, if I have a parser that
> generates Ocaml AST using q_MLast, how do I do to feed that to
> the Ocaml compiler?
If your grammar is in the file 'foo.ml', compile it with ocamlc. It
produces the file 'foo.cmo'. And to compile a file 'bar.ml' using your
grammar, do:
ocamlc -pp 'camlp5 ./foo.cmo pr_dump.cmo' bar.ml
--
Daniel de Rauglaudre
http://pauillac.inria.fr/~ddr/
> 2. What is the simplest way of replacing (not extending) Ocaml's
> grammar using Camlp5 ? Or equivalently, if I have a parser that
> generates Ocaml AST using q_MLast, how do I do to feed that to
> the Ocaml compiler?
I added a (small) chapter about how to make one's syntax for OCaml
using Camlp5. This chapter will be in next version (5.16) but you
can see it there:
http://pauillac.inria.fr/~ddr/camlp5/doc/html/redef.html
For the current Camlp5 version (5.15), this part works except that
you have to compile the file "mysyntax.ml" (of that chapter) by:
ocamlc -pp "camlp5r pa_extend.cmo q_MLast.cmo" -I $(camlp5 -where) -c mysyntax.ml
--
Daniel de Rauglaudre
http://pauillac.inria.fr/~ddr/
_______________________________________________