Finite-state machine builder (based on regular grammar)