2

在 Mathematica 中,我们使用FullFormorTreeFormDeveloper'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}
  }
}
4

1 回答 1

1

可能您最好的选择 ATM 是codeparser。它将与 Mathematica 的下一版本捆绑在一起,但您现在可以使用PacletInstall["CodeParser"]它来安装它。

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

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`WriteExpressionJSONString你提到的或者ExportString[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"
    ]
]
于 2020-10-25T19:59:27.937 回答