3 Regular Expressions

Theory of Computation · Unit 3 · 6 hrs

Regular Expressions

Exam-focused notes for Regular Expressions (Theory of Computation, CSC262): what the TU syllabus asks and how it has actually been tested, with 13 solved past questions from this unit.

Regular Expressions in short

This unit gives regular expressions as an algebraic, textual alternative to drawing an automaton, proves the two notations are equally expressive, and develops the tools, the pumping lemma and the table filling algorithm, used to show a language is not regular and to shrink a DFA to its smallest form.

Regular Expressions: notes

Regular Expressions

A regular expression is an algebraic formula built from the symbols of an alphabet and a small set of operators that describes exactly a regular language, the class of languages accepted by finite automata. Regular expressions are defined inductively over an alphabet $\Sigma$: $\emptyset$ is a regular expression denoting the empty language; $\epsilon$ is a regular expression denoting the language ${\epsilon}$; for each symbol $a \in \Sigma$, $a$ is a regular expression denoting the language ${a}$; and if $r$ and $s$ are regular expressions denoting languages $L(r)$ and $L(s)$, then $r \mid s$, $rs$, and $r^$ are regular expressions denoting $L(r)\cup L(s)$, $L(r)L(s)$, and $L(r)^$ respectively. Every regular expression is built by finitely many applications of these rules, which is what makes it possible to prove properties of regular languages by induction on the structure of the expression that describes them.

Regular Operators

A regular expression is built from two kinds of ingredient: symbols, the individual input characters or $\epsilon$, and operators, which combine smaller expressions into larger ones. The operators are union ($\mid$ or $\cup$), whose language is the set union of its operands; concatenation (written by simple juxtaposition, or $\cdot$), whose language is every string formed by following a string of the first operand with a string of the second; Kleene closure (${}^$), whose language is every string formed by concatenating zero or more strings from the operand's language; and positive closure ($^+$), defined by $r^+ = rr^$, whose language excludes the case of zero repetitions. For example, in $(0 \mid 1)01^*$, the symbols are $0$ and $1$, the operators are union and Kleene star (with concatenation implicit between adjacent factors), and the described language is every binary string that starts with either $0$ or $1$, is followed by exactly one $0$, and ends with zero or more $1$s.

Regular Languages and their applications

A language is regular exactly when some regular expression describes it, equivalently exactly when some finite automaton accepts it. Regular expressions are used throughout software engineering wherever a pattern, rather than an exact string, needs to be matched: validation, checking that user input such as an email address or a password conforms to a set of formatting rules; search and selection, picking out the substrings of a larger text that match a pattern; tokenization, splitting a stream of characters into words or tokens such as keywords, identifiers, and numeric literals; and the lexer (lexical analyzer) stage of a compiler, which is essentially a collection of regular expressions, one per token category, run simultaneously over the source text.

Algebraic Rules for Regular Expressions

Regular expressions obey algebraic identities analogous to those of ordinary arithmetic, which let one regular expression be rewritten into an equivalent but simpler one:

LawIdentity
Identity for union$r \mid \emptyset = r$
Identity for concatenation$r\epsilon = \epsilon r = r$
Annihilator for concatenation$r\emptyset = \emptyset r = \emptyset$
Idempotent union$r \mid r = r$
Commutative union$r \mid s = s \mid r$
Associative union$(r\mid s)\mid t = r\mid(s\mid t)$
Associative concatenation$(rs)t = r(st)$
Distributive$r(s\mid t) = rs \mid rt$, $\ (s\mid t)r = sr\mid tr$
Closure of closure$(r^)^ = r^*$
Closure and epsilon$\epsilon \mid rr^* = r^*$

Concatenation does not distribute over itself and is not commutative ($rs \neq sr$ in general), which mirrors the fact that string order matters. These identities are exactly the tool used to simplify the expression produced when converting a DFA to a regular expression, below.

