Parsing an OCaml File with OCaml

I want to parse OCaml files (.ml) using OCaml. I want to split files into abstract syntax trees for analysis. I tried using camlp4 but no luck. Has anyone else successfully done this before? Is this the best way to parse an OCaml file?

+4
source share
2 answers

(I suppose you already know the main parts of OCaml: how to write OCaml code, how to link modules and libraries, write assembly scripts, etc. If you do not, study them first.)

The best way is to use the genuine OCaml code analyzer used by the OCaml compiler, as it is 100% compatible by definition.

CamlP4 OCaml, , : .

, .ml , P4. : P4, .

OCaml, - compam-libs.common OCamlFind. OCaml.

driver/compile.ml OCaml, : , , , . .ml, ( ) Compile.implementation. .mli Compile.interface.

.

+5

-dparsetree ocaml?

hello.ml:

let _ = print_endline "Hello AST"

:

$ ocamlc -dparsetree hello.ml

:

[
  structure_item (hello.ml[1,0+0]..[1,0+33])
    Pstr_eval
    expression (hello.ml[1,0+8]..[1,0+33])
      Pexp_apply
      expression (hello.ml[1,0+8]..[1,0+21])
        Pexp_ident "print_endline" (hello.ml[1,0+8]..[1,0+21])
      [
        <label> ""
          expression (hello.ml[1,0+22]..[1,0+33])
            Pexp_constant Const_string("Hello AST",None)
      ]
]

. -ppx , ( OCaml 4,02). .

+2

Source: https://habr.com/ru/post/1537602/


All Articles