ANTLR toStringTree() 未被识别

发布于 2025-01-05 22:38:07 字数 1039 浏览 2 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁拥 2025-01-12 22:38:07

首先添加 import:

import org.antlr.runtime.tree.CommonTree;

然后尝试这个:

CommonTree tree = (CommonTree)parser.program().getTree();
System.out.println(tree.toStringTree());

请注意,只有在解析器语法中有以下选项时,这才有效:

options {
  output=AST;
}

First add the import:

import org.antlr.runtime.tree.CommonTree;

and then try this:

CommonTree tree = (CommonTree)parser.program().getTree();
System.out.println(tree.toStringTree());

Note that this will only work if your have the following option in your parser grammar:

options {
  output=AST;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文