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

Blake Friedman blake.friedman at gmail.com
Sun Sep 21 07:07:52 PDT 2008


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

Thanks
-Blake

P.S. Apologies for the cryptic subject, wasn't all that sure how to
describe the problem.


More information about the antlr-interest mailing list