[antlr-interest] AST generation in a recursive rule

mzukowski at yci.com mzukowski at yci.com
Thu Oct 23 14:42:30 PDT 2003


That's an unusual tree structure, it's like you have down and right mixed
up.  Think siblings and children.  All the catch blocks would be siblings,
right?  I would recommend something like:

idCatch--(right)--> (another idCatch, etc.)
   |
(down)
   |
   v
Exception1--(right)-->e1--(right)-->block--(right)-->null

The code:

statement :
   TRY^ block catchBlocks 
      (idFINALLY:FINALLY! finallyBlock:block!
	   {## = #(##,#(idFINALLY,#finallyBlock))} )?
   ;
 
catchBlocks :
	(catchBlock)+
	{##=#(#[CATCH_BLOCK],##);}
    ;

catchBlock:
   (CATCH^ LPAREN! identifier IDENTIFIER RPAREN! block
   )+
   ;

Also, try using toStringList() to print out your tree, you'll get a better
idea of the structure that way.

catchBlocks uses a standard tree building idiom to root a tree with an
imaginary node (which you'll define in your tokens{} section)

Note I split your catchBlock rule into two rules to be able to easily build
the tree I want to build.  I could have done the same with the "finally"
block part of "statement" too.

Monty

-----Original Message-----
From: Jeff Vincent [mailto:JVincent at Novell.Com] 
Sent: Thursday, October 23, 2003 2:29 PM
To: antlr-interest at yahoogroups.com
Subject: [antlr-interest] AST generation in a recursive rule

Hey all,
 
I can't seem to get my mind around how to solve the following problem I am
having.  I have rules to match a try-catch construct similar to that in
Java:
 
statement! :
   stateTry:TRY tryBlock:block idCATCH:catchBlock ( idFINALLY:FINALLY
finallyBlock:block )?
   { #statement = #(#stateTry, #tryBlock, #(#[CATCH], #idCATCH),
#(#idFINALLY, #finallyBlock)); } 
   ;
 
catchBlock! :
   ( idCATCH:CATCH LPAREN! eType:identifier eID:IDENTIFIER RPAREN!
catchBody:block
       { #catchBlock = #(#catchBlock, #idCATCH, #eType, #eID, #catchBody); }
   )+
   ;
These rules match input similar to the following:
 
try {
}
catch(Exception1 e1) {
}
catch(Exception2 e2) {
}
catch(Exception3 e3) {
}
finally {
}
 
I would like the resulting AST tree for the catchBlock rule to look like the
following:
 
idCatch--(right)-->Exception1--(right)-->e1--(right)-->block--(right)-->null
   |
(down)
   |
   v
idCatch--(right)-->Exception2--(right)-->e2--(right)-->block--(right)-->null
   |
(down)
   |
   v
idCatch--(right)-->Exception3--(right)-->e3--(right)-->block--(right)-->null
   |
(down)
   |
   v
 null
 
Everything parses without errors, but the resulting AST from the catchBlock
rule contains only the first and last catch AST's, the rest are somehow
discarded.  What I suspect is happening is that instead of adding an
additional child, it overwrites it.  What is the syntax to construct an AST
by appending consecutive subtrees to the resulting root AST for the
catchBlock rule?  I am unable to find (or missed) any reference on how to do
this.
 
Thanks,
 
Jeff

Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service. 

 

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




More information about the antlr-interest mailing list