[antlr-interest] Tree-Parser Grammer Question

Ney, Richard richard.ney at aspect.com
Mon Nov 11 23:26:01 PST 2002


I have battled this issue for two days now so I'm at a loss to how to solve
this problem. I created a initial parser for a basic SQL Select. One of my
key rules in the initial parser looks like this:

groupExpression
	: ("avg"^|"max"^|"min"^|"sum"^) OPEN_PAREN! (additiveExpression |
(("all" | "distinct") field_name)) CLOSE_PAREN! (IDENT^)
	| ("count"^ OPEN_PAREN! (("all" | "distinct")? field_name | STAR)
CLOSE_PAREN! (IDENT^)?)
	| additiveExpression (IDENT^)?
	;

The result of this rule is that it builds the tree with the final name of
the column always being to top-most root. I.E.

Select test, count(*), count(*) test from foo;

Results in a tree that looks like

select
	test

	count
		*

	test
		count
			*
from
	foo

Now in my tree parser I have a two rules that look like this:

selectColumn 
	: fieldName (groupExpression)?
	;
	
groupExpression
	: #("avg" mathExpression)
	{
	}
	| #("sum" mathExpression)
	{
	}
	| #("count" ("all" | "distinct")? (fieldName| STAR))
	{
	}
	| #("max" mathExpression)
	{
	}
	| #("min" mathExpression)
	{
	}
	;

These rules handle the 1st and 3rd cases in the select statement but not the
2nd where the count(*) doesn't have an alias. Does anyone have an idea how
to write something like

selectColumn
	: (fieldName)? (groupExpression)?
	;

that is a little less ambiguous?

-Richard

----------------------------------------------------------------------------
------------------------
Richard Ney	Aspect Communications
Principal Software Engineer
http://www.aspect.com <http://www.aspect.com>
Main:  408.325.2200
mailto:richard.ney at aspect.com <mailto:richard.ney at aspect.com>
SJ Office: 408.325.2464
    	Home Office: 916.797.9602
----------------------------------------------------------------------------
------------------------

The Three Laws of Infernal Dynamics:
1. An object in motion will always be headed in the wrong direction.
2. An object at rest will always be in the wrong place.
3. The energy required to change either of these states will always be more
than you wish to expend, but never so much as to make the task appear
prospectively impossible.


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


More information about the antlr-interest mailing list