代码之家  ›  专栏  ›  技术社区  ›  aneccodeal

是否可以为OCAML程序获取AST?

  •  5
  • aneccodeal  · 技术社区  · 16 年前

    我希望能够为给定的ocaml程序获得ast(例如,我希望遍历ast并生成代码的检测版本或进行某种转换)。任何ocaml工具都支持这个功能吗?

    4 回复  |  直到 9 年前
        1
  •  4
  •   ygrek    16 年前

    camlp4 是一种方式。这里是一个 motivating example . 这些文档是稀疏的-真的,但是人们可以通过wiki,现有的例子来阅读。 tutorials 甚至可能是camlp4来源。

        2
  •  2
  •   Axle    16 年前

    你要找的是[camlp4][1]。我以前没有使用过camlp4,所以我不能证明它作为软件的优点。我听说过有人使用camlp5[ http://pauillac.inria.fr/~ddr/camlp5/]. which,according to wikipedia,has better documentation than the current version of camlp4.

    重新。我听说过有人使用Camlp5[ http://pauillac.inria.fr/~ddr/camlp5/] 根据维基百科的说法,它有比当前版本的camlp4更好的文档。

        3
  •  1
  •   Michaël Le Barbier    10 年前

    由于OCAML 4.02.1,可以使用 PPX tools written bu Alain Frisch 准确地做到这一点。例子:

    % 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)})])}
    =========
    

    也可以使用该程序来转储普通代码文件的AST,并且各种选项控制转储的精度。例如,在上面的示例中,AST的位置参数是隐藏的。

        4
  •  0
  •   tekknolagi    9 年前

    你可以使用 compiler-libs 为了达到这个目的。见 Parsetree , Asttypes 和 Ast_helper .

    推荐文章