Pattern 15Tree Pattern Matcher

Purpose

This pattern walks trees, triggering actions or tree rewrites as it encounters tree patterns of interest.

The process of matching and rewriting trees is formally called term rewriting.

Discussion

Using a tree pattern matcher differs from using a tree grammar in two important ways:

  • We have to specify patterns only for the subtrees we care about.

  • We don’t need to direct the tree walk.

As we saw in Decoupling Tree Traversal from Pattern Matching, a tree pattern matcher is analogous to text rewriting tools such as awk, sed, and perl. We get to focus on input patterns of interest and what to do when we match those patterns. Whichever pattern matcher tool we’re using deals with tree walking and when to apply ...

Get Language Implementation Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.