[antlr-interest] Transforming trees: modify input tree

Monty Zukowski monty at codetransform.com
Wed Apr 21 10:24:44 PDT 2004


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