[antlr-interest] Tree grammar bizarre behaviour?

Gavin Lambert antlr at mirality.co.nz
Mon Feb 8 00:02:32 PST 2010


At 13:34 8/02/2010, Iwud H8u wrote:
 >token1 returns [ObjectA a]
 >    :  ^(C_ID (conId=classId
 >{$a=evaluateAndReturnAsObjectA($conId.text); })
 >);
 >
 >token2 returns [ObjectA m]
 >    :  ^(C_ID (id=classId 
{$m=evaluateAndReturnAsObjectA($id.text);
 >}) );
[...]
 >Oddly, the Java methods generated for the methods have the
 >following return types :
 > --- MyTree.token1_return token1() { ... }
 > --- ObjectA token2() { .. }
 >
 >I'm slightly at my wits end, understanding whats going on. What 
am
 >I missing here?

Generally, a rule will return a value directly if it only has one 
return value, or a structure containing all the return values if 
it has more than one.  Evidently something about the token1 rule 
is making it want to return multiple values; perhaps it's the 
context in which it's used in another rule?  If you have a look at 
the contents of the structure it might give you an additional 
hint.

If it does return a *_return structure, then it will have a member 
matching the declared value.  (ie. token1_return will contain an 
ObjectA called "a".)



More information about the antlr-interest mailing list