[antlr-interest] Why cant += be used without an "output=" option (and other Q's)
Gavin Lambert
antlr at mirality.co.nz
Tue Feb 19 03:07:13 PST 2008
At 14:32 19/02/2008, Benjamin Shropshire wrote (off-list):
>I'm guessing from that statement that something like this would
be
>done
>
>a returns [MyType c] :
> (b {if(c == null) c = new MyType(); c.accumulateB($b);
})*
> (g {if(c == null) c = new MyType(); c.accumulateG($g);
})*
> ;
Well, you could do that, but probably a more common use would be:
a returns [MyType c]
@init { $c = new MyType(); }
: ( b { $c.accumulateB($b); }
| g { $c.accumulateG($g); }
)*
;
Note: I changed the grammar slightly to permit 'b's and 'g's to be
intermixed. If they're strictly ordered then things get even
simpler than this, since they can be factored out to type-specific
subrules. Also this version will return an 'empty' object instead
of a null when no items are present, but that seems reasonable to
me.
And BTW: don't forget to hit the Reply All button, not just the
Reply button.
More information about the antlr-interest
mailing list