[antlr-interest] ANTLR3 Mapping AST Tree to StringTemplate

Terence Parr parrt at cs.usfca.edu
Tue Mar 20 13:09:05 PDT 2007


On Mar 20, 2007, at 11:25 AM, Kailey Joanette wrote:
> Correct me if I'm wrong but the syntax for doing what I want with  
> an AST
> would look something like this:
>
> modifier returns [StringTemplate code=template("access")]
>         :
>         {StringTemplate e1=null;}
>                (
>                        ^(MODIFIER name='public') {code.setAttribute 
> ("name",
> e1);}
>             |   ^(MODIFIER name='protected') {code.setAttribute 
> ("name",
> e1);}
>             |   ^(MODIFIER name='private') {code.setAttribute 
> ("name", e1);}
>             |   ^(MODIFIER name='static') {code.setAttribute 
> ("name", e1);}
>             |   ^(MODIFIER name='abstract') {code.setAttribute 
> ("name", e1);}
>             |   ^(MODIFIER name='final') {code.setAttribute("name",  
> e1);}
>             |   ^(MODIFIER name='native') {code.setAttribute 
> ("name", e1);}
>             |   ^(MODIFIER name='synchronized') {code.setAttribute 
> ("name",
> e1);}
>             |   ^(MODIFIER name='transient') {code.setAttribute 
> ("name",
> e1);}
>             |   ^(MODIFIER name='volatile') {code.setAttribute 
> ("name", e1);}
>             |   ^(MODIFIER name='strictfp') {code.setAttribute 
> ("name", e1);}
>            )
>         ;

I'd do

options {output=template;}
modifier
	:	^(MODIFIER m=('protected'|...)) -> access(name={$m.text})
	;

The m= might be an issue. if so, add an action that grabs input.LT(1)

> Now if I could just figure out how to manipulate my AST trees.  
> Being this is
> the first time I've ever had to do any language parsing/translating
> whatsoever.

ANTLR v3 can currently only parse trees not rewrite them...pissing me  
off as I need it myself, but must get v3 done; features frozen.

Ter




More information about the antlr-interest mailing list