[antlr-interest] How to create many AST trees from one rule
Indhu Bharathi
indhu.b at s7software.com
Fri Oct 30 10:46:20 PDT 2009
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: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Jared Klumpp
Sent: Friday, October 30, 2009 1:14 AM
To: antlr-interest at antlr.org
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20091030/af75fbb8/attachment.html
More information about the antlr-interest
mailing list