[antlr-interest] Tricky tree rewrite without wildcards for subtree roots

Jim Idle jimi at temporal-wave.com
Wed Apr 28 08:29:30 PDT 2010


Well, you cannot use . as the root as that will just match everything, but in fact your root is D anyway, so wall you need is something akin to:

tree grammar T;

options
{
	output=AST;
}
a 	:	 x EOF ;

x 	:   ^(A B C ^(D c+=x)) 
	|   .
	;


With the rewrite rules added. This assumes that your representation below is accurate of course. Here you should then rewrite subtrees of D that are subtrees of A, in a recursive fashion.

Jim


> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Sebastian Bächle
> Sent: Wednesday, April 28, 2010 1:19 AM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] Tricky tree rewrite without wildcards for
> subtree roots
> 
> Hi,
> 
> I want to rewrite a subtrees of the form
> 
>      \
>       A
>     / | \
>    /  |  \
>   B   C   D
>          / \
>         E...F
> 
> to
> 
>       \
>        D
>      / \ \
>     A   \  \
>    / \   \   \
>   B  C    E...F
> 
> 
> The problem is that D may be the subtree of an "arbitrary complex"
> expression
> that I don't know in advance - and I don't want to know. I am just
> interested
> in the surrounding construct defined by A, B, and C and want to become
> them
> the first child subtree of D, which itself takes over the place of its
> former
> parent A in the resulting tree.
> 
> Is there a smart way to to do this with tree rewrite rules of antlr
> 3.2? Or
> must I manually modify the tree?
> 
> I can think of a tree pattern rule similar to this
> 
> ^(a=A b=B c=C ^(d=. children+=.*)) -> ^({$d.token} ^($a $b $c)
> $children+)
> 
> but this is rejected with the error:
> 
> Wildcard invalid as root; wildcard can itself be a tree
> 
> I found a discussion from 2008 in the archives ("wildcard in tree
> grammar")
> about that particular topic. The design decision was to interpret "."
> in a
> tree grammar always as a whole subtree, forbidding constructs like the
> one
> above. As far as I understood, the underyling mechanism with
> interleaved
> UP/DOWN tokens for tree grammars, however, is potentially capable to
> match
> such wildcard patterns.
> 
> I appreciate your hints and comments.
> 
> 
> Regards,
> 
> Sebastian
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> email-address





More information about the antlr-interest mailing list