[antlr-interest] partial re-write rules

Robert Soule robert.soule at gmail.com
Tue Jan 20 12:16:58 PST 2009


Hi,

I am trying to use re-write rules to build an AST.  In one part
of my grammar, I would like to produce a different AST node
depending on if there is a ';' literal or not.

curly :  '{' blockOrLiteral '}'  -> ^(CURLY blockOrLiteral);

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?

thank you,
Robert


More information about the antlr-interest mailing list