[antlr-interest] Setting return values

Benjamin Niemann pink at odahoda.de
Tue Jul 24 06:51:17 PDT 2007


kevin_mitchell at agilent.com wrote:

> I've been trying to port my grammar from V2 to V3, which I'm beginning to
> regret... :-(  I use a lot of arguments and values in my grammar, and I'm
> beginning to wonder whether I've misunderstood how V3 is supposed to work,
> because at first glance my grammar is becoming a lot more cumbersome than
> the V2 version.  Here's a concrete example.  In V2 I might have written
> something like
> 
> declaration returns [Declaration d] :
>   d=valueDeclaration
> | d=simpleTypeDeclaration
> | d=genericTypeDeclaration
> ;
> 
> But in V3 it looks like I now have to write something like
> 
> declaration returns [Declaration d]
> @after { $d = dec; }
> : dec=valueDeclaration
> | dec=simpleTypeDeclaration
> | dec=genericTypeDeclaration
> ;
> 
> I.e. I have to use an auxiliary name, dec in this case, and then assign to
> $d at the end.  Is this correct, or have I misunderstood something?

Correct.
Though it would be a nice feature, if you could directly assign to return
values as in your first snipped. I'd say that this is a pretty frequent use
case that should be supported. That would have to take care of multiple
return values though, if it should work generally.

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://pink.odahoda.de/



More information about the antlr-interest mailing list