Home > Net >  How do I install antlr4 for Python3 on Windows
How do I install antlr4 for Python3 on Windows

Time:07-22

I'm trying to install antlr4 for Python 3 on Windows. I run the following pip command successfully:

pip install antlr4-python3-runtime

Installs the packages, no problem. I'm using the Miniconda environment, and the files are where they are expected.

When I try to run antlr4 from the command line, though, error is returned:

'antlr4' is not recognized as an internal or external command, operable program or batch file

I've verified that my path variables are as expected... and I run other packages installed via pip in the Miniconda environment with no issue. I've also tried installing to the main Python installation on Windows via CMD, and it installs without issue... but same response when I try to run. I also tried to do this on my Mac, same issue.

I'm assuming there is an issue with the antlr4 build, but I wanted to make sure I wasn't missing anything before moving on.

Update 0 @Bart's answer is the way to go, but now I'm having trouble running the .jar file. It's throwing an error that says that my Java is out of date (that I' mon class file version 52 and it requires 55). But I have Java 1.8, which should be higher than that. Here is the error below:

C:\Users\mathg>java -jar C:\Users\mathg\miniconda3\Scripts\antlr-4.10.1-complete.jar -help
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: org/antlr/v4/Tool has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$100(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

Update 1 I've done some more digging around, and followed the installation instructions here: antlr4: Getting Started. Updated all the environment variables, created the .bat files as required, and I ran again... but even though it's on my PATH now, still same error. There may be something wonky with my Java install, but I did do a clean reinstall. I did find a similar issue on the GitHub here, but seems to be resolved. Related antlr4 GitHub issue

Update 2 The answer to my issue with running the antlr4 .jar file was to reinstall an earlier version of Java. That completely fixed it. If anyone else is going down this rabbit hole, take a look at the GitHub issue link I posted.

CodePudding user response:

antlr4 is not a binary shipped with antlr4-python3-runtime. It is just an alias for the command:

java -jar /usr/local/lib/antlr-4.10.1-complete.jar

In other words, when you want to generate a parser from your .g4 grammar file, you need to download the antlr-4.10.1-complete.jar file and have a Java runtime installed. You only need Java to generate the parser classes, after which you need the Python runtime classes to use these generated parser classes.

For example, you have a grammar called MyLanguage.g4:

grammar MyLanguage;

parse
 : GREET NAME EOF
 ;

GREET : 'Hi' | 'Hello';
NAME  : [a-zA-Z] ;
SPACE : [ \t\r\n] -> skip;

Then this is what you'll have to do:

  1. generate parser classes:
java -jar antlr-4.10.1-complete.jar MyLanguage.g4 -Dlanguage=Python3

which will generate MyLanguageLexer.py, MyLanguageParser.py (and some listener classes).

  1. use the lexer and parser:
from antlr4 import *
from MyLanguageLexer import MyLanguageLexer
from MyLanguageParser import MyLanguageParser

if __name__ == '__main__':
    lexer = MyLanguageLexer(InputStream('Hi Trekkie'))
    parser = MyLanguageParser(CommonTokenStream(lexer))
    parse_tree = parser.parse()
    print(parse_tree.toStringTree(recog=parser))

The script above only needs the library you installed with antlr4-python3-runtime (and are importing with from antlr4 import *). If you runt this script, the following will be printed:

(parse Hi Trekkie <EOF>)
  • Related