CS-352 - Spring 2024 - Lec5
CS-352 - Spring 2024 - Lec5
Spring 2024
Compiler Design
CS-352
Lecture (5)
1
Outline
2
Outline
3
Conversion of an NFA to DFA
4
Conversion of an NFA to DFA
(Cont.)
• Example 1: NFA without λ Transition
5
Conversion of an NFA to DFA
(Cont.)
• Example 1: NFA without λ Transition
6
Conversion of an NFA to DFA
(Cont.)
• Example 1: NFA without λ Transition
0 {0, 1} 0
1 Ø 2
2 Ø Ø
7
Conversion of an NFA to DFA
(Cont.)
• Example 1: NFA without λ Transition
Transition from state q with input a Transition from state q with input b
0 {0, 1} 0
1 Ø 2
2 Ø Ø
8
Conversion of an NFA to DFA
(Cont.)
• Example 1: NFA without λ Transition
q δ(q, 0) δ(q, 1)
0 {0, 1} 0
1 Ø 2
2 Ø Ø
9
Conversion of an NFA to DFA
(Cont.)
Subset Construction Table
Starts with q δ(q, 0) δ(q, 1)
Initial state
0 {0, 1} 0
q δ(q, 0) δ(q, 1)
0 {0, 1} 0
1 Ø 2
2 Ø Ø
10
Conversion of an NFA to DFA
(Cont.)
Subset Construction Table
q δ(q, 0) δ(q, 1)
0 {0, 1} 0
{0, 1}
q δ(q, 0) δ(q, 1)
0 {0, 1} 0
1 Ø 2
2 Ø Ø
11
Conversion of an NFA to DFA
(Cont.)
Subset Construction Table
q δ(q, 0) δ(q, 1)
0 {0, 1} 0
{0, 2}
q δ(q, 0) δ(q, 1)
0 {0, 1} 0
1 Ø 2
2 Ø Ø
12
Conversion of an NFA to DFA
(Cont.)
Subset Construction Table
q δ(q, 0) δ(q, 1)
0 {0, 1} 0
Stops here as there are no
more reachable states {0, 1} {0, 1} {0, 2}
{0, 2} {0, 1} 0
q δ(q, 0) δ(q, 1)
0 {0, 1} 0
1 Ø 2
2 Ø Ø
13
Conversion of an NFA to DFA
(Cont.)
Subset Construction Table
q δ(q, 0) δ(q, 1)
0 {0, 1} 0
{0, 2} {0, 1} 0
14
Conversion of an NFA to DFA
(Cont.)
• Example 2: NFA without λ Transition
15
Conversion of an NFA to DFA
(Cont.)
• Example 1: NFA without λ Transition
q0 {q0, q1} q1
q1 Ø {q0, q1}
16
Conversion of an NFA to DFA
(Cont.)
Subset Construction Table
Starts with q δ(q, 0) δ(q, 1)
Initial state
q0 {q0, q1} q1
{q0, q1}
q1
q δ(q, 0) δ(q, 1)
q0 {q0, q1} q1
q1 Ø {q0, q1}
17
Conversion of an NFA to DFA
(Cont.)
Subset Construction Table
q δ(q, 0) δ(q, 1)
q0 {q0, q1} q1
q1
q δ(q, 0) δ(q, 1)
q0 {q0, q1} q1
q1 Ø {q0, q1}
18
Conversion of an NFA to DFA
(Cont.)
Subset Construction Table
q δ(q, 0) δ(q, 1)
q0 {q0, q1} q1
q1 Ø {q0, q1}
Ø
Transition Table of NFA
q δ(q, 0) δ(q, 1)
q0 {q0, q1} q1
q1 Ø {q0, q1}
19
Conversion of an NFA to DFA
(Cont.)
Subset Construction Table
q δ(q, 0) δ(q, 1)
q0 {q0, q1} q1
q1 Ø {q0, q1}
Ø Ø Ø
Transition Table of NFA
q δ(q, 0) δ(q, 1)
q0 {q0, q1} q1
q1 Ø {q0, q1}
20
Conversion of an NFA to DFA
(Cont.)
Subset Construction Table
q δ(q, 0) δ(q, 1)
q0 {q0, q1} q1
Ø Ø Ø
21
Outline
22
Minimization of DFA – Example 1
Subset Construction Table
q δ(q, 0) δ(q, 1)
0 {0, 1} 0
{0, 2} {0, 1} 0
23
Minimization of DFA – Example 1
(Cont.)
Subset Construction Table
q δ(q, 0) δ(q, 1)
0 {0, 1} 0
{0, 2} {0, 1} 0
24
Minimization of DFA – Example 2
Alphabet
NFA State DFA State
a b
{0, 1, 2, 4, 7} A B C
{1, 2, 3, 4, 6, 7, 8} B B D
Subset Construction Table
{1, 2, 4, 5, 6, 7} C B C
{1, 2, 4, 5, 6, 7, 9} D B E
{1, 2, 4, 5, 6, 7, 10} E B C
25
Minimization of DFA – Example 2
(Cont.)
Subset Construction Table
q δ(q, a) δ(q, b)
A B C
B B D
C B C
D B E
E B C
26
Minimization of DFA – Example 2
(Cont.)
Subset Construction Table
q δ(q, a) δ(q, b)
A B C
B B D
C B C Merge A and C
D B E
E B C
q δ(q, a) δ(q, b)
A B CA
B B D
C B C
D B E
E B CA
27
Minimization of DFA – Example 2
(Cont.)
q δ(q, a) δ(q, b)
A B A
Subset Construction Table B B D
D B E
E B A
Minimizing DFA
28