What this unit covers

  • Regular Expressions
  • Regular Operators
  • Regular Languages and their applications
  • Algebraic Rules for Regular Expressions
  • Equivalence of Regular Expression and Finite Automata
  • Reduction of Regular Expression to ε – NFA
  • Conversion of DFA to Regular Expression
  • Properties of Regular Languages
  • Pumping Lemma
  • Application of Pumping Lemma
  • Closure Properties of Regular Languages over (Union, Intersection, Complement)
  • Minimization of Finite State Machines: Table Filling Algorithm

Minimization of Finite State Machines

2081 · 10 marks

List any two regular operators. Minimize the following finite state machine using Table Filling algorithm.[10]

Show solved answer

Regular Operators and DFA Minimization Using Table Filling Algorithm


Part 1: Two Regular Operators

Regular operators are the operations used to construct regular expressions from smaller ones. Any two of the following are acceptable:

  1. Union (| or +): If $R$ and $S$ are regular expressions, then $R + S$ denotes the set of strings belonging to either $R$ or $S$.

    • Example: $(a \mid b)$ denotes ${a, b}$
  2. Concatenation (.): If $R$ and $S$ are regular expressions, then $RS$ denotes strings formed by a string of $R$ followed by a string of $S$.

    • Example: $ab$ denotes ${ab}$
  3. Kleene Star (*): If $R$ is a regular expression, then $R^{*}$ denotes zero or more concatenations of $R$.

    • Example: $a^{*}$ denotes ${\varepsilon, a, aa, aaa, \ldots}$

Part 2: DFA Minimization Using Table Filling

Given Data (extracted transition table)

State01
$\to A$BC
$B$AD
$C$EF
$D$EF
$E$EF
$*F$FF
  • Start state: $A$
  • Final state(s): ${F}$

Note: The problem statement references a machine whose diagram/table is not fully reproduced in the prompt. The solution uses the transition table extracted above. If the intended machine differs, the extracted table above is what this solution assumes.


Step 1: Initialize the triangular table

Mark every pair where exactly one state is final. Final = ${F}$.

Marked pairs (one is $F$): $(A,F), (B,F), (C,F), (D,F), (E,F)$.

B | _ |
C | _ | _ |
D | _ | _ | _ |
E | _ | _ | _ | _ |
F | X | X | X | X | X |
  | A | B | C | D | E |

Step 2: Iterative marking

For each unmarked pair, check transitions on $0$ and $1$; mark if any successor pair is marked.

Pass 1:

  • $(A,B)$: $0\to(B,A)$ unmarked, $1\to(C,D)$ unmarked → unmarked
  • $(A,C)$: $1\to(C,F)$ marked → MARK
  • $(A,D)$: $1\to(C,F)$ marked → MARK
  • $(A,E)$: $1\to(C,F)$ marked → MARK
  • $(B,C)$: $0\to(A,E)$ marked → MARK
  • $(B,D)$: $0\to(A,E)$ marked → MARK
  • $(B,E)$: $0\to(A,E)$ marked → MARK
  • $(C,D)$: $0\to(E,E)$ same, $1\to(F,F)$ same → unmarked
  • $(C,E)$: $0\to(E,E)$ same, $1\to(F,F)$ same → unmarked
  • $(D,E)$: $0\to(E,E)$ same, $1\to(F,F)$ same → unmarked

Pass 2 (re-check remaining unmarked pairs):

  • $(A,B)$: $0\to(A,B)$ still unmarked, $1\to(C,D)$ still unmarked → remains unmarked
  • $(C,D)$: both transitions to same states → remains unmarked
  • $(C,E)$: both transitions to same states → remains unmarked
  • $(D,E)$: both transitions to same states → remains unmarked

No further changes → algorithm terminates.

Final table:

B | _ |
C | X | X |
D | X | X | _ |
E | X | X | _ | _ |
F | X | X | X | X | X |
  | A | B | C | D | E |

Step 3: Group equivalent (unmarked) states

Unmarked pairs: $(A,B)$, $(C,D)$, $(C,E)$, $(D,E)$.

  • $A \equiv B$
  • $C \equiv D \equiv E$
  • $F$ (alone)

Equivalence classes: $${A, B}, \quad {C, D, E}, \quad {F}$$


Step 4: Minimized DFA

