antlr4-Can't load Hello as lexer or parser
Asked Answered
D

7

65

I recently have to use parser to do a project. I download ANTLR4 and follow the steps described in the book The Definitive ANTLR4 Reference. The following are the steps I type in command line:

 1. export CLASSPATH=".:/<Mydirectory>/antlr-4.2.2-complete.jar:$CLASSPATH"
 2. alias antlr4='java -jar /<My directory>/antlr-4.2.2-complete.jar'
 3. alias grun='java org.antlr.v4.runtime.misc.TestRig'
 4. antlr4 Hello.g4

All the things work fine, it generates java files that I need. However, after I enter

 5. grun Hello r -tokens

It reports

Can't load Hello as lexer or parser.

I google some info, but still cannot figure out what happened.

Deaconess answered 26/4, 2014 at 19:3 Comment(3)
oh, I solved that problem by using javac -g .*java in that directoryDeaconess
I got the same problem, I solved it by adding . to classpath. (I already compiled java files)Caron
Note that the CLASSPATH separator is a semi-colon (';') now. At least on Windows.Bullen
C
52

You generated Java source files by running ANTLR on the grammar, but TestRig is looking for compiled .class files. You need to run the Java compiler on the source files before using TestRig.

Crisper answered 26/4, 2014 at 20:20 Comment(3)
You mean I have to compile java source code like HelloBaseListener.java, HelloLexer.java and HelloParser.java? I use command "java *.java" to compile all the java files, but it reports "Could not find or load main class HelloBaseListener.java"Deaconess
@Deaconess You need "javac *.java", not "java *.java". javac is the java compiler, which takes java files and compiles them to class files.Ronn
I forgot to add . i.e. the current directory, where my .class files are, in the class path while invoking the TestRig.Spoliate
G
40

I had exactly the same error on Windows. My batch is set like this at first:

java -cp "C:\ANTLR\antlr-4.4-complete.jar" org.antlr.v4.runtime.misc.TestRig %*

It turns out all my compiled Hello*.class files are located in current folder and I forget to include the current folder in the -cp. After I change to below. It works. Note the leading dot in the -cp.

java -cp ".;C:\ANTLR\antlr-4.4-complete.jar" org.antlr.v4.runtime.misc.TestRig %*

Goines answered 13/12, 2014 at 7:49 Comment(3)
This has been the solution for me - including the leading dot and semicolon to the CLASSPATH variable. Thanks!Pustulate
saved my day. My CLASSPATH looks like .;C:\Programs\ANTLRv4\antlr-4.7-complete.jar and my grun bat file looks like java org.antlr.v4.gui.TestRig %*Ulphi
this was it for me too it was the dot.Prescriptible
L
4

After all of your *.java files have been generated, and your *.class files have been generated, and your %CLASSPATH% is set correctly, based on the "Hello World" example grammar, you should not see this error.

If you don't have the "antlr-runtime-4.4.jar" in your %CLASSPATH% environment variable and run "grun" code from the book, you will receive this error when you run your generated parser based on the grammar you supplied.

%CLASSPATH% should be:

C:\libraries\antlr-4.4-complete.jar;C:\libraries\antlr-runtime-4.4.jar;

Enter the input after this command is entered. Like "hello parrt" in the book example. Then enter CTRL+Z (in Windows) to end the input, or CTRL+D (on Unix).

Langtry answered 22/9, 2014 at 6:11 Comment(0)
N
4

On macOS, the correct incantations appear to be (assuming you've downloaded ANTLR 4.8, and created a file Hello.g4):

java -jar antlr-4.8-complete.jar Hello.g4
javac -cp antlr-4.8-complete.jar Hello*.java
java --class-path ".:antlr-4.8-complete.jar" org.antlr.v4.gui.TestRig Hello r -gui

These commands will generate the Java source files, compile them, and then start TestRig, waiting for you to enter an expression.

Netta answered 12/8, 2020 at 17:3 Comment(0)
B
3

In order to do this correctly, I'm going to assume that you've got a JDK installed and added to your system PATH variable.

In this case you will need two batch files to help you out.

antlr4.bat:

java -cp antlr-4.2-complete.jar org.antlr.v4.Tool %*

grun.bat:

java -cp .;antlr-4.2-complete.jar org.antlr.v4.runtime.misc.TestRig %*

This also assumes that you have the antlr-4.2-complete.jar in the current directory. Beyond that simply follow the instructions in the getting started guide.

Note that I used the -cp flag because, on Windows 7, I'm not seeing a CLASSPATH variable.

Bout answered 24/7, 2014 at 20:2 Comment(0)
P
2

You need Antlr jar file and the .bat files in your CLASSPATH. I know because I had the exact same problem.

Windows

Go to the start menu and search for Environment Variables and you will find Set Environment Variables for Users or something like that. There should be a section labeled Variables for all Users. If you see %CLASSPATH%, click it, then click the edit button under that box. Go to the very end of the list and add C:\**PATH TO JAR FILE**\antlr-X.X-complete.jar; to the end. Repeat that with the path to each of the .bat files. If you don't see %CLASSPATH%, then under the box, click the button that says add, and add %CLASSPATH%, and do the same process.

OSX

I am not as familiar with OSX and the storage system, but you can do export CLASSPATH="/Users/**USER**/**PATH TO JAR FILE**/antlr-X.X-complete.jar:$CLASSPATH" in the terminal. Repeat, but with the paths to each of the .bat files. Simple as that hopefully. Let me know if this is wrong, because like I said above, OSX is not my strong point. You could also check out this question on StackOverflow: How can I permanently add a class path to my Mac terminal?

I added OSX instructions just in case anyone else had this problem, but on OSX! :)

ALSO: Replace the X's in antlr-X.X-complete.jar with the version number, or just make it the name of the jar file. Whatever floats your boat.

Pyrotechnics answered 29/12, 2014 at 0:58 Comment(0)
C
0

For MacOS users using homebrew with antlr4.

brew install antlr

Two things:

  • When using brew, the Antlr jar DOES NOT install in the typical location you see everywhere else. It installs here: /opt/homebrew/Cellar/antlr/4.10.1/antlr-4.10.1-complete.jar

  • In Java, you can optionally pass in different classes during compilation. javac looks at $CLASSPATH to find any additional java classes it should look for when compiling a program.

If javac *.java is failing after generating a grammer with antlr Hello.g4. Make sure your classpath is set correctly so the generated java code can look for the other code in $CLASSPATH to compile correctly

export CLASSPATH="$CLASSPATH:/opt/homebrew/Cellar/antlr/4.10.1/antlr-4.10.1-complete.jar"

NOTE: Newer versions of javac don't require the "." in front of CLASSPATH.

Carrillo answered 1/9, 2022 at 21:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.