CS Atlas GATE 2027
TOC course
0 / 91 complete

TOC/Phase 2 · Regular Expressions & Regular Languages

Lesson 2.2.1 · Item 34 of 91

Regex → ε-NFA (Thompson construction)

Read at your own pace. Try the questions before opening the answers.

On this page

Recall warm-up

  1. What is ECLOSE(q)?
    Stuck? Peek at 1.2.2 ε-NFA and ε-closure
    • ε-NFA: δ : Q × (Σ ∪ {ε}) → P(Q).
    • ECLOSE(q) = ε-reachable states incl. q (graph reachability on ε-edges).
    • Run: start ECLOSE(q₀); each symbol: move, then ECLOSE. Accept iff final set ∩ F ≠ ∅.
    • Same power as DFA/NFA; great for wiring machines together.

    Read lesson 1.2.2

  2. Simplify (a*)*a*.
    Stuck? Peek at 2.1.2 Writing regexes, identities and equivalence
    • ∅ + r = r; ∅r = ∅; εr = r; ∅* = ε* = ε; (r*)* = r*; r*r* = r*; ε + rr* = r*; (rs)*r = r(sr)*.
    • (r+s)* = (r*s*)* = (r*+s*)* = r*(sr*)* = (r*s)*r*.
    • ≠: (r+s)* vs r*+s* vs r*s*; (rs)* vs r*s*.
    • No “aa”: (b+ab)*(ε+a). At most one a: b*(ε+a)b*. Even #0: 1*(01*01*)*.
    • Equality: compare shortest strings; counterexample kills; identities or minimal DFAs prove.

    Read lesson 2.1.2

  3. Subset construction upper bound for a 6-state NFA?
    Stuck? Peek at 1.2.3 Subset construction and the 2ⁿ blow-up
    • Subset construction: start {q₀} (or ECLOSE(q₀)); δ(S,a) = ⋃ δ(q,a) (then ECLOSE); accept iff S ∩ F ≠ ∅; ∅ = dead state.
    • ≤ 2ⁿ states; count only reachable sets.
    • ε-removal: δ'(q,a) = ECLOSE(δ(ECLOSE(q), a)); F' = {q : ECLOSE(q) ∩ F ≠ ∅}.
    • k-th from end is 1: NFA k+1, DFA 2ᵏ. k-th from start: DFA k+2.
    • DFA ≡ NFA ≡ ε-NFA in power.

    Read lesson 1.2.3

The Hook

Your editor’s search box takes a regex you typed and must turn it into a machine that scans text. It can’t ask a human to design a DFA. It needs a mechanical recipe that works for every regex, by building small machines for small pieces and wiring them together with ε-moves. That recipe is Thompson’s construction, and it proves half of Kleene’s theorem: every regex describes a regular language.

Predict First

You have one machine for r and one for s, each with one start and one accepting state. Using only ε-moves and two new states, how would you build a machine for r + s?

Core Concept

Prediction check

New start state with ε-moves into both starts; both old accepting states get ε-moves to a new single accepting state (and stop being accepting). Exactly 2 new states and 4 ε-moves.

Thompson: a + b

States: s, a1, a2, b1, b2, f

Transitions: s>a1: ε; s>b1: ε; a1>a2: a; b1>b2: b; a2>f: ε; b2>f: ε

r + s: a new start fans out with ε, the two accept states merge into a new one with ε. Step through a and b.

The invariant

Every Thompson machine has exactly one start state, exactly one accepting state, no moves into the start, and no moves out of the accepting state. That is what makes the wiring always work.

one start
no arrows come into it
one accept
no arrows leave it
so
blocks snap together with ε-moves
The invariant that makes the construction always work.

Base cases

  • ∅: start and accept, no move between them.
  • ε: start ─ε→ accept.
  • a: start ─a→ accept.
Base case: a

States: s, f

Transitions: s>f: a

Base case a: two states, one arrow. (ε: the arrow is ε. ∅: no arrow at all.)

Inductive cases

  • r + s: new start ─ε→ start(r), start(s); accept(r), accept(s) ─ε→ new accept. (+2 states)
  • rs: accept(r) ─ε→ start(s); start = start(r), accept = accept(s). (+0 states; many books merge the two states, giving one less)
  • r*: new start s′, new accept f′; s′ ─ε→ start(r), s′ ─ε→ f′ (zero repetitions), accept(r) ─ε→ start(r) (repeat), accept(r) ─ε→ f′. (+2 states)
steps illustration
The three inductive cases — press Next.

Size

Each symbol occurrence adds 2 states; each + and each * adds 2 more. So the ε-NFA has at most 2 × (number of symbols and operators) states — linear in the regex length. (Then subset construction may blow it up to a DFA, and minimization shrinks it again.)

2a6a+b8(a+b)*10(a+b)*b10(ab+a)*
strict Thompson states
Size grows linearly: 2 per symbol, +2 per union and per star.

Smaller hand-built ε-NFAs

