[antlr-interest] tree transformation question (if-elif-else ->
if-else)
Klaas Hofstra
antlr at klaashofstra.com
Fri Jul 1 08:29:25 PDT 2005
Hi,
I have a problem with tree transformations that must have been solved before
because it seems to me that it occurs frequently when converting one language
to another.
I need to convert the following pseudo-code:
if expression
statements
elif expression
statements
elif expression
statements
else
statements
into:
if expression
statements
else
if expression
statements
else
if expression
statements
else
statements
Translated into trees, I need to transform the tree :
tree1 =
(IFBLOCK (IF E1 S1) (ELIF E2 S2) (ELIF E3 S3) (ELSE S4))
into:
tree2 =
(IFBLOCK (IF E1 S1) (ELSE (IFBLOCK (IF E2 S2) (ELSE
(IFBLOCK (IF E3 S3) (ELSE S4))))))
, where E=EXPRESSION, S=STATEMENTS
My problem is that the rewrite rule for ELIF is something like:
(ELIF E S) -> (ELSE (IFBLOCK (IF E S) subtree_of_next_elif_or_else)
When traversing the tree from left to right with a treeparser,
'subtree_of_next_elif_or_else' is not available when it is needed.
How can this problem be solved?
Thanks in advance,
Klaas
More information about the antlr-interest
mailing list