[antlr-interest] Help: What does this mean?

John B. Brodie jbb at acm.org
Sat Nov 17 06:13:54 PST 2007


Hello

Austin Hastings asked:
>I get this warning:
>
>   [antlr3] warning(200): 
>/home/austin/gunit/sources/org/antlr/gunit/TestSuite.g:504:51: Decision 
>can match input such as "'&&'" using multiple alternatives: 1, 2
>   [antlr3] As a result, alternative(s) 2 were disabled for that input
>
>
>For this code:
>
>outputExpr_and returns [Assertion assertion]
>    : op1=outputExpr_primary ('&&' op2=outputExpr_and)*
...snipped...
>    ;
>
>I don't see two alternatives, unless the * is considered an alternative 
>somehow. The 'outputExpr_primary' production is always something 
>concrete, either a parenthesized subexpression or a list of keywords - 
>never empty. So (1) what are "alternatives 1,2" for this case; and (2) 
>how could I display them - is there a "painfully verbose" antlr switch I 
>don't know about?

Yes the * operator generates 2 alternatives. Think about what * means
e.g. "zero or more occurances". There must be some kind of choice
(alternative) in there somewhere.

these two rules are equivalent:

repeat : ( item )* ;
recurse : /*empty*/ | item recurse ;

i believe that ANTLR replaces * with its recursive equivalent during
its analysis.

In your rule you have both recursion and repetition and is thus
ambiguous. E.g. when the next oE_primary is seen after the '&&', we do
not know wheter to recurse starting a new oE_and or to continue
the repetition of the current oE_and.

You should probably either do

outputExpr_and : outputExpr_primary ( '&&' outputExpr_and )? ;

or

outputExpr_and : outputExpr_primary ( '&&' outputExpr_primary )* ;

HTH
   -jbb


More information about the antlr-interest mailing list