Is it possible the get the AST for an OCaml program?
Asked Answered
T

4

6

I'd like to be able to get the AST for a given OCaml program (I'd like to walk the AST and generate an instrumented version of the code or do some kind of transformation, for example). Do any of the OCaml tools support this functionality?

Talented answered 29/6, 2010 at 20:50 Comment(1)
I think the accepted answer is outdated (camlp4 is not maintained since 2017); I would accept one of the last two answers mentioning ppxs or compiler-libs.Tye
E
4

Since OCaml 4.02.1 it is possible to use the PPX tools written bu Alain Frisch to precisely do this. Example:

% ocamlfind ppx_tools/dumpast -e "1 + 2"
1 + 2
==>
{pexp_desc =
  Pexp_apply ({pexp_desc = Pexp_ident {txt = Lident "+"}},
   [("", {pexp_desc = Pexp_constant (Const_int 1)});
    ("", {pexp_desc = Pexp_constant (Const_int 2)})])}
=========

It is possible to use this program to dump the AST of a normal code file as well, and various options control the degree of precision of the dump. In the example above, for instance, the location parameters of the AST are hidden.

Enwreathe answered 11/3, 2016 at 18:30 Comment(0)
L
4

camlp4 is a way to go. Here is a motivating example. The docs are sparse - true, but one can make his way reading through wiki, existing examples, tutorials, and maybe even camlp4 sources.

Leanora answered 30/6, 2010 at 7:22 Comment(3)
What does that motivating example do?Talented
Insert calls co Camlp4prof.count (read "arbitrary") function at the beginning of each parsed function definition passing name and location parameters.Leanora
An Active discussion on resources to learn camlp4: groups.google.com/group/fa.caml/browse_thread/thread/…Ess
E
4

Since OCaml 4.02.1 it is possible to use the PPX tools written bu Alain Frisch to precisely do this. Example:

% ocamlfind ppx_tools/dumpast -e "1 + 2"
1 + 2
==>
{pexp_desc =
  Pexp_apply ({pexp_desc = Pexp_ident {txt = Lident "+"}},
   [("", {pexp_desc = Pexp_constant (Const_int 1)});
    ("", {pexp_desc = Pexp_constant (Const_int 2)})])}
=========

It is possible to use this program to dump the AST of a normal code file as well, and various options control the degree of precision of the dump. In the example above, for instance, the location parameters of the AST are hidden.

Enwreathe answered 11/3, 2016 at 18:30 Comment(0)
S
2

What you're looking for is [camlp4][1]. I haven't used camlp4 before, so I can't attest to it's virtues as software. I have heard of people using camlp5 [http://pauillac.inria.fr/~ddr/camlp5/] which, according to wikipedia, has better documentation than the current version of camlp4.

Sexless answered 30/6, 2010 at 2:55 Comment(1)
I thought camlp4 was used to modify & customize the OCaml syntax, but I suppose it makes sense that it can get to the AST somehow.Talented
S
2

You can use compiler-libs to achieve this. See Parsetree, Asttypes, and Ast_helper.

Stralka answered 24/2, 2017 at 19:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.