[antlr-interest] AST construction question

Monty Zukowski monty at codetransform.com
Fri Sep 24 09:31:04 PDT 2004


On Sep 23, 2004, at 9:22 PM, Paul J. Lucas wrote:

> 	I've looked at some sample grammars and I seen both:
>
> 		someRule
> 		    : TOKEN^ foo bar
> 		        {
> 			    #someRule = #([SOME_RULE,"SOME_RULE",someRule);
> 			}
> 		    ;
>
This roots with an imaginary token above, yeilding a tree grammar of 
#(SOME_RULE, #(TOKEN foo bar));

> 	and:
>
> 		someRule
> 		    : t:TOKEN^ { #t.setType( SOME_RULE ); } foo bar
> 		    ;
>
This just changes the root, doesn't add a new root.  #(SOME_RULE foo 
bar);

> 	(The former seems fairly verbose.)
> 	Why would you do one over the other?
>

Rooting with an imaginary token is fairly common in rules with many 
alternatives, especially if you are using the tree to preserve some 
semantics.  A glance at my GCC grammar will show some examples of this.

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