[antlr-interest] Keyword for referencing the enclosing rule in rewrite scope?

Gavin Lambert antlr at mirality.co.nz
Wed Aug 13 13:25:43 PDT 2008


At 07:03 14/08/2008, Sam Harwell wrote:
>As I mentioned before, this construct as it exists in v3.1 will 
>give a compile error even when a label reference is unambiguous 
>according to the language rules. The error's purpose is solely to 
>alert the user to the possibility they meant something other than 
>what they wrote (could be a warning?). To prevent the problem, I 
>propose adding a special keyword to consistently designate the 
>enclosing rule's label in the rewrite scope.

It's not '$rule' that's ambiguous, it's the 'rule' in the tree 
rewrite.  So the reference *is* ambiguous according to the 
language rules.

>rule
>:               (              ID
>                                 -> ^(ID)
>                 )
>                 (              NUMBER rule
>                                 ->            ^(NUMBER $rule 
> rule)
>                                 )?
>                 ;

If I recall a previous discussion on this correctly, ANTLR 
produces a warning and also different behaviour than what you 
might be expecting ('rule' is treated equivalent to '$rule' in 
that context, IIRC).

Adding a new keyword like you propose might be useful in other 
cases, but it still wouldn't remove this ambiguity.  As Ter said, 
the right thing to do here is to add a label to your use of 'rule' 
as a subrule and use that in the tree construction instead.



More information about the antlr-interest mailing list