Reputation: 179
I'm currently trying to translate OCaml programs (with a fairly standard/limited grammar) into Racket, and I'm trying to see if there is a way to do the parsing to intermediate representation using camlp4. I tried to build a lexer and parser using ocamlyacc and ocamllex but considering how large the grammar can be, it became quite complicated. So, I searched around and found camlp4 to have some of this already built-in, but I can't seem to look up how to get the AST of some OCaml code using it. Any documentation/examples/ideas? Also, if you have any suggestions on how to do this better, that'd be great as well! Thanks.
Upvotes: 1
Views: 145
Reputation: 3739
Just use compiler-libs, which is distributed with the compiler. You can use the ocaml parser itself directly that way.
Here is an example of code that reads a .ml file. The documentation for the parser is quite decent. You will obtain, after parsing, a Parsetree.
Upvotes: 4