ANTRL4:无法让 Python ANTLR 生成解析树的图形 [英] ANTRL4: Can't get Python ANTLR to generate a graphic of the parse tree
问题描述
我有一个简单的 HelloWorld.g4
语法(见底部).我能够使用这个成功生成 .py
文件:
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
现在我想使用带有 -gui
标志的 TestRig
来生成解析树 GUI.我安装了 ANTRL Python 运行时(antlr4-python2-runtime-4.5.tar.gz
).我可以打开一个 Python 解释器并输入:
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
并且解释器识别antlr4
模块.
当我像这样运行 TestRig
时:
When I run the TestRig
like this:
set CLASSPATH=.;antlr-complete.jar;%CLASSPATH%
java org.antlr.v4.gui.TestRig HelloWorld message -gui < input.txt
我收到此错误消息:
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.
我不知道如何让 TestRig
工作.注意:当目标语言是 Java 时,我可以让 TestRig
使用相同的 HelloWorld
语法.
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 ;
推荐答案
今天也遇到了这个问题:问题是 testrig 需要生成的 java 源代码.但是由于您使用的是 Python,除非您明确地运行 antlr4
两次:一次用于目标语言 Python2(或 3),一次用于 -Dlanguage=java
.
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
.
请参阅 此答案,其中建议先运行 language=java
目标.然后对问题本身的评论来编译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.
为了完整性和在它被遗忘之前:确保您的 $CLASSPATH
环境变量已设置,以便它同时包含一个点 '.'
和 '.'
的路径代码>antlr*.jar 文件.例如在 unix 上:
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"
以下是我猜在正确设置 $CLASSPATH
后您必须执行的步骤:
Here's a step by step of what I guess you have to do once the $CLASSPATH
is set correctly:
为java编译:
> antlr4 -Dlanguage=Java HelloWorld.g4
# or: java org.antlr.v4.Tool -Dlanguage=Java HelloWorld.g4
请注意,我的语法文件中有 options { language=Python3 }
并且 -D
覆盖没有按预期工作.所以我删除了选项块,现在在命令行上指定语言目标.
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.
然后将*.java
文件编译成*.class
文件:
Then compile the *.java
files into *.class
files:
> javac -g *.java
然后运行测试:
> grun HelloWorld message
# or: java org.antlr.v4.gui.TestRig HelloWorld message -gui < input.txt
这篇关于ANTRL4:无法让 Python ANTLR 生成解析树的图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!