我在python中使用ANTLR构建Java解析器。下面是我用来解析JAVA代码的主要代码
def ASTconversion(file_path):
code = open(file_path, 'r').read()
lexer = JavaLexer(antlr4.InputStream(code))
stream = antlr4.CommonTokenStream(lexer)
parser = JavaParser(stream)
tree = parser.compilationUnit()
is_syntax_errors = tree.parser._syntaxErrors #Binary
return tree.toStringTree(recog=parser),is_syntax_errors
ast, is_syntax_errors = ASTconversion(code_path)
print(ast)
下面包含上述代码段的输出
(compilationUnit (typeDeclaration (classOrInterfaceModifier public) (classDeclaration class Number (classBody { (classBodyDeclaration (block { (blockStatement (statement (expression (expression (expression (primary System)) . out) . (methodCall println ( (expressionList (expression (primary (literal "Printing Numbers")))) ))) ;)) (blockStatement (statement for ( (forControl (forInit (localVariableDeclaration (typeType (primitiveType int)) (variableDeclarators (variableDeclarator (variableDeclaratorId i) = (variableInitializer (expression (primary (literal (integerLiteral 1))))))))) ; (expression (expression (primary i)) <= (expression (primary (literal (integerLiteral 10))))) ; (expressionList (expression (expression (primary i)) ++))) ) (statement (block { (blockStatement (statement (expression (expression (expression (primary System)) . out) . (methodCall println ( (expressionList (expression (primary i))) ))) ;)) })))) })) }))) )
基于这个输出,我有两个问题要问
1. How I can visualize this parser output as a Graphical AST?
2. If the code contains any syntax error I can find that as in the code. But How can I track the syntax error?
Python目标似乎不具备从Python代码中生成gun解析树视图的能力。这很有意义,因为这是Java运行时中的Java应用程序
如果遵循ANTLR Quick start,应该安装
grun
命令,并且(假设语法中没有特定于Python的语义谓词、头等),可以使用-gui
选项和grun
查看解析树的图形表示此外,Visual Studio代码和IntelliJ ANTLR插件都使您能够根据语法测试输入,并在IDE(VS Code plugin)(IntelliJ plugin)中查看解析树的可视化
为了从解析中恢复错误,您可能应该查看Python运行时的ctest.py测试。它将向您展示如何实现自己的errorListener:
摘录:
当然,您可以替换自己的代码来收集并保留
ErrorListener
类扩展中的错误。关键之处在于,您需要实现自己的错误侦听器,然后使用addErrorlistener()
来使用它(您可能还需要调用removeErrorListner()
来删除可能只是写入sysout的默认侦听器)相关问题 更多 >
编程相关推荐