Error handling in finite state machines

Depending on how you wish to construct and use them, as with parsers, there are two implementations of finite state machine. An inline state machine is one that has its grammar converted into an executable machine when the program starts up, and then proceeds to use that state machine to sequence input events, all in the same running program. By contrast an offline state machine has its source code generated by the parser generator program. This new automatically generated source code is added to a source code project and compiled into that application before the state machine can be run. An advantage of the offline state machine is that there is no startup cost in compiling the state machine description into executable code.

By just writing a grammar and processing it to become a state machine, we have a program that can track a sequence of input events and make sure that these events correspond to the sequencing rules we have defined in the sets of state transitions. As soon as an input event is encountered that does not match any transition for the current state, the FSM engine will flag an error and will either terminate the run or throw away the event to keep processing the input sequence. This error-handling behaviour is dependent on how you configure the grammar.

When creating an offline finite state machine, in which the parser generator creates the source code for a state machine, the -r option to the ParseLR command determines how invalid input events are processed. Specifying -r on the command line along with the -f required to build a state machine will cause input events that are unrecognised to be just ignored. Omitting the -r option will cause the state machine to abort at the unrecognised input event.

When creating an inline state machine, an argument to the FSMFactory<T>.InitializeFromGrammar method tells it which of these two types of error handling to build into the state machine engine. This is discussed later when describing how to create inline state machines.

If debug logging is enabled for the state machine by providing a debug TextWriter to the FSM instance, suitable output log entries will be made to tell you when error handling causes events to be dropped.