如何摆脱我的ANTLR3语法中的以下多个替代警告?

时间:2011-11-14 16:57:59

标签: antlr antlr3 antlrworks

[11:45:19] warning(200): mygrammar.g:14:57: Decision can match input such as "','" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
[11:45:19] warning(200): C:\Users\Jarrod Roberson\mygrammar.g:14:57: Decision can match input such as "','" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input

我希望能够将函数嵌套在其他函数中。

myfunction(x) ->
  sqr(a) -> a * a,
  y -> sqr(x).

这是它抱怨的行

function : ID '(' args ')' '->' statement (',' statement)* ;

这就是考虑替代方案

statement : ATOM
          | expression
          | assignment
          | function
          ;

我使用.作为我的陈述结束规则

program : (statement'.')*;

以下是ANTLRWorks中的synatx图表

syntax diagram http://www.vertigrated.com/images/alternatives.png

我真的很喜欢在没有任何警告的情况下编译/工作的东西。如何解决此警告情况?

1 个答案:

答案 0 :(得分:2)

  

Jarrod Roberson写道:

     

我真的很喜欢在没有任何警告的情况下编译/工作的东西。如何解决此警告情况?

您的解析器可以解析以下输入:

f(x)-> g(y)-> y*y, x=y

在两个不同的解析树中:

enter image description here

enter image description here

您可以通过强制解析器向前看并确保在实际匹配这些规则之前提前',' statement来解决此问题。您可以通过使用包含所述规则的语法谓词((...)=>部分)来实现此目的:

function
  :  ID '(' args ')' '->' statement ((',' statement)=> ',' statement)* 
  ;

但是,如果您的function规则具有某种“结束”令牌(您尚未定义),则不需要谓词。从您之前的问题和您的示例:

myfunction(x) ->
  sqr(a) -> a * a,
  y = sqr(x).

您似乎正在使用'.'作为function的结尾。如果您将其添加到function规则:

function
  :  ID '(' args ')' '->' statement (',' statement)* '.'
  ;

你根本不需要谓词。