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

Arnar Birgisson arnarb at oddi.is
Sat Oct 25 03:20:29 PDT 2003


Hello..
 
This tree is probably not what Monty intended. What makes the recursion
is the + in the catchBlockNode rule, I think it's a typo. It should read
 
 catchBlockNode :
  CATCH^ LPAREN! identifier IDENTIFIER RPAREN! block
  ;
 
IMHO the recursive tree doesn't make sense in this situation. Try making
the above change, and you should get the tree
 
(CATCHBLOCK (CATCH Exception1 e1 {) (CATCH Exception2 e2 {) ... )
 
That way, the tree parser rules should present themselves, if not, let
us know and we'll try to help.
 
Arnar

-----Original Message-----
From: Jeff Vincent [mailto:JVincent at Novell.Com] 
Sent: 24. október 2003 23:02
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
<http://docs.yahoo.com/info/terms/> . 


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


More information about the antlr-interest mailing list