Terence Parr Blog from November, 2008

Skip to end of sidebar Go to start of sidebar
Example tree rewriting with patterns

So, let's do some rewriting using the pattern matching filter=true mode. Again, the VecMath.g parser will build trees but we'll avoid building an entire tree grammar. We'll focus on some patterns we want to rewrite.

Here's the grammar to build trees.

And now, let's distribute scalar-vector multiplies. 4*[1,2] -> [4*1,4*2].

Give it a shot:

Output:

Now for stuff that needs to repeatedly get applied to subtrees.

Test code:

Running:

I'm replacing multiply with shift left by 1 and then combining shifts.

Woohoo! Tree pattern matching, rewriting a reality

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:

Implementing tree pattern matching

Writing down random thoughts as I have them.

Imagine rules with strategy name, order name, and apply name:

strategy has visit order and pattern application rule

default is

collect all rules with same strategy into rule

gen applyOnce() for each generated rule

This creates node stream for t from existing buffer if possible and then
calls the pattern grouping rule. Easy if we don't do replaces. Replace on way up: we have to walk new tree. On way down, we also have to walk new tree.

We're rewriting tree, perhaps we should update node stream buffer as we go using a rewrite engine approach. Hmm...messes with node indexes.

Can we walk trees without creating buffer of entire tree? Perhaps we can make an on-demand buffer that doesn't go into subtrees unless the parser does. The wildcard would tell it to skip. Only problem is lookahead. Maybe we let LT(i) do the on-demand loading. We could add a skipOverSubtree() method to node stream so wildcard could avoid descending.

Patter ^( VAR . ID ) yields:

On-demand serialization of a tree in java (no continuations) would be a pain. Would have to maintain a stack. Might be better for deep tree anyway. skip in node stream would skip over DOWN...UP sequence or just one token if not DOWN.