[antlr-interest] AST cardinality from called rules

Sam Harwell sharwell at pixelminegames.com
Tue Apr 5 18:42:51 PDT 2011


The AST operators are the most efficient manner:

localVariableDeclaration
    : LOCAL^ variableDeclarationList
    ;

variableDeclarationList
    : variableDcl ( ','! variableDcl )*
    ;

Or use rewrites:

localVariableDeclaration
    : LOCAL variableDeclarationList -> ^(LOCAL variableDeclarationList)
    ;

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

-----Original Message-----
From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Graham Mer
Sent: Tuesday, April 05, 2011 4:40 PM
To: antlr-interest at antlr.org
Subject: [antlr-interest] AST cardinality from called rules

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