[antlr-interest] controlling tree sequence in a parser
Michiel Vermandel
Michiel_Vermandel at axi.be
Tue Apr 18 02:01:48 PDT 2006
Thanks a lot!
This does the trick :-)
Michiel
"Bryan Ewbank" <ewbank at gmail.com>
Sent by: antlr-interest-bounces at antlr.org
14/04/2006 20:05
To
"ANTLR Interest" <antlr-interest at antlr.org>
cc
Subject
Re: [antlr-interest] controlling tree sequence in a parser
Oh, that's what you're asking; thanks for the clarification...
To rearrange the children of a node, you must build the node directly
- ANTLR's default is that everything is linked as siblings from
left-to-right, unless you use the "^" marker. Right now, you must be
building the tree for the <select_statement>... something like this, I
hope:
// use the "select" as the root node for the tree; discard "into"
and "from" keywords
select_expression :
"select"^ select_list
"into"! variable_list
"from"! table_reference_list
;
Or, perhaps, this (I tend toward this style):
// use an artificial node for the root node, discard all keywords
select_expression :
"select"! select_list
"into"! variable_list
"from"! table_reference_list
{
## = #( #[SELECT_EXPR,"SELECT_EXPR"], ##);
}
;
To build the tree as you want it (with the table_reference_list item
first, you have to be more explicit:
// create an artificial node for the expression, discard the
unnecessary
// keywords the items are placed in the generated tree in the order
// directed by the ## statement, so change it as necessary.
select_expression :
"select"! slist:select_list
"into"! vlist:variable_list
"from"! tlist:table_reference_list
{
## = #( #[SELECT_EXPR,"SELECT_EXPR"],
#tlist,
#slist,
#vlist);
}
;
Caveat/Disclaimer: The above discussion assumes that select_list,
variable_list, and table_reference_list all produce a single tree
rather than a list of trees. I assumed this from the diagrams that
you drew, and it's really the only way to produce a regular tree from
the description of the language I assumed based on what's here.
Hope this helps!
- Bryan Ewbank
On 4/14/06, Michiel Vermandel <Michiel_Vermandel at axi.be> wrote:
> Consider this statement:
>
> SELECT dummy
> FROM dual
>
> With my current grammar I get an AST tree like this:
>
> start_rule [4]
> select_statement [14]
> select_list [15]
> column_reference [50]
> dummy[299]
> table_list [16]
> table_reference [46]
> dual [299]
> ... I wanted to change the AST tree to the following and in this way
have the
> table reference registered before the column:
>
> start_rule [4]
> select_statement [14]
> table_list [16]
> table_reference [46]
> dual [299]
> select_list [15]
> column_reference [50]
> dummy[299]
>
> So, how can I move up the result of the table_list subrule?
> > select_expression
> > :
> > "select" select_list
> > "into" variable_list
> > "from" table_reference_list^ // <-- not accepted
> > ;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20060418/2039fab4/attachment.html
More information about the antlr-interest
mailing list