Lecture 6.1 - NFA to DFA toc and compiler design

AbuZahed5 10 views 27 slides Feb 27, 2025
Slide 1
Slide 1 of 27
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27

About This Presentation

compiler design


Slide Content

Non Deterministic Finite Automata
To
Deterministic Finite Automata Construction
1
Course Name: Compiler Design
Course Code: CSE331
Level:3, Term:3
Department of Computer Science and Engineering
Daffodil International University

There are three main cases of non-determinism in NFAs:
1.Transition to a state without consuming any input.
2.Multiple transitions on the same input symbol.
3.No transition on an input symbol.
To convert NFAs to DFAs we need to get rid of non-
determinism from NFAs.
Non Deterministic Features of NFA

Using Subset construction method to convert NFA to DFA
involves the following steps:
•For every state in the NFA, determine all reachable statesfor
every input symbol.
•The set of reachable states constitute a single statein the
converted DFA (Each state in the DFA corresponds to a subset of
states in the NFA).
•Find reachable statesfor each new DFAstate, until no more new
states can be found.
Subset Construction Method

Fig1. NFA without λ-transitions
1
3
2
4
5
a
a
a
b
a
b
b
a
a,b
a,b
Subset Construction Method

Fig1. NFA without λ-transitions
Step1
1
3
2
4
5
a
a
a
b
a
b
b
a
a,b
a,b
Construct a transition table showing
all reachable states for every state
for every input signal.
Subset Construction Method

Fig1. NFA without λ-transitions
Fig2. Transition table
1
3
2
4
5
a
a
a
b
a
b
b
a
a,b
a,b
Subset Construction Method

Fig1. NFA without λ-transitions
Fig2. Transition table
1
3
2
4
5
a
a
a
b
a
b
b
a
a,b
a,b
q δ(q,a)δ(q,b)
1 {1,2,3,4,5}{4,5}
2 {3} {5}
3 ∅
{2}
4 {5} {4}
5 ∅ ∅
Subset Construction Method

Fig1. NFA without λ-transitions
Fig2. Transition table
1
3
2
4
5
a
a
a
a
a
b
b
b
a,b
a,b
q δ(q,a)δ(q,b)
1 {1,2,3,4,5}{4,5}
2 {3} {5}
3 ∅
{2}
4 {5} {4}
5 ∅ ∅
Transition from state q with
input a
Transition from state q
with input b
Starts here
Subset Construction Method

Fig2. Transition table
q δ(q,a)δ(q,b)
1 {1,2,3,4,5}{4,5}
2 {3} {5}
3

{2}
4 {5} {4}
5 ∅ ∅
Step2
Thesetofstatesresultingfromevery
transitionfunctionconstitutesanew
state.Calculateallreachablestates
foreverysuchstateforeveryinput
signal.
Subset Construction Method

Fig2. Transition table
q δ(q,a)δ(q,b)
1 {1,2,3,4,5}{4,5}
2 {3} {5}
3

{2}
4 {5} {4}
5 ∅ ∅
q δ(q,a)δ(q,b)
1 {1,2,3,4,5}{4,5}
Starts with
Initial state
Fig3. Subset Construction table

Fig2. Transition table
q δ(q,a)δ(q,b)
1 {1,2,3,4,5}{4,5}
2 {3} {5}
3

{2}
4 {5} {4}
5 ∅ ∅
q δ(q,a)δ(q,b)
1 {1,2,3,4,5}{4,5}
{1,2,3,4,5}
{4,5}
Starts with
Initial state
Fig3. Subset Construction table

Fig2. Transition table
q δ(q,a)δ(q,b)
1 {1,2,3,4,5}{4,5}
2 {3} {5}
3

{2}
4 {5} {4}
5 ∅ ∅
q δ(q,a)δ(q,b)
1 {1,2,3,4,5}{4,5}
{1,2,3,4,5}
{4,5}
Starts with
Initial state
Fig3. Subset Construction table
Step3
Repeat this process(step2) until no
more new states are reachable.

