[antlr-interest] Rewrite rule for multiple alternatives

Terence Parr parrt at cs.usfca.edu
Thu Aug 2 12:56:28 PDT 2007


On Aug 2, 2007, at 12:46 PM, Andy Tripp wrote:

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

Here ya go :)

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

Ter


More information about the antlr-interest mailing list