[antlr-interest] partial re-write rules

Gavin Lambert antlr at mirality.co.nz
Tue Jan 20 12:37:48 PST 2009


At 09:16 21/01/2009, Robert Soule wrote:
 >blockOrLiteral :
 >     ('{') => curly ';'? stmt*          // This is the trouble
 >      |    // some other choices;
 >
 >Here, if the ';' exists, I want to create one node, but if not, 
I
 >want to create a different node. I can write this as:
 >
 >   ('{') => curly ';' stmt*     -> ^(FIRST curly stmt*)
 > | ('{') => curly  stmt*       -> ^(SECOND curly stmt*)
 > | // more choices
 >
 >But this syntax seems a bit cumbersome. Is there a better way to 

 >express this?

Well, it's better to left-factor it, like so:

rule
   : ('{') => curly
       ( ';' stmt* -> ^(FIRST curly stmt*)
       | stmt* -> ^(SECOND curly stmt*)
       )
   | /* other stuff */
   ;

But that's even more cumbersome, if anything :)



More information about the antlr-interest mailing list