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

Adrian Tineo tineo at ac.uma.es
Wed May 19 01:49:52 PDT 2004


Hi everyone

> 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.

I had that same question not long ago. In short, I would say that you really 
can't move freely in the tree with grammar based tree-parsers.

As far as I know (and I don't know that much so please correct if this is 
wrong) you can only reference parts of the subtree that results from the 
subrule you are in. If you want to work on a fairly big subtree with quite 
some nodes to operate with, you need to go up in the rules. This can be 
inconvenient, and besides it doesn't solve all your problems. Sometimes you 
need to touch the tree in another place outside of the subrule. 

In my case, I am working with Mr Zukowski's C grammar to source-to-source 
translate some input C code. One of the things I have to do is expand some 
pointer expresssions like

struct t1 {
	int data;
	struct t1 *nxt;
}*ptr;
[...]
ptr=ptr->nxt;

in 

struct t1{
	int data;
	struct t1 *nxt;
}*ptr, *tmp;	
[...]
tmp=ptr->nxt;
ptr=tmp;

where not only I have to act on the assign expression subrule but I need to go 
up the tree and declare the new tmp variable in order to have a valid 
output . This can't be easily done with antlr-built treeparser because it 
can't be easily described with actions within the grammar.

Of course we could have a symbol table defined and work with it in the 
subrule, but overall as I realized the changes that needed to be made, I 
opted for a hand-built treeparser where I take an input tree and modify it as 
I need with total freedom to move between the nodes. For that I extended the 
nodes provided by Mr. Zukowsky to add some useful operations for moving 
around in the tree.

> 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?

This is mostly the approach I followed... building the walker by hand.

> 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.


Adrian Tineo



 
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