我在使用访问者类时遇到了 ANTLR 4 的问题。 我尝试编写以下代码:
import bla.gen.InputLexer;
import bla.gen.InputParser;
import org.antlr.v4.runtime.ANTLRFileStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
public class Main {
public static void main(String[] args) throws Exception {
InputLexer lexer = new InputLexer(new ANTLRFileStream("pl_example.lp"));
InputParser parser = new InputParser(new CommonTokenStream(lexer));
parser.setBuildParseTree(true);
ParseTree tree = parser.prog();
ParserVisitor visitor = new ParserVisitor();
visitor.visit();
}
}
我尝试模仿书中示例中的代码: https://pragprog.com/titles/tpantlr2/source_code
(我无法访问这本书,只能访问示例)。
但是我遇到了错误,因为方法 parser.prog() 不存在...
我使用 ANTLR 4.5。
你知道如何用这个版本生成ParseTree吗?
最佳答案
用于检索解析树的方法名称与选择的条目解析规则相同。如果您为条目解析规则使用了不同的名称,则该方法将被调用。
关于java - 没有方法program()来构建解析树对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29685615/