[antlr-interest] Re: tree grammar help

Bans VGLab bans.vglab at gmail.com
Thu Jul 20 03:57:40 PDT 2006


Hello Michael

I hope you are trying to walk an AST generated by SQL.

I have few suggestions; you may follow any or more that's convenient to you:

1. while parsing, make the DOT as parent using ^ operator. That'll make the
AST easier to walk. In general, it's true for any operator.

2. You should make use of Heterogeneous parsing. This allows you to abstract
most of the information in the node itself, which is done at  the time of
parsing. In your case, the aspects like "Qualifer" and "alias" can be put
inside a COLUM_NODE at the time of parsing. This has advantages. If tomorrow
you want to support something like "Catalog1.Schema1.Tab1.Col1", your tree
parser will not be affected. The qualifier aspect within the COLUM_NODE will
abstract this information, which can be evolved anytime later.

3. Don't treat AST as the regular Syntax Tree (what we studied in Aho
Ulman's dragon book, I guess). An ideal AST removes unnecessary keywords
like "AS" while parsing. It should only capture the relevant components. The
relation between these components should be manifested with appropriates
edges in the tree. This eases tree-parsing a lot, in terms of
tims-space-complexities and coding.

-Sujeet Banerjee
SMTS
Persistent Systems Private Limited.


On 7/14/06, michael.pflug at thomson.com < michael.pflug at thomson.com> wrote:
>
>  I have a tree grammar with the rule:
>
> : tableName=identifier DOT columnName=identifier  ((LITERAL_as)?
> alias=identifier)?
>
>
>
> We are trying to make the first identifier with DOT optional like this:
>
> : (tableName=identifier DOT)? columnName=identifier  ((LITERAL_as)?
> alias=identifier)?
>
>
>
> The generation passes, but generates a bad IF statement.  _t.getType() can
> not equal SQL_IDENTIFIER and DOT at the same time
>
> It looks like this:
>
> *if ((_t.getType()==SQL_IDENTIFIER) && (_t.getType()==DOT))* {
>
>                                     tableName=identifier(_t);
>
>                                     _t = _retTree;
>
>                                     AST tmp16_AST_in = (AST)_t;
>
>                                     match(_t,DOT);
>
>                                     _t = _t.getNextSibling();
>
>                         }
>
>                         else if ((_t.getType()==SQL_IDENTIFIER) &&
> (_tokenSet_0.member(_t.getType()))) {
>
>                         }
>
>                         else {
>
>                                     throw new NoViableAltException(_t);
>
>                         }
>
>
>
> Any ideas on a grammar that would work?
>
>
>
> Thanks.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20060720/23714439/attachment.html


More information about the antlr-interest mailing list