ANTRL4: Can't get Python ANTLR to generate a graphic of the parse tree
Asked Answered
G

1

5

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 ; 
Gibber answered 5/8, 2015 at 19:25 Comment(0)
C
7

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
Crinkumcrankum answered 7/8, 2015 at 9:53 Comment(4)
Thanks for the message cfi. It shouldn't be necessary to generate the parse tree graphic from Java classes. I know at least one person in this world who has generated the parse tree graphic by applying testrig to the Python files: github.com/godber/pds3_grammar (scroll down to the bottom of the page and you will see a beautiful "Parse Tree Inspector" graphic. I've been in touch with the follow who created that and showed him what I did (described in my above post) and he doesn't see anything wrong. This is a mystery indeed. I wish that Terrance Parr would chime in on this issue.Gibber
That's interesting information, but did you try out my suggestion? Even if there might be a simpler way, this was what worked for me (and others as well). So it's worth a try. Or is there a reason why you could not accept this - even if it is just a workaround? Are there other constraints? Running the antlr4 tool twice is no real impact, imho. A small script, and you'll never notice again.Crinkumcrankum
Note that the guy does build java targets. See the makefile, and he does compile them into *.class files, and thus he does it exactly in the way I described and thus he does not have an issue ;-)Crinkumcrankum
Thank you very much cfi. I see what you mean. Yes, he does run testrig on the Java .class files, not on the Python.class files. I didn't catch that when reading his makefile. Thanks again!Gibber

© 2022 - 2024 — McMap. All rights reserved.