Finite-state machine

State machine redirects here. For innite state ma-

chines, see State transition system. For fault-tolerance Push Coinmethodology, see State machine replication.SFSM redirects here. For the Italian railway company, Un- Locked lockedsee Circumvesuviana.Finite Automata redirects here. For the electro- Push Coinindustrial group, see Finite Automata (band).Classes of automata State diagram for a turnstileA nite-state machine (FSM) or nite-state automa-ton (FSA, plural: automata), nite automaton, or sim-ply a state machine, is a mathematical model of compu-tation. It is an abstract machine that can be in exactly oneof a nite number of states at any given time. The FSMcan change from one state to another in response to someexternal inputs; the change from one state to another iscalled a transition. A FSM is dened by a list of its states,its initial state, and the conditions for each transition.The behavior of state machines can be observed in manydevices in modern society that perform a predeterminedsequence of actions depending on a sequence of eventswith which they are presented. Examples are vendingmachines, which dispense products when the proper com-bination of coins is deposited, elevators, whose sequenceof stops is determined by the oors requested by riders,trac lights, which change sequence when cars are wait-ing, and combination locks, which require the input ofcombination numbers in the proper order.The nite state machine has less computational powerthan some other models of computation such as theTuring machine.[1] The computational power distinctionmeans there are computational tasks that a Turing ma-chine can do but a FSM cannot. This is because a FSMsmemory is limited by the number of states it has. FSMsare studied in the more general eld of automata theory.

1 Example: coin-operated turn- A turnstile

stile customer passes through, the arms are locked again untilAn example of a mechanism that can be modeled by a another coin is inserted.state machine is a turnstile.[2][3] A turnstile, used to con- Considered as a state machine, the turnstile has two pos-trol access to subways and amusement park rides, is a gate sible states: Locked and Unlocked.[2] There are two pos-with three rotating arms at waist height, one across the en- sible inputs that aect its state: putting a coin in the slottryway. Initially the arms are locked, blocking the entry, (coin) and pushing the arm (push). In the locked state,preventing patrons from passing through. Depositing a pushing on the arm has no eect; no matter how manycoin or token in a slot on the turnstile unlocks the arms, times the input push is given, it stays in the locked state.allowing a single customer to push through. After the Putting a coin in that is, giving the machine a coin in-

12 3 REPRESENTATIONS

put shifts the state from Locked to Unlocked. In the

unlocked state, putting additional coins in has no eect;that is, giving additional coin inputs does not change thestate. However, a customer pushing through the arms,giving a push input, shifts the state back to Locked.The turnstile state machine can be represented by a statetransition table, showing for each possible state, the tran-sitions between them (based upon the inputs given to the Fig. 1 UML state chart example (a toaster oven)machine) and the outputs resulting from each input:

The turnstile state machine can also be represented by a

directed graph called a state diagram (above). Each stateis represented by a node (circle). Edges (arrows) showthe transitions from one state to another. Each arrow islabeled with the input that triggers that transition. An in-put that doesn't cause a change of state (such as a coininput in the Unlocked state) is represented by a circulararrow returning to the original state. The arrow into theLocked node from the black dot indicates it is the initialstate.

2 Concepts and terminology

A state is a description of the status of a system that iswaiting to execute a transition. A transition is a set of ac-tions to be executed when a condition is fullled or whenan event is received. For example, when using an audiosystem to listen to the radio (the system is in the radiostate), receiving a next stimulus results in moving tothe next station. When the system is in the CD state,the next stimulus results in moving to the next track. Fig. 2 SDL state machine exampleIdentical stimuli trigger dierent actions depending onthe current state. state (e.g. C). The complete actions information is notIn some nite-state machine representations, it is also directly described in the table and can only be added us-possible to associate actions with a state: ing footnotes. A FSM denition including the full actions information is possible using state tables (see also virtual an entry action: performed when entering the state, nite-state machine). and

