3

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.

Kevin
  • 1,548
  • 2
  • 19
  • 34
  • I would encourage people to use the official Java tool and runtime. Harwell has not updated his code for almost 2 years now, and it is two revisions behind the latest, which is 4.8. To build and run a JAVA language target on Windows, assuming you have a Java program written, type from a Windows cmd prompt, `"c:\...full-path...\java.exe" -jar "c:\...full-path...\antlr-4.8-complete.jar" -visitor arithmetic.g4`, `"c:\...full-path...\javac.exe" -cp "c:\...full-path...\antlr-4.8-complete.jar" *.java`, `"c:\...full-path...\java.exe" -cp ".;c:\...full-path...\antlr-4.8-complete.jar" Program` – kaby76 Aug 07 '20 at 12:14
  • 1
    Thank you for your help. The issue was confusion caused by the ordering of elements in the PATH and CLASSPATH variables. The SDK exe files were sometimes picking up the non-SDK class libraries, and vice versa. Part of the problem was that "." was first in the CLASSPATH. – Kevin Aug 07 '20 at 23:42

1 Answers1

1

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

  1. "ANTLR4 Standard" version
  2. "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

peter.cyc
  • 1,763
  • 1
  • 12
  • 19
  • Thank you for your suggestion. I managed to download the Nuget packages, create some VS projects, and can now edit and test everything within Visual Studio. – Kevin Aug 07 '20 at 23:43