tree grammar to visit leaves of an AST
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   gerhard_erwin
Posted On:   Thursday, October 9, 2008 05:55 PM

I used to be able to write a trivial tree grammar to traverse an AST tree and print out the text associated with each of the leaves. I can't figure out how to get this to work in using antlr 3.1. My latest attempt is ------------------------------------------------------ node_or_leaf : (nodes | leaves)+ ; nodes: ^(i=. { inc( ""NODE "" + $i.text);} node_or_leaf+ { dec(); } ); leaves : ^(i=. { inc( ""Leaf "" + $i.text); dec(); }) | (j=. { inc( ""LEAF "" + $j.text); dec(); }) ; ------------------------------------------------ where inc() and   More>>

I used to be able to write a trivial tree grammar
to traverse an AST tree and print out the text associated
with each of the leaves.
I can't figure out how to get this to work in using antlr 3.1.

My latest attempt is

------------------------------------------------------


node_or_leaf : (nodes | leaves)+ ;



nodes: ^(i=. { inc( ""NODE "" + $i.text);} node_or_leaf+ { dec(); } );



leaves : ^(i=. { inc( ""Leaf "" + $i.text); dec(); })

| (j=. { inc( ""LEAF "" + $j.text); dec(); })

;


------------------------------------------------

where inc() and dec() simply control the indent level
for a printout.

Any hints would be most welcome!    <<Less
About | Sitemap | Contact