[antlr-interest] AST cardinality from called rules

Michael Bedward michael.bedward at gmail.com
Tue Apr 5 19:09:24 PDT 2011


Hi Graham

Does this give you what you want ?

localVariableDeclaration
   : LOCAL! variableDeclarationList
   ;

variableDeclarationList
   : variableDcl ( ',' variableDcl )* -> ^(LOCAL variableDcl)+
   ;

If you also use the variableDeclarationList rule for non-local
declarations you could give it a parameter...

localVariableDeclaration
   : LOCAL! variableDeclarationList[LOCAL]
   ;

variableDeclarationList[int type]
   : variableDcl ( ',' variableDcl )*
   -> {type == LOCAL}? ^(LOCAL variableDcl)+
   -> ^(OTHER variableDcl)+
   ;


Michael


On 6 April 2011 07:39, Graham Mer <gd.antlr at gmail.com> wrote:
> Hello list,
>
> The following rule generates the AST that I want:
>
> localVariableDeclaration
>    : LOCAL variableDcl ( ',' variableDcl )* -> ^(LOCAL variableDcl)+
>    ;
>
> But how do I generate an equivalent AST if the variableDcl list is not
> in the localVariableDeclaration rule, as in the following fragment?
>
> localVariableDeclaration
>    : LOCAL variableDeclarationList
>    ;
>
> variableDeclarationList
>    : variableDcl ( ',' variableDcl )*
>    ;
>
> I want to produce a set of ^(LOCAL variableDcl) subtrees, but I want
> to reuse the variableDeclarationList from other rules, so I'd rather
> not inline it.
>
> Thanks!
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>


More information about the antlr-interest mailing list