英文:
ANTLR / java / SDK generate-compile-execute sequence fails on Windows10 command window
问题
我试图在Windows 10上使用ANTLR 4.8编译一个名为hello.g4的小型语法文件,我的最终目标是在Visual Studio内运行ANTLR。
问题是,有些事情阻止我在恒定的PATH设置下运行生成、编译、执行序列。我不了解Java的工作原理,无法确定出了什么问题(或者如何修复它)。
最新的Java SDK 14.0文件的位置:
c:\program files\...\sdk 14.0\(包括java.exe和javac.exe等一堆文件)
原始的Java 8运行时位置:
c:\program files (x86)\common files\Oracle\java\javapath(三个文件java.exe、javaw.exe等)
类路径指向我的工作目录(ANTLR生成的Java文件所在的位置)以及ANTLR jar文件本身。ANTLR完整的jar文件与其他所有文件位于同一目录中。下面的示例都使用相同的CLASSPATH设置。
CLASSPATH=.;c:\dev\bin\antlr-4.8-complete.jar
我没有设置JRE_HOME、JAVA_HOME或任何类似的环境变量,只有CLASSPATH和PATH。
一个工作的序列
这个序列有效(表示语法和工具有效):
PATH=(Java 8运行时位置);(SDK位置);... 其他路径
// 使用SDK路径显式生成解析器(在PATH=Java8之前)
"C:\Program Files\Java\jdk-14.0.2\bin\java" org.antlr.v4.Tool hello.g4
// 现在将PATH变量切换为将SDK放在前面
// 这会成功编译和运行生成的文件
PATH=(SDK位置);(Java 8运行时位置);... 其他路径
"C:\Program Files\Java\jdk-14.0.2\bin\javac" hello*.java
"C:\Program Files\Java\jdk-14.0.2\bin\java" org.antlr.v4.gui.TestRig %*
一个失败的序列
CLASSPATH=.;c:\dev\bin\antlr-4.8-complete.jar(与上述相同)
如果SDK在路径中排在前面,生成会成功,但编译会失败
PATH=(SDK位置);(Java 8运行时位置);... 其他路径
OK: "C:\Program Files\Java\jdk-14.0.2\bin\java" org.antlr.v4.Tool hello.g4
失败: "C:\Program Files\Java\jdk-14.0.2\bin\javac" hello*.java
会生成数百个错误,其中包括这样的错误。
fooLexer.java:6: 错误: 包org.antlr.v4.runtime不存在
import org.antlr.v4.runtime.*;
^
我没有名为org.antlr.v4.runtime的文件;它们可能在antlr.4.8.complete.jar文件中吗?
生成阶段必须首先设置Java 8的PATH
编译和执行阶段必须首先设置SDK的PATH。
我只是使用命令行窗口来运行命令 - 没有涉及任何IDE。
有人能告诉我如何修复问题,以便我可以在不更改PATH变量的情况下运行生成、编译、执行循环吗?谢谢。PS。我已经在SO上阅读了半打可能是“重复”的问题,但它们都涉及不同的设置(包括IDE),我从中尝试的一切对我都没有用。
英文:
I'm trying to compile a tiny hello.g4 grammar file using ANTLR 4.8 on Windows 10. My end target is to run ANTLR inside of Visual Studio.
The problem is that something is happening to prevent me from running the generate, compile, execute sequence with constant PATH settings. I don't know enough about how Java works to determine what is wrong (or how to fix it).
The location of the latest Java SDK 14.0 files:
c:\program files\...\sdk 14.0\(a pile of files including java.exe and javac.exe)
The original Java 8 runtime location:
c:\program files (x86)\common files\Oracle\java\javapath (three files java.exe, javaw.exe, etc.)
The classpath points to my working directory (where the ANTLR java files are generated) and to the ANTLR jar file itself. The ANTLR complete jar file is in the same directory as everything else. The examples below both use the same CLASSPATH setting.
CLASSPATH=.;c:\dev\bin\antlr-4.8-complete.jar
I do not have JRE_HOME or JAVA_HOME or any such environment variables set. Only CLASSPATH and PATH.
A WORKING SEQUENCE
This sequence works (indicating that the grammar and tools work)
PATH=(the Java8 runtime location);(the SDK location);... other paths
// generate the parser with the SDK path explicitly (with PATH=Java8 in front)
"C:\Program Files\Java\jdk-14.0.2\bin\java" org.antlr.v4.Tool hello.g4
// now switch the PATH variable to put the SDK first
// this compiles and runs the generated files successfully
PATH=(the SDK location);(the Java8 runtime location);... other paths
"C:\Program Files\Java\jdk-14.0.2\bin\javac" hello*.java
"C:\Program Files\Java\jdk-14.0.2\bin\java" org.antlr.v4.gui.TestRig %*
A FAILED SEQUENCE
CLASSPATH=.;c:\dev\bin\antlr-4.8-complete.jar (unchanged from above)
If the SDK is first in the path, generation works, but compilation fails
PATH=(the SDK location);(the Java8 runtime location);... other paths
OK: "C:\Program Files\Java\jdk-14.0.2\bin\java" org.antlr.v4.Tool hello.g4
FAILS: "C:\Program Files\Java\jdk-14.0.2\bin\javac" hello*.java
Hundreds of errors are generated, among them ones like this.
fooLexer.java:6: error: package org.antlr.v4.runtime does not exist
import org.antlr.v4.runtime.*;
^
I have no files named org.antlr.v4.runtime; could it/they be in the antlr.4.8.complete.jar file or something?
The Java8 PATH must be first for the generation phase
The SDK PATH must be first for the compilation and execution phases.
I'm also just using a command line window to run the commands - no IDE is involved.
Can anyone tell me how to fix things so that I can run a generation, compile, execution cycle without flipping my PATH variable? Thank you. PS. I have read half a dozen potentially "duplicate" questions here on SO, but they all involve a different setup (with IDEs) and nothing I tried from them worked for me.
答案1
得分: 1
自ANTLR 4.6.5-beta001版本的ANTLR4 for C#发布以来,可以在VS中直接使用ANTLR4预编译语法文件到C#,然后编译和运行您的应用程序。您只需安装NuGet包。今天,您有几个选择:
- "ANTLR4标准"版本
- "ANTLR4CS"优化版本
它可以直接使用。更新您的语法,保存它,它会被预编译。按F5键,您就可以运行它了!更多信息请参阅此答案。
如果您需要调整构建的内部工作方式,请查看此链接。
要解决您的路径问题,AntlrCS包安装的代码生成器是antlr4.exe:jar文件存储在其中,因此不再有路径问题。我有一个简单的设置,可以在VS之外预编译语法。从Antlr4Dy文件夹开始工作,我在src子文件夹中生成C#代码。代码生成器包与Antlr4.exe一起下载到指定的文件夹中。
"C:\Users\...\source\repos\Antlr4Dy\packages\Antlr4.CodeGenerator.4.6.6\tools\net45\Antlr4.exe" C:\Users\...\source\repos\Antlr4Dy\Speak.g4 -o src -Dlanguage=CSharp -package Antlr4x -no-listener -visitor
英文:
Since ANTLR 4.6.5-beta001 release of ANTLR4 for C#, it is possible to use ANTLR4 directly within VS to precompile grammar files to C#, then compile and run your app. All you need is install the nuget package. Today, you have several choices
- "ANTLR4 Standard" version
- "ANTLR4CS" optimized version
It works out of the box. Update your grammar, save it, it gets precompiled. Hit F5 and you are running it! More info in this answer
If you need to fiddle the inside workings of the build, check this link
To solve your path problem, the code generator installed by the AntlrCS package is antlr4.exe: jar is stored inside, so no more path problems. I have a simple setup that precompiles grammars outside VS. Working from the Antlr4Dy folder, I generate C# code in the src sub folder. The Code generator package is downloaded into the given folder, along with Antlr4.exe
"C:\Users...\source\repos\Antlr4Dy\packages\Antlr4.CodeGenerator.4.6.6\tools\net45\Antlr4.exe" C:\Users...\source\repos\Antlr4Dy\Speak.g4 -o src -Dlanguage=CSharp -package Antlr4x -no-listener -visitor
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论