[antlr-interest] Bug with "ambiguous rules" check

Gavin Lambert antlr at mirality.co.nz
Sat Aug 2 17:45:32 PDT 2008


At 10:34 3/08/2008, Sam Harwell wrote:
 >I think I may confused about a piece of the syntax. As a 
reference,
 >the following block is valid:
 >
 >logical_or_expression
 >	:	(	logical_and_expression
 >			-> ^(logical_and_expression)
 >		)
 >		(	'||' logical_and_expression
 >			-> ^(AST_OR $logical_or_expression
 >logical_and_expression)
 >		)*
 >	;

Yes, because you're not re-using anything, so there's no 
ambiguity.

 >> assignment_expression
 >> // left-factoring the assignment expression and
 >> conditional_expression for speed
 >>         :       (       logical_or_expression
 >>                         -> ^(logical_or_expression)
 >>                 )
 >>                 (       assignment_operator
 >>                         assignment_expression
 >>                         -> ^(assignment_operator
 >> $assignment_expression assignment_expression)
 >>                 |       '?' expression ':' 
assignment_expression
 >>                         -> ^(AST_CONDITIONAL
 >> $assignment_expression expression assignment_expression)
 >>                 )?
 >>         |       throw_expression
 >>                 -> ^(throw_expression)
 >>         ;

I think the issue here is that ANTLR is worried that you might 
have typed 'assignment_expression' when you really meant 
'$assignment_expression'.

If you make it more explicit then the warning should go away:

assignment_expression
          :       (       logical_or_expression
                          -> ^(logical_or_expression)
                  )
                  (       assignment_operator
                          a=assignment_expression
                          -> ^(assignment_operator
                              $assignment_expression $a)
                  |       '?' expression ':' 
a=assignment_expression
                          -> ^(AST_CONDITIONAL
                              $assignment_expression expression 
$a)
                  )?
          |       throw_expression
                  -> ^(throw_expression)
          ;

(And incidentally: why are the true-part and the false-part of the 
ternary operator [assuming that's what that is] not using the same 
rule?  Surely anything permitted in one should be permitted in the 
other?)



More information about the antlr-interest mailing list