Skip to content
Advertisement

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/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 ?

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement