[antlr-interest] Transforming trees: modify input tree

Adrian Tineo tineo at ac.uma.es
Wed Apr 21 10:53:11 PDT 2004


Thanks for your answer Monty.

I'd prefer to keep the proper line number with the nodes rather than modifying 
the new line behaviour of the emitter, I think it is more organized and that 
it could grow better. 

Nevertheless, I have been thinking about ways of handling the tree. I am 
currently trying to write a transform-treeparser in a by-hand fashion because 
I need to move freely in the tree and not only with the subtrees resulting 
from rules.

My approach is to get certain kinds of nodes with findAllPartial and exchange 
nodes as needed. This way I also get freedom to update line numbers as I go.

I am going to need the double-linked facility of the TNode class provided to 
go back and forth from certain nodes, but from what I read is "currently 
incomplete". What do I need to add/change to implement it?

Thanks for your support, and great work with the parsing and tree 
generation ;=)

Adrian Tineo

El Miércoles, 21 de Abril de 2004 19:24, Monty Zukowski escribió:
> You can walk through the tree, updating line numbers as needed, or you
> can modify the newline handling code of GnuCEmitter.g to do what you
> want.  It's kind of tricky to mix line number preservation and new
> code.
>
> The emitter is designed to print newlines or #line directives only when
> it sees that the line number has changed from what was last printed.  A
> different strategy might be more appropriate in your case.
>
> Monty
>
> On Apr 21, 2004, at 4:46 AM, Adrian Tineo wrote:
> > Hello there
> >
> > I am using the GNU C grammar available at antlr.org. Thanks to this
> > grammar I
> > can easily create a tree for a C program. I need to modify that tree
> > and
> > print it back to code. Fortunately, the GNU C grammar comes with a
> > GnuCEmittter which does just that, it prints out a tree back to code.
> >
> > If I have
> > 	line i:	a=b;
> > 	line i+1:   (left blank)
> > 	line i+2:	return 0;
> > I want to have
> > 	line i:	tmp=b;
> > 	line i+1:	a=tmp;
> > 	line i+2:	tmp=NULL;
> > 	line i+3:	(left blank)
> > 	line i+4:	return 0;
> >
> > As you can see I need to change the node for a=b for three nodes, that
> > I want
> > to print to different lines and still get the proper separation with
> > the
> > following code.
> >
> > But I get
> > 	line i:	tmp=b;
> > 	line i+1:	a=tmp;
> > 	line i+2:	tmp=NULL; return 0;
> >
> > I can easily specify the line numbers for the newly created nodes but
> > in order
> > to keep the separations right, I would need to update the line number
> > for
> > every node that comes afterwards.
> >
> > As I am extending the treeparser grammar and don't want to override
> > the rules
> > I don't need to work with, I would need to update the line numbers in
> > the
> > INPUT tree so automatic tree generation for the rules I don't override
> > would
> > copy the rest of the input nodes(with the updated line numbers) to the
> > ouput
> > tree.
> >
> > And the question is, how can I do that? How can I have access to the
> > input
> > tree for an AST-transforming treeparser?
> > Or is there a better way to handle this problem?
> >
> > Any help appreciated. Thanks.
> >
> > Adrian Tineo
> >
> >
> >
> >
> > Yahoo! Groups Links
>
> Monty Zukowski
> ANTLR & Java Consultant
> http://www.codetransform.com



 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
     http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
     antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
     http://docs.yahoo.com/info/terms/
 



More information about the antlr-interest mailing list