[antlr-interest] AST generation in a recursive rule...the
sequel (Tree parser)
Jeff Vincent
JVincent at Novell.Com
Mon Oct 27 11:14:31 PST 2003
Great! Thanks again Monty/Arnar ... I took out the ()+ and it worked
the first time. I thought I had tried that, but may have tried it
before I had eliminated other errors in my rules.
And, yes, the "eBlock:." is meant not to descend the subtree for the
block portion of the rule. The reason is that I am building a run-time
scripting language and the act of parsing an AST during runtime in
effect is causing that sub-tree to be executed. This means that I can't
parse some things unless certain conditions are met. In a
try-catch-finally clause, an catch block is only exectued (parsed) if an
exception is caught that matches a defined catch clause (obviously). In
a for-loop, then the block of the for loop will be executed based on the
number of times the condition is met, etc. Traditionally, I typically
match only the root node of a conditional statement (IF, WHILE, FOR,
TRY, etc.) and manually pull the subcomponents to be evaluated and
executed based on run-time evaluation. However, there are times when it
is easier if I can get the tree parser to match it directly, as in the
case of the catch statements. In this case, I end up building up a
Vector of catch clauses that will be searched when an exception is
caught. If a matching exception is found, then the corresponding block
will be parsed. It all works pretty slick if I do say so...given that
it is all interpreted at run-time.
Anyway, thanks for your input.
Jeff
>>> mzukowski at yci.com 10/27/2003 9:05:49 AM >>>
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/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20031027/9269b685/attachment.html
More information about the antlr-interest
mailing list