Let:

  • $P = {A, B}$ (start state)
  • $Q = {C, D, E}$
  • $R = {F}$ (final state)

Compute transitions (using any representative):

  • $P$: on $0$, $\delta(A,0)=B\in P$; on $1$, $\delta(A,1)=C\in Q$
  • $Q$: on $0$, $\delta(C,0)=E\in Q$; on $1$, $\delta(C,1)=F\in R$
  • $R$: on $0$, $\delta(F,0)=F\in R$; on $1$, $\delta(F,1)=F\in R$

Minimized transition table:

State01
$\to P$PQ
$Q$QR
$*R$RR

The minimized DFA has 3 states: ${A,B}$, ${C,D,E}$, ${F}$.

Open this answer on its own page →

2078 · 10 marks

Find the minimum state DFA for the given DFA below:

States01
ABF
BEC
CBD
*DEF
EBC
FBA

[10]

Transition table: States 0 1 -------------- A B F B E C C B D D E F E B C F B A - Start state: A - Final state: D - Alphabet: {0, 1} - Final: {D} - Non-final: {A, B, C, E, F} Mark all pairs (X, D) for X in {A,B,C,E,F} as distinguishable: Marked: (A,D), (B,D), (C,D), (E,D), (F,D) (A,C): 0: (B,B); ...

Open this answer on its own page →

Regular Expressions

2080 · 5 marks

Write regular expression over {a,b} that represents a. Strings having exactly two a's and at least two b's. b. Strings having an even number of a's and each a followed by at least one b. [5]

Show solved answer

Regular Expressions over {a, b}

Given data

  • Alphabet: $\Sigma = {a, b}$
  • Part (a): strings with exactly two a's AND at least two b's.
  • Part (b): strings with an even number of a's, and each a immediately followed by at least one b.

Part (a): Exactly two a's and at least two b's

Structure. With exactly two a's, every string has the shape:

$$b^,a,b^,a,b^*$$

The three $b^*$ blocks (before, between, after) hold all the b's. The constraint is that the total number of b's across all three blocks is at least 2.

Deriving a compact expression. The plain shape $b^*ab^ab^$ allows 0 or 1 b's, which we must forbid. So we subtract those cases by forcing the b-count $\ge 2$. This is cleanly expressed by casing on where the "guaranteed" b's sit. A correct enumeration (each covers b-total $\ge 2$, union covers all):

$$R = b^{2+}ab^ab^ ;+; b^+ab^+ab^* ;+; b^+ab^*ab^+ ;+; b^ab^{2+}ab^ ;+; b^*ab^+ab^+ ;+; b^*ab^*ab^{2+}$$