an exit action: performed when exiting the state.

3.2 UML state machines

The Unied Modeling Language has a notation for de-

3 Representations scribing state machines. UML state machines overcome the limitations of traditional nite state machines whileFor an introduction, see State diagram. retaining their main benets. UML state machines in- troduce the new concepts of hierarchically nested states and orthogonal regions, while extending the notion of actions. UML state machines have the characteristics of3.1 State/Event table both Mealy machines and Moore machines. They sup- port actions that depend on both the state of the systemSeveral state transition table types are used. The most and the triggering event, as in Mealy machines, as wellcommon representation is shown below: the combination as entry and exit actions, which are associated with statesof current state (e.g. B) and input (e.g. Y) shows the next rather than transitions, as in Moore machines. 3

in many dierent areas, including electrical engineer-

state 1 ing, linguistics, computer science, philosophy, biology, opened mathematics, and logic. Finite state machines are a class of automata studied in automata theory and the theory of E: open computation. In computer science, nite state machines door are widely used in modeling of application behavior, de- sign of hardware digital systems, software engineering, compilers, network protocols, and the study of computa- transition tion and languages.

close open 5 Classication

transition condition Finite state machines can be subdivided into transducers,

Fig. 3 Example of a simple nite state machine

3.3 SDL state machines 6

The Specication and Description Language is a standard

from ITU that includes graphical symbols to describe ac-tions in the transition: Fig. 4 Acceptor FSM: parsing the string nice

send an event Acceptors, also called recognizers and sequence detec-

receive an event tors, produce binary output, indicating whether or not the received input is accepted. Each state of an FSM is ei- start a timer ther accepting or not accepting. Once all input has been received, if the current state is an accepting state, cancel a timer the input is accepted; otherwise it is rejected. As a rule, start another concurrent state machine input is a sequence of symbols (characters); actions are not used. The example in gure 4 shows a nite state ma- decision chine that accepts the string nice. In this FSM, the only accepting state is state 7.SDL embeds basic data types called Abstract Data A (possibly innite) set of symbol sequences, aka. formalTypes, an action language, and an execution semantic language, is called a regular language if there is some Fi-in order to make the nite state machine executable. nite State Machine that accepts exactly that set. For ex- ample, the set of binary strings with an even number of3.4 Other state diagrams zeroes is a regular language (cf. Fig. 5), while the set of all strings whose length is a prime number is not.[5]:18,71There are a large number of variants to represent an FSM A machine could also be described as dening a language,such as the one in gure 3. that would contain every string accepted by the machine but none of the rejected ones; that language is accepted by the machine. By denition, the languages accepted by4 Usage FSMs are the regular languages; a language is regular if there is some FSM that accepts it.In addition to their use in modeling reactive systems The problem of determining the language accepted by apresented here, nite state machines are signicant given nite state acceptor is an instance of the algebraic4 5 CLASSIFICATION

path problemitself a generalization of the shortest path

1problem to graphs with edges weighted by the elements openedof an (arbitrary) semiring.[6][7][8]

close sensor opened

open 4 2 opening closing

close

sensor closed open 3 closed

Fig. 5: Representation of a nite-state machine; this example

shows one that determines whether a binary number has an evennumber of 0s, where S1 is an accepting state. Fig. 6 Transducer FSM: Moore model exampleThe start state can also be an accepting state, in whichcase the automaton accepts the empty string. The entry action (E:) in state Opening starts a mo-An example of an accepting state appears in Fig.5: tor opening the door, the entry action in state Clos-a deterministic nite automaton (DFA) that detects ing starts a motor in the other direction closing thewhether the binary input string contains an even number door. States Opened and Closed stop the mo-of 0s. tor when fully opened or closed. They signal to theS 1 (which is also the start state) indicates the state at outside world (e.g., to other state machines) the sit-which an even number of 0s has been input. S1 is there- uation: door is open or door is closed.fore an accepting state. This machine will nish in anaccept state, if the binary string contains an even number sensor closedof 0s (including any binary string containing no 0s). Ex- 1 2amples of strings accepted by this DFA are (the empty opened closedstring), 1, 11, 11, 00, 010, 1010, 10110, etc I: I:

