【问题标题】:Parsing out Abstract Syntax Tree of a VB code using antlr and python使用 antlr 和 python 解析 VB 代码的抽象语法树
【发布时间】:2021-06-10 07:33:25
【问题描述】:

我的代码(在 python 中):

lexer = VisualBasic6Lexer(antlr4.InputStream(code))
stream = antlr4.CommonTokenStream(lexer)
parser = VisualBasic6Parser(stream)
tree = parser.visitModule()  //This visitModule() is not the coreect method.
    print(tree)

我点击了这个链接: Parsing some Java code with Python using ANTLR

这里,他们用过的Java

tree = parser.compilationUnit()

在 VB 中,这个 compilationUnit() 的替代方法是什么

我使用的语法- https://github.com/antlr/grammars-v4/blob/master/vb6/VisualBasic6Lexer.g4 https://github.com/antlr/grammars-v4/blob/master/vb6/VisualBasic6Parser.g4

我的目标是使用 python 将 VB 代码的抽象语法树导出到 JSON 文件中。

查看此链接中的输出https://github.com/Sdccoding/proleap-vb6-parser

我希望我的输出采用这种美化的方式。

【问题讨论】:

标签: antlr abstract-syntax-tree


【解决方案1】:

通常,您需要访问的根节点位于解析器文件的开头。 另外,根节点的一个特点是它以 EOF 结尾。

对于你的情况,

tree = parser.startRule()

【讨论】:

  • 如何美化我得到的输出?我以这种方式开始:(startRule(module(moduleBody(moduleBodyElement(subStmt(visibility Private))我想要这个:(startRule(module(moduleBody(moduleBodyElement(subStmt
  • 您想要的树似乎不完整,顺便说一句,您的输入代码是什么?
  • 因为不允许长cmets,所以我给出了不完整的输出。我的意思是,我想看到美化的json(如树),但我得到的不是美化版本...它是这样写的 (startRule (module (moduleBody (moduleBodyElement (subStmt (visibility Private)))。
  • 导入json,然后print json.dumps(tree, sort_keys=True, indent=4)
  • 没有变化..它仍然只是一样。我希望我的输出是github.com/Sdccoding/proleap-vb6-parser,这个链接的输出(在树状结构中)。我得到的是纯字符串
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多