在Python中可视化ANTLR生成的Java代码AST

2024-05-13 22:20:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我在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?

Tags: thepathtreeparseriscodecanstatement
1条回答
网友
1楼 · 发布于 2024-05-13 22:20:30

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:

摘录:

class ErrorListener(antlr4.error.ErrorListener.ErrorListener):

    def __init__(self):
        super(ErrorListener, self).__init__()
        self.errored_out = False

    def syntaxError(self, recognizer, offendingSymbol, line, column, msg, e):
        self.errored_out = True


def sub():
    # Parse the input file
    input_stream = antlr4.FileStream("c.c")

    lexer = CLexer(input_stream)
    token_stream = antlr4.CommonTokenStream(lexer)

    parser = CParser(token_stream)


    errors = ErrorListener()
    parser.addErrorListener(errors)

当然,您可以替换自己的代码来收集并保留ErrorListener类扩展中的错误。关键之处在于,您需要实现自己的错误侦听器,然后使用addErrorlistener()来使用它(您可能还需要调用removeErrorListner()来删除可能只是写入sysout的默认侦听器)

相关问题 更多 >