gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 17:27:22 24 4
gpt4 key购买 nike

当 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.

我感到困惑的原因是语法非常复杂,直到我将另一个子规则放在文件中也使用句子的另一个地方。它接受上述规则,直到发生这种情况,这看起来很奇怪。我正在寻找有关如何最好地进行调试和了解如何发生这种情况的提示?

谢谢,
理查德

最佳答案

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

ANTLRWorks 可以帮助找出这些歧义。鉴于以下语法:

grammar T;

parse
: other? WORD? EOF
;

other
: WORD
;

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

解析器不知道如何处理 parse适本地统治。输入如 foo (一个 WORD token )可以与 other EOF 匹配来自 WORD EOF ,这是警告:

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



方法。

使用 ANTLRWorks 生成解析器和词法分析器会导致问题的以下可视化:

enter image description here

是的,我意识到这只是一个微不足道的例子,你的问题有点棘手,但这里没有 chalice 。如果您可以发布生成解析器和词法分析器没有问题的语法以及生成这些警告的已编辑语法,我可能会看看它是否可以看到问题。

关于antlr 匹配输入使用多个替代错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6499378/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com