-2

I'm trying to parse some Java code in Python using ANTLRv4. I've tried to follow this post, but I get the following error:

 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'

I can't figure out what I'm doing wrong. The file I'm trying to parse is proper Java, it's extracted from the docker-maven-plugin package. I've tried with other files, but I get the same error.

Any idea ?

asked Oct 19, 2021 at 13:14
6
  • AttributeError: 'LexerATNSimulator' object has no attribute 'line_number' Commented Oct 19, 2021 at 13:30
  • Which version of antlr4 do you use to create the parser? Which version of the antlr4-python3-runtime do you use to run the generated parser? Commented Oct 19, 2021 at 13:39
  • How did you install the antlr4-python3-runtime? I couldn't find the string line_number in the antlr4-python3-runtime packages that I inspected (downloaded from pypi, versions 4.9.2 and 4.8) Commented Oct 19, 2021 at 21:12
  • Note that this is not a problem with the java code that you are trying to parse - this is a problem with the antlr4 runtime where one class of the antlr4 runtime (antlr4.Lexer) tries to access an attribute in another class of the antlr4 runtime (antlr4.atn.LexerATNSimulator) that doesn't exist. Commented Oct 19, 2021 at 21:14
  • @ThomasKläger I'm using antlr 4.9.2 and antlr4-python3-runtime 4.9.2 downloaded using pip install. If you have any working duo of versions, I can try it. Commented Oct 20, 2021 at 7:19

1 Answer 1

0

Actually it was just a problem of violent refactorization... I've changed line to line_number in my code, and it actually changed it in librairies too. Changing it back to line cleared the problem.

Thanks to @Thomas Kläger for making me realize it.

answered Oct 25, 2021 at 9:03
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.