I have a simple HelloWorld.g4
grammar (see it at the bottom). I am able to successfully generate the .py
files using this:
set CLASSPATH=.;antlr-complete.jar;%CLASSPATH%
java org.antlr.v4.Tool -Dlanguage=Python2 HelloWorld.g4
Now I would like to use the TestRig
with the -gui
flag to generate a parse tree GUI. I have the ANTRL Python runtime installed (antlr4-python2-runtime-4.5.tar.gz
). I can open a Python interpreter and type:
import antlr4
and the interpreter recognizes the antlr4
module.
When I run the TestRig
like this:
set CLASSPATH=.;antlr-complete.jar;%CLASSPATH%
java org.antlr.v4.gui.TestRig HelloWorld message -gui < input.txt
I get this error message:
Can't load HelloWorld as lexer or parser
From my investigations I have found several posts listing the same error message. However, in those cases they forgot to include period (.) in their classpath. But as you can see, I have included it in my classpath.
I'm out of ideas on how to get the TestRig
to work. Note: I have no problem getting the TestRig
to work with the same HelloWorld
grammar when the target language is Java.
Any help you can provide would be greatly appreciated.
HelloWorld.g4
grammar HelloWorld;
options { language=Python; }
message : GREETING NAME;
GREETING : 'Hello' ;
NAME : [a-zA-Z]+ ;
WS : [ \t\r\n]+ -> skip ;
Ran into this today as well: The problem is that the testrig expects the generated java source code. But since you're on Python you don't have it unless you explicitly run antlr4
twice: Once for target language Python2 (or 3) and once for -Dlanguage=java
.
See this answer which suggests to run the language=java
target first. Then the comment on the question itself to compile the java files.
And for completeness and before it's forgotten: Ensure your $CLASSPATH
env variable is set up so that it includes both a dot '.'
and the path to the antlr*.jar
file. For example on unix:
export CLASSPATH=".:/<Mydirectory>/antlr-4.2.2-complete.jar:$CLASSPATH"
Here's a step by step of what I guess you have to do once the $CLASSPATH
is set correctly:
Compile for java:
> antlr4 -Dlanguage=Java HelloWorld.g4
# or: java org.antlr.v4.Tool -Dlanguage=Java HelloWorld.g4
Note that I have the options { language=Python3 }
in my grammar file and the -D
override did not work as expected. So I removed the option block and specify the language target on the command line now.
Then compile the *.java
files into *.class
files:
> javac -g *.java
Then run the testrig:
> grun HelloWorld message
# or: java org.antlr.v4.gui.TestRig HelloWorld message -gui < input.txt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With