Finite State Machine Simulator: Build and Test Automata

simulator intermediate ~12 min
Loading simulation...
DFA with 3 states — processing binary input

A 3-state deterministic finite automaton over binary alphabet {0, 1}. The machine transitions between states based on each input symbol, accepting or rejecting the complete input string based on the final state.

Formula

DFA: M = (Q, Σ, δ, q₀, F) where δ: Q × Σ → Q
Number of possible DFAs with |Q| states and |Σ| symbols = |Q|^(|Q|·|Σ|) × 2^|Q|
Regular language: L(M) = { w ∈ Σ* : δ*(q₀, w) ∈ F }

Machines That Recognize Patterns

A finite automaton is one of the simplest models of computation: a machine with a fixed number of states that reads input one symbol at a time and transitions between states according to predefined rules. Despite this simplicity, finite automata are powerful enough to underpin regular expressions, lexical analysis in compilers, network protocols, and hardware controllers. Every regex you write compiles down to one of these machines.

Deterministic vs. Nondeterministic

A deterministic finite automaton (DFA) has exactly one transition for each state-symbol pair — given a state and an input, the next state is uniquely determined. A nondeterministic finite automaton (NFA) allows multiple transitions or epsilon moves. Remarkably, the subset construction theorem proves that every NFA can be converted to an equivalent DFA, though the DFA may have exponentially more states.

The Limits of Finite Memory

Finite automata have no scratchpad memory beyond their current state. This means they cannot count unboundedly: the language of balanced parentheses, for instance, requires a pushdown automaton (which adds a stack). The pumping lemma provides a formal proof technique for showing a language is not regular — a key result in the Chomsky hierarchy of formal languages.

From Theory to Silicon

In hardware, finite state machines are implemented directly with flip-flops (one per state bit) and combinational logic (for transition functions). A 3-state FSM needs 2 flip-flops (since 2^2 ≥ 3). The state encoding, transition logic, and output logic are synthesized from HDL descriptions into actual gates — the same gates you explored in the Boolean gates simulator.

FAQ

What is a finite state machine?

A finite state machine (FSM) is a computational model with a fixed number of states, an input alphabet, transition rules, a start state, and accepting states. It reads input symbols one at a time, transitioning between states according to its rules. If it ends in an accepting state, the input is 'accepted.' FSMs are equivalent in power to regular expressions.

What is the difference between DFA and NFA?

A DFA (deterministic finite automaton) has exactly one transition per state per input symbol — no ambiguity. An NFA (nondeterministic finite automaton) can have multiple transitions for the same symbol, or epsilon transitions (moving without consuming input). Despite this flexibility, NFAs and DFAs recognize exactly the same class of languages.

What can finite automata NOT compute?

Finite automata cannot count unboundedly. They cannot recognize languages like {aⁿbⁿ : n ≥ 0} (equal numbers of a's followed by b's) because they have no memory beyond their fixed states. This limitation is formally proven by the pumping lemma. Context-free grammars and pushdown automata handle such patterns.

Where are finite state machines used in practice?

FSMs are used in lexical analyzers (tokenizing source code), network protocol design (TCP state diagram), UI navigation, vending machines, traffic light controllers, regular expression engines, and hardware control units. They model any system with a finite number of distinct configurations.

Sources

Embed

<iframe src="https://homo-deus.com/lab/logic-circuits/finite-automaton/embed" width="100%" height="400" frameborder="0"></iframe>
View source on GitHub