1
votes

I'm using an ANTLR grammar with Python. It used to work fine, but now I'm getting the following error when I import the lexer:

ValueError: Bad version string '!Unknown version!'

I can trace this back to the antlr3 executable:

$ antlr3 -version
ANTLR Parser Generator Version !Unknown version!

The generated parser and lexer seem fine, except for the version string. I am using the antlr3 provided by Ubuntu's repositories (12.04). I guess I could install ANTLR by hand, but I think this should work out of the box - maybe I've misconfigured something somewhere?. Is there a known trick I am missing, or is this a bug?

1

1 Answers

3
votes

I've been trying to do the same, and concluded that the current antlr3 package in Ubuntu's repositories doesn't work properly with the Python runtime support. The other Ubuntu package python-antlr is even more obsolete as it's supposed to work with ANTLR 2. I suggest that you uninstall antlr3 and install ANTLR by hand together with the right Python runtime support.

Since I've just been through this myself, and found that the instructions are a bit scattered around, here's a summary of how to do it.

  1. Uninstall the antlr3 package:

    sudo apt-get purge antlr3
    
  2. Get the complete Java binaries for the latest ANTLR 3.x:

    wget http://www.antlr3.org/download/antlr-3.5.tar.gz
    
  3. Prepare to install the Python runtime:

    tar xzf antlr-3.5.tar.gz
    cd antlr3-antlr-3.5/runtime/Python
    
  4. Before you can call the install script, you have to patch the version number to point to what's available online. You can do this by editing ez_setup.py and changing DEFAULT_VERSION = "0.6c5" into DEFAULT_VERSION = "0.6c11".

  5. Now you should be able to install the Python runtime with:

    sudo python setup.py install
    

    To see if it works, try import antlr3 into a Python shell. You shouldn't get any error.

  6. Now, you have to install the corresponding version of ANTLR proper, which is in Java. For this, make sure you have a JVM installed (e.g., sudo apt-get install default-jdk).

  7. Get the latest ANTLR 3 jar:

    wget http://www.antlr3.org/download/antlr-3.5-complete.jar
    
  8. Permanently add the jar to your classpath (of course, you may move it where you want and adjust the path accordingly):

    echo 'export CLASSPATH=~/antlr-3.5-complete.jar:$CLASSPATH' >> ~/.bashrc
    
  9. Optional, but recommended: add a shell alias to call ANTLR 3:

    echo "alias antlr3='java -jar ~/antlr-3.5-complete.jar'" >> ~/.bashrc
    

That's it! After you reread your .bashrc, see if everything works with antlr3 -version. You should get a message such as:

ANTLR Parser Generator  Version 3.5