sensor opened

5.2 Classiers Fig. 7 Transducer FSM: Mealy model exampleA classier is a generalization of a nite state machinethat, similar to an acceptor, produces a single output on Mealy machine The FSM uses only input actions, i.e.,termination but has more than two terminal states. output depends on input and state. The use of a Mealy FSM leads often to a reduction of the num- ber of states. The example in gure 7 shows a5.3 Transducers Mealy FSM implementing the same behaviour as in the Moore example (the behaviour depends on theMain article: Finite-state transducer implemented FSM execution model and will work, e.g., for virtual FSM but not for event-driven FSM).Transducers generate output based on a given input There are two input actions (I:): start motor to closeand/or a state using actions. They are used for control the door if command_close arrives and start mo-applications and in the eld of computational linguistics. tor in the other direction to open the door if com-In control applications, two types are distinguished: mand_open arrives. The opening and closing intermediate states are not shown.

Moore machine The FSM uses only entry actions, i.e.,

output depends only on the state. The advantage 5.4 Generators of the Moore model is a simplication of the be- haviour. Consider an elevator door. The state ma- Sequencers, or generators, are a subclass of the accep- chine recognizes two commands: command_open tor and transducer types that have a single-letter input al- and command_close, which trigger state changes. phabet. They produce only one sequence which can be 5

seen as an output sequence of acceptor or tranducer out- For both deterministic and non-deterministic FSMs, itputs. is conventional to allow to be a partial function, i.e. (q, x) does not have to be dened for every combina- tion of q S and x . If an FSM M is in a state5.5 Determinism q , the next symbol is x and (q, x) is not dened, then M can announce an error (i.e. reject the input). This isA further distinction is between deterministic (DFA) useful in denitions of general state machines, but lessand non-deterministic (NFA, GNFA) automata. In a useful when transforming the machine. Some algorithmsdeterministic automaton, every state has exactly one tran- in their default form may require total functions.sition for each possible input. In a non-deterministic au-tomaton, an input can lead to one, more than one, or A nite state machine has the same computational powerno transition for a given state. The powerset construc- as a Turing machine that is restricted such its head maytion algorithm can transform any nondeterministic au- only perform read operations, and always has to movetomata into a (usually more complex) deterministic au- from left to right. That is, each formal language acceptedtomata with identical functionality. by a nite state machine is accepted by such a kind of restricted Turing machine, and vice versa.[14]A nite state machine with only one state is called a com-binatorial FSM. It uses only input actions. This concept A nite state transducer is a sextupleis useful in cases where a number of nite state machines (, , S, s0 , , ) , where:are required to work together, and when it is convenientto consider a purely combinatorial part as a form of FSM is the input alphabet (a nite non-empty setto suit the design tools.[9] of symbols). is the output alphabet (a nite, non-empty set of symbols).6 Alternative semantics S is a nite, non-empty set of states.There are other sets of semantics available to represent s0 is the initial state, an element of S . In astate machines. For example, there are tools for mod- nondeterministic nite automaton, s0 is a seteling and designing logic for embedded controllers. [10] of initial states.They combine hierarchical state machines (which usually is the state-transition function: : S have more than one current state), ow graphs, and truth S.tables into one language, resulting in a dierent formal- is the output function.ism and set of semantics.[11] These charts, like Harelsoriginal state machines,[12] support hierarchically nestedstates, orthogonal regions, state actions, and transition If the output function is a function of a state and inputactions.[13] alphabet ( : S ) that denition corre- sponds to the Mealy model, and can be modelled as a Mealy machine. If the output function depends only on a state ( : S ) that denition corresponds to7 Mathematical model the Moore model, and can be modelled as a Moore ma- chine. A nite-state machine with no output function atIn accordance with the general classication, the follow- all is known as a semiautomaton or transition system.ing formal denitions are found: If we disregard the rst output symbol of a Moore ma- chine, (s0 ) , then it can be readily converted to an A deterministic nite state machine or acceptor output-equivalent Mealy machine by setting the output deterministic nite state machine is a quintuple function of every Mealy transition (i.e. labeling every (, S, s0 , , F ) , where: edge) with the output symbol given of the destination is the input alphabet (a nite, non-empty set Moore state. The converse transformation is less straight- of symbols). forward because a Mealy machine state may have dier- ent output labels on its incoming transitions (edges). Ev- S is a nite, non-empty set of states. ery such state needs to be split in multiple Moore machine s0 is an initial state, an element of S . states, one for every incident output symbol.[15] is the state-transition function: : S S (in a nondeterministic nite automaton it would be : S P(S) , i.e., would 8 Optimization return a set of states). F is the set of nal states, a (possibly empty) Main article: DFA minimization subset of S .6 10 SEE ALSO

Optimizing an FSM means nding a machine with the 9.3 Finite state machines and compilersminimum number of states that performs the samefunction. The fastest known algorithm doing this is Finite automata are often used in the frontend of pro-the Hopcroft minimization algorithm.[16][17] Other tech- gramming language compilers. Such a frontend mayniques include using an implication table, or the Moore comprise several nite state machines that implement areduction procedure. Additionally, acyclic FSAs can be lexical analyzer and a parser. Starting from a sequence ofminimized in linear time.[18] characters, the lexical analyzer builds a sequence of lan- guage tokens (such as reserved words, literals, and identi- ers) from which the parser builds a syntax tree. The lexi- cal analyzer and the parser handle the regular and context-9 Implementation free parts of the programming languages grammar.[21]

9.1 Hardware applications

10 See also Abstract state machines (ASM)

Articial intelligence (AI)

Abstract State Machine Language (AsmL)

Behavior model

Communicating nite-state machine

Control systemFig. 9 The circuit diagram for a 4-bit TTL counter, a type of Control tablestate machine Decision tablesIn a digital circuit, an FSM may be built using a DEVS: Discrete Event System Specicationprogrammable logic device, a programmable logic con-troller, logic gates and ip ops or relays. More specif- Extended nite-state machine (EFSM)ically, a hardware implementation requires a register tostore state variables, a block of combinational logic that Finite state machine with datapathdetermines the state transition, and a second block ofcombinational logic that determines the output of an Hidden Markov modelFSM. One of the classic hardware implementations is the Petri netRichards controller. Pushdown automatonIn a Medvedev circuit, the output is directly connectedto the state ip-ops minimizing the time delay ip-ops Quantum nite automata (QFA)and output.[19][20] Recognizable languageThrough state encoding for low power state machines maybe optimized to minimize power consumption. Sequential logic

Specication and Description Language

9.2 Software applications State diagram

The following concepts are commonly used to build soft- State patternware applications with nite state machines: SCXML

12.6 Finite Markov chain processes

We may think of a Markov chain as a process that moves successively through a set of states s1 , s2 , , sr. if it is in state si it moves on to the next stop to state sj with prob- ability pij. These probabilities can be exhibited in the form of a tran- sition matrix (Kemeny (1959), p. 384)

13 External links Finite State Automata at DMOZ Modeling a Simple AI behavior using a Finite State Machine Example of usage in Video Games Free On-Line Dictionary of Computing description of Finite State Machines NIST Dictionary of Algorithms and Data Structures description of Finite State Machines Interactive FSM: Control Circuit, demonstrates the logic ow of the Finite State Machines. FSM simulator, simulates DFAs, NFAs and - NFAs, including generated by regular expression.10 14 TEXT AND IMAGE SOURCES, CONTRIBUTORS, AND LICENSES