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

Kay Röpke kroepke at classdump.org
Thu Aug 14 05:14:20 PDT 2008


Hi!

(Ter, question at the bottom)

On Aug 13, 2008, at 11:04 PM, Oliver B. Fischer wrote:

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

Now that I'm actually awake, I remembered something.

> Oliver B. Fischer schrieb:
> | Dear all,
> |
> | I would like to use labels in the actions of my tree parser to  
> have an
> | alternative way to access the tree nodes. Unfortunately ANTLR  
> reports an
> | error I can't understand.
> |
> | Here is my action definition:
> |
> |
> | term returns [DataType rt]
> |   : factor { $rt = $factor.rt; }
> |     -> template(f={$factor.st})"<f>"
> |   | ^(ASTERISK t1=term t2=term)
> |   | ^(SLASH t1=term t2=term)
> |   | ^(AND t=term term)
> |     { System.out.println($t); }
> |   ;
> |


You can actually get the root tree node for a subrule reference, but  
your tree parser must output an AST.

The following grammars illustrate it (tested with 3.1):

grammar Foo;

options {
	output=AST;
}

FOO	:	'FOO';
BAR	:	'BAR';
BAZ	:	'BAZ';
WS	:	(' ' | '\t' | '\n' | '\r') { $channel=HIDDEN; }
	;

rule:	FOO BAR BAZ -> ^(FOO BAR BAZ);

tree grammar FooWalker;

options {
	tokenVocab=Foo;
	ASTLabelType=CommonTree;
	output=AST;
}

walk:	rule {System.out.println($rule.tree.toStringTree());}
	;

rule:
	^(FOO BAR BAZ)
	;

The interesting lines in FooWalker.java are:

             adaptor.addChild(root_0, rule1.getTree());
             System.out.println((rule1!=null? 
((CommonTree)rule1.tree):null).toStringTree());

If the tree grammar is not generating ASTs, then there's apparently no  
way to get .tree using action syntax. I'm not sure if that's on  
purpose, it seems that it is a useful thing to get the tree at that  
point (without doing too much work). After looking the runtime code,  
it seems that we actually return the tree in the call to match(), but  
ignore the return value. So it _appears_ that you could get the tree  
at no extra runtime cost (since we do it anyway).

OTOH, I think .start and .tree should always be the same node (unless  
you have tree rewrite rules, of course), but I might overlook  
something here. Still not enough coffee to recover from yesterday :)
But if you have output=AST and no rewrite rules, the added overhead of  
building a copy of the input tree wouldn't make sense, either. So the  
real question is, why don't we grant access to .tree? At most it could  
be a leaf node, but that does no harm. Ter?

cheers,
-k
-- 
Kay Röpke
http://classdump.org/








More information about the antlr-interest mailing list