[antlr-interest] AST generation in a recursive rule

Jeff Vincent JVincent at Novell.Com
Thu Oct 23 14:28:48 PDT 2003


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





-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20031023/6ec4facb/attachment.html


More information about the antlr-interest mailing list