TLDR: Modeling Finite State Machines with Python Coroutines
Date: 2020-04-19 Source: https://arpitbhayani.me/blogs/fsm-python
Overview
Explore Finite State Machines (FSM) with Python coroutines. Learn to model FSMs for regex, divisibility, & SQL validation. Finite State Machine is a mathematical model of computation that models a sequential logic.
Key Points
- Finite State Machine is a mathematical model of computation that models a sequential logic.
- Generators: Generators are resumable functions that yield values as long as someone, by calling next function, keeps asking it.
- Coroutines: Coroutines, just like generators, are resumable functions but instead of generating values, they consume values on the fly.
- State: From the FSM above we model the state q2 as The coroutine runs as an infinite loop in which it waits for the input token at the yield statement.
- FSM Class: To keep things encapsulated we will define a class for FSM which holds all the states and maintains the current state of the machine.