Finite State Machine – What is a Final State?

finite-state machine

I am playing around the state machine concept trying to better understand the best areas of their application. The formal definition of an FSM contains the following elements:

  • finite non-empty set of allowed states;
  • an initial state;
  • an alphabet ("allowed input symbols");
  • state-transition function;
  • and a set of final states.

I looked at a few online sources on this topic and noticed that all of them are focusing on the transition (state + input = new state) aspect of FSM. However, very little is said specifically about the final states of a machine.


So what I'd like to know is:

  • either [the definition of] what exactly is the a final state;
  • or what is the purpose of the final state (including practical applications).

That may let me infer the answers to a whole bunch of other related questions I have. E.g., what should happen if the state machine receives an input symbol that's not allowed; if that is an error then what's the way to recover; should an FSM-based system ready for a possibility of never reaching a final state; what if the setup of a machine makes it impossible to reach a final state in the first place; should an error state be a first class object of a FSM or not, whether the state machine should or should not store the input symbol sequence in real life systems…

Best Answer

There are several variations of the definition of a finite state machine. The one you give is common in CS classes, particularly in regards to formal languages and the theory of parsing. In this context, the FSM is often directly related to a regular expression, and the goal is feed strings into the FSM to see if they match a regular expression.

The set of final states defines which states are final states. They aren't a different sort of thing. Instead of a set of final states, you can instead imagine that some states are simply labeled as final states. Indeed, the having a set of final states is isomorphic to adding a bit to each state indicating whether it is final or not.

The purpose of knowing which states are final is that a string is matched (or accepted) by the state machine if we are in a final state when we reach the end of the string. Without distinguishing final states, every prefix of the input would also be "matched". For example, for a FSM like

1 -a-> 2 -b-> 3,

if 3 is a final state (and the others are not), then it will (only) match the string "ab". If all are final states then it would match the strings "", "a", and "ab".

Beyond parsing, many problems can fit in this mold especially when we consider variations such as allowing infinite streams of input and adding output to states and/or transitions. This move allows FSMs to do more than just match things, but even matching can be more powerful than you might think (e.g. see model checking). Examples include describing protocols, communicating processes, game logic, fraud detection, business processes, and also doing model checking, hardware synthesis, and orchestration. Physical systems may also be partially modeled by finite state machines.

Related Topic