[antlr-interest] Why cant += be used without an "output=" option (and other Q's)

Benjamin Shropshire shro8822 at vandals.uidaho.edu
Tue Feb 19 10:24:55 PST 2008


Gavin Lambert wrote:

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

I'm not following that? Would that be something like adding "bs : b*;  
gs : g*; "? I'm not seeing the advantage of that. (It will most likely 
hit me here about 10 min after hitting send)

> And BTW: don't forget to hit the Reply All button, not just the Reply 
> button.
>
oops.



More information about the antlr-interest mailing list