[antlr-interest] Rewrite rules from alternative tokens, how?

Johannes Luber jaluber at gmx.de
Sun Sep 21 07:25:57 PDT 2008


Blake Friedman schrieb:
> Hi,
> 
> I've run into problems trying to do something like this:
> 
> options {PREFIX;}
> 
> prefix : a=('-' | '+') prefix -> ^(PREFIX $a prefix)
>           | int
>           ;
> int : '0'..'9'+
> 
> To generate trees such as:
> 
> (FACTOR  - (FACTOR - 12))
> 
> Which is to say, how do I identify which token was selected and add it
> to my rewrite rule?
> 
> At the moment I've identified two workarounds:
> 
> prefix : '-' prefix -> ^(PREFIX '-' prefix)
>           | '+' prefix -> ^(PREFIX '+' prefix)
>           | int
>           ;
> 
> and
> 
> prefix : umOK prefix -> ^(PREFIX umok prefix)
>           | int
>           ;
> umOK : ('-'|'+') ;
> 
> The first isn't great, and the second is OK (but I'm not wild about
> it).  I've been scratching around on the online documentation and
> Terence's book, but I'm a little overwhelmed :).

The original version doesn't work because of a bug. You also managed to
determine the alternatives, so I don't know what to add here.

Johannes
> 
> Thanks
> -Blake
> 
> P.S. Apologies for the cryptic subject, wasn't all that sure how to
> describe the problem.
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
> 



More information about the antlr-interest mailing list