[antlr-interest] tree rewrite: breaking apart subtrees

Laurie Harper laurie at holoweb.net
Wed Jan 6 11:58:54 PST 2010

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