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

时间:2011-06-27 21:53:44

标签: antlr antlr3

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

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

谢谢, 理查德

1 个答案:

答案 0 :(得分:10)

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

ANTLRWorks可以帮助找到这些含糊之处。鉴于以下语法:

grammar T;

parse
  :  other? WORD? EOF
  ;

other
  :  WORD
  ;

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

解析器不知道如何正确处理parse规则。诸如foo(一个WORD令牌)之类的输入可以由other EOF 匹配WORD EOF,这就是警告:

  

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

的装置。

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

enter image description here

是的,我意识到这只是一个微不足道的例子,你的问题相当棘手,但AFAIK在这里没有圣杯。如果你可以发布生成解析器的语法& lexer没有问题产生这些警告的编辑语法,我可能会看看它是否能看到问题。