antlr 使用多个替代项匹配输入错误

发布于 2024-11-17 05:08:20 字数 768 浏览 6 评论 0原文

当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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吹泡泡o 2024-11-24 05:08:20

那很难。特别是对于较大的语法,更改(或添加)规则可能会导致难以追踪的歧义。

ANTLRWorks 可以帮助发现这些歧义。给定以下语法:

grammar T;

parse
  :  other? WORD? EOF
  ;

other
  :  WORD
  ;

WORD
  :  ('a'..'z' | 'A'..'Z')+
  ;

解析器不知道如何正确处理 parse 规则。输入如 foo(一个 WORD 标记)可以通过 other EOF 通过 WORD EOF 进行匹配,这就是警告:

决策可以使用多个替代项来匹配输入,例如“WORD”

使用 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:

grammar T;

parse
  :  other? WORD? EOF
  ;

other
  :  WORD
  ;

WORD
  :  ('a'..'z' | 'A'..'Z')+
  ;

the parser does not know how to handle the parse rule properly. Input such as foo (one WORD token) could be matched by other EOF and by WORD EOF, which is what the warning:

Decision can match input such as "WORD" using multiple alternatives

means.

Generating a parser and lexer using ANTLRWorks results in the following visualization of the problem:

enter image description here

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文