首页 > 编程语言 >使用ANTLR用Python解析一些Java代码

使用ANTLR用Python解析一些Java代码

时间:2022-09-26 17:35:26浏览次数:83  
标签:__ Java antlr ANTLR Java8Parser Python antlr4 java Java8Lexer

我想在Python中使用ANTLR构建一个Java解析器

我从ANTLR存储库下载了语法:

Lexer:https://github.com/antlr/grammars-v4/blob/master/java/java/JavaLexer.g4

分析器:https://github.com/antlr/grammars-v4/blob/master/java/java/JavaParser.g4

然后,我使用script.bat生成了我需要的python代码:

java -jar antlr-4.8-complete.jar -Dlanguage=Python3 Java8Lexer.g4
java -jar antlr-4.8-complete.jar -Dlanguage=Python3 Java8Parser.g4

antlr-4.8-complete.jar在这里下载:https://www.antlr.org/download/antlr-4.8-complete.jar

这将生成以下文件列表:

  • Java8Lexer.interp
  • Java8Lexer.py
  • Java8Lexer.tokens
  • Java8Parser.interp
  • Java8Parser.py
  • Java8Parser.tokens
  • Java8ParserListener.py

然后我编写了以下代码来解析java文件:

import antlr4
from antlr4 import *
from java.antlr_unit2 import Java8Parser, Java8Lexer

def main():
    code = open('test.txt', 'r').read()
    lexer = Java8Lexer.Java8Lexer(antlr4.InputStream(code))
    stream = antlr4.CommonTokenStream(lexer)
    parser = Java8Parser.Java8Parser(stream)
    tree = parser.expression()
    print (tree)

if __name__ == '__main__':
    main()

我的测试java代码test.txt如下所示:

package org.jabref.gui.fieldeditors;
import java.util.ArrayList;
/**
 * This class contains some code
 */
public class TextInputControlBehavior {

    private static final boolean SHOW_HANDLES = Properties.IS_TOUCH_SUPPORTED && !OS.OS_X;

}

由于这段代码太短,下面是我要分析的代码示例:https://pastebin.com/KNxfasKQ

当我运行此代码时,我得到以下信息:

line 1:0 extraneous input 'package' expecting {'boolean', 'byte', 'char', 'double', 'float', 'int', 'long', 'new', 'short', 'super', 'this', 'void', IntegerLiteral, FloatingPointLiteral, BooleanLiteral, CharacterLiteral, StringLiteral, 'null', '(', '!', '~', '++', '--', '+', '-', Identifier, '@'}
[]

我做错什么了吗?语法不是我写的,我只是从ANTLR repo那里拿的

编辑:帕维尔·斯米尔诺夫的回答帮助了我,现在我没有得到警告。但是现在这个程序看起来很慢,我得到了一个空的树作为输出

解决了:我正在打印tree,但我必须print(tree.toStringTree(recog=parser))

最后的代码是:

import antlr4
from antlr4 import *
from java.antlr_unit2 import Java8Parser, Java8Lexer

def main():
    code = open('test.txt', 'r').read()
    lexer = Java8Lexer.Java8Lexer(antlr4.InputStream(code))
    stream = antlr4.CommonTokenStream(lexer)
    parser = Java8Parser.Java8Parser(stream)
    tree = parser.compilationUnit()
    print(tree.toStringTree(recog=parser))

if __name__ == '__main__':
    main()

您的文本文件包含一个compilationUnit,而不是您试图用其进行解析的expression

tree = parser.expression()

仔细查看解析器规则,您需要的规则是

compilationUnit
    : packageDeclaration? importDeclaration* typeDeclaration* EOF
    ;

必须称之为

tree = parser.compilationUnit()

标签:__,Java,antlr,ANTLR,Java8Parser,Python,antlr4,java,Java8Lexer
From: https://www.cnblogs.com/hushaojun/p/16731689.html

相关文章