[antlr-interest] tree rewrite: breaking apart subtrees

Jared Klumpp jklumpp at harmonia.com
Thu Jan 7 06:15:28 PST 2010

See "Rewrite rule element cardinality" in the Definitive Antlr Reference (pg. 184), it seems you want something like:

vars            : VARDECL type (VARIABLE ID literal?)+
        -> ^(VARDECL type ^(VARIABLE ID literal))+;


Date: Wed, 6 Jan 2010 14:58:54 -0500
From: Laurie Harper <laurie at holoweb.net>
Subject: [antlr-interest] tree rewrite: breaking apart subtrees
To: antlr-interest at antlr.org
Message-ID: <FEFCE222-7000-48DC-8684-ACA5ECC441FB at holoweb.net>
Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes

I'm trying to construct a parser/translator that will transform an 
extended version of a C-like language 'X' into standard 'X'. I can't 
figure out quite what I need in my tree grammar to get the result I 
want... For example, I have an input AST that looks something like this:

(VARDECL integer
     (VARIABLE ivar1 (LITERAL 1))
     (VARIABLE ivar2 (LITERAL 2))
     (VARIABLE ivar3))
(VARDECL integer
     (VARIABLE ivar4))

I need to rewrite it to look like this:

(VARDECL integer
     (VARIABLE ivar1 (LITERAL 1)))
(VARDECL integer
     (VARIABLE ivar2 (LITERAL 2)))
(VARDECL integer
     (VARIABLE ivar3))
(VARDECL integer
     (VARIABLE ivar))

My tree grammar contains a rule like this:

vars            : ^(VARDECL type (^(VARIABLE ID literal?))+)
        -> ^(VARDECL type)+ ^(VARIABLE ID literal)+;

but that's not giving a result that's even close to right :-) I've 
tried all sorts of variations as I try to puzzle out the tree rewrite 
syntax, to no avail. Can anyone offer any insight?



More information about the antlr-interest mailing list