ANTLR4 语法中第一个解析器规则的返回声明问题 [英] Troubles with returns declaration on the first parser rule in an ANTLR4 grammar
问题描述
我使用 returns 作为我的解析器规则,它适用于除第一个之外的所有解析器规则.如果我的语法中的第一个解析器规则使用 returns 声明,ANTLR4 会抱怨如下:
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:
在匹配规则时期待 ARG_ACTION
如果我在上面添加另一个不使用返回"的解析器规则,ANTLR 不会抱怨.
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?
推荐答案
在语法中,您需要将解析器规则(以小写字母开头)置于词法分析器规则(以大写字母开头)之前.遇到词法分析器规则后,[
会触发 LEXER_CHAR_SET
而不是 ARG_ACTION
,所以编译器看到的令牌流看起来就像你在传递返回值应该在的一组字符.
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.
这篇关于ANTLR4 语法中第一个解析器规则的返回声明问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!