[antlr-interest] AST generation in a recursive rule...the sequel (Tree parser)

Jeff Vincent JVincent at Novell.Com
Fri Oct 24 16:02:08 PDT 2003


Thanks to all those that have helped me so far (Monty, Arnar)!  I am
still struggling to wrap my mind around it and I am now stuck trying to
get a rule to match the tree in the tree parser, so please have patience
('cause I'm losing mine ;).  
 
I built the parser catch rules based on Monty's examples (see below)
and it generates the following AST as shown by ACTUAL output from
toStringList() :
 
      ( CATCHBLOCK ( CATCH ( CATCH ( CATCH ( CATCH Exception1 e1 { )
Exception2 e2 { ) Exception3 e3 { ) Exception4 e4 { ) )
 
Please help me here if I am reading it wrong, but assuming that the
token nearest an opening parenthesis is a sub-tree root, I think the
actual tree I am building translates to the following tree-like form
(siblings right, children down):

 
CATCHBLOCK
   |
   V
CATCH --> Exception4 --> e4 --> block
   |
   V
CATCH --> Exception3 --> e3 --> block
   |
   V
CATCH --> Exception2 --> e2 --> block

   |
   V
CATCH --> null
   |
   V
Exception1 --> e1 --> block
 
I think the output for the tree Monty initially suggested should look
more like the following toStringList() form :
 
     ( CATCHBLOCK ( CATCH Exception4 e4 { ) ( CATCH  Exception3 e3 { )
( CATCH  Exception2 e2 { ) ( CATCH Exception1 e1 { ))
 

but I digress.  My problem now is parsing the actual AST from within
the TreeParser.   Here are my TreeParser rules:

 
 catchBlock :
  #(CATCHBLOCK (catchNodes)+)
  ;
 
 catchNodes :
    #(CATCH catchNodes catchNodes)
  | identifier IDENTIFIER eBlock:.
  ;
 
The above TreeParser rules correctly recurse down and match the "CATCH
Exception1 e1 { " sub-tree, but thereafter gets a NullPointerException
because upon attempting to match "Exception2 e2 {" because the sibling
of the last catch statement is null (I think).  The last catch is also
not consistent with the others (assuming I am seeing it correctly).  I
would appreciate any enlightenment.  Below are my Parser rules that
builds this tree for reference.
 
Thanks for helping me grasp this stuff,

Jeff
 
<----------------Parser Rules----------------->
//Based on examples from Monty taken from previous e-mail thread
catchBlockList :
  ( catchBlockNode )+
  {   ## = #(#[CATCHBLOCK, "catchBlock"], ##);
       String s = #catchBlockList.toStringList();
       System.out.println(s);
  }
  ;

 
 catchBlockNode :
  ( CATCH^ LPAREN! identifier IDENTIFIER RPAREN! block )+
  { String s = #catchBlockNode.toStringList();
     System.out.println(s);
  }
  ;
<----------------Parser Rules: END------------->
 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20031024/458c3f2b/attachment.html


More information about the antlr-interest mailing list