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

mzukowski at yci.com mzukowski at yci.com
Mon Oct 27 08:05:49 PST 2003


Like Arnar said, I meant the second rule to not have a ()+:

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);
  }
  ;

Tree parser something like this:

catchBlock :
  #(CATCHBLOCK (catchNodes)+)
  ;
 
 catchNodes :
    #(CATCH identifier IDENTIFIER eBlock:.)
  ;

Note your . for the block means the tree parser will only match the root and
will not decend into that subtree, I assume that is intended.

Monty

-----Original Message-----
From: Jeff Vincent [mailto:JVincent at Novell.Com] 
Sent: Friday, October 24, 2003 4:02 PM
To: antlr-interest at yahoogroups.com
Subject: [antlr-interest] AST generation in a recursive rule...the sequel
(Tree parser)

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------------->
 

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