简体   繁体   English

如何在 Windows 上为 Python3 安装 antlr4

[英]How do I install antlr4 for Python3 on Windows

I'm trying to install antlr4 for Python 3 on Windows.我正在尝试在 Windows 上为 Python 3 安装 antlr4。 I run the following pip command successfully:我成功运行以下pip命令:

pip install antlr4-python3-runtime

Installs the packages, no problem.安装包,没问题。 I'm using the Miniconda environment, and the files are where they are expected.我正在使用 Miniconda 环境,文件在预期的位置。

When I try to run antlr4 from the command line, though, error is returned:但是,当我尝试从命令行运行antlr4时,会返回错误:

'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.我已经验证了我的路径变量是否符合预期......并且我在 Miniconda 环境中运行通过 pip 安装的其他软件包没有问题。 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.我还尝试通过 CMD 安装到 Windows 上的主要 Python 安装,它安装没有问题……但是当我尝试运行时,响应相同。 I also tried to do this on my Mac, same issue.我也尝试在我的 Mac 上执行此操作,同样的问题。

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.我假设 antlr4 构建存在问题,但我想确保在继续之前我没有遗漏任何东西。

Update 0 @Bart's answer is the way to go, but now I'm having trouble running the .jar file.更新 0 @Bart 的答案是要走的路,但现在我在运行 .jar 文件时遇到了问题。 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).它抛出一个错误,说我的 Java 已过时(我的 mon 类文件版本为 52,它需要 55)。 But I have Java 1.8, which should be higher than that.但我有 Java 1.8,应该比这更高。 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 .更新 1我做了更多的挖掘工作,并按照此处的安装说明进行操作: 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.更新了所有环境变量,根据需要创建了 .bat 文件,然后我再次运行......但即使它现在在我的 PATH 上,仍然是同样的错误。 There may be something wonky with my Java install, but I did do a clean reinstall.我的 Java 安装可能有些问题,但我确实重新安装了。 I did find a similar issue on the GitHub here, but seems to be resolved.我确实在 GitHub 上找到了类似的问题,但似乎已解决。 Related antlr4 GitHub issue相关的 antlr4 GitHub 问题

Update 2 The answer to my issue with running the antlr4 .jar file was to reinstall an earlier version of Java.更新 2我运行 antlr4 .jar 文件的问题的答案是重新安装早期版本的 Java。 That completely fixed it.这完全解决了它。 If anyone else is going down this rabbit hole, take a look at the GitHub issue link I posted.如果还有其他人正在进入这个兔子洞,请查看我发布的 GitHub 问题链接。

antlr4 is not a binary shipped with antlr4-python3-runtime . antlr4不是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.换句话说,当您想从.g4语法文件生成解析器时,您需要下载antlr-4.10.1-complete.jar文件并安装 Java 运行时。 You only need Java to generate the parser classes, after which you need the Python runtime classes to use these generated parser classes.您只需要 Java 来生成解析器类,之后您需要 Python 运行时类来使用这些生成的解析器类。

For example, you have a grammar called MyLanguage.g4 :例如,您有一个名为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).这将生成MyLanguageLexer.pyMyLanguageParser.py (和一些监听器类)。

  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 * ).上面的脚本只需要您使用antlr4-python3-runtime安装的库(并且正在使用from antlr4 import * )。 If you runt this script, the following will be printed:如果您运行此脚本,将打印以下内容:

(parse Hi Trekkie <EOF>)

“此版本的 Java 运行时仅识别最高 52.0 的类文件版本”上面的错误消息表示您的 java 版本是 java 8 或 java 1.8,而不是 java 18 或称为 java 18,它们是两个不同的版本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM