DFA Minimization Based On Hopcroft's Algorithm Along With Slides On Brzozowski's Algorithm Also Found in Lecture 5
DFA Minimization Based On Hopcroft's Algorithm Along With Slides On Brzozowski's Algorithm Also Found in Lecture 5
FALL 2010
Lexical Analysis:
DFA Minimization
Comp 412
Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved.
Students enrolled in Comp 412 at Rice University have explicit permission to make copies
of these materials for their personal use.
Faculty from other educational institutions may use these materials for nonprofit
educational purposes, provided this copyright notice is preserved.
Automating Scanner Construction
RENFA (Thompson’s construction) The Cycle of Constructions
• Build an NFA for each term
• Combine them with -moves
minimal
RE NFA DFA
NFA DFA (subset construction) DFA
S
Q
Q has transitions
on to R, S, & T
T
R
I S
Q
Think of I as the
image of S under
This part must have an -transition to
the inverse of the one or more other states in one or more
transition function other partitions.
I –1( s, ) Otherwise, it does not split!
Comp 412, Fall 2010 7
Hopcroft's Algorithm
W {F, S-F}; P {F, S-F}; // W is the worklist, P the current partition
while ( W is not empty ) do begin
select and remove s from W ; // s is a set of states
for each in do begin
let I –1( s ); // I is set of all states that can reach s on
for each p P such that p I is not empty
and p is not contained in I do begin
partition p into p1 and p2 such that p1 p I ; p2 p – p1;
P (P – p) p1 p2 ;
if p W
then W (W – p) p1 p2 ;
else if |p1| ≤ |p2 |
then W W p1;
else W W p2;
end
end
end
S
pj
a a
a b b
s0 s1 s3 s4
b a a b
s2
b
Comp 412, Fall 2010 For the record, example was right in 1999, broken in 2000 12
A Detailed Example (DFA Minimization)
a a
a b b
s0 s1 s3 s4
b a a b
s2
b
Comp 412, Fall 2010 13
A Detailed Example (DFA Minimization)
a a
a b b
s0 s1 s3 s4
b a a b
s2
b
Comp 412, Fall 2010 14
A Detailed Example (DFA Minimization)
a a
a b b
s0 s1 s3 s4
b a a b
s2
b
Comp 412, Fall 2010 15
A Detailed Example (DFA Minimization)
a a
a b b
s0 s1 s3 s4
b a a b
s2
b
Comp 412, Fall 2010 16
A Detailed Example (DFA Minimization)
a a
a b b
s0 s1 s3 s4
b a a b
s2
b
Comp 412, Fall 2010 17
A Detailed Example (DFA Minimization)
a a
a b b
s0 s1 s3 s4
b a a b
s2
b
Comp 412, Fall 2010 18
A Detailed Example (DFA Minimization)
a a
a b b
s0 s1 s3 s4
b a a b
s2
b
Comp 412, Fall 2010 19
A Detailed Example (DFA Minimization)
b a a b
s2
b
Comp 412, Fall 2010 20
A Detailed Example (DFA Minimization)
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
b
Comp 412, Fall 2010 20% reduction in number of states 21
DFA Minimization
What about a ( b | c )* ?
b
q4 q5
q0 a
q1 q2 q3 q8 q9
q6 c
q7
First, the subset construction:
States -closure(Move(s,*))
DFA NFA a b c b
s0 q0 s1 none none s2
b
q1, q2, q3, a
s1 none s2 s3 s0 s1 b c
q 4 , q6 , q9
c
q 5 , q8 , q9 ,
s3
s2 q 3 , q4 , q6 none s2 s3
c
s3 q 7 , q8 , q9 ,
q 3 , q4 , q6 none s2 s3
Comp 412, Fall 2010 From last lecture … 22
DFA Minimization
Split on s2
b
Current Partition a b c a
s0 s1 b c
P0 {s1,s2,s3} {s0} none none none c
s3
c
Split on s2
b
Current Partition a b c a
s0 s1 b c
P0 {s1,s2,s3} {s0} none none none c
s3
c
minimal
RE NFA DFA
DFA
Comp 412, Fall 2010 25
Abbreviated Register Specification
Thompson’s construction produces
r 0
r 1
r 2
… …
…
s0 sf
r 8
r 9
8
9
sf8
sf9
minimal
RE NFA DFA
DFA
Comp 412, Fall 2010 27
Abbreviated Register Specification
The DFA minimization algorithm builds
0,1,2,3,4,
r 5,6,7,8,9
s0 sf
minimal
RE NFA DFA
DFA
Comp 412, Fall 2010 28
Automating Scanner Construction
RENFA (Thompson’s construction) The Cycle of Constructions
• Build an NFA for each term
• Combine them with -moves
minimal
RE NFA DFA
NFA DFA (subset construction) DFA
minimal
RE NFA DFA
DFA
Comp 412, Fall 2010 30
Limits of Regular Languages
Not all languages are regular
RL’s CFL’s CSL’s
You cannot construct DFA’s to recognize these languages
• L = { pkqk } (parenthesis languages)
• L = { wcwr | w *}
Neither of these is a regular language (nor an RE)
T { F, {S-F}}
• Partition P 2S
P{} • Start off with 2 subsets of S:
{F} and {S-F}
while ( P T)
PT • The while loop takes PiPi+1 by
T{} splitting 1 or more sets
for each set pi P • Pi+1 is at least one step closer to
T T Split(pi) the partition with |S | sets
• Maximum of |S | splits
Split(S)
for each c Note that
if c splits S into s1 & s2 • Partitions are never combined
then return {s1, s2} • Initial partition ensures that
return S final states remain final states
a b c
s1 s2 s3 s4
abc | bc | ad
b c
s0 s5 s6 s7
s6
d
b Subset construction eliminates -
c
s1 s2 s3 transitions and merges the paths for a.
a
It leaves duplicate tails, such as bc.
b c
s0 s4 s5
a d
s8 s9 s10
a b c
s1 s2 s3
subset(reverse(NFA))
a d
s8 s9 s11
d
a s2
b
And subset it, again
b c
s0 s3 s11
The Cycle of Constructions
Minimal DFA
minimal
RE NFA DFA
DFA
Comp 412, Fall 2010 39
Brzozowski