[antlr-interest] tree parser rule referencing other parts of the tree

excel_robot dermot_oneill at hotmail.com
Mon May 17 14:07:08 PDT 2004


Hi Guys,
I finished a small example of writing a source to source translator.
You can find it at
http://www.wumpa.com/antlr/antlr_example.html
I still have more questions than answers but the 1 big question is.
How do you reference other parts of the tree while in a certain rule.

My Example translates a simple T-SQL SELECT statement to an Oracle 
SELECT statement.
T-SQL Input:
SELECT @var1=col1, at var=col2 FROM table1,table2

T-SQL AST:
SELECT
|-SELECT_LIST
| |-=
| | |- at var1
| | |-col1
| | 
| |-=
|   |- at var2
|   |-col2
|  
|-FROM
| |-table1
| |-table2   


PL/SQL AST:
SELECT
|-SELECT_LIST
| |-col1
| |-col2
|
|-INTO
| |-var1
| |-var2
|  
|-FROM
| |-table1
| |-table2   


PL/SQL Output:
SELECT col1,col2 INTO var,var2 FROM table1,table2;

The T-SQL AST is modified to the PL/SQL AST by ASTPass1.g
The problem lies in referencing the "INTO" node when in the rule 
which recognizes the SELECT_LIST.
I could
1)pass a reference of the INTO sub tree down to the rule. 
 **but this may pass through several rules in real life.
 **lots of messy parameters.
 **might have to pass lots of different trees around.
2)use a global variable to hold reference to the INTO tree. 
 **but problems may exist with nested select statements which would 
overwrite my 1 member variable.
3)build some type of tree walker utility.
 **the rule would call a utility to walk back up the tree to get 
my "INTO" node reference.
 **I like this idea. Are there any utilities to do this?
4)create a big rule which encompasses everything I need, including 
the INTO part of the tree.
 **might have to "turn off" all the rules it encompases in the super 
grammar.

Am I going about this all wrong? Is that how you use the tree? Is 
there a best approach?

Any comments appreciated.
Dermot.








 
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