[antlr-interest] Collecting disparate subtrees into one tree?

Damien Cassou damien.cassou at gmail.com
Wed Jul 1 00:23:40 PDT 2009


On Wed, Jul 1, 2009 at 2:02 AM, J. Stephen Riley
Silber<jsrs701 at yahoo.com> wrote:
> Here's a question for you.  (Apologies if this is explained somewhere; I
> haven't found it yet.)  Suppose I have a language where I have procedures
> and functions (Oh! Pascal!), such as this:
>
>     procedure a() { ... }
>     function x() { ... }
>     procedure b() { ... }
>     function y() { ... }
>     procedure c() { ... }
>     function z() { ... }
>
> Now suppose, when I'm emitting the final representation, I want to group the
> procs and funcs together--I'm thinking I should somehow rewrite the tree to
> get it looking like this simplified version:
>
>     ^( CODE ^( PROCS a b c ) ^( FUNCS x y z ) )
>
> Is there a clever way, in tree-rewriting syntax, to attach a subtree as a
> child of a completely unrelated node?

doesn't something like this work?

code: procs* funcs*
  -> ^(CODE ^(PROCS procs*) ^(FUNCS funcs*))

-- 
Damien Cassou
http://damiencassou.seasidehosting.st

"Lambdas are relegated to relative obscurity until Java makes them
popular by not having them." James Iry


More information about the antlr-interest mailing list