Camlp4 provides different things :
1. a general library to implement grammars and grammar extensions/modifications
2. parsers, using that library, for the OCaml syntax (actually, syntaxes)
3. plumbing to get the parsed OCaml AST to the various OCaml compiler tools
4. tools to meta-generate useful transformation passes on big algebraic datatypes (used in the lib on OCaml ASTs, can be used to implement further syntactic transformation passes, or on your own AST for another language/structure)

You can reuse 1. and 4. to write parsers and transformation on any language you would like to parse, not just OCaml. There is a small example of such use on the Camlp4 wiki :
  http://brion.inria.fr/gallium/index.php/Full_parser_tutorial
You may also be interested in Jake Donham's post on using Camlp4 for parsing:
  http://ambassadortothecomputers.blogspot.com/2010/05/reading-camlp4-part-6-parsing.html

If you want to see a bigger example, you can look into the Camlp4 distribution for the OCaml parser implemented using Camlp4 facilities : it's in
  Camlp4Parsers/Camlp4OCamlRevisedParser.ml (it's a parser for the revised syntax; the standard syntax is defined on top of it, as a syntax extension)

I have myself used Camlp4 tools for parsing in the past and have found them quite satisfying. Once you're used to all those uppercase, the parser code is actually rather clean and readable. On my use cases, the resulting parser was shorter than the corresponding code using a combination of ocamllex + ocamlyacc / menhir. It is also much nicer than using stream parsers directly, as it provides facilities that are more powerful than recursive descent without lookahead. The problem with Camlp4 is that its parsing behavior is not as clearly defined (it mostly behaves as a recursive descent parser, except on the nice macros that you can use, or when it does automagic factorisation, but you're not exactly sure what the differences are) and for that reason it's quite hard to debug. That said, yacc parsers also are a pain to debug (though menhir is much nicer in that regard). I have no experience with fancier GLR parsers such as dypgen.

I also quite like the Camlp4MapGenerator / Camlp4FoldGenerator. They make simple transforms on big ASTs very simple to use, using object-oriented programing, the visitor pattern and nice default behavior. That's actually my top one example of usefulness of OOP's implementation inheritance. Finally, Camlp4 quotation system is also very nice, but it's more aimed at integrating other language/syntaxes inside an OCaml code.


On Thu, Mar 31, 2011 at 6:14 PM, Jean-Baptiste Jeannin <jeannin@cs.cornell.edu> wrote:
Hello,

We are trying to use camlp4 to replace the front-end (lexer / parser up to building the Abstract Syntax Tree) of an experimental interpreter for a language close to OCaml. However we would like to keep our backend (the interpreter itself), as it behaves very differently from a normal interpreter, and this behavior cannot be translated into OCaml's code easily (or even at all).

However it seems that using camlp4, we can only use OCaml's own backend. In the camlp4 documentation, I could not find any way of getting the abstract syntax tree to feed it to another interpreter. Is this true or could camlp4 serve our purpose?

Thank you,

Jean-Baptiste Jeannin
CS department, Cornell University

--
Caml-list mailing list.  Subscription management and archives:
https://sympa-roc.inria.fr/wws/info/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs