[antlr-interest] empty tree assertion rule
Monty Zukowski
monty at codetransform.com
Wed Oct 13 14:14:13 PDT 2004
On Oct 13, 2004, at 7:25 AM, whaefelinger wrote:
>
>
> Hi,
> being a newbee to antlr and it's abilities I'm always bit afraid to
> "forget" to visit parts of a generated AST when formulating rules on
> how to walk the tree within the tree parser.
>
> So I came up with the idea to write a tree parser rule to ensure
> that given input tree is empty:
>
> 001 nullp
> 002 {
> 003 if( #nullp != null ) {
> 004 System.err.println("error: tree not empty!");
> 005 System.exit(1);
> 006 else {
> 007 return;
> 008 }
> 009 }
> 010 : #(INT INT)
> 011 ;
>
>
> This rule would then be used like this:
>
> expr returns [float r]
> {
> float a,b;
> a=b=0;
> r=0;
> }
> : #(PLUS a=expr b=expr nullp) { r = a+b; }
> ;
>
> That means that my parser would generate an (unexpected)
> expr like
>
> (+ 1 2 3)
>
> I would get this reported.
>
> Of course the parser just generates this bad expr only
> if there's something wrong with the grammar. So when
> releasing the parser, the "nullp" rule should be removed
> for efficency.
>
> Now for the question:
>
> Q1: does something like 'nullp' already exist and I'm just
> not aware of?
>
> Q2: is there absolutly no need for 'nullp' because I missed
> the point?
>
>
Interesting idea. I'm not aware of anything else like it. You could
also use an action in place of a rule but that might clutter your
existing actions.
What I do to check my trees is go from parser->tree->tree walker w/o
actions->tree emitter and then diff the output of that from the
original. That's how my tests are written for the GCC toolkit. But if
you are not doing a full source to source translator you would have to
do something different.
Monty
ANTLR & Java Consultant -- http://www.codetransform.com
ANSI C/GCC transformation toolkit --
http://www.codetransform.com/gcc.html
Embrace the Decay -- http://www.codetransform.com/EmbraceDecay.html
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/antlr-interest/
<*> To unsubscribe from this group, send an email to:
antlr-interest-unsubscribe at yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
More information about the antlr-interest
mailing list