Use scope variables to remember what you saw in a rule and emit it in
another rule further down in the chain. This will create the tree you want.
But I feel 'TYPE' node is not needed. It is good to build your AST as terse
as possible.

 

grammar Test;

options {

                output=AST;

}

 

tokens {

                DECL;

                TYPE;

}

 

decl

scope {

                Token typeTkn;

}

                :               type {$decl::typeTkn=$type.start;} varList
-> varList

                ;

                

type:     INT

                ;

                

varList

                :               var (COMMA var)* -> var+

                ;

                

var          :               ID -> ^(DECL ID ^(TYPE {$decl::typeTkn}))

                ;

                

INT         :               'int'

                ;

 

ID            :               'a'..'z'+

                ;

                

COMMA

                :               ','

                ;

 

WS         :               (' ' | '\t' | '\n')+ {$channel=HIDDEN;}

                ;

                

 

Cheers, Indhu

S7 Software

 

 

From: [email protected]
[mailto:[email protected]] On Behalf Of Jared Klumpp
Sent: Friday, October 30, 2009 1:14 AM
To: [email protected]
Subject: [antlr-interest] How to create many AST trees from one rule

 

I'm trying to create a new DECL for each identifier - if this were C, int a,
b; would become (DECL A (TYPE INT)) (DECL B (TYPE INT)).

My rules (untested):
decl: type IDENTS SEMI
-> ^(DECL idents ^(TYPE $type));

idents: IDENT (COMMA IDENT)*
-> IDENT+;

My problem is this creates (DECL A B (TYPE INT)), how can I split this into
multiple trees when idents collects the identifiers into one tree?

Thanks,
Jared 


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"il-antlr-interest" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en
-~----------~----~----~----~------~----~------~--~---

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

Reply via email to