[antlr-interest] [newbie:] Unablanced tree resulting from SQL-parser

Nigel Sheridan-Smith nbsherid at secsme.org.au
Mon Sep 20 13:33:33 PDT 2004




> -----Original Message-----
> From: framiboe [mailto:framiboe at yahoo.de]
> Sent: Monday, 20 September 2004 6:55 PM
> To: antlr-interest at yahoogroups.com
> Subject: [antlr-interest] [newbie:] Unablanced tree resulting from SQL-
> parser
> 
> 
> Hi all,
> 
> it's my first contact to antlr and I managed "MS SQL Select
> Statement"-grammar by Tomasz Jastrzebski to produce Java-Code. It
> works, partly. It runs without exception and tokenizes a complex SQL-
> statement into its atoms. That's good. But the resulting tree is a
> totally unbalanced tree, because every node was put "right" under its
> parent. I've expected to receive a logical tree, something like
> 
> "select" -> column-list -> columns
>     v
> "from" -> table-list
>     v
> "where"
> 
> and so on. I hope, that was understandable?! What I receive now, is
> more a linked list of tokens instead of a tree.
> 

It’s a tree structure as such, but 'right' refers to the list of siblings
and 'down' refers to the list of children below this node. I believe the
implementation is probably a linked list of some sort. 

The structure of the Abstract Syntax Tree is defined by the '^' and '!'
characters in the parser rules and the '#' rules in the parser tree.

^ = This is the root of this rule (with left-most precedence I believe); all
other tokens become children.

! = Do not add this element to the tree (i.e. good for non-required text
tokens such as "INTO" in "INSERT INTO table ...").

#(x y z) = Defines a tree parser rule. The first token in the list
afterwards is the root token (i.e. 'x'). Subsequent tokens are the children
tokens. 

So by adjusting the parser and tree parser rules you can change the
behaviour of the tree construction. From what you have said, it sounds like
the SQL grammar does not have an AST, so you will need to modify the rules
to create the tree that is most suitable to you.

The following structure is probably going to work the easiest...

"select"       ->    "from"       ->      "where"
   v                    v
0+ columns           0+ tables



Nigel

--
Nigel Sheridan-Smith
PhD research student

Faculty of Engineering
University of Technology, Sydney
Phone: 02 9514 7946
Fax: 02 9514 2435




 
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