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

Sam Harwell sharwell at pixelminegames.com
Sat Aug 2 15:34:08 PDT 2008


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)
		)*
	;

However, if I add a $ to either logical_and_expression in the rewrite
rules, it is an error. The enclosing rule is automatically a label (can
be referenced here as $logical_or_expression), so the only way to have
the $-sign/lack-there-of be ambiguous would be to write something
dreadful such as:

logical_or_expression
	:	(	logical_and_expression
			-> ^(logical_and_expression)
		)
		(	'||'
logical_or_expression=logical_and_expression
			-> ^(AST_OR $logical_or_expression
logical_and_expression)
		)*
	;

Is there another case I'm missing where the rule in my original email is
actually ambiguous?

Sam

-----Original Message-----
From: Terence Parr [mailto:parrt at cs.usfca.edu] 
Sent: Saturday, August 02, 2008 1:41 PM
To: Sam Harwell
Cc: antlr-interest
Subject: Re: [antlr-interest] Bug with "ambiguous rules" check

Hi Sam,
$r can refer to r reference or enclosing rule r in recursive rules.   
The error is correct I believe.
T
On Aug 2, 2008, at 10:35 AM, Sam Harwell wrote:

> The following code generates an error, even though the rule is not  
> ambiguous. As far as I can tell, this rule shouldn't even produce a  
> warning. If the $ isn't enough to make the code clear (visually; it  
> already is syntactically), we need a keyword to reference the  
> enclosing rule (which might not be a bad idea anyway). The error is  
> reported in codegen.g, and removing the if statement that checks for  
> it let my grammar compile/work again :)
>
> 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)
>         ;
>
> ANTLR Parser Generator  Version 3.1b2 (July 17, 2008)  1989-2008
> Grammar.g3(6159,29): error 132: reference $assignment_expression is  
> ambiguous; rule assignment_expression is enclosing rule and  
> referenced in the production (assuming enclosing rule)
> Grammar.g3(6161,25): error 132: reference $assignment_expression is  
> ambiguous; rule assignment_expression is enclosing rule and  
> referenced in the production (assuming enclosing rule)
>
>



More information about the antlr-interest mailing list