Yasir Explains/Compiler Design/Automata/Finite Automata
Automata

Finite Automata

On this page

What is a Finite Automaton?Formal Definition — The 5-TupleState DiagramsAccepting and Rejecting StringsTransition Table Representation
Automata

Finite Automata

Understand the formal 5-tuple model that describes any finite-state machine, learn how a machine processes an input string, and see why finite automata are the computational model behind every lexer.

What is a Finite Automaton?

A finite automaton (FA) is an abstract machine that:

  1. Has a finite number of states.
  2. Reads an input string one symbol at a time, left to right.
  3. At each step, moves from its current state to a new state based on the current symbol.
  4. After reading the entire input, it either accepts the string (if it ended in an accepting state) or rejects it.

Finite automata are the mathematical model behind lexical analysers. Every token pattern described by a regular expression can be recognised by a finite automaton.


Informal Example — A Turnstile

Imagine a subway turnstile. It has two states: Locked and Unlocked.

Example
Actions: push (P) coin (C)
Locked ──C──▶ Unlocked
Locked ──P──▶ Locked (push on a locked gate → stays locked)
Unlocked ──P──▶ Locked (push unlocks the gate → now locked again)
Unlocked ──C──▶ Unlocked (inserting a coin when unlocked → stays unlocked)

This turnstile is a finite automaton with:

  • States: {Locked, Unlocked}
  • Input symbols (alphabet): {P, C}
  • Transitions: as shown above
  • Start state: Locked
  • (There's no accepting state here — this is a Moore machine, but the concept is identical)

Formal Definition — The 5-Tuple

A finite automaton is formally defined as a 5-tuple:

Example
M = ( Q, Σ, δ, q₀, F )
ComponentSymbolMeaning
StatesQA finite, non-empty set of states
AlphabetΣA finite set of input symbols
Transition functionδThe "rules" for moving between states
Start stateq₀The state the machine begins in (q₀ ∈ Q)
Accepting statesFA subset of Q; if the machine ends here it accepts (F ⊆ Q)

Two Types of Finite Automata

TypeTransitionsε-moves
DFA (Deterministic)Exactly one target per (state, symbol)Not allowed
NFA (Non-deterministic)Zero, one, or many targets per (state, symbol)Allowed

Both accept exactly the same class of languages — the regular languages. NFAs are easier to design; DFAs are easier to implement. The next two topics cover each in detail.

State Diagrams

Finite automata are typically drawn as state transition diagrams:

  • Circles represent states.
  • Double circles represent accepting (final) states.
  • Arrows represent transitions, labelled with the input symbol.
  • A filled arrow (→) points to the start state.

Diagram Conventions

Example
→ q0 means q0 is the start state
(q0) means q0 is a regular (non-accepting) state
((q1)) means q1 is an accepting state (double circle)
(q0) --a--> (q1) means: in state q0, on reading 'a', go to state q1

Example — Automaton Accepting Strings Ending in 'a'

Language: all strings over {a, b} that end with the character 'a'. Examples of accepted strings: a, ba, aa, bba, aba, bbba, …

Reading the diagram:

  • q0 = start state, "have not just seen 'a'"
  • q1 = accepting state, "most recent symbol was 'a'"
  • On reading 'a' from q0 → move to q1 (we just saw 'a')
  • On reading 'b' from q0 → stay in q0 (still haven't ended with 'a')
  • On reading 'a' from q1 → stay in q1 (still ending with 'a')
  • On reading 'b' from q1 → go back to q0 (the 'a' is no longer the last symbol)

Trace on "bba":

Example
Input: b b a
State: q0 → q0 → q0 → q1 ← ends in q1 (accepting) ✓

Trace on "ab":

Example
Input: a b
State: q0 → q1 → q0 ← ends in q0 (not accepting) ✗

Accepting and Rejecting Strings

Given a finite automaton M = (Q, Σ, δ, q₀, F) and an input string w = a₁a₂…aₙ:

Acceptance Definition

Example
M accepts w if and only if there exists a sequence of states
r₀, r₁, r₂, …, rₙ such that:
1. r₀ = q₀ (start in the initial state)
2. δ(rᵢ, aᵢ₊₁) = rᵢ₊₁ for each i (follow transitions)
3. rₙ ∈ F (end in an accepting state)

If all three conditions hold, the machine accepts the string. If any condition fails — especially if rₙ ∉ F — the machine rejects the string.


Extended Transition Function δ*

For convenience, we define δ* (delta-star) as the extended transition function that takes a state and an entire string (not just one symbol):

Example
δ*(q, ε) = q (reading nothing: stay in q)
δ*(q, wa) = δ(δ*(q, w), a) (read string w, then symbol a)

With this, we can write the acceptance condition compactly:

Example
M accepts w ⟺ δ*(q₀, w) ∈ F

Language of a Finite Automaton

The set of all strings that automaton M accepts is called the language of M, written L(M):

Example
L(M) = { w | δ*(q₀, w) ∈ F }

Key theorem (Kleene): A language L is regular (described by a regular expression) if and only if some finite automaton M recognises it, i.e., L = L(M).

Transition Table Representation

A transition table is a tabular alternative to the state diagram — especially useful for larger automata.

Example — Automaton for Strings Ending in 'a'

Example
| a | b
──────────────────
→ q0 | q1 | q0
* q1 | q1 | q0

Notation:

  • → marks the start state
  • ***** marks accepting states
  • Rows = states, Columns = input symbols
  • Cell = the target state after the transition

Reading the table:

  • Row q0, column a → q1 (matches the diagram arrow q0 --a--> q1)
  • Row q0, column b → q0 (loop on q0 with b)
  • Row q1, column a → q1 (loop on q1 with a)
  • Row q1, column b → q0 (go back to q0)

Trace using the Transition Table — "bab"

Example
Start: q0
Read 'b': q0 → q0 (row q0, col b = q0)
Read 'a': q0 → q1 (row q0, col a = q1)
Read 'b': q1 → q0 (row q1, col b = q0)
End state: q0 — NOT accepting → string "bab" is REJECTED ✗

Trace — "aba"

Example
Start: q0
Read 'a': q0 → q1 (row q0, col a = q1)
Read 'b': q1 → q0 (row q1, col b = q0)
Read 'a': q0 → q1 (row q0, col a = q1)
End state: q1 — ACCEPTING → string "aba" is ACCEPTED ✓