How to parse Java code in Python using ANTLRv4

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/", line 14, in <module>
  File "/home/xxxxxxx/xxxxxxx/xxxxxxx/antlr/", line 9, in main
    tree = parser.compilationUnit()
  File "/home/xxxxxxx/xxxxxxx/xxxxxxx/antlr/", line 4182, in compilationUnit
    self.enterRule(localctx, 62, self.RULE_compilationUnit)
  File "/home/xxxxxxx/xxxxxxx/xxxxxxx/lib/python3.8/site-packages/antlr4/", line 374, in enterRule
    self._ctx.start = self._input.LT(1)
  File "/home/xxxxxxx/xxxxxxx/xxxxxxx/lib/python3.8/site-packages/antlr4/", line 62, in LT
  File "/home/xxxxxxx/xxxxxxx/xxxxxxx/lib/python3.8/site-packages/antlr4/", line 187, in lazyInit
  File "/home/xxxxxxx/xxxxxxx/xxxxxxx/lib/python3.8/site-packages/antlr4/", line 190, in setup
  File "/home/xxxxxxx/xxxxxxx/xxxxxxx/lib/python3.8/site-packages/antlr4/", line 112, in sync
    fetched = self.fetch(n)
  File "/home/xxxxxxx/xxxxxxx/xxxxxxx/lib/python3.8/site-packages/antlr4/", line 124, in fetch
    t = self.tokenSource.nextToken()
  File "/home/xxxxxxx/xxxxxxx/xxxxxxx/lib/python3.8/site-packages/antlr4/", 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 ?


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.