where $b^{2+} = bbb^$ (at least two b's) and $b^+ = bb^$.

A shorter equivalent that is fully acceptable in an exam:

$$\boxed{R = b^*a,b^a,b^\ \text{restricted to total } b\text{'s} \ge 2}$$

expressed formally as the union above. In terms of "at least one block has $bb$, OR two blocks each have a $b$":

$$R = bbb^* a, b^* a, b^* + b^* a, bbb^* a, b^* + b^* a, b^* a, bbb^* + bb^* a, bb^* a, b^* + bb^* a, b^* a, bb^* + b^* a, bb^* a, bb^*$$

Check. Every accepted string must carry exactly two a's, so test $baab$: it has one b before the a's, the two a's adjacent, and one b after, giving a b-total of 2, and it matches the term $b^+ab^*ab^+$. The string $aab$ carries only one b and is correctly rejected. The string $bbaa$ has two b's ahead of the first a and matches the term $b^{2+}ab^ab^$.


Part (b): Even number of a's, each a followed by at least one b

Structure.

  • Each a must be immediately followed by at least one b, so the basic unit is $ab^+$ (i.e. $abb^*$).
  • The number of a's must be even, so units come in pairs: $(ab^+ab^+)$, repeated zero or more times.
  • Extra b's may appear at the start (before any a).

Regular expression:

$$\boxed{R = b^,(ab^+,ab^+)^}$$

Verification:

StringMatch?Reason
$\varepsilon$Yeszero a's (even)
$bb$Yes$b^*$ only
$abab$Yesone pair $ab,ab$
$abbabb$Yes$abb,abb$
$babbab$Yes$b^*=b$, then $abb,ab$
$ab$Noone a (odd)
$aab$Nofirst a not followed by b
$abba$Nolast a not followed by b

Final Answers

(a) Exactly two a's and at least two b's: $$R = b^{2+}ab^ab^ + b^+ab^+ab^* + b^+ab^*ab^+ + b^ab^{2+}ab^ + b^*ab^+ab^+ + b^*ab^*ab^{2+}$$

(b) Even number of a's, each a followed by at least one b: $$R = b^(ab^+ab^+)^$$

Open this answer on its own page →

2078 · 5 marks

Give the regular expressions for the following language over alphabet {a, b}. a. Set of all strings with substring bab or abb. b. Set of all strings whose 3rd symbol is 'a' and 5th symbol is 'b'. [5]

We need all strings over {a, b} that contain bab as a substring, or contain abb as a substring (or both). Step-by-step reasoning: - Any string containing bab: prefix can be any string over {a,b}, then bab, then any suffix over {a,b} - RE: (a+b) bab (a+b) - Any string containing abb: similarly - R...

Open this answer on its own page →

2076 · 5 marks

Give the regular expressions for following language over alphabet {0, 1}. a. Set of all strings with 2nd symbol from right is 1. b. Set of all strings starting with 00 or 11 and ending with 10 or 01. [5]

Analysis: To ensure the 2nd symbol from the right is 1, the string must have the form: - The last symbol can be either 0 or 1, i.e., (0+1) - The 2nd from right must be 1 - Before that, there can be any string of 0s and 1s (including empty), i.e., (0+1) Regular Expression: $$R = (0+1)^ \cdot 1 \cd...

Open this answer on its own page →

2080.1 · 5 marks

Construct regular expression over {1,2,...,9} that represents a. strings of even numbers with length 4 starting with 2 and ending with 8. b. strings starting with odd numbers and ending with even numbers. [5]

  • Concatenation (.): places symbols/expressions one after another - Union (+): matches either expression - Kleene Star (): zero or more repetitions - Kleene Plus (+): one or more repetitions --- Constraints: - Length exactly 4 - First symbol: 2 - Last symbol: 8 - All symbols must be even numbers ...
Open this answer on its own page →

Pumping Lemma

2080 · 5 marks

Using pumping lemma, prove that the language is not regular. $L = {a^i b^j c^k \mid j=i+k}$ [5]

  • Language: $L = {a^i b^j c^k \mid j = i + k,\ i,k \ge 0}$ - Alphabet: ${a, b, c}$ - Method required: Pumping Lemma for regular languages. --- Suppose $L$ is regular. Then by the Pumping Lemma, there exists a pumping length $p \ge 1$ such that any string $s \in L$ with $s \ge p$ can be writte...
Open this answer on its own page →

2079 · 10 marks

State and prove the Pumping Lemma for regular languages. How can you show with example that pumping lemma is used to prove that a given language is not a regular? Explain.[10]

If A is a regular language, then there exists a pumping length 'p' such that any string 's' where s = p may be divided into three parts s = xyz, satisfying all of the following conditions: 1. xy^i z ∈ A for every i = 0 (the string can be "pumped" any number of times and still belong to A) 2. y 0 ...

Open this answer on its own page →

2076 · 5 marks

Show that language is not a regular language. $L = { 0^m 1^m \mid m \geq 1 }$ [5]

--- If A is a regular language, then there exists a pumping length p such that any string s with s ≥ p can be divided into three parts s = xyz satisfying: 1. xy^i z ∈ A for every i ≥ 0 2. y 0 (y cannot be empty) 3. xy ≤ p (xy together must not exceed pumping length) --- Step 1: Assume L is regula...

Open this answer on its own page →

Reduction of Regular Expression to ε – NFA

2079 · 5 marks

Convert the following regular expression into equivalent Finite Automata. a.(0+1)∗10(1+0)a. (0+1)10(1+0)a.(0+1)∗10(1+0)b.1∗0(0+1)∗1b. 10(0+1)*1b.1∗0(0+1)∗1[5]

Two regular expressions to convert to Finite Automata: - (a) $(0+1)^,1,0,(1+0)$ - (b) $1^,0,(0+1)^,1$ Alphabet: $\Sigma = {0, 1}$ for both. --- Break into parts: 1. $(0+1)^$ = any string of 0s and 1s (including empty) 2. $10$ = literal "10" 3. $(1+0)$ = one symbol, either 0 or 1 Construct...

Open this answer on its own page →

2076 · 10 marks

Define the NFA with ϵ−transition\epsilon-transitionϵ−transition and ϵ−closure\epsilon-closureϵ−closure of a state. Show that for every regular expression r, representing a language L, there is ϵ−NFA\epsilon-NFAϵ−NFA accepting the same language. Also convert regular expression (a+b)ab into equivalent Finite Automata.[10]

Show solved answer

NFA with ε-Transition, ε-Closure, and Regular Expression to FA Conversion


1. NFA with ε-Transition (ε-NFA)

An NFA with ε-transition (also called ε-NFA) is a Nondeterministic Finite Automaton that allows transitions on the empty string ε (without consuming any input symbol).

Formally, an ε-NFA is defined as a 5-tuple:

M = (Q, Σ, δ, q₀, F)

where:

  • Q = finite set of states
  • Σ = finite input alphabet
  • δ : Q × (Σ ∪ {ε}) → 2^Q = transition function (can take ε as input)
  • q₀ ∈ Q = start state
  • F ⊆ Q = set of final/accepting states

The key difference from a standard NFA is that the transition function accepts ε as a valid input, allowing the automaton to move from one state to another without reading any symbol.


2. ε-Closure of a State

The ε-closure of a state q is the set of all states reachable from q by following zero or more ε-transitions (including q itself).

ε-closure(q) = {p ∈ Q | p is reachable from q using only ε-transitions}

Properties:

  • q is always in ε-closure(q) (reflexive)
  • If p ∈ ε-closure(q) and r ∈ ε-closure(p), then r ∈ ε-closure(q) (transitive)

Example: If q --ε--> p --ε--> r, then ε-closure(q) = {q, p, r}


3. Theorem: For Every Regular Expression r, There Exists an ε-NFA Accepting L(r)

Proof by Structural Induction (Thompson's Construction Rules):

We show construction rules for each case of a regular expression.


Base Cases

Rule 1: r = ∅ (empty language)

  →[q₀]  (no transitions, no final state)

The ε-NFA has start state q₀ with no accepting states.


Rule 2: r = ε (empty string)

  →[q₀] --ε--> ((q_f))

One ε-transition from start to final state.


Rule 3: r = a (single symbol a ∈ Σ)

  →[q₀] --a--> ((q_f))

One transition on symbol a from start to final state.


Inductive Cases

Assume ε-NFAs N(r) and N(s) exist for regular expressions r and s.


Rule 4: r = r₁ | r₂ (Union / Alternation)

Create a new start state and new final state, connect with ε-transitions:

              ε --> [N(r₁)] --> ε
  →[q₀] --<                        >--> ((q_f))
              ε --> [N(r₂)] --> ε
  • New start state q₀ goes to start of N(r₁) and N(r₂) via ε
  • Final states of N(r₁) and N(r₂) go to new final state q_f via ε

Rule 5: r = r₁ . r₂ (Concatenation)

Connect the final state of N(r₁) to the start state of N(r₂) via ε:

  →[N(r₁)] --ε--> [N(r₂)]-->((q_f))
  • Final state of N(r₁) becomes non-final
  • ε-transition connects it to start of N(r₂)

Rule 6: r = r₁ (Kleene Star / Closure)*

         ε (loop back)
         ↑___________↓
  →[q₀] --ε--> [N(r₁)] --ε--> ((q_f))
      |_________________________↑
                  ε (skip, for empty string)
  • New start state q₀ connects to start of N(r₁) via ε
  • Final state of N(r₁) loops back to start of N(r₁) via ε
  • New start state also connects directly to new final state via ε (to accept ε)

Since every regular expression is built from these base cases and operations, and we have shown an ε-NFA construction for each, by induction, every regular expression has an equivalent ε-NFA. ∎


4. Convert Regular Expression (a+b)ab to Equivalent Finite Automaton

The regular expression is: (a+b)*ab*

This means: strings over {a,b} that contain at least one 'a', ending with zero or more b's -- more precisely, any string of a's and b's, followed by 'a', followed by zero or more b's.

Step 1: Break the expression into parts

PartMeaning
(a+b)*Zero or more a's or b's (any string)
aSingle symbol a
b*Zero or more b's

Step 2: Build ε-NFA for each part

Part 1: (a+b)*

         a
    ε ↗[q₁]↘ ε
[q₀]          [q₃] --ε--> ((q₄))
    ε ↘[q₂]↗ ε
         b

With loop: q₄ --ε--> q₀ (for the * closure), and q₀ --ε--> q₄ (for accepting zero repetitions of (a+b), i.e. the empty string). This gives the complete ε-NFA fragment for (a+b)*, with q₀ as its start state and q₄ as its accepting state.

Part 2: Single symbol a

  →[q5] --a--> ((q6))

Part 3: b*

        ε (loop back)
        ↑___________↓
  →[q7] --ε--> [q8] --b--> ((q9))
      |_____________________↑
                  ε (skip, for zero b's)

Step 3: Concatenate the Three Parts

By the concatenation rule (Rule 5 above), the accepting state of one block is joined to the start state of the next block with an ε-transition, and it stops being an accepting state in its own right (only the final block keeps an accepting state):

  • q₄ --ε--> q₅ (end of the (a+b)* block feeds into the mandatory a)
  • q₆ --ε--> q₇ (end of the mandatory a feeds into the b* block)

Step 4: Complete ε-NFA for (a+b)*ab*

StateOn εOn aOn b
q0 (start)q1, q2, q4----
q1--q3--
q2----q3
q3q0----
q4q5----
q5--q6--
q6q7----
q7q8, q9----
q8q9--q8
q9 (accept)------

Verification:

  • On "ab": q0 --ε--> q4 (zero repetitions of (a+b)) --ε--> q5 --a--> q6 --ε--> q7 --ε--> q9 (zero b's). Accepted.
  • On "aabb": q0 --ε--> q1 --a--> q3 --ε--> q0 --ε--> q4 --ε--> q5 --a--> q6 --ε--> q7 --ε--> q8 --b--> q8 --b--> q8 --ε--> q9. The symbols consumed, in order, are a (one turn of the star), a (the mandatory a), b, b (two turns of b*), i.e. exactly "aabb". Accepted.

This ε-NFA accepts precisely the language of (a+b)*ab*: any string over {a, b} (possibly empty), followed by a mandatory a, followed by zero or more b's. This completes the required conversion of the regular expression to an equivalent finite automaton via Thompson's construction.

Open this answer on its own page →

Application of Pumping Lemma

2078 · 5 marks

Show that L = {ana^nan | n is a prime number} is not a regular language. [5]

Note: The language can be simplified as L = {a^(3n) n is prime}, i.e., strings of a's whose length is 3 times a prime number. --- If A is a regular language, then there exists a pumping length p such that any string s in A with s = p can be written as s = xyz, satisfying: 1. xy^i z ∈ A for every ...

Open this answer on its own page →

Conversion of DFA to Regular Expression

2080.1 · 5 marks

State Arden's theorem. Convert following DFA into its regular expression using Arden theorem.

01
$\rightarrow *Q1$$Q1$$Q2$
$Q2$$Q3$$Q2$
$Q3$$Q1$$Q2$

[5]

Transition table: State 0 1 ------------- →Q1 Q1 Q2 Q2 Q3 Q2 Q3 Q1 Q2 - Start state: Q1 - Final state: Q1 If $P$ and $Q$ are regular expressions over $\Sigma$, and $P$ does not contain $\varepsilon$, then the equation $$R = Q + RP$$ has the unique solution $$R = QP^$$ Incoming transitions: - Into...

Open this answer on its own page →