[antlr-interest] Tree grammar confusion

Piper, Martin Martin.Piper at qg.com
Wed Aug 25 07:38:57 PDT 2010


Greetings, 

I apologize if this is being received in duplicate by anyone, I originally sent it yesterday and doesn't seem to have been received by the group (not showing in the archives or the daily brief) 
I am having some trouble with my tree grammar and am hoping to maybe get an explanation as to what I am doing wrong.
I've set up a parser grammar to generate an AST, then intend to walk that AST with a tree grammar to do the actual work.
When just testing that my tree grammar properly recognizes the AST produced by the parser, I keep coming across similar problems.
The output of the parser is as expected.
 Input:

select tbl1.col1, tbl2.col2
 from Table_one tbl1
 join Table_two tbl2
   on col1 = col1
where tbl2.col2 = 3
order by tbl1.col1

AST Output from parser:
(select (COLUMN_LIST tbl1.col1 tbl2.col2) (from (join (ALIAS (TABLE Table_one) tbl1) (ALIAS (TABLE Table_two) tbl2) (= col1 col1))) (where (= tbl2.col2 3)) (ORDER BY (COLUMN_LIST tbl1.col1)))

Output of tree grammar:
SybaseSqlTree.g: node from line 6:0 mismatched tree node: ORDER BY expecting <UP>
Parser grammar rules producing AST: Each of the into, from, where, etc clauses produce their own subtrees.
select_cmd         :
                select_cmd_core (UNION (ALL)? select_cmd_core)+ order_by_clause? compute_clause? ->^(SELECT ^(UNION select_cmd_core+) order_by_clause? compute_clause?)
    |          select_cmd_core order_by_clause? compute_clause?-> ^(select_cmd_core order_by_clause? compute_clause?)
                
;
select_cmd_core:
                SELECT (ALL|DISTINCT)? (TOP unsigned_number_literal)?
                select_list_clause 
                (into_clause)?
                (from_clause)?
                (where_clause)?
                (group_by_clause)?
                (having_clause)?-> 
                ^(SELECT ALL? DISTINCT? ^(TOP unsigned_number_literal)? select_list_clause into_clause? from_clause? where_clause? group_by_clause? having_clause?)
;

Relevant tree grammar excerpt....
select_cmd         :
^(SELECT ^(UNION select_cmd_core+) order_by_clause? compute_clause?)
                |              ^(select_cmd_core order_by_clause? compute_clause?);



select_cmd_core:
                ^(SELECT ALL? DISTINCT? 
                                top_clause? 
                                select_list_clause 
                                into_clause? 
                                from_clause? 
                                where_clause? 
                                group_by_clause? 
                                having_clause?)
;


My main confusion is that when generating a tree 

^(rule_1 A B) 
rule_1: TOKEN C D->^(TOKEN C D)
produces a tree (TOKEN C D A B)

but the same when walking the tree seems to expect to find ((TOKEN C D) A B)



Martin Piper
IT - Enterprise Business Solutions
 
Quad/Graphics
Innovative People Redefining Print
 
Sussex, Wisconsin
414-566-6823 phone
 
martin.piper at qg.com
www.QG.com



More information about the antlr-interest mailing list