Troubles with returns declaration on the first parser rule in an ANTLR4 grammar

╄→гoц情女王★ 提交于 2019-12-22 02:40:17

问题


I am using returns for my parser rules which works for all parser rules except the first one. If the first parser rule in my grammer uses the returns declaration ANTLR4 complains as follows:

expecting ARG_ACTION while matching a rule

If I add another parser rule above which does not use "returns" ANTLR does not complain.

Here you have a grammar reduced to the problem:

grammar FirstParserRuleReturnIssue;
ID  :   ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*;
aRule returns [String s]: ID { $s = $ID.text; };

I searched to find a special role of the first rule that could explain the behaviour but did not find anything. Is it a bug? Do I miss some understanding?


回答1:


You need to place parser rules (start with a lowercase letter) before lexer rules (start with an uppercase letter) in your grammar. After encountering a lexer rule, the [ triggers a LEXER_CHAR_SET instead of ARG_ACTION, so the token stream seen by the compiler looks like you're passing a set of characters where the return value should be.



来源:https://stackoverflow.com/questions/16396872/troubles-with-returns-declaration-on-the-first-parser-rule-in-an-antlr4-grammar

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!