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

后端 未结 1 1957
再見小時候
再見小時候 2020-12-19 12:59

I have a simple HelloWorld.g4 grammar (see it at the bottom). I am able to successfully generate the .py files using this:

set CLAS         


        
相关标签:
1条回答
  • 2020-12-19 13:04

    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
    
    0 讨论(0)
提交回复
热议问题