Fig2. Transition table
q δ(q,a)δ(q,b)
1 {1,2,3,4,5}{4,5}
2 {3} {5}
3

{2}
4 {5} {4}
5 ∅ ∅
q δ(q,a)δ(q,b)
1 {1,2,3,4,5}{4,5}
{1,2,3,4,5}{1,2,3,4,5}{2,4,5}
{4,5}
{2,4,5}
Fig3. Subset Construction table

Fig2. Transition table
q δ(q,a)δ(q,b)
1 {1,2,3,4,5}{4,5}
2 {3} {5}
3

{2}
4 {5} {4}
5 ∅ ∅
q δ(q,a)δ(q,b)
1 {1,2,3,4,5}{4,5}
{1,2,3,4,5}{1,2,3,4,5}{2,4,5}
{4,5} 5 4
{2,4,5}
5
4
Fig3. Subset Construction table

Fig2. Transition table
q δ(q,a)δ(q,b)
1 {1,2,3,4,5}{4,5}
2 {3} {5}
3

{2}
4 {5} {4}
5 ∅ ∅
q δ(q,a)δ(q,b)
1 {1,2,3,4,5}{4,5}
{1,2,3,4,5}{1,2,3,4,5}{2,4,5}
{4,5} 5 4
{2,4,5}
{3,5} {4,5}
5
4
{3,5}
Fig3. Subset Construction table

Fig2. Transition table
q δ(q,a)δ(q,b)
1 {1,2,3,4,5}{4,5}
2 {3} {5}
3

{2}
4 {5} {4}
5 ∅ ∅
q δ(q,a)δ(q,b)
1 {1,2,3,4,5}{4,5}
{1,2,3,4,5}{1,2,3,4,5}{2,4,5}
{4,5} 5 4
{2,4,5}
{3,5} {4,5}
5 ∅ ∅
4
{3,5}

Fig3. Subset Construction table

Fig2. Transition table
q δ(q,a)δ(q,b)
1 {1,2,3,4,5}{4,5}
2 {3} {5}
3

{2}
4 {5} {4}
5 ∅ ∅
q δ(q,a)δ(q,b)
1 {1,2,3,4,5}{4,5}
{1,2,3,4,5}{1,2,3,4,5}{2,4,5}
{4,5} 5 4
{2,4,5}
{3,5} {4,5}
5 ∅ ∅
4 5 4
{3,5}
We already got 4 and 5.
So we don’t add them again.
Fig3. Subset Construction table

Fig2. Transition table
q δ(q,a)δ(q,b)
1 {1,2,3,4,5}{4,5}
2 {3} {5}
3

{2}
4 {5} {4}
5 ∅ ∅
q δ(q,a)δ(q,b)
1 {1,2,3,4,5}{4,5}
{1,2,3,4,5}{1,2,3,4,5}{2,4,5}
{4,5} 5 4
{2,4,5}
{3,5} {4,5}
5 ∅ ∅
4 5 4
{3,5}

2

2
Fig3. Subset Construction table

Fig2. Transition table
q δ(q,a)δ(q,b)
1 {1,2,3,4,5}{4,5}
2 {3} {5}
3

{2}
4 {5} {4}
5 ∅ ∅
q δ(q,a)δ(q,b)
1 {1,2,3,4,5}{4,5}
{1,2,3,4,5}{1,2,3,4,5}{2,4,5}
{4,5} 5 4
{2,4,5}
{3,5} {4,5}
5 ∅ ∅
4 5 4
{3,5}

2
∅ ∅ ∅
2
Fig3. Subset Construction table

Fig2. Transition table
q δ(q,a)δ(q,b)
1 {1,2,3,4,5}{4,5}
2 {3} {5}
3

