TOC UNIT Ii.1
TOC UNIT Ii.1
THEORY OF COMPUTATION
UNIT-II
REGULAR EXPRESSION AND FINITE AUTOMATA
Regular Expression,Finite automata with null move,Regular Expression to finite
automata
Arden Theorem,Pumping lemma for regular Expressions
FA with output: moore machine,melay machine with Euivalence.
REGULAR EXPRESSION
The syntax of regular expressions defines how patterns are expressed. Below are the
main components:
1. Alphabet Symbols: Individual characters from the input alphabet (e.g., ‘a’, ‘b’,
‘0’, ‘1’).
2. Concatenation: Writing two expressions next to each other signifies their
sequence. For example, ab matches “a” followed by “b”.
3. Union: Represented by the pipe symbol |. For example, a|b matches either “a” or
“b”.
4. Kleene Star: Represented by *, it denotes zero or more repetitions of a pattern.
For example, a* matches “”, “a”, “aa”, and so on.
5. Parentheses: Used to group expressions for clarity and precedence. For
example, (a|b)c matches “ac” or “bc”.
1. Union: Combines two patterns such that either can match. For example, a|
b matches “a” or “b”.
2. Concatenation: Matches sequences of patterns. For example, ab matches “a”
followed by “b”.
3. Kleene Star: Allows repetition of a pattern. For example, a* matches “”, “a”,
“aa”, “aaa”, and so on.
These operations form the basis for defining all regular languages, making regular
expressions a powerful tool for describing patterns.
Regular expressions and finite automata are equivalent in their expressive power. This
means that for every regular expression, there exists a finite automaton (DFA or NFA)
that recognizes the same language, and vice versa.
This equivalence highlights the theoretical foundation of regular expressions and their
practical use in defining and recognizing regular languages.
Let’s construct an NFA for the regular expression (a|b)*ab, which matches any string
of ‘a’s and ‘b’s that ends with “ab”.
(a|b)*: Matches any sequence of ‘a’s and ‘b’s, including the empty string.
ab: Ensures that the string ends with the exact sequence “ab”.
We will construct an NFA for this regular expression using Thompson’s construction
method.
To construct the NFA, we break the regular expression into its components and build
NFAs for each part:
For (a|b): Create two parallel transitions from a state, one for ‘a’ and another for
‘b’.
For (a|b)*: Add a loop to handle repetition, allowing the NFA to return to the
starting state after reading ‘a’ or ‘b’.
For ab: Add two sequential transitions: one for ‘a’, then another for ‘b’.
Connect the NFA for (a|b)* to the start of the NFA for ab.
Ensure the final state of the NFA for ab is the accept state for the combined NFA.
Include ε-transitions where necessary to combine the components seamlessly.
Start State: q₀
Accept State: q₂
Key Transitions:
State Diagram
State diagram showing all transitions for ‘a’, ‘b’, and ε, with q₀ as the start state and
q₂ as the accept state.
FINITE AUTOMATA WITH NULL MOVE
In this method, we try to remove all the ε-transitions from the given Non-
deterministic finite automata (NFA) −
Step 1 − Find out all the ε-transitions from each state from Q. That will be called as ε-
closure(qi) where, qi ∈Q.
Step 2 − Then, δ1 transitions can be obtained. The δ1 transitions means an ε-closure
on δ moves.
Step 3 − Step 2 is repeated for each input symbol and for each state of given NFA.
Step 4 − By using the resultant status, the transition table for equivalent NFA without
ε can be built.
Before solving some examples based on conversion of NFA with ε to NFA without ɛ
above rule should be remembered.
Solution: Step 1: We will first obtain ε - closure of each state i.e. we will find out ε -
reachable states from current state.
Hence
As ε - closure (q0) means with null input (no input symbol) we can reach to q0, q1 or
q2. In a similar manner for q1 and q2 ε - closures are obtained.
Step 2: Now we will obtain δ' transitions for each state on each input symbol.
= ε - closure (q0 U ϕ U ϕ)
= ε closure (ϕ U q1 U ϕ)
= ε - closure (q1)
= ε closure (δ (q1,q2), 0)
= ε closure (ϕUϕ)
= ε - closure (ϕ)
=ϕ
= ε - closure (q1 U ϕ)
= ε - closure (q1)
= {q1, q2}
= ε - closure (ϕ)
=ϕ
= ε - closure (δ (q2,1))
= ε - closure (ϕ)
δ' (q2, 1) = ϕ
= ε - closure (q2)
= ε - closure (q2)
= {q2}
= ε closure (ϕ U q2)
= {q2}
= ε - closure (q2)
= {q2}
δ'(q0, 0) = {q0, q1, q2}, δ'(q0, 1) = {q1, q2), δ'(q0, q2) = {q2}
Step 3:
Step 4:
Here q0, q1 and q2 is a final state because ε closure (q0), ε - closure (q1) and ε -
closure (q2) contains final state q2.
Example 2 : Convert the following NFA with e to NFA without ε.
Solution: We will first obtain ε - closure of every state. The ɛ - closure is basically an
ε - transition from one state to other. Hence
= {3, 4, 6, 7, 8, 9}
ε - closure (8) = {8, 3, 4, 6, 9}
= {3, 4, 6, 8, 9}
Now we will obtain δ' transitions for each state and for each input symbol.
= ε - closure (1)
{1}
= ε - closure (ϕ)
=ϕ
= ε - closure (ϕ)
=ϕ
= ε - closure (2)
= {2, 3, 4, 6, 9}
= ε - closure (ϕ U ϕ U 5 U ϕ U ϕ )
= ε - closure (5)
= {3, 4, 5, 6, 8, 9}
= ε - closure (ϕ U ϕ U ϕ U 7 U ϕ)
= ε - closure (7)
= {3, 4, 6, 7, 8, 9}
= ε - closure (ϕ U 5 U ϕ)
= ε - closure (5)
= {3, 4, 5, 6, 8, 9}
= ε - closure (ϕ U ϕ U 7)
= ε - closure (7)
= {3, 4, 6, 7, 8, 9}
= ε - closure (5)
= {3, 4, 5, 6, 8, 9}
= ε - closure (ϕ)
=ϕ
= ε - closure (ϕ U 5 U ϕ U ϕ U ϕ U ϕ U ϕ)
= ε - closure (5)
= {3, 4, 5, 6, 8, 9}
= ε - closure (ϕ U ϕ U ϕ U 7 U ϕ U ϕ)
= ε - closure (7)
= {3, 4, 6, 7, 8, 9}
= ε - closure (ϕ)
=ϕ
= ε - closure (7)
= {3, 4, 6, 7, 8, 9}
= ε - closure (ϕ U 5 U ϕ U ϕ U ϕ U ϕ)
= ε - closure (5)
= {3, 4, 5, 6, 8, 9}
= ε - closure (ϕ U ϕ U 7 U ϕ U ϕ U ϕ)
= ε - closure (7)
= {3, 4, 6, 7, 8, 9}
δ' (8, a) = ε - closure (δ (δ`(8, ε), a))
= ε - closure (ϕ U 5 U ϕ U ϕ U ϕ)
= ε - closure (5)
= {3, 4, 5, 6, 8, 9}
= ε - closure (ϕ U ϕ U 7 U ϕ U ϕ)
= ε - closure (7)
= {3, 4, 6, 7, 8, 9}
= ε - closure (ϕ)
=ϕ
= ε - closure (ϕ)
=ϕ
Now we will build the transition table using above calculated δ' transitions.
State {2} is a final state since ε - closure (2) contains 9 which is actually a final state
in given NFA. Similarly state 5, 7, 8 and 9 are final states.
= ε - closure (q1)
= {q1, q2}
=ϕ
= ε - closure (ϕ U ϕ)
=ϕ
= ε - closure (q2)
= ε - closure (q2)
= {q2}
= ε - closure (ϕ)
=ϕ
δ' (q2, b)
= ε - closure (q2)
= {q2}
States q1 and q2 becomes the final as closures of q1 and q2 contains the final state q2.
The NFA can be shown by transition diagram as shown in Fig. 1.9.5.
= ε - closure (ϕ U q1 U q1).
= ε - closure (q1)
= (q1, q2)
= ε - closure (q0)
= ε - closure (q1)
= {q1, q2}
= ε closure (ϕ U q0)
= ε - closure (q1)
= (q1, q2)
= ε - closure (q0)
Example 5: Construct an NFA without ε transitions for the NFA given in Fig.
= ε - Closure (q0)
= ε - Closure (q1)
= ε - Closure (ϕ)
δ' (q1, 0) = ϕ
= ε - Closure (q1)
Step 1: Consider M = (Q, Σ, δ, q0, F) is a NFA with ε. We have to convert this NFA
with ε to equivalent DFA denoted by
Then obtain,
ε - closure (q0) = {P1, P2, P3... Pn} then [P1, P2, P3 ,... Pn] becomes a start state of
DFA.
Step 2: We will obtain δ transitions on [P1, P2, P3, ... Pn ] for each input.
where a is input ϵ Σ.
Step 3: The states obtained [P1, P2, P3, ... Pn] ϵ QD. The states containing final state
Pi is a final state in DFA.
Solution:
= ε - closure {q1}
= ε - closure {q0}
δ' (A, a) = B
δ` (A, b) = A
= ε - closure {q1}
Now we will obtain d' transition. Let e-closure (q0) = {q0, q1, q2} call it as state A.
= ε - closure {q0}
= ε - closure {q2}
δ` (A, 0) = A
δ` (A, 1) = B
δ` (A,2) = C
i.e.
Hence,
= ε - closure {ϕ}
=ϕ
= ε - closure {q1}
Hence,
δ` (B, 0) = ϕ
δ` (B, 1) = B
δ` (B, 2) = C
= ε - closure {ϕ}
=ϕ
= ε - closure {ϕ}
=ϕ
= q2
Hence the DFA is,
As A = {q0, q1, q2} in which final state q2 lies hence A is final state in B = {q1, q2}
the state q2 lies hence B is also final state in C = {q2}, the state q2 lies hence C is also
a final state.
= ε - closure (p)
= ε - closure (q)
δ' (A, b) = B
State B = {p, q}
δ' (B, a)
= ε - closure (p, q)
= ε - closure(p) Ụ ɛ - closure(q)
δ' (B, a) = B.
= ε - closure (q U r)
= ε - closure (q, r)
δ`(B, b) = D.
= ε - closure (r U ϕ)
= ε - closure (r)
= {q, r}
δ' (B, c) = C
State C = {q, r}
= ε - closure (q U r)
= ε - closure (q) U ε - closure(r)
= {p, q} U {q, r}
δ' (C, a) = D
= ε - closure (r U ϕ)
= ε - closure (r)
δ' (C, b) = C
= ε - closure (ϕ U p)
= ε - closure (p)
δ' (C, c) = A
State D = {p, q, r}
= ε - closure (p U q U r)
= ε - closure (q U r U ϕ)
= ε - closure (q, r)
δ' (D, b) = D
= ε - closure (r U ϕ U p)
= {q, r} U {P}
δ' (D, c) = D
Solution:
= ε - closure(2)
=ϕ
δ' (A digit) = ϕ
= ε – closure (6)
= ε – closure (9)
= {9, 7, 4, 5, 8, 10} i.e.
= ε – closure (6)
=C
= ε – closure (9)
=D
= ε - closure (6)
=C
= ε - closure (9)
=D