ANTLR toStringTree() 未被识别
我正在使用antlr编写语法,并且我有一个主要函数来测试语法接受的程序。主要功能是:
package compiler;
import org.antlr.runtime.ANTLRInputStream;
import org.antlr.runtime.CommonTokenStream;
public class runner {
public static void main(String[] args) throws Exception {
ANTLRInputStream input = new ANTLRInputStream(System.in);
SmallCLexer lexer = new SmallCLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
SmallCParser parser = new SmallCParser(tokens);
SmallCParser.program_return result = parser.program();
String tree = result.tree.toStringTree();
System.out.println(tree);
}
}
我的问题是它无法识别 toStringTree() 命令,我想知道是否有人能明白为什么?
我发现了这个: http://www.antlr.org/api/Java/interfaceorg_1_1antlr_1_1runtime_1_1tree_1_1_tree.html
它声称该函数包含在 BaseTree 中。
我还编辑了代码以使用 org.antlr.runtime.* 导入所有 antlr 文件,但这仍然无法解决此问题。我只是不明白为什么它不能识别它。
I'm using antlr to write a grammar, and I have a main function to test the programs accepted by the grammar. The main function is:
package compiler;
import org.antlr.runtime.ANTLRInputStream;
import org.antlr.runtime.CommonTokenStream;
public class runner {
public static void main(String[] args) throws Exception {
ANTLRInputStream input = new ANTLRInputStream(System.in);
SmallCLexer lexer = new SmallCLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
SmallCParser parser = new SmallCParser(tokens);
SmallCParser.program_return result = parser.program();
String tree = result.tree.toStringTree();
System.out.println(tree);
}
}
My problem is that it's not recognising the toStringTree() command and I was wondering if anyone could see why?
I found this:
http://www.antlr.org/api/Java/interfaceorg_1_1antlr_1_1runtime_1_1tree_1_1_tree.html
Which claims that the function is contained in BaseTree.
I also edited my code to import all of the antlr files with org.antlr.runtime.* but this still doesn't fix this. I just don't understand why it can't recognise it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先添加 import:
然后尝试这个:
请注意,只有在解析器语法中有以下选项时,这才有效:
First add the import:
and then try this:
Note that this will only work if your have the following option in your parser grammar: