[antlr-interest] Using labels in actions of a tree parser

Kay Röpke kroepke at classdump.org
Wed Aug 13 14:08:07 PDT 2008


Hi!

(shamelessly copying the list because it's of general interest)

On Aug 13, 2008, at 10:48 PM, Oliver B. Fischer wrote:

> Kay Röpke schrieb:
> | ANTLR requires you to access some know attribute of `term` (via  
> label
> | t), i.e. accessing a plain rule reference is not allowed in parser  
> or
> | tree parser grammars.
> | IIRC this is because it would be ambiguous with respect to dynamic
> | scopes (where it is permissible to write $term, but that gives you  
> the
> | Stack object for the rule scope).
>
> But is there a way to get access to the tree node itself? I tried to  
> use
> the ".tree" attribute, but it leads to an compile error, since .tree
> isn't translated to ".getTree()".
>
> So, what can I do to get the tree node?


The problem here is that there's not one tree node, since rules can  
match 0 or more nodes.
To access the first node matched (or the first node potentially  
matched if none was matched in reality) you can use $term.start, which  
will give you the corresponding node.
I don't really know what you are trying to accomplish, so that may or  
may not be what you want.

Three attributes are predefined for tree grammar rules: .text, .start  
and .st. Additionally of course the return values you specify.
.tree isn't there, because the only thing that makes sense is .start  
(which is a better name for it anyway). Although one could argue that  
maybe .tree should be the same as .start here. mmh. Have to think  
about that one.

cheers,
-k

-- 
Kay Röpke
http://classdump.org/








More information about the antlr-interest mailing list