如何使用 ANTLRv4 解析 Python 中的 Java 代码
How to parse Java code in Python using ANTLRv4
我正在尝试使用 ANTLRv4 解析 Python 中的一些 Java 代码。我尝试遵循 ,但出现以下错误:
File "/home/xxxxxxx/xxxxxxx/xxxxxxx/antlr/proto_antlr.py", line 14, in <module>
main()
File "/home/xxxxxxx/xxxxxxx/xxxxxxx/antlr/proto_antlr.py", line 9, in main
tree = parser.compilationUnit()
File "/home/xxxxxxx/xxxxxxx/xxxxxxx/antlr/Java8Parser.py", line 4182, in compilationUnit
self.enterRule(localctx, 62, self.RULE_compilationUnit)
File "/home/xxxxxxx/xxxxxxx/xxxxxxx/lib/python3.8/site-packages/antlr4/Parser.py", line 374, in enterRule
self._ctx.start = self._input.LT(1)
File "/home/xxxxxxx/xxxxxxx/xxxxxxx/lib/python3.8/site-packages/antlr4/CommonTokenStream.py", line 62, in LT
self.lazyInit()
File "/home/xxxxxxx/xxxxxxx/xxxxxxx/lib/python3.8/site-packages/antlr4/BufferedTokenStream.py", line 187, in lazyInit
self.setup()
File "/home/xxxxxxx/xxxxxxx/xxxxxxx/lib/python3.8/site-packages/antlr4/BufferedTokenStream.py", line 190, in setup
self.sync(0)
File "/home/xxxxxxx/xxxxxxx/xxxxxxx/lib/python3.8/site-packages/antlr4/BufferedTokenStream.py", line 112, in sync
fetched = self.fetch(n)
File "/home/xxxxxxx/xxxxxxx/xxxxxxx/lib/python3.8/site-packages/antlr4/BufferedTokenStream.py", line 124, in fetch
t = self.tokenSource.nextToken()
File "/home/xxxxxxx/xxxxxxx/xxxxxxx/lib/python3.8/site-packages/antlr4/Lexer.py", line 130, in nextToken
self._tokenStartLine = self._interp.line_number
AttributeError: 'LexerATNSimulator' object has no attribute 'line_number'
我不知道我做错了什么。我要解析的文件是 proper Java, it's extracted from the docker-maven-plugin 包。我试过其他文件,但我得到了同样的错误。
有什么想法吗?
实际上这只是一个暴力重构的问题......我已经在我的代码中将行更改为line_number,它实际上也在库中更改了它。将其改回 line
即可解决问题。
感谢@Thomas Kläger 让我意识到这一点。
我正在尝试使用 ANTLRv4 解析 Python 中的一些 Java 代码。我尝试遵循
File "/home/xxxxxxx/xxxxxxx/xxxxxxx/antlr/proto_antlr.py", line 14, in <module>
main()
File "/home/xxxxxxx/xxxxxxx/xxxxxxx/antlr/proto_antlr.py", line 9, in main
tree = parser.compilationUnit()
File "/home/xxxxxxx/xxxxxxx/xxxxxxx/antlr/Java8Parser.py", line 4182, in compilationUnit
self.enterRule(localctx, 62, self.RULE_compilationUnit)
File "/home/xxxxxxx/xxxxxxx/xxxxxxx/lib/python3.8/site-packages/antlr4/Parser.py", line 374, in enterRule
self._ctx.start = self._input.LT(1)
File "/home/xxxxxxx/xxxxxxx/xxxxxxx/lib/python3.8/site-packages/antlr4/CommonTokenStream.py", line 62, in LT
self.lazyInit()
File "/home/xxxxxxx/xxxxxxx/xxxxxxx/lib/python3.8/site-packages/antlr4/BufferedTokenStream.py", line 187, in lazyInit
self.setup()
File "/home/xxxxxxx/xxxxxxx/xxxxxxx/lib/python3.8/site-packages/antlr4/BufferedTokenStream.py", line 190, in setup
self.sync(0)
File "/home/xxxxxxx/xxxxxxx/xxxxxxx/lib/python3.8/site-packages/antlr4/BufferedTokenStream.py", line 112, in sync
fetched = self.fetch(n)
File "/home/xxxxxxx/xxxxxxx/xxxxxxx/lib/python3.8/site-packages/antlr4/BufferedTokenStream.py", line 124, in fetch
t = self.tokenSource.nextToken()
File "/home/xxxxxxx/xxxxxxx/xxxxxxx/lib/python3.8/site-packages/antlr4/Lexer.py", line 130, in nextToken
self._tokenStartLine = self._interp.line_number
AttributeError: 'LexerATNSimulator' object has no attribute 'line_number'
我不知道我做错了什么。我要解析的文件是 proper Java, it's extracted from the docker-maven-plugin 包。我试过其他文件,但我得到了同样的错误。
有什么想法吗?
实际上这只是一个暴力重构的问题......我已经在我的代码中将行更改为line_number,它实际上也在库中更改了它。将其改回 line
即可解决问题。
感谢@Thomas Kläger 让我意识到这一点。