简体   繁体   English

ANTLR 4.7.1生成的解析器中的问题

[英]issue in the generated parser with ANTLR 4.7.1

I created a grammar called goal.g4 我创建了一个名为goal.g4的语法

Then, from terminal (on a mac) I launched "java -cp "antlr-4.7.1-complete.jar" org.antlr.v4.Tool goal.g4 -listener -visitor" that terminates correctly. 然后,从终端(在Mac上)启动了正确终止的“ java -cp“ antlr-4.7.1-complete.jar” org.antlr.v4.Tool Goal.g4 -listener -visitor“。

Then I use the following java code: 然后,我使用以下java代码:

input = new ANTLRInputStream(fis); /* where fis is a FileInputStream */
goalLexer lexer = new goalLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
goalParser parser = new goalParser(tokens);
parser.start();

my point is: how to retrieve the ParseTree object from the parser? 我的观点是:如何从解析器中检索ParseTree对象? As suggested I replaced the init() method with the start() methods. 如建议的那样,我用start()方法替换了init()方法。 However eclipse tells me that parser.start() does not exist... 但是eclipse告诉我parser.start()不存在...

How do I solve this? 我该如何解决? Thanks Luca 谢谢卢卡

Look in your grammar. 看你的语法。 You have to identify the start rule you want to go with. 您必须确定要使用的开始规则。 Usually the start rule is near the top of the file and/or is one of the few (or only one) that is not called by any other rule. 通常,起始规则位于文件的顶部附近,并且/或者是其他规则未调用的少数几个(或只有一个)之一。

Whatever rule you choose it will return a parse tree context, which forms the root of your parse tree, eg 无论选择哪种规则,它都会返回一个解析树上下文,该上下文构成了解析树的根,例如

ParseTree tree = parser.start();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM