0% found this document useful (0 votes)
19 views38 pages

Lecture 4 - NFA To DFA

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views38 pages

Lecture 4 - NFA To DFA

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Lecture # 4

Theory Of Automata
By
Dr. Ahmed Hesham Mostafa
Next

NFA

Regular
expressions DFA

Lexical Implementation of DFA


Specification

2
Speeding Up The Scanner

• A DFA is like an NFA, but with tighter restrictions


▪ Every state must have exactly one transition defined for every letter.
▪ ε-moves are not allowed.
The subset construction of a DFA from an NFA
Algorithm
• INPUT: An NFA N.
• OUTPUT: A DFA D accepting the same language as N.
• algorithm constructs a transition table Dtran for D.
• Each state of D is a set of NFA states, and we construct Dtran so D will
simulate in parallel all possible moves N can make on a given input
string.
• The start state of D is ε-closure(s0), and the accepting states of D are
all those sets of N's states that include at least one accepting state of
N.
Operations on NFA states
• Dentitions of several functions that describe basic computations on
the states of N that are needed in the algorithm.
• Note that s is a single state of NFA, while T is a set of states of NFA.
Example 1 - From NFA to DFA
• Then get the NFA for (alb)*abb

Example 1 - From NFA to DFA
NFA State DFA State a b
• the NFA accepting (a|b)*abb; {0,1,2,4,7} A

ε-closure(0) = {0,1,2,4,7} = A

▪ since these are exactly the states


reachable from state 0 via a path
all of whose edges have label ε.
▪ Note that a path can have zero
edges, so state 0 is reachable
from itself by an ε -labeled path.
Example 1 - From NFA to DFA
• The input alphabet is {a, b). Thus, our first step is to mark A and compute
▪ Dtran[A, a] = ε -closure(move(A, a))
▪ Dtran[A, b] = ε - closure(move(A, b))

• Among the states 0, 1, 2, 4, and 7,


only 2 and 7 have transitions on a,
to 3 and 8, respectively. NFA State DFA State a b
{0,1,2,4,7} A B
Thus, move(A, a) = {3,8).
{1,2,3,4,6,7,8} B
Also, ε -closure({3,8}) = {1,2,3,4,6,7,8}
, so we conclude Dtran[A, a] = ε -closure(move(A, a)) = ε -closure({3, 8}) = B
Example 1 - From NFA to DFA
• Now, we must compute Dtran[A, b]. Among the states in A, only 4 has a
transition on b, and it goes to 5. Thus,
Dtran[A, b] = ε -closure(move(A, b)) = ε -closure({5}) = {I, 2, 4,5, 6,7} = C

NFA State DFA State a b


{0,1,2,4,7} A B C
{1,2,3,4,6,7,8} B
{I, 2, 4,5, 6,7} C
Example 1 - From NFA to DFA
• B={1,2,3,4,6,7,8}
• Now, we must compute Dtran[B, a].
Dtran[B, a] = ε -closure(move(B, a)) = ε -closure({3,8}) = B
Now, we must compute Dtran[B, b].
Dtran[B, b] = ε -closure(move(B, b)) = ε -closure({5,9}) = {1, 2, 4, 5, 6, 7, 9} = D

NFA State DFA State a b


{0,1,2,4,7} A B C
{1,2,3,4,6,7,8} B B D
{I, 2, 4,5, 6,7} C
{1, 2, 4, 5, 6, 7, 9} D
Example 1 - From NFA to DFA
• C= {I, 2, 4,5, 6, 7}
▪ Now, we must compute Dtran[C, a].
Dtran[C, a] = ε -closure(move(C, a)) = ε -closure({3,8}) = B
▪ Now, we must compute Dtran[C, b].
Dtran[C, b] = ε -closure(move(C, b)) = ε -closure({5}) = C
NFA State DFA State a b
{0,1,2,4,7} A B C
{1,2,3,4,6,7,8} B B D
{I, 2, 4,5, 6,7} C B C
{1, 2, 4, 5, 6, 7, 9} D
Example 1 - From NFA to DFA
• D= {I, 2, 4, 5, 6, 7, 9}
▪ Now, we must compute Dtran[D, a].
Dtran[D, a] = ε -closure(move(D, a)) = ε -closure({3,8}) = B
▪ Now, we must compute Dtran[D, b].
Dtran[D, b] = ε -closure(move(D, b)) = ε -closure({5,10}) = {1, 2, 3, 5, 6, 7, 10} = E

NFA State DFA State a b


{0,1,2,4,7} A B C
{1,2,3,4,6,7,8} B B D
{I, 2, 4,5, 6,7} C B C
{1, 2, 4, 5, 6, 7, 9} D B E
{1, 2, 3, 5, 6, 7, 10} E
Example 1 - From NFA to DFA
• E = {1, 2, 3, 5, 6, 7, 10} //note 10 is the acceptance state
▪ Now, we must compute Dtran[E, a].
Dtran[E, a] = ε -closure(move(E, a)) = ε -closure({3,8}) = B
▪ Now, we must compute Dtran[E, b].
Dtran[E, b] = ε -closure(move(E, b)) = ε -closure({5}) = C
NFA State DFA State a b
{0,1,2,4,7} A B C
{1,2,3,4,6,7,8} B B D
{I, 2, 4,5, 6,7} C B C
{1, 2, 4, 5, 6, 7, 9} D B E
{1, 2, 3, 5, 6, 7, 10} E B C
Example 1 - From NFA to DFA
• Transition table Dtran for DFA
Example 1 - From NFA to DFA
• the DFA for (a | b)* abb
Minimizing the Number of States of a DFA
• Divide state into two groups Accepting and non-accepting states.
• Find the repeated states from transition table.
• Remove the one of the repeated states (remove from down states).
• Replace the occurrence of the deleted state with the equivalent state
from upper state in table.
• Don't remove the acceptance state, just replace the removed states.
Minimizing the Number of States of a DFA
a b
A B C
B B D
C B C
D B E
E B C

• State A,C ,E have the same transitions {B,C}.


• State E is Acceptance state can not be removed.
• So, there are now two equivalent and repeated states A and C.
• One of them must be removed {remove from down state}.
• So, state C will be removed, and all its occurrence will be replaced by State A.
Minimizing the Number of States of a DFA
• The minimum state DFA will be:
b
a b
a
A B A
B B D a

D B E a b b
E B A A B D E

b a
Subset Construction Method
NFA without ε-transitions
Subset Construction Method
NFA without ε-transitions
Fig1. NFA without ε-transitions

3
b a
a Step1
a
a 2 b
Construct a transition table showing all
a,b reachable states for every state for
1 5
every input signal.
a,b a
4

b
Subset Construction Method
NFA without ε-transitions
Transition table
Fig1. NFA without ε-transitions

3 State a b
b a 1 {1,2,3,4,5} {4,5}
a
a ??? ???
a 2 b 2 {3}
??? {5}
???
a,b
1 5 3 {2}

??? ???
a,b
4
a 4 {5}
??? {4}
???
5 ∅
??? ∅
???
b
Subset Construction Method
NFA without ε-transitions
Transition from state q with Transition from state q
Transition table
input a with input b
Fig1. NFA without ε-transitions

3 State a b
b a Starts here 1 {1,2,3,4,5} {4,5}
a
a ??? ???
a 2 b 2 {3}
??? {5}
???
a,b
1 5 3 {2}

??? ???
a,b
4
a 4 {5}
??? {4}
???
5 ∅
??? ∅
???
b
Subset Construction Method
NFA without ε-transitions

Step2
State a b
1 {1,2,3,4,5} {4,5} The set of states resulting from every
transition function constitutes a new
2 {3} {5} state.
Calculate all reachable states for every
3 ∅ {2} such state for every input signal using
union operation.
4 {5} {4} Note 1 is the start state
Note 5 is accept state , so every new
5 ∅ ∅ state will contain 5 will be considered
accept state
Subset Construction table

State a b Starts with


Initial state
State a b
{1,2,3,4,5} 1 {1,2,3,4,5} {4,5}
1 {4,5}

2 {3} {5}

3 ∅ {2}

4 {5} {4}

5 ∅ ∅
Transition table Subset Construction table

State a b Starts with State a b


Initial state
1 {1,2,3,4,5} {4,5} 1 {1,2,3,4,5} {4,5}
{1,2,3,4,5}
2 {3} {5}
{4,5}
3 ∅ {2}

4 {5} {4}

5 ∅ ∅
Transition table Subset Construction table

State a b Starts with State a b


Initial state
1 {1,2,3,4,5} {4,5} 1 {1,2,3,4,5} {4,5}
{1,2,3,4,5}
2 {3} {5}
{4,5}
3 ∅ {2}

4 {5} {4}

5 ∅ ∅

Step3
Repeat this process(step2) until no
more new states are reachable.
Transition table Subset Construction table

State a b State a b
{1,2,3,4,5} 1 {1,2,3,4,5} {4,5}
1 {4,5}
{1,2,3,4,5} {1,2,3,4,5}
??? {2,4,5}
???
2 {3} {5}
{4,5}
3 ∅ {2} {2,4,5}
4 {5} {4}

5 ∅ ∅
Transition table Subset Construction table

