Decoupling Tree Traversal from Pattern Matching

Imagine we need to print a list of all assignments to simple variables in a Python program. It would be overkill to create or use an existing full Python parser. The pattern we’re looking for is unique. Using regular expressions and UNIX awk, we could filter Python programs looking for assignments with a one-liner:

 
$ ​awk '/[ \t]+[a-zA-Z]+ =/ {print $1}' < myprog.py ​​# $1 is left of '='
 
style
 
x
 
keymap
 
pt
 

Don’t worry about the details (which aren’t perfect anyway). The key is that we made a tool consisting of a just one pattern and associated action. We left the details of traversing the input to awk. We really don’t care, in this case, if awk walks the input lines forward, backward, or ...

Get Language Implementation Patterns now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.