Antlr v3 & Trees (was: [antlr-interest] if-then-else - Grammar generates faulty parser code)

Monty Zukowski monty at codetransform.com
Thu Apr 22 13:48:56 PDT 2004


On Apr 22, 2004, at 1:23 PM, John D. Mitchell wrote:

> I'm perplexed.  Do you have some example where the addition of an 
> imaginary
> node is some kind of problem?  The only examples that I can think of 
> would
> all be issues of a very poorly designed tree to begin with.

Consider the following tree grammar fragment:

Example A:

selectStatement
	:
	queryExpression
	(computeClause)?
	(forClause)?
	(optionClause)?
	;

computeClause
	:
	#(COMPUTE
		(identifier expression)+
		(BY (expression)+)?
          )
	;

My initial thought was to not have computeClause (and others) be 
optional.  Instead I would have a node like COMPUTE_NULL.  But why?  
I'm serious, what exactly are the advantages of the following?

Example B:

selectStatement
	:
	queryExpression
	computeClause
	forClause
	optionClause
	;

computeClause
	:
	COMPUTE_NULL
	| #(COMPUTE
		(identifier expression)+
		(BY (expression)+)?
          )
	;

I know I'm being pedantic, but I really am struggling to justify my 
urge to give trees firm positional structure.  But maybe it's just me.  
Which would you favor and why?  To me A is looking succinct, is 
unambiguous, and results in a leaner tree.  B is more rigid but does 
not seem to save on computation, it only defers the decision until 
inside computeClause.  And if I'm totally off base with even suggesting 
B then help me refine when it is good practice to go for the tree which 
favors empty placeholder nodes.

And there may be other alternative examples.  Jump in any time folks :)

Monty Zukowski

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