State a b State a b
1 {1,2,3,4,5} {4,5} 1 {1,2,3,4,5} {4,5}
{1,2,3,4,5} {1,2,3,4,5} {2,4,5}
2 {3} {5}
{4,5} 5
??? 4
???
3 ∅ {2}
{2,4,5}
4 {5} {4}
5
5 ∅ ∅ 4
Transition table Subset Construction table

State a b State a b
1 {1,2,3,4,5} {4,5} 1 {1,2,3,4,5} {4,5}
{1,2,3,4,5} {1,2,3,4,5} {2,4,5}
2 {3} {5}
{4,5} 5 4
3 ∅ {2}
{2,4,5} {3,5}
??? {4,5}
???
4 {5} {4}
5
5 ∅ ∅ 4
{3,5}
Transition table Subset Construction table

State a b State a b
{1,2,3,4,5} 1 {1,2,3,4,5} {4,5}
1 {4,5}
{1,2,3,4,5} {1,2,3,4,5} {2,4,5}
2 {3} {5}
{4,5} 5 4
3 ∅ {2} {3,5} {4,5}
{2,4,5}
4 {5} {4} 5 ∅
??? ∅
???
5 ∅ ∅ 4
{3,5}


Transition table Subset Construction table

State a b State a b
1 {1,2,3,4,5} {4,5} 1 {1,2,3,4,5} {4,5}
{1,2,3,4,5} {1,2,3,4,5} {2,4,5}
2 {3} {5}
{4,5} 5 4
3 ∅ {2}
{2,4,5} {3,5} {4,5}
4 {5} {4}
5 ∅ ∅
5 ∅ ∅ 4 5
??? 4
???
{3,5}
We already got 4 and 5.
So we don’t add them again.

Note ∅ is Dead State
Transition table Subset Construction table

State a b State a b
1 {1,2,3,4,5} {4,5} 1 {1,2,3,4,5} {4,5}
{1,2,3,4,5} {1,2,3,4,5} {2,4,5}
2 {3} {5}
{4,5} 5 4
3 ∅ {2}
{2,4,5} {3,5} {4,5}
4 {5} {4}
5 ∅ ∅
5 ∅ ∅ 4 5 4
{3,5} ∅
??? 2
???

2
Transition table Subset Construction table

State a b State a b
1 {1,2,3,4,5} {4,5} 1 {1,2,3,4,5} {4,5}
{1,2,3,4,5} {1,2,3,4,5} {2,4,5}
2 {3} {5}
{4,5} 5 4
3 ∅ {2}
{2,4,5} {3,5} {4,5}
4 {5} {4}
5 ∅ ∅
5 ∅ ∅ 4 5 4
{3,5} ∅ 2

∅ ∅
??? ∅
???
2
Transition table Subset Construction table

State a b State a b
1 {1,2,3,4,5} {4,5} 1 {1,2,3,4,5} {4,5}
{1,2,3,4,5} {1,2,3,4,5} {2,4,5}
2 {3} {5}
{4,5} 5 4
3 ∅ {2}
{2,4,5} {3,5} {4,5}
4 {5} {4}
5 ∅ ∅
5 ∅ ∅ 4 5 4
{3,5} ∅ 2

∅ ∅ ∅
2 3
??? 5
???
3
Transition table Subset Construction table

State a b State a b
1 {1,2,3,4,5} {4,5} 1 {1,2,3,4,5} {4,5}
{1,2,3,4,5} {1,2,3,4,5} {2,4,5}
2 {3} {5}
{4,5} 5 4
3 ∅ {2}
{2,4,5} {3,5} {4,5}
4 {5} {4}
5 ∅ ∅
5 ∅ ∅ 4 5 4
{3,5} ∅ 2

∅ ∅ ∅
Stops here as there are no 2 3 5
more reachable states
3 ∅
??? 2
???
Resulting FA after applying Subset
Subset Construction table Construction.
a
State a b
1 {1,2,3,4,5} {4,5} b a
12345 245
{1,2,3,4,5} {1,2,3,4,5} {2,4,5}
35
{4,5} 5 4 a
a,b a
{2,4,5} {3,5} {4,5}
b
b
5 ∅ ∅ 1
∅ a
3

4 5 4 b a,b b
a
{3,5} ∅ 2
2
a
∅ ∅ ∅ 45 5 b
2 3 5 b 4 a
3 ∅ 2
b
References

• Compilers – Principles, Techniques and Tools, Second Edition by Alfred V.


Aho, Ravi Sethi, Jeffery D. Ullman.
• Compiler Construction. Principles And Practice Kenneth C.
• Keith D. Cooper & Linda Torczon ,Cooper: Engineering a Compiler, 2e Lecture Slides
(elsevier.com),2010

37
THANKS
SEE U NEXT LECTURE

You might also like