[antlr-interest] How to return my own objects ?

Johannes Luber jaluber at gmx.de
Sun Aug 17 16:18:43 PDT 2008


Le Lann Jean-Christophe schrieb:
> Johannes Luber a écrit :
>> Le Lann Jean-Christophe schrieb:
>>   
>>> Hello,
>>>
>>> I am struggling to return my own objects in the rules (v3.1). Normally
>>> this is obvious like :
>>>
>>> expr returns [MyExpr expr] :
>>>    ^( '+' e1=expr e2=expr) { $expr = new MyExpr("+",e1,e2);}
>>>    | INT                   { $expr = new MyExpr(new String($INT.text));}
>>>        ;
>>> this perfectly generates (MyExpr returned) :
>>> // DumpModified.g:8:1: expr returns [MyExpr expr] : ( ^( '+' e1= expr
>>> e2= expr ) | INT );
>>>    public final MyExpr expr() throws RecognitionException {
>>>
>>> but in a bigger grammar in which I have this rule :
>>>
>>> architecture returns [Architecture arch] :
>>>                    ^(ARCHITECTURE ID <skipped>)            {$arch=new
>>> Architecture(new String(ID.text));}
>>>
>>> the generated code is :
>>>
>>> // Walker.g:23:1: architecture returns [Architecture arch] : ^(
>>> ARCHITECTURE ID <skipped>) ;
>>>    public final Walker.architecture_return architecture() throws
>>> RecognitionException {
>>>
>>> ==> "Walker.architecture_return" returned while I expected "Architecture"
>>>
>>> What I am missing ? Could you explain or give me a pointer (I have the
>>> book) ?
>>>     
>>
>> architecture_return is a generated type, which encapsulates an
>> Architecture object. ANTLR hasn't been optimized yet to not to do that
>> if only one object is returned from a rule.
>>
>> Johannes
>>   
> I am not sure to understand : why did I get a 'correct' code in the
> first case ? What is the difference between the two examples above ?
> Do you mean that it is the alternative in the first case that caused
> antlr to generate a correct code ?
> 
> Thx again.
> JC
>  
I don't know why one rule behaves differently (is "<skipped>" verbatim
in the file?), but at least you can access the Architecture object,
can't you?

Johannes


More information about the antlr-interest mailing list