SEM04a-NFA Construction and Minimum DFA
SEM04a-NFA Construction and Minimum DFA
SEM04a-NFA Construction and Minimum DFA
1
Conversion : NFA DFA Algorithm
2
Converting NFA to DFA – 1st Look
a 3 b 4
2
0 1 5 8
6 c 7
From State 0, Where can we move without consuming any input ?
This forms a new state: 0,1,2,6,8 What transitions are defined for this
new state ?
3
The Resulting DFA
a
0, 1, 2, 6, 8 a 3
a
c b
1, 2, 5, 6, 7, 8
c 1, 2, 4, 5, 6, 8
c
Which States are FINAL States ?
a
A How do we handle
B
a alphabet symbols not
a b
c defined for A, B, C, D ?
D
c C
c
4
Algorithm Concepts
NFA N = ( Q, , q0, , F )
-Closure(q) : q Q
: set of states in Q that are reachable
No input is from q via -moves of N that originate
consumed
from s.
-Closure(T) : T Q
: NFA states reachable from all t T
on -moves only.
(T, a) : T Q, a
: Set of states to which there is a
transition on input a from some t T
2 3
a
start a
0 1 6 7 8 b 9
b
b
4 5 10
b : -closure((A,b)) = -closure(({0,1,2,4,7},b))
adds {5} ( since move(4,b)=5)
b : -closure((C,b)) = -closure(({1,2,4,5,6,7},b))}
= {1,2,4,5,6,7} = C
Define Dtran[C, b] = C.
8
Conversion Example – continued (3)
5th , we calculate for state D on {a,b}
a : -closure((D, a)) = -closure(({1,2,4,5,6,7,9},a))}
= {1,2,3,4,6,7,8} = B
Define Dtran[D, a] = B.
b : -closure((D,b)) = -closure(({1,2,4,5,6,7,9},b))}
= {1,2,4,5,6,7,10} = E
Define Dtran[D, b] = E.
b : -closure((E,b)) = -closure(({1,2,4,5,6,7,10},b))}
= {1,2,4,5,6,7} = C
Define Dtran[E,b] = C.
9
Conversion Example – continued (4)
This gives the transition table Dtran for the DFA of:
Input Symbol
Dstates a b
A B C
B B D
C B C
D B E
E* B C
b C b
a
start a b b
A B D E
a
a a
10
Algorithm For Subset Construction
push all states in T onto stack;
initialize -closure(T) to T;
while stack is not empty do begin
pop t, the top element, off the stack;
for each state u with edge from t to u labeled do
if u is not in -closure(T) do begin
add u to -closure(T) ;
push u onto stack
end
end 11
Algorithm For Subset Construction – (2)
a:
b:
ab:
| ab :
a*
( | ab )* :
14
Motivation: Construct NFA For:
: start i f
a:
start 0 1
a
b: start b
A B
ab: start a
0 1 A b B
| ab :
a*
( | ab )* :
15
Construction Algorithm :
R.E. NFA
Construction Process :
1st : Identify subexpressions of the regular expression
symbols
r|s
rs
r*
16
Piecing Together NFAs
17
Piecing Together NFAs – continued(1)
3.(a) If s, t are regular expressions, N(s), N(t) their NFAs s|t
has NFA:
N(s)
start i
f L(s) L(t)
N(t)
where i and f are new start / final states, and -moves are
introduced from i to the old start states of N(s) and N(t) as
well as from all of their final states to f.
18
Piecing Together NFAs – continued(2)
3.(b) If s, t are regular expressions, N(s), N(t) their NFAs st
(concatenation) has NFA:
start
i N(s) N(t) f L(s) L(t)
overlap
Alternative:
start i f
N(s) N(t)
start i f
N(s)
20
Properties of Construction
Let r be a regular expression, with NFA N(r), then
21
Pulling Together Concepts
• Designing Lexical Analyzer Generator
Reg. Expr. NFA construction
NFA DFA conversion
DFA simulation for lexical analyzer
• Recall Lex Structure
(a | b)*abb
Pattern Action
e.g.
Pattern Action
(abc)*ab
… …
etc.
Recognizer!
- Each pattern recognizes lexemes
- Each pattern described by regular expression
22
Example
P1 : a
P2 : abb 3 patterns
P3 : a*b+
NFA’s :
start a P1
1 2
P2
start a b b
3 4 5 6
a b
start P3
7 8
b
23
Example – continued (2)
Combined NFA :
a
1 2 P1
a b b
0 3 4 5 6 P2
start
a b
P3
7 8
b
Examples a a b a
{0,1,3,7} {2,4,7} {7} {8} death
pattern matched: - P1 - P3 -
a b b
{0,1,3,7} {2,4,7} {5,8} {6,8} break tie in
pattern matched: - P1 P3 P2,P3 favor of P2
24
Example – continued (3)
Alternatively Construct DFA: (keep track of correspondence
between patterns and new accepting states)
Input Symbol
STATE a b Pattern
{0,1,3,7} {2,4,7} {8} none
{2,4,7} {7} {5,8} P1
{8} - {8} P3
{7} {7} {8} none
{5,8} - {6,8} P3
break tie in
{6,8} - {8} P2 favor of P2
25
Minimizing the Number of States of
DFA
1. Construct initial partition of S with two groups:
accepting/ non-accepting.
2. (Construct new )For each group G of do begin
1. Partition G into subgroups such that two states s,t
of G are in the same subgroup iff for all symbols a
states s, t have transitions on a to states of the same
group of .
2. Replace G in new by the set of all these subgroups.
3. Compare new and . If equal, final:= then proceed to 4,
else set := new and goto 2.
4. Aggregate states belonging in the groups of final
26
A Detailed Example
The DFA for ( a | b )* abb
Character
a a State a b
a b b s0 s1 s2
s0 s1 s3 s4 s1 s1 s3
a s2 s1 s2
b a b
s3 s1 s4
s2 s4* s1 s2
27
A Detailed Example (DFA Minimization)
Character
State a b
s0 s1 s2
a a
a b b s1 s1 s3
s0 s1 s3 s4
s2 s1 s2
b a a b
s3 s1 s4
s2
s4* s1 s2
b
28
A Detailed Example (DFA Minimization)
Character
State a b
s0 s1 s2
a a
a b b s1 s1 s3
s0 s1 s3 s4
s2 s1 s2
b a a b
s3 s1 s4
s2
s4* s1 s2
b
29
A Detailed Example
(DFA Minimization)
Current Partition Worklist s Split on a Split on b
Character
State a b
s0 s1 s2
a a
s1 s1 s3
a b b
s0 s1 s3 s4
s2 s1 s2
b a a b s3 s1 s4
s2
s4* s1 s2
b
30
A Detailed Example
(DFA Minimization)
Current Partition Worklist s Split on a Split on b
Character
State a b
s0 s1 s2
a a
a b b s1 s1 s3
s0 s1 s3 s4
s2 s1 s2
b a a b
s3 s1 s4
s2
s4* s1 s2
b
31
A Detailed Example
(DFA Minimization)
Current Partition Worklist s Split on a Split on b
Character
State a b
s0 s1 s2
a a
a b b s1 s1 s3
s0 s1 s3 s4
s2 s1 s2
b a a b
s3 s1 s4
s2
s4* s1 s2
b
32
A Detailed Example
(DFA Minimization)
Current Partition Worklist s Split on Split on b
a
P0 {s4} {s0,s1,s2,s3} {s4} {s4} none {s3}
{s0,s1,s2,s3} {s0,s1,s2}
P1 {s4} {s3} {s0,s1,s2} {s3} {s0,s1,s2} {s3} none {s1} {s0,s2}
Character
State a b
s0 s1 s2
a a
a b b s1 s1 s3
s0 s1 s3 s4
s2 s1 s2
b a a b
s3 s1 s4
s2
s4* s1 s2
b
33
A Detailed Example
(DFA Minimization)
Current Partition Worklist s Split on a Split on b
Character
State a b
s0 s1 s2
a a
a b b s1 s1 s3
s0 s1 s3 s4
s2 s1 s2
b a a b
s3 s1 s4
s2
s4* s1 s2
b
34
A Detailed Example
(DFA Minimization)
Current Partition Worklist s Split on a Split on b
Character
State a b
a a s0 s1 s2
a b b
s0 s1 s3 s4 s1 s1 s3
b a a b s2 s1 s2
s2 s3 s1 s4
b s4* s1 s2
35
A Detailed Example
(DFA Minimization)
Current Partition Worklist s Split on a Split on b
Character
State a b
a a
Empty worklist s0 s1 s2
s0
a
s1
b
s3
b
s4
done! s1 s1 s3
b a a s2 s1 s2
b
s2 s3 s1 s4
b s4* s1 s2
36
A Detailed Example
(DFA Minimization)
Current Partition Worklist s Split on a Split on b
a a b a
a
a b b a b b
s0 s1 s3 s4 s1 s3 s4
s0 , s2
b a a b a b
s2
37
DFA Minimization
What about a ( b | c )* ?
b
q4 q5
q0
a
q1 q2 q3 q8 q9
q6 c
q7
First, States
the subset construction:
-closure((s,*))
DFA NFA a b c b
s0 q0 s1 none none s2
b
q1, q2, q3, a
s2 s3 s0 s1 b c
s1 q 4 , q6 , q9 none
c
s3
q 5 , q8 , q9 ,
s2 q 3 , q4 , q6 none s2 s3
c
s3 q 7 , q8 , q9 ,
q 3 , q4 , q6 none s2 s3
38
Example (solve it yourself !!!)
a
a
A a F
B
a b
b a
D
C
b
b b
a a
A,C,D
Minimized DFA: B,F
b
b
39
From NFA to DFA
• We need some method for eliminating ε-transitions
and multiple transitions from a state on a single
input character.
• Eliminating -transitions involves the construction
of -closures.
• Eliminating multiple transitions involves keeping
track of the set of the states instead of single states.
40
- Closure
41
Example 2.14
• a*
a
1 2 3 4
• 1 = {1, 2, 4}
• 2 = {2}
• 3 = {2, 3, 4}
• 4 = {4}
42
-Closure of Set of States
• -closure of a set of states is defined as the union
of -closures of each individual state.
• If Q = {q1, q2,…qn} is a set of states, then
Q = q 1 q2 … qn
• In the previous example we had
1 = {1, 2, 4} and 3 = {2, 3, 4}
Let Q = {1, 3}
Q = {1, 3} = 1 3 = {1, 2, 4} {2, 3, 4} =
{1, 2, 3, 4}
43
The Subset Construction
• Given NFA M.
• Need to construct a corresponding DFA M’.
1. Compute -closure of the start state of M; this becomes
the start state of M’.
2. For this set and for each subsequent set, we compute
transitions on character a as follows
1. Given a set of states Q and a character a, compute the set of
states Q’a = {t | for some q in Q there is a transition from q to t}
2. Compute Q’a, the -closure of Q’a. This becomes a new state.
There is a transition from Q to Q’a on the character a.
3. Continue with this process until no new states or
transitions are created.
4. Mark as accepting those constructed states that contain
an accepting state of M.
44
Example 2.15
a
1 2 3 4 a
a
1,2,4 2,3,4
• 1 = {1, 2, 4} – start state
• {1, 2, 4}a = {3}
• {3} = {2, 3, 4} – new state and transition
• {2, 3, 4}a = {3}
• {3} = {2, 3, 4} - no new state, new transition
45
Example 2.16
a b
2 3 4 5
1 8
a
6 7
a b
1,2,6 3,4,7,8 5,8
46
Example 2.17
letter
5 6
letter
1 2 3 4 9 10
7
digit
8
letter 4,5,6,7,9,10
letter
letter 2,3,4,5,7,10
1 digit
letter
digit
4,5,7,8,9,10
digit
47
The End
48