podington
podington

Reputation: 179

How do I use camlp4 in an OCaml program to parse another OCaml program into an AST?

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

Answers (1)

Drup
Drup

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

Related Questions