Skip to Main Content
Language Implementation Patterns
book

Language Implementation Patterns

by Terence Parr
December 2009
Intermediate to advanced content levelIntermediate to advanced
380 pages
9h 2m
English
Pragmatic Bookshelf
Content preview from Language Implementation Patterns

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

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Implementation Patterns

Implementation Patterns

Kent Beck

Publisher Resources

ISBN: 9781680500097Errata Page