How can I write out the same java code that I recieved as input using tree walker ??
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Jhon_Smith
Posted On:   Thursday, August 8, 2002 07:06 PM

Hi, I use antlr tools (parser, tree walker) to parse and walk java code and sometimes I need to write out the same java code. I don't know well tree walker to do that. For example: code java : int x=1; int y=3; int z=x+y; I want to write in the output file the same code. the problem that I have with tree walker is to write exactly the same code but the walker writes the output as: int x=1; int y=3; x+y int z=+; I don't know well tree walker and how can I use it in the right way to output the same code. here are the rules in tree walker : expr returns [String tt] {String text="";tt="";} : #(ASSIGN a:expr b:expr)    More>>

Hi,

I use antlr tools (parser, tree walker) to parse and walk java code and sometimes I need to write out the same java code. I don't know well tree walker to do that. For example:

code java :
int x=1;
int y=3;
int z=x+y;

I want to write in the output file the same code. the problem that I have with tree walker is to write exactly the same code but the walker writes the output as:

int x=1;
int y=3;
x+y
int z=+;

I don't know well tree walker and how can I use it in the right way to output the same code. here are the rules in tree walker :

expr returns [String tt] {String text="";tt="";} :
#(ASSIGN a:expr b:expr) {text=" "+a.getText()+"="+b.getText()+text+";"; System.out.println(text); tt+=text;}

| #(PLUS a26:expr b26:expr) {text=" "+a26.getText()+"+"+b26.getText()+text+";";System.out.println(text);tt+=text;}


thank you for any help!
John.

   <<Less

Re: How can I write out the same java code that I recieved as input using tree walker ??

Posted By:   Andy_Tripp  
Posted On:   Wednesday, August 14, 2002 04:31 PM

John,


I've been working on a JavaEmitter that walks a Java AST, printing it out as Java code.
It's actually quite simple, but I'm having trouble getting rid of annoying little spacing
problems.
I'll share it once I've gotten it more robust.


I just hit a snag on this though, so here's a question for the ANTLR gurus:
There is no "STATEMENT" node in the Java AST because of the way the grammar's
defined - the rule for "statement" doesn't get its own node. One of the alternatives
for statment is an EXPRESSION followed by SEMI. I need to output a SEMI
when I see an EXPRESSION node in the AST, but ONLY when the EXPRESSION node's
parent is a "STATEMENT", but there is no "STATEMENT" parent.


I could just look for an "SLIST" parent, but it seems kinda kludgy. Especially
since there's no built-in getParent() method in AST or CommonAST.


Would I need to change java.g to get around this, or am I missing something?


Andy

About | Sitemap | Contact