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

Sebastian Bächle baechle at cs.uni-kl.de
Wed Apr 28 01:19:06 PDT 2010


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


More information about the antlr-interest mailing list