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 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.