Woohoo! Tree pattern matching, rewriting a reality

Skip to end of sidebar
Go to start of sidebar
Skip to end of metadata
Go to start of metadata

Can't resist showing off new filter mode for tree grammars (this is working in my dev branch). Imagine I built some trees with Cymbal.g and want to define symbols and push/pop scopes. Previously you had to give full tree grammar even though we'd only have actions in a few spots and don't care about structure (we trust tree builder). By doing tree pattern matching, we get to focus only on those subtrees we care about.

We are also separating the patterns from the strategy to apply them (many thanks to Eelco Visser and guidance I got from his Stratego/XT tool. I should also mention that Jurge Vinju of ASF+SDF fame explained a lot of his rewrite technology this summer when I visited him at CWI in Amsterdam...btw, ever wonder why Holland has so many good coders? Weird. A new Dutch USF grad student, Jasper Roel, has lots of potential). I have defined a default down-then-up strategy that applies matching rules once on way down and repeatedly on way up. It invokes rules topdown and bottomup if defined. You can create your own easily with a bit of code. Then just create a tree parser (Def here) and ask it to execute the downup strategy:

Here are the patterns:

Ain't that cool?

Here is the downup strategy where we are only matching not rewriting:

Then for tree grammars with filter=true, output=AST:

We applyRepeatedly on way up and pass around result trees in case we need to walk them.

org.antlr.runtime.tree.TreeFilter and TreeRewriter are superclass of generated tree parser and derive from TreeParser.

I define two rules you can implement but you could define any strategy and any rule dependencies you wanted:

Labels:
None
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.