我有一组Java源代码,我需要修改这些.java文件(删除空格,注释等)。为此,我从this存储库下载了Java词法分析器和解析器文件,并进行了编译使用antlr-4.7.2-complete.jar。我还使用pip安装了antlr4-python3-runtime。
我尝试使用下面的代码在示例HelloWorld程序中删除多行注释,但得到了以下回溯。我该如何解决这个问题?
用于编译词法分析器和解析器:
java -jar [path_to_antlr-4.7.2-complete.jar] -Dlanguage=Python3 [path_to_lexer_file]
java -jar [path_to_antlr-4.7.2-complete.jar] -Dlanguage=Python3 [path_to_parser_file]
示例Java文件:
public class HelloWorld {
public static void main(String[] args){
/*
System.out.println("Hello World");
*/
}
}
用于更改文件的Python代码:
source = open("./HelloWorld.java", "r")
codeStream = InputStream(source.read())
lexer = JavaLexer.JavaLexer(codeStream)
token_stream = CommonTokenStream(lexer)
token_stream.fill()
rewriter = TokenStreamRewriter.TokenStreamRewriter(token_stream)
for token in token_stream.tokens:
if token.type == JavaLexer.JavaLexer.COMMENT:
rewriter.deleteToken(token)
Traceback (most recent call last):
File "/home/alp/PycharmProjects/JavaParsingTutorial/parser.py", line 31, in <module>
rewriter.deleteToken(token)
File "/usr/local/lib/python3.6/dist-packages/antlr4/TokenStreamRewriter.py", line 80, in deleteToken
self.delete(self.DEFAULT_PROGRAM_NAME, token, token)
File "/usr/local/lib/python3.6/dist-packages/antlr4/TokenStreamRewriter.py", line 88, in delete
self.replace(program_name, from_idx, to_idx, None)
File "/usr/local/lib/python3.6/dist-packages/antlr4/TokenStreamRewriter.py", line 71, in replace
if any((from_idx > to_idx, from_idx < 0, to_idx < 0, to_idx >= len(self.tokens.tokens))):
TypeError: '>' not supported between instances of 'CommonToken' and 'CommonToken'