SMC Parser

Clean Code, Episode 29

By Robert C. Martin

76 minDec 2014

OK boys and girls, things are really starting to heat up. In this episode we start writing a compiler!

Remember,that's what we said we were going to do. We said we'd write a compiler that generates finite state machines. Well, that's what were doing. And, boy, is it a hoot.

We begin by exploring the syntax of our state machine language. That syntax begins as a simple state transition table, and then we add a few cute bells and whistles, like super states, entry, and exit actions, that make writing state machines a bit more convenient.

Next we formalize our syntax by expressing it in Backus-Naur Form (BNF).

Then we write a lexical analyzer to identify the terminal tokens of our syntax.

Then we write a parser that identifies the grammar of those terminal tokens. And, ironically, that parser is a finite state machine!

Then we use the Builder Pattern to write a builder that constructs the internal representation of our syntax.

And if you don't think that's enough for one episode, you're crazy.

So, put your propeller caps on and take a big swig of brain juice, because we're about to plow headlong into the heady topic of Writing the State Machine Compiler!

The source code for the State Machine Compiler is included in the "extras"; and can also be found at: https://github.com/unclebob/CC_SMC