|
View:
New views
17 Messages
—
Rating Filter:
Alert me
|
|
|
Parsing java source code from top to bottomHello All,
I am trying to parse java source code to create a xml tree of the source code. I would like to start working from top to bottom, that is if I have if(x==1){ int y = x+1; } I want to make "if(x==1)" the root and then { }, a child of the root, then int y = x+1, a child of the {}. Right now when I parse it I get it backward. That is, int y =x+1; then { } then if(x==1). BlockStatement then Block then IfStatement. Instead of IfStatement, Block and BlockStatement. How do I make it parse from top to bottom instead of working its way from bottom to top? I hope my question make sense. Thanks in advance. |
|
|
Re: Parsing java source code from top to bottomIt looks like you are visiting the nodes in the parse tree (assuming you are using jjtree) in the wrong order. You need to output the parent node before it's children.
On 26/08/2009, kponenation <kp1nation@...> wrote:
-- - J.Chris Findlay (c: |
|
|
Re: Parsing java source code from top to bottomThanks for the reply. Yes, I'm using jjtree.
The way I print is like this String tmp = ""; t = jjtn000.jjtGetFirstToken(); while(true){ if(tmp.equals("")){ tmp= t.image; } else{ if(t.image.equals(".") ||t.image.equals("(") ||t.image.equals(")") ){ tmp = tmp+ t.image; } else if(tmp.endsWith(".") || tmp.endsWith("(") || tmp.endsWith(")")){ tmp = tmp+t.image; } else{ tmp = tmp + " "+ t.image; } } if(t == jjtn000.jjtGetLastToken()){ break; } t = t.next; } System.out.println(tmp); And I added the above code for Block, BlockStatement, and IfStatement sections in my jjt file which I downloaded off of the web. I believe it's for Java grammer 1.6. Anyway, It prints in the order of BlockStatement, Block, and IfStatement. And also Block shows the content of BlockStatement inside { } and IfStatement shows the content of Block inside if(x==1). If I want to only display if(x==1) instead of the whole thing. What should I do and what can I do to make the program print IfStatement before anything else? Thanks in advance.
|
|
|
Re: Parsing java source code from top to bottomSo in other words you are not using the tree structure at all that jjtree gives you.
Where is that code? As if it's where I think it is then of course it'll print the children first. I meant, after the parse returns you the root node of the tree, walk it yourself, printing out stuff as needed. Also, below you completely ignore any special tokens that may be attached so if you use a grammar that uses MORE rather than SKIP then you'll miss them. See the docs about that. On Wed, Aug 26, 2009 at 9:10 AM, kponenation <kp1nation@...> wrote:
-- - J.Chris Findlay (c: |
|
|
Re: Parsing java source code from top to bottomThanks Chris.
So I should wait for the parser to parse the whole thing then try to traverse the tree structure instead of what I'm doing below? which I think is printing stuff out while it's parsing. Did I understand this correctly? Thanks again.
|
|
|
Re: Parsing java source code from top to bottomPretty much. In general if you need to work with the parse-tree you'll need to wait till you have it all.
On Wed, Aug 26, 2009 at 10:03 AM, kponenation <kp1nation@...> wrote:
-- - J.Chris Findlay (c: |
|
|
Re: Parsing java source code from top to bottomThanks. This might sound too stupid but how do you traverse the parse-tree once you have it?
|
|
|
Re: Parsing java source code from top to bottomEither by using the visitor pattern or by following the links from one node to the next (e.g. it's children array or whatever it's called).
On Wed, Aug 26, 2009 at 11:33 AM, kponenation <kp1nation@...> wrote:
-- - J.Chris Findlay (c: |
|
|
Re: Parsing java source code from top to bottomOn Aug 25, 2009, at 7:33 PM, kponenation wrote: > > Thanks. This might sound too stupid but how do you traverse the > parse-tree > once you have it? > You can see some small visitor examples if you download my book's example source code here: http://generatingparserswithjavacc.com/gpwj_examples.zip and unzip it and look in the examples/jjtree/calculator_visitor/ directory. Yours, Tom --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscribe@... For additional commands, e-mail: users-help@... |
|
|
Re: Parsing java source code from top to bottomThank you all.
I'll go try it and come ask questions if I have any. I really appreciate the help.
|
|
|
Re: Parsing java source code from top to bottomAfter reading your book and your sample source code.
I set MULTI and VISITOR options to true and tried to go through the tree once my program finishes parsing it. This is from my main method SimpleNode e = parser.CompilationUnit(); JavaVisitor j = new JavaVisitor(); e.jjtAccept(j, null); System.out.println(j.test); My jjt file generated JavaVisitor.java and JavaParserVisitorAdapter.java In JavaParserVisitorAdapter.java there is a visit method that takes ASTCompilationUnit public Object visit(ASTCompilationUnit node, Object data){ return node.childrenAccept(this, data);} And in JavaVisitor I've added a visit method public Object visit(ASTCompilationUnit node, Object data){ test += node.image+" aaaaaaaaaaaaaaaa "; return super.visit(node, data); } Just to see if it's working. But when I run it it loops forever. I've added some code to display its child nodes token image. public Object childrenAccept(JavaParserVisitor visitor, Object data) { if (children != null) { for (int i = 0; i < children.length; ++i) { children[i].jjtAccept(visitor, data); SimpleNode n = (SimpleNode)children[i]; System.out.println(n.image); } } return data; } It shows the parsed tree over and over again. What am I doing wrong here? I would really appreciate any advice. Thank you.
|
|
|
RE: Parsing java source code from top to bottomHi
The visit method you added in JavaVisitor probably creates the recursive loop. Check the visit / accept / ... calls. You should not add a visit method, but modify the existing one to produce the display. Marc MAZAS Phone : +33 (0)4 72 18 47.19 Mobile : +33 (0)6 89 86 50 56 -----Message d'origine----- De : kponenation [mailto:kp1nation@...] Envoyé : jeudi 27 août 2009 16:43 À : users@... Objet : Re: [JavaCC] Parsing java source code from top to bottom After reading your book and your sample source code. I set MULTI and VISITOR options to true and tried to go through the tree once my program finishes parsing it. This is from my main method SimpleNode e = parser.CompilationUnit(); JavaVisitor j = new JavaVisitor(); e.jjtAccept(j, null); System.out.println(j.test); My jjt file generated JavaVisitor.java and JavaParserVisitorAdapter.java In JavaParserVisitorAdapter.java there is a visit method that takes ASTCompilationUnit public Object visit(ASTCompilationUnit node, Object data){ return node.childrenAccept(this, data);} And in JavaVisitor I've added a visit method public Object visit(ASTCompilationUnit node, Object data){ test += node.image+" aaaaaaaaaaaaaaaa "; return super.visit(node, data); } Just to see if it's working. But when I run it it loops forever. I've added some code to display its child nodes token image. public Object childrenAccept(JavaParserVisitor visitor, Object data) { if (children != null) { for (int i = 0; i < children.length; ++i) { children[i].jjtAccept(visitor, data); SimpleNode n = (SimpleNode)children[i]; System.out.println(n.image); } } return data; } It shows the parsed tree over and over again. What am I doing wrong here? I would really appreciate any advice. Thank you. Tom Copeland wrote: > > > On Aug 25, 2009, at 7:33 PM, kponenation wrote: > >> >> Thanks. This might sound too stupid but how do you traverse the >> parse-tree once you have it? >> > > You can see some small visitor examples if you download my book's > example source code here: > > http://generatingparserswithjavacc.com/gpwj_examples.zip > > and unzip it and look in the examples/jjtree/calculator_visitor/ > directory. > > Yours, > > Tom > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: users-unsubscribe@... > For additional commands, e-mail: users-help@... > > > -- View this message in context: http://www.nabble.com/Parsing-java-source-code-from-top-to-bottom-tp25137797p25169600.html Sent from the java.net - javacc users mailing list archive at Nabble.com. --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscribe@... For additional commands, e-mail: users-help@... --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscribe@... For additional commands, e-mail: users-help@... |
|
|
RE: Parsing java source code from top to bottomThanks for your reply.
I've included super.visit in my JavaVisitor so that I could pass the control back to the super class so it can go visit other child nodes. This is my visit method in JavaVisitor. public Object visit(ASTCompilationUnit node, Object data){ test += node.image+" aaaaaaaaaaaaaaaa "; return super.visit(node, data); } And this is from JavaParserVisitorAdapter which is the superclass of JavaVisitor: public Object visit(ASTCompilationUnit node, Object data){ return node.childrenAccept(this, data); } I still don't get why it's looping. Thanks.
|
|
|
RE: Parsing java source code from top to bottomHi
Some things do not look clear in what you said (<My jjt file generated JavaVisitor.java and JavaParserVisitorAdapter.java/>) If in your jjt grammar you have PARSER_BEGIN(JavaParser), JJTree will generate the "JavaParserVisitor" interface, that's all. You have to code the JavaParserVisitorAdapter (which must implement the interface, and so visit all the nodes) and a visitor, say JavaVisitor (which must extend the adapter). Is that what you did ? Besides this, I would code in JavaVisitor just overloading the visit method, without calling the super class visit method, which is probably the origin of the recursion) public Object visit(ASTCompilationUnit node, Object data){ test += node.image+" aaaaaaaaaaaaaaaa "; return node.childrenAccept(this, data); } Marc MAZAS -----Message d'origine----- De : kponenation [mailto:kp1nation@...] Envoyé : jeudi 27 août 2009 22:28 À : users@.... Objet : RE: [JavaCC] Parsing java source code from top to bottom Thanks for your reply. I've included super.visit in my JavaVisitor so that I could pass the control back to the super class so it can go visit other child nodes. This is my visit method in JavaVisitor. public Object visit(ASTCompilationUnit node, Object data){ test += node.image+" aaaaaaaaaaaaaaaa "; return super.visit(node, data); } And this is from JavaParserVisitorAdapter which is the superclass of JavaVisitor: public Object visit(ASTCompilationUnit node, Object data){ return node.childrenAccept(this, data); } I still don't get why it's looping. Thanks. Mazas Marc wrote: > > Hi > The visit method you added in JavaVisitor probably creates the > recursive loop. Check the visit / accept / ... calls. > You should not add a visit method, but modify the existing one to > produce the display. > > > Marc MAZAS > Phone : +33 (0)4 72 18 47.19 > Mobile : +33 (0)6 89 86 50 56 > > > -----Message d'origine----- > De : kponenation [mailto:kp1nation@...] Envoyé : jeudi 27 août > 2009 16:43 À : users@... Objet : Re: [JavaCC] Parsing > java source code from top to bottom > > > After reading your book and your sample source code. > I set MULTI and VISITOR options to true and tried to go through the > tree once my program finishes parsing it. > > This is from my main method > > SimpleNode e = parser.CompilationUnit(); JavaVisitor j = new > JavaVisitor(); e.jjtAccept(j, null); System.out.println(j.test); > > My jjt file generated JavaVisitor.java and > JavaParserVisitorAdapter.java > > In JavaParserVisitorAdapter.java there is a visit method that takes > ASTCompilationUnit > > public Object visit(ASTCompilationUnit node, Object data){ > return node.childrenAccept(this, data);} > > And in JavaVisitor I've added a visit method > > public Object visit(ASTCompilationUnit node, Object data){ > test += node.image+" aaaaaaaaaaaaaaaa "; > return super.visit(node, data); > } > > Just to see if it's working. > > But when I run it it loops forever. I've added some code to display > its child nodes token image. > > public Object childrenAccept(JavaParserVisitor visitor, Object data) { > if (children != null) { > for (int i = 0; i < children.length; ++i) { > children[i].jjtAccept(visitor, data); > SimpleNode n = (SimpleNode)children[i]; > System.out.println(n.image); > } > } > return data; > } > > It shows the parsed tree over and over again. > What am I doing wrong here? > I would really appreciate any advice. > > Thank you. > > > > > Tom Copeland wrote: >> >> >> On Aug 25, 2009, at 7:33 PM, kponenation wrote: >> >>> >>> Thanks. This might sound too stupid but how do you traverse the >>> parse-tree once you have it? >>> >> >> You can see some small visitor examples if you download my book's >> example source code here: >> >> http://generatingparserswithjavacc.com/gpwj_examples.zip >> >> and unzip it and look in the examples/jjtree/calculator_visitor/ >> directory. >> >> Yours, >> >> Tom >> >> >> --------------------------------------------------------------------- >> To unsubscribe, e-mail: users-unsubscribe@... >> For additional commands, e-mail: users-help@... >> >> >> > > -- > View this message in context: > http://www.nabble.com/Parsing-java-source-code-from-top-to-bottom-tp25 > 137797p25169600.html Sent from the java.net - javacc users mailing > list archive at Nabble.com. > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: users-unsubscribe@... > For additional commands, e-mail: users-help@... > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: users-unsubscribe@... > For additional commands, e-mail: users-help@... > > > -- View this message in context: http://www.nabble.com/Parsing-java-source-code-from-top-to-bottom-tp25137797p25179368.html Sent from the java.net - javacc users mailing list archive at Nabble.com. --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscribe@... For additional commands, e-mail: users-help@... --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscribe@... For additional commands, e-mail: users-help@... |
|
|
RE: Parsing java source code from top to bottomThanks Marc.
Yes, I created/implemented JavaParserVisitorAdapter and JavaVisitor. Sorry for the confusion. I just tried what you just suggested by returning node.childrenAccept(this, data) instead of calling the visit method in the super class. Now, the program halts after reading "package blahblah;" Any suggestions? thanks, James
|
|
|
RE: Parsing java source code from top to bottom< the program halts after reading "package blahblah /> ??? Or "the program displays only "package blahblah"" ??? Add your trace in all visit methods (ie for all nodes) in your JavaVisitor, and make sure your adapter also visits all the nodes, otherwise you'll see only what happens in only the nodes that are visited. Marc MAZAS -----Message d'origine----- De : kponenation [mailto:kp1nation@...] Envoyé : vendredi 28 août 2009 15:46 À : users@... Objet : RE: [JavaCC] Parsing java source code from top to bottom Thanks Marc. Yes, I created/implemented JavaParserVisitorAdapter and JavaVisitor. Sorry for the confusion. I just tried what you just suggested by returning node.childrenAccept(this, data) instead of calling the visit method in the super class. Now, the program halts after reading "package blahblah;" Any suggestions? thanks, James Hi Some things do not look clear in what you said (<My jjt file generated JavaVisitor.java and JavaParserVisitorAdapter.java/>) If in your jjt grammar you have PARSER_BEGIN(JavaParser), JJTree will generate the "JavaParserVisitor" interface, that's all. You have to code the JavaParserVisitorAdapter (which must implement the interface, and so visit all the nodes) and a visitor, say JavaVisitor (which must extend the adapter). Is that what you did ? Besides this, I would code in JavaVisitor just overloading the visit method, without calling the super class visit method, which is probably the origin of the recursion) public Object visit(ASTCompilationUnit node, Object data){ test += node.image+" aaaaaaaaaaaaaaaa "; return node.childrenAccept(this, data); } Marc MAZAS -- View this message in context: http://www.nabble.com/Parsing-java-source-code-from-top-to-bottom-tp25137797p25190186.html Sent from the java.net - javacc users mailing list archive at Nabble.com. --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscribe@... For additional commands, e-mail: users-help@... --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscribe@... For additional commands, e-mail: users-help@... |
|
|
RE: Parsing java source code from top to bottomSorry. I should have said that the program displays only package blahblah and it just hangs instead of halting. I'll see what I can find out.
Thanks again for the advice. James
|
| Free embeddable forum powered by Nabble | Forum Help |