Chapter 2

Regular expressions and conversions

Regular expressions, finite automata and consistently left- or right-linear grammars describe the same language class. Convert between representations by preserving the language, not by matching the appearance of a diagram.

2.1 Expression semantics and identities

Past papers · CS 2014–2026 (23 papers): 4 questions, 5 marks

The basic expressions are ∅\varnothing, ϵ\epsilon and symbols a∈Σa\in\Sigma. Build expressions using union, concatenation and star. Precedence is star, then concatenation, then union. Parentheses override precedence. In a regex, ϵ\epsilon denotes the singleton language {ϵ}\{\epsilon\}.

Requirement over Σ={0,1}\Sigma=\{0,1\}One expression or construction
Ends in a fixed string vvΣ∗v\Sigma^*v
Contains a fixed string vvΣ∗vΣ∗\Sigma^*v\Sigma^*
Exactly one 110∗10∗0^*10^*
Odd number of 11 symbols0∗1(0∗10∗1)∗0∗0^*1(0^*10^*1)^*0^*
Even number of 11 symbols0∗(10∗10∗)∗0^*(10^*10^*)^*
Contains both 0000 and 1111Σ∗(00Σ∗11+11Σ∗00)Σ∗\Sigma^*(00\Sigma^*11+11\Sigma^*00)\Sigma^*
Avoids a fixed patternComplement the complete DFA for containing it

2.2 Arden equations

Past papers · CS 2014–2026 (23 papers): 1 question, 2 marks

Write one equation for each state's accepted continuations. Include ϵ\epsilon at accepting states. A transition labelled aa from state ii to state jj contributes aXjaX_j to XiX_i.

2.3 State elimination and regex to NFA

Past papers · CS 2014–2026 (23 papers): 7 questions, 10 marks

For regex to ϵ\epsilon-NFA, give a symbol an entry-to-exit edge, give ϵ\epsilon an empty-move edge, and give ∅\varnothing no path. Union branches through new entry and exit states. Concatenation joins the first exit to the second entry. Star adds an empty bypass and a repeat edge. The construction uses a number of states linear in expression size; determinisation may then be exponential.

The extra term enters kk, loops there any number of times and exits. Preserve all old labels while computing one elimination step. Different elimination orders can produce different-looking equivalent expressions.

2.4 Regular grammars

A right-linear grammar uses A→aBA\to aB, A→aA\to a and optionally A→ϵA\to\epsilon; finite terminal strings may replace single aa after expansion. A left-linear grammar uses A→BaA\to Ba, A→aA\to a and optionally A→ϵA\to\epsilon. Use one orientation throughout. A grammar with at most one variable anywhere on a right-hand side is linear, but need not be regular: S→aSb∣ϵS\to aSb\mid\epsilon is linear and generates a nonregular language.

Convert a right-linear grammar to an NFA by making variables states. A rule A→aBA\to aB gives an aa edge; A→aA\to a goes to a new final state; A→ϵA\to\epsilon makes AA final. The start variable becomes the start state. Reverse this construction to obtain a right-linear grammar from an NFA.

Last-minute recall

  • Regex equality means language equality.
  • Star binds more tightly than concatenation and union.
  • Arden's multiplication side determines where the star goes.
  • The no-ϵ\epsilon condition gives Arden uniqueness.
  • State elimination includes all loops through the removed state.
  • A linear grammar need not be a regular grammar.

Revise offline: the whole Theory of Computation handbook as one PDF

40 pages · 1.7 MBFree to read online. Sign in to download the PDF.We save the handbook, version and request time in My downloads. Privacy policy

All chapters · Corrections