[antlr-interest] tree to tree rewrites

Terence Parr parrt at cs.usfca.edu
Mon Jun 27 11:39:56 PDT 2005


Howdy,

Ok, so it looks like I have tree parsing working nicely. :)  The code  
generation templates are essentially identical so it was fairly  
straightforward--I tricked the LL(*) analyzer into seeing A B as  
different from ^(A B) by adding imaginary navigation nodes DOWN and  
UP.  Works great.

Now I've got to make a decision about rewrites in a tree parser.   
I.e., given a tree and option output=AST, should the tree parser  
build a completely new tree or should it just rewrite the current  
one?  I'm trying to think of a situation where you want to keep a  
tree intact during a multiple phase translation.  I suppose debugging  
is one reason, but then if you want to keep that tree you can easily  
just do a dupTree on it before you pass it to the rewrite phase.   
Further, that dup will be faster than having the parser dup nodes.

So, imagine you have

tree parser t;
options {output=AST;}
decl : ^(DECL type declarator) ;
type : "int" | "float" ;
declarator
      : ID -> ^(ASSIGN ID INT["0"])
      ;

This parses trees like

   DECL
   / \
int  a

and rewrites them to

   DECL
   /  \
int  ASSIGN
        / \
       a   0

The DECL and type nodes should stay intact; only the declarator  
result has changed.  The ID node will be *reused* but merged into a  
new ^(ASSIGN ID INT["0"]) subtree that has two imaginary nodes.  The  
result becomes the new declarator tree up in decl.

Anybody see a reason why this "rewrite rather than completely new  
copy" concept should not be the functionality for tree parsers?

Ter
--
CS Professor & Grad Director, University of San Francisco
Creator, ANTLR Parser Generator, http://www.antlr.org
Cofounder, http://www.jguru.com



More information about the antlr-interest mailing list