ANTLR4语法中第一个解析器规则上带有return声明的问题 [英] Troubles with returns declaration on the first parser rule in an ANTLR4 grammar

查看:176
本文介绍了ANTLR4语法中第一个解析器规则上带有return声明的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对解析器规则使用返回值,该规则适用于除第一个解析器规则以外的所有解析器规则.如果我语法中的第一个解析器规则使用 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语法中第一个解析器规则上带有return声明的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