【发布时间】: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 Python 运行时中的默认打印例程不能很好地打印缩进。您将不得不编写树的输出例程、基于堆栈或递归方法。有关示例,请参阅github.com/Sdccoding/proleap-vb6-parser/blob/master/src/test/…。如果你写一个,写第二个方法来输出json。
标签: antlr abstract-syntax-tree