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

Johannes Luber jaluber at gmx.de
Sun Aug 17 16:01:50 PDT 2008


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


More information about the antlr-interest mailing list