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

Monty Zukowski monty at codetransform.com
Wed May 19 15:02:15 PDT 2004


On May 19, 2004, at 1:49 AM, Adrian Tineo wrote:

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

This was a big topic of discussion at the latest cabal -- how to manage 
those out of rule references.  Basically we decided to create our own 
scoped namespace and shove special labels in there pointing to the tree 
node.  Easy enough to do manually, in fact Ter did that in half an hour 
or so while we sat there.

ANTLR 3 will incorporate that with more sophistication as it forms the 
basis of some really cool tree building stuff that Loring came up with.

Monty Zukowski

ANTLR & Java Consultant -- http://www.codetransform.com
ANSI C/GCC transformation toolkit -- 
http://www.codetransform.com/gcc.html
Embrace the Decay -- http://www.codetransform.com/EmbraceDecay.html



 
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