[antlr-interest] partial re-write rules

Jim Idle jimi at temporal-wave.com
Tue Jan 20 13:15:43 PST 2009


Gavin Lambert wrote:
> 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 :)
>
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>   

rule
   : '{'
       s=semi stmt* -> ^($s curly stmt*)
   ;

semi
 : ';' -> FIRST
 | -> SECOND
 ;


Jim


More information about the antlr-interest mailing list