antlr4-Can't load Hello as lexer or parser

user3162707 picture user3162707 · Apr 26, 2014 · Viewed 23.3k times · Source

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.

Answer

Sam Harwell picture Sam Harwell · Apr 26, 2014

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.