antlr 使用多个替代项匹配输入错误
当antlr v3.1 根据此规则进行编译时,我收到一条警告
sentence
:
(CAPITAL_LETTERS_AND_NUMBERS | INT | ANY_WORD )
(
INT
| CAPITAL_LETTERS_AND_NUMBERS
| ANY_WORD
)*;
。警告是:
5:2: Decision can match input such as "CAPITAL_LETTERS_AND_NUMBERS" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
Semantic predicates were present but were hidden by actions.
Decision can match input such as "INT" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
Semantic predicates were present but were hidden by actions.
我感到困惑的原因是相当复杂的语法,直到我将另一个子规则放在也使用句子的文件中的另一个位置。它接受上述规则,直到发生这种情况,这看起来很奇怪。我正在寻找有关如何最好地进行调试和理解这种情况如何发生的提示?
谢谢, 理查德
I'm receiving a warning when antlr v3.1 compiles on this rule
sentence
:
(CAPITAL_LETTERS_AND_NUMBERS | INT | ANY_WORD )
(
INT
| CAPITAL_LETTERS_AND_NUMBERS
| ANY_WORD
)*;
The warning is:
5:2: Decision can match input such as "CAPITAL_LETTERS_AND_NUMBERS" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
Semantic predicates were present but were hidden by actions.
Decision can match input such as "INT" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
Semantic predicates were present but were hidden by actions.
The reason I'm confused is the grammar which is quite complex passes until I put another subrule in another place in the file that uses sentence as well. It accepts the above rule until this happens which seems strange. I'm looking for hints as to how best to go about debugging and understanding how this could happen?
Thanks,
Richard
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那很难。特别是对于较大的语法,更改(或添加)规则可能会导致难以追踪的歧义。
ANTLRWorks 可以帮助发现这些歧义。给定以下语法:
解析器不知道如何正确处理
parse
规则。输入如foo
(一个WORD
标记)可以通过other EOF
和 通过WORD EOF 进行匹配
,这就是警告:。
使用 ANTLRWorks 生成解析器和词法分析器会导致以下问题的可视化结果:
是的,我意识到这这只是一个简单的例子,你的问题有点棘手,但据我所知这里没有圣杯。如果您可以发布生成解析器和的语法;词法分析器没有问题和产生这些警告的编辑语法,我可能会看一下它,看看是否可以看到问题。
That's difficult. Especially with larger grammars, changing (or adding) rules can cause ambiguities which are hard to track down.
ANTLRWorks can assist in finding these ambiguities. Given the following grammar:
the parser does not know how to handle the
parse
rule properly. Input such asfoo
(oneWORD
token) could be matched byother EOF
and byWORD EOF
, which is what the warning:means.
Generating a parser and lexer using ANTLRWorks results in the following visualization of the problem:
Yes, I realize that this is just a trivial example, and that your problem is quite a bit trickier, but there's AFAIK no holy grail here. If you could post the grammar that generates a parser & lexer without a problem and the edited grammar that produces these warnings, I might have a look at it to see if I can see the problem.