{2}
4 {5} {4}
5 ∅ ∅
q δ(q,a)δ(q,b)
1 {1,2,3,4,5}{4,5}
{1,2,3,4,5}{1,2,3,4,5}{2,4,5}
{4,5} 5 4
{2,4,5}
{3,5} {4,5}
5 ∅ ∅
4 5 4
{3,5}

2
∅ ∅ ∅
2 3
5
3
Fig3. Subset Construction table

Fig2. Transition table
q δ(q,a)δ(q,b)
1 {1,2,3,4,5}{4,5}
2 {3} {5}
3

{2}
4 {5} {4}
5 ∅ ∅
q δ(q,a)δ(q,b)
1 {1,2,3,4,5}{4,5}
{1,2,3,4,5}{1,2,3,4,5}{2,4,5}
{4,5} 5 4
{2,4,5}
{3,5} {4,5}
5 ∅ ∅
4 5 4
{3,5}

2
∅ ∅ ∅
2 3
5
3

2
Fig3. Subset Construction table
Stops here as there are
no more reachable states

q δ(q,a)δ(q,b)
1 {1,2,3,4,5}{4,5}
{1,2,3,4,5}{1,2,3,4,5}{2,4,5}
{4,5} 5 4
{2,4,5}
{3,5} {4,5}
5 ∅ ∅
4 5 4
{3,5}

2
∅ ∅ ∅
2 3 5
3

2
1
45
12345 245
35
5
4

3
2
a
a
a
a
a
a
a
a
b
b
b
b
b
b
b
b
a,b
a,b
Fig3. Subset Construction table
Fig4. Resulting FA after applying
Subset Construction to fig1

NFA to DFA Conversion
LetX=(Q
x,∑,δ
x,q
0,F
x)beanNFAwhichacceptsthelanguageL(X).Wehaveto
designanequivalentDFAY=(Q
y,∑,δ
y,q
0,F
y)suchthatL(Y)=L(X).
ThefollowingprocedureconvertstheNFAtoitsequivalentDFA−
Algorithm:
Input−AnNFA
Output−AnequivalentDFA
Step1−CreatestatetablefromthegivenNFA.
Step2−Createablankstatetableunderpossibleinputalphabetsforthe
equivalentDFA.
Step3−MarkthestartstateoftheDFAbyq0(SameastheNFA).
Step4−FindoutthecombinationofStates{Q0,Q1,...,Qn}foreachpossibleinput
alphabet.
Step5−EachtimewegenerateanewDFAstateundertheinputalphabet
columns,wehavetoapplystep4again,otherwisegotostep6.
Step6−ThestateswhichcontainanyofthefinalstatesoftheNFAarethefinal
statesoftheequivalentDFA.
23

Example
Let us consider the NFA
shown in the figure:
q δ(q,0) δ(q,1)
a {a,b,c,d,e} {d,e}
b {c} {e}
c ∅ {b}
d {e} ∅
e ∅ ∅
The state transition table of
the NFA is:
24

q δ(q,0) δ(q,1)
a {a,b,c,d,e}{d,e}
b {c} {e}
c ∅ {b}
d {e} ∅
e ∅ ∅
Using the algorithm, we find its
equivalent DFA.
The state transition table of the
DFA is:
q δ(q,0) δ(q,1)
[a] [a,b,c,d,e] [d,e]
[a,b,c,d,e][a,b,c,d,e] [b,d,e]
[d,e] [e] ∅
[b,d,e] [c,e] [e]
[e] ∅ ∅
[c, e] ∅ [b]
[b] [c] [e]
[c] ∅ [b]
Example…
The state transition table
of the NFA is:
25

Example…
The state transition table of
the DFA is:
q δ(q,0) δ(q,1)
[a] [a,b,c,d,e][d,e]
[a,b,c,d,e][a,b,c,d,e][b,d,e]
[d,e] [e] ∅
[b,d,e] [c,e] [e]
[e] ∅ ∅
[c, e] ∅ [b]
[b] [c] [e]
[c] ∅ [b]
The state diagram of the DFA is as follows:
26

THANK YOU
27
Tags