我可以获取Wolfram语言表达的AST吗?

时间:2018-12-19 14:21:43

标签: wolfram-mathematica abstract-syntax-tree wolfram-language

在Mathematica中,我们使用FullFormTreeFormDeveloper'WriteExpressionJSONString来获取给定表达式的语法详细信息。如何为任何表达式获取完整的AST(抽象语法树)?例如,是否有任何函数toAST如此

toAST["a +b c\nSin[%];"]

将给出如下结果:

{
  {
    Plus,
    0,
    6,
    {a, 0, 1},
    {Multiply, 3, 6, {b, 3, 4}, {c, 5, 6}}
  }, 
  {
    CompoundExpression,
    7,
    14,
    {Sin, 7, 13, {Out, 11, 12}},
    {Null, 14, 14}
  }
}

1 个答案:

答案 0 :(得分:0)

最好的自动取款机可能是codeparser。它会与下一个版本的Mathematica捆绑在一起,但是您可以使用PacletInstall["CodeParser"]进行安装。

您要使用的功能(即AST)为CodeParse。 (您可以使用CodeConcreteParse获得CST。)文档似乎有点稀缺。

Needs["CodeParser`"];
ast = CodeParse[parseStr]

(* Output *)
ContainerNode[String, {CallNode[
   LeafNode[Symbol, 
    "Plus", <||>], {LeafNode[Symbol, 
     "a", <|Source -> {{1, 1}, {1, 2}}|>], 
    CallNode[
     LeafNode[Symbol, 
      "Times", <||>], {LeafNode[Symbol, 
       "b", <|Source -> {{1, 4}, {1, 5}}|>], 
      LeafNode[Symbol, 
       "c", <|Source -> {{1, 6}, {1, 7}}|>]}, <|Source -> {{1, 4}, {1,
          7}}|>]}, <|Source -> {{1, 1}, {1, 7}}|>], 
  CallNode[LeafNode[Symbol, 
    "CompoundExpression", <||>], {CallNode[
     LeafNode[Symbol, 
      "Sin", <|Source -> {{2, 1}, {2, 4}}|>], {CallNode[
       LeafNode[Symbol, 
        "Out", <||>], {}, <|Source -> {{2, 5}, {2, 
           6}}|>]}, <|Source -> {{2, 1}, {2, 7}}|>], 
    LeafNode[Symbol, 
     "Null", <|Source -> {{2, 8}, {2, 8}}|>]}, <|Source -> {{2, 
       1}, {2, 8}}|>]}, <||>]

您可以使用您提到的Developer`WriteExpressionJSONStringExportString[ast, "ExpressionJSON"]来获得与您想要的输出非常接近的输出,尽管更为冗长(所以我将其压缩在这里):

ExportString[ast[[2;;]], "ExpressionJSON", Compact -> 3]

(* Output *)
[
    "ContainerNode",
    [
        "List",
        [
            "CallNode",
            ["LeafNode","Symbol","'Plus'",["Association"]],
            ["List",["LeafNode","Symbol","'a'",["Association",["Rule","Source",["List",["List",1,1],["List",1,2]]]]],["CallNode",["LeafNode","Symbol","'Times'",["Association"]],["List",["LeafNode","Symbol","'b'",["Association",["Rule","Source",["List",["List",1,4],["List",1,5]]]]],["LeafNode","Symbol","'c'",["Association",["Rule","Source",["List",["List",1,6],["List",1,7]]]]]],["Association",["Rule","Source",["List",["List",1,4],["List",1,7]]]]]],
            ["Association",["Rule","Source",["List",["List",1,1],["List",1,7]]]]
        ],
        [
            "CallNode",
            ["LeafNode","Symbol","'CompoundExpression'",["Association"]],
            ["List",["CallNode",["LeafNode","Symbol","'Sin'",["Association",["Rule","Source",["List",["List",2,1],["List",2,4]]]]],["List",["CallNode",["LeafNode","Symbol","'Out'",["Association"]],["List"],["Association",["Rule","Source",["List",["List",2,5],["List",2,6]]]]]],["Association",["Rule","Source",["List",["List",2,1],["List",2,7]]]]],["LeafNode","Symbol","'Null'",["Association",["Rule","Source",["List",["List",2,8],["List",2,8]]]]]],
            ["Association",["Rule","Source",["List",["List",2,1],["List",2,8]]]]
        ]
    ],
    [
        "Association"
    ]
]