In exams you rarely need strict Thompson; any correct ε-NFA is fine. Shortcut constructions: a* = one state with a self-loop that is both start and accepting; ab = a chain; (a+b)* = one state with loops a, b.

a*
1 state with a self-loop; start = accept
(a+b)*
1 state looping on a and b
ab
a 3-state chain
In exams any correct ε-NFA is fine — hand-built ones are much smaller.

Putting it together

  • Thompson builds an ε-NFA by induction: base machines, then ε-wiring for +, concatenation and *.
  • Each block has one start (no arrows in) and one accept (no arrows out).
  • Size ≤ 2 × (symbols + operators); regex ⇒ regular.

Visual

   r + s                              r s                         r*
        ε ┌─[ r ]─┐ ε                 [ r ] ─ε→ [ s ]           ┌──── ε ────┐
  ○ ─────►┤       ├─────► ◎                                   ▼           │
        ε └─[ s ]─┘ ε                                  ○ ─ε→ [ r ] ─ε→ ◎
                                                        └──────── ε ───────►┘ (skip)
Thompson ε-NFA for (a+b)*b

States: s, i, a1, a2, b1, b2, j, f, c1, c2

Transitions: s>i: ε; i>a1: ε; i>b1: ε; a1>a2: a; b1>b2: b; a2>j: ε; b2>j: ε; j>i: ε; j>f: ε; s>f: ε; f>c1: ε; c1>c2: b

The machine above is the Thompson ε-NFA for (a+b)*b: a star around a union, concatenated with b. Step through “ab”: the start closure already contains a1, b1 and c1 (zero repetitions).

Worked Examples

Easy

Draw a Thompson-style ε-NFA for ab.

Show the full solution

1 ─a→ 2 ─ε→ 3 ─b→ 4, start 1, accept 4. (Merging 2 and 3 gives 1 ─a→ 2 ─b→ 3.) Trace “ab”: {1} → {2,3} → {4} accept; “a”: ends {2,3} reject.

Medium

How many states does strict Thompson produce for (a + b)*?

Show the full solution

a: 2, b: 2, union +2 → 6; star +2 → 8.

GATE-level

Strict Thompson (no merging) for (ab + a)*: number of states?

Show the full solution

a: 2, b: 2, ab: 4 (concatenation adds no states); second a: 2; union: 4 + 2 + 2 = 8; star: 10. 10 states. (A hand-built NFA needs only 2: q0 accepting, q0 ─a→ q1, q1 ─b→ q0, q1 ─ε→ q0.)

GATE Traps

  • In r*, forgetting the skip edge (start ─ε→ accept) loses ε.
  • In r*, the loop must go from accept(r) back to start(r), not from the new accept.
  • State counts differ between “strict Thompson” and versions that merge concatenation states — read the question.

Memory Hook

Thompson is Lego with one stud in, one stud out: every block has one start, one end, and ε-bricks snap them together.

Connect the Dots

Regex → ε-NFA → (subset) DFA: every regex is regular. Next: the other direction, DFA → regex, which completes Kleene’s theorem and is a frequent GATE calculation.

Self-Test

  1. NAT

    States in strict Thompson ε-NFA for a + b?

    Write your answer before opening the key.

  2. NAT

    States in strict Thompson ε-NFA for a*?

    Write your answer before opening the key.

  3. MCQ

    In the Thompson construction for r*, which ε-move makes ε accepted?

    1. accept(r) → start(r)
    2. new start → new accept
    3. new start → start(r)
    4. accept(r) → new accept
  4. MSQ

    Properties of every Thompson ε-NFA:

    1. exactly one accepting state
    2. no transitions into the start state
    3. no transitions out of the accepting state
    4. no ε-moves

Rule Card

  • Invariant: one start (no in-moves), one accept (no out-moves).
  • ∅, ε, a: 2 states. r+s: +2 states, 4 ε. rs: ε from accept(r) to start(s). r*: +2 states, 4 ε (enter, skip, loop back, exit).
  • States ≤ 2 × (symbols + operators). Regex → ε-NFA → DFA proves regex ⇒ regular.

Concept Ledger

  • Thompson construction
  • single start / single accept invariant

Answer Vault

Reveal answers when you’re ready

Warm-up answers

  1. States reachable from q by ε-moves only, including q.
  2. a*.
  3. 2⁶ = 64.

Self-test answers

  1. 6 — 2 + 2 + 2.
  2. 4 — 2 + 2.
  3. B — The skip edge.
    • A: That allows repetition.
    • C: Enters r.
    • D: Exits after at least one pass.
  4. ABC — The invariant.
    • D: It is full of ε-moves.

Flashcards

  1. Thompson: new states added for r + s and r*?
    Reveal answer2 each.
  2. Thompson r*: the four ε-moves?
    Reveal answernew start→start(r), new start→new accept, accept(r)→start(r), accept(r)→new accept.
  3. Strict Thompson states for (a+b)*?
    Reveal answer8

Ready for the next idea?

Mark this item complete when you’ve finished reading and working through it.