[antlr-interest] Rewrite rule for multiple alternatives

Andy Tripp antlr at jazillian.com
Thu Aug 2 12:46:22 PDT 2007


Gavin Lambert wrote:
> At 00:46 3/08/2007, Jan Obdrzalek wrote:
>> what is the correct way to write this rewrite rule?
>>
>>    :    '{' (a | b | c)* '}'  -> ^(COMPOUND (a | b | c)*)
>
> Haven't tried it, but this ought to work:
>
> x : '{' y* '}' -> ^(COMPOUND y*);
> y : a | b | c;

I find myself doing this refactoring a lot. It's a shame having to 
refactor the grammar
to make the rewrite rule do what you want. I wonder if ANTLR could be 
enhanced
to allow the sort of thing that Jan has above? If not, how about 
allowing him to assign
a name to any arbirarily complex expression on the left like this:

'{' mystuff=(a | b | c)* '}'  -> ^(COMPOUND $mystuff )



More information about the antlr-interest mailing list