0

Trying to setup ANTLR for python following this instructions http://blog.anvard.org/articles/2016/03/15/antlr-python.html

and additionally using a local copy of antlr-4.7.2-complete.jar

there are syntax errors.

With python2 this:

> from antlr4 import *
  File "/usr/local/lib/python2.7/dist-packages/antlr4/__init__.py", 
line     1, in <module>
    from antlr4.Token import Token
  File "/usr/local/lib/python2.7/dist-packages/antlr4/Token.py", line 59
    def text(self, text:str):
                       ^

With python3 this:

> Traceback (most recent call last):
  File "test_Hello.py", line 1, in <module>
    from antlr4 import *
  File "/usr/local/lib/python3.6/dist-packages/antlr4/__init__.py", 
   line     5, in <module>
    from antlr4.BufferedTokenStream import TokenStream
  File "/usr/local/lib/python3.6/dist-packages/antlr4    
    /BufferedTokenStream.py", line 18, in <module>
    from antlr4.error.Errors import IllegalStateException
  File "/usr/local/lib/python3.6/dist-packages/antlr4/error/Errors.py",     
    line 39, in <module>
    from antlr4.Recognizer import Recognizer
  File "/usr/local/lib/python3.6/dist-packages/antlr4/Recognizer.py", line    
    6, in <module>
    from __builtin__ import unicode
ModuleNotFoundError: No module named '__builtin__'

To generate the python code I used a fresh copy of Antlr4 - Version 4.7.1 - jar in the working directory and generated the python files for python2 like this:

alias antlr4='java -jar antlr-4.7.2-complete.jar'
antlr4 -Dlanguage=Python2 Hello.g4

and

alias antlr4='java -jar antlr-4.7.2-complete.jar'
antlr4 -Dlanguage=Python3 Hello.g4

for python3.

Python 2 version is 2.7.15+ Python 3 version is 3.6.8.

I already had a look at some similar question, like this: Python+ANTLR4: No module named antlr4

but could not find a solution to this problem.

Mike75
  • 504
  • 3
  • 18
  • 1)isnt that builtinS , 2)and this is imported iny py3 by default? https://stackoverflow.com/questions/9047745/where-is-the-builtin-module-in-python3-why-was-it-renamed – user8426627 Jun 12 '19 at 12:06
  • changed "import __builtin__" to "import buildins". Next error is: "cannot import name 'unicode'". Maybe I should ask: "why are there such compatibility problems at all?" – Mike75 Jun 12 '19 at 14:24
  • I took a look at the files in /usr/local/lib/python2.7/dist-packages/antlr4. It seems, that most of the include files have variable - type definitions in function defs like this: def create(self, source, type:int, text:str, channel:int, start:int, stop:int, line:int, column:int): Is that python 2.7 - compatible at all? (e.g. https://stackoverflow.com/questions/2489669/function-parameter-types-in-python) – Mike75 Jun 12 '19 at 16:01
  • no, its python 3 some last version feature. Install latest python for that – user8426627 Jun 12 '19 at 16:15
  • I understand. But the libraries are installed to be included with python2.7. With python 3 there is another problem (please see the error messages in the question). So it seems, that the code installed with pip is neither compatible with python2 nor python3. – Mike75 Jun 12 '19 at 16:29
  • ahahahaha)))) seems in antlr text:str is incompatibel for python 2 and No module named '__builtin__' for python 3 they must be kidding, man you must edit antlr :DD delete the line ModuleNotFoundError: No module named '__builtin__' cause builtins are imported by default – user8426627 Jun 12 '19 at 16:38
  • Thanks. Did you delete this line? Did antlr4 work then? – Mike75 Jun 12 '19 at 22:24
  • i did pip install antlr4-python3-runtime and i gont no errors. – user8426627 Jun 12 '19 at 22:54
  • O.k.: I just commented out the "import builtin"-line. This seems to work with python3. – Mike75 Jun 14 '19 at 18:06

1 Answers1

0

You can try my solution by using:

  1. python 3.7.6
  2. pip 22.1.2
  3. antlr4-python3-runtime 4.9

This answer comes from here.

M.Vu
  • 397
  • 2
  • 9