CMSC 330: Orgniztion of Progrmming Lnguges DFAs, nd NFAs, nd Regexps (Oh my!) CMSC330 Spring 2018
Types of Finite Automt Deterministic Finite Automt (DFA) Exctly one sequence of steps for ech string All exmples so fr Nondeterministic Finite Automt (NFA) My hve mny sequences of steps for ech string Accepts if ny pth ends in finl stte t end of string More compct thn DFA Ø But more expensive to test whether string mtches CMSC 330 Spring 2018 2
Quiz 1: Which DFA mtches this regexp? b(b +b?) A. B. C. 0 b 1 b 2 b 3 b 4 b 0 1 b b 2 3 0 b 1 02,b D. None of the bove CMSC 330 Spring 2018 3
Quiz 1: Which DFA mtches this regexp? b(b +b?) A. B. C. 0 b 1 b 2 b 3 b 4 b 0 1 b b 2 3 0 b 1 02,b D. None of the bove CMSC 330 Spring 2018 4
Compring DFAs nd NFAs NFAs cn hve more thn one trnsition leving stte on the sme symbol DFAs llow only one trnsition per symbol I.e., trnsition function must be vlid function DFA is specil cse of NFA CMSC 330 Spring 2018 5
Compring DFAs nd NFAs (cont.) NFAs my hve trnsitions with empty string lbel My move to new stte without consuming chrcter ε e-trnsition DFA trnsition must be lbeled with symbol DFA is specil cse of NFA CMSC 330 Spring 2018 6
DFA for ( b)*bb CMSC 330 Spring 2018 7
NFA for ( b)*bb b Hs pths to either S0 or S1 Neither is finl, so rejected bbbb Hs pths to different sttes One pth leds to S3, so ccepts string CMSC 330 Spring 2018 8
NFA for (b b)* b Hs pths to sttes S0, S1 bb Hs pths to S0, S1 Need to use ε-trnsition CMSC 330 Spring 2018 9
Compring NFA nd DFA for (b b)* DFA NFA CMSC 330 Spring 2018 10
NFA Acceptnce Algorithm Sketch When NFA processes string s NFA must keep trck of severl current sttes Ø Due to multiple trnsitions with sme lbel Ø ε-trnsitions If ny current stte is finl when done then ccept s Exmple After processing Ø NFA my be in sttes S1 S2 S3 S1 S2 ε S3 CMSC 330 Spring 2018 11
Forml Definition A deterministic finite utomton (DFA) is 5-tuple (Σ, Q, q 0, F, δ) where Σ is n lphbet Q is nonempty set of sttes q 0 Î Q is the strt stte F Q is the set of finl sttes δ : Q x Σ Q specifies the DFA's trnsitions Ø Wht's this definition sying tht δ is? A DFA ccepts s if it stops t finl stte on s CMSC 330 Spring 2018 12
Forml Definition: Exmple Σ = {0, 1} Q = {S0, S1} q 0 = S0 F = {S1} input stte symbol δ 0 1 S0 S0 S1 S1 S0 S1 or s { (S0,0,S0),(S0,1,S1),(S1,0,S0),(S1,1,S1) } CMSC 330 Spring 2018 13
Nondeterministic Finite Automt (NFA) An NFA is 5-tuple (Σ, Q, q 0, F, δ) where Σ, Q, q0, F s with DFAs δ Q x (Σ È {ε}) x Q specifies the NFA's trnsitions S1 S2 ε Exmple S3 Σ = {} Q = {S1, S2, S3} q 0 = S1 F = {S3} δ = { (S1,,S1), (S1,,S2), (S2,ε,S3) } An NFA ccepts s if there is t lest one pth vi s from the NFA s strt stte to finl stte CMSC 330 Spring 2018 14
Relting REs to DFAs nd NFAs Regulr expressions, NFAs, nd DFAs ccept the sme lnguges! DFA cn reduce NFA cn trnsform cn reduce RE CMSC 330 Spring 2018 15
Reducing Regulr Expressions to NFAs Gol: Given regulr expression A, construct NFA: <A> = (Σ, Q, q 0, F, δ) Remember regulr expressions re defined recursively from primitive RE lnguges Invrint: F = 1 in our NFAs Ø Recll F = set of finl sttes Will define <A> for bse cses: σ, ε, Where σ is symbol in Σ And for inductive cses: AB, A B, A* CMSC 330 Spring 2018 16
Reducing Regulr Expressions to NFAs Bse cse: σ σ <σ> = ({σ}, {S0, S1}, S0, {S1}, {(S0, σ, S1)} ) CMSC 330 Spring 2018 17
Reduction Bse cse: ε <ε> = (, {S0}, S0, {S0}, ) Bse cse: < > = (, {S0, S1}, S0, {S1}, ) CMSC 330 Spring 2018 18
Reduction: Conctention Induction: AB <A> <B> <A> = (Σ A, Q A, q A, {f A }, δ A ) <B> = (Σ B, Q B, q B, {f B }, δ B ) CMSC 330 Spring 2018 19
Reduction: Conctention Induction: AB <A> <B> <A> = (Σ A, Q A, q A, {f A }, δ A ) <B> = (Σ B, Q B, q B, {f B }, δ B ) <AB> = (Σ A È Σ B, Q A È Q B, q A, {f B }, δ A È δ B È {(f A,ε,q B )} ) CMSC 330 Spring 2018 20
Reduction: Union Induction: A B <A> = (Σ A, Q A, q A, {f A }, δ A ) <B> = (Σ B, Q B, q B, {f B }, δ B ) CMSC 330 Spring 2018 21
Reduction: Union Induction: A B <A> = (Σ A, Q A, q A, {f A }, δ A ) <B> = (Σ B, Q B, q B, {f B }, δ B ) <A B> = (Σ A È Σ B, Q A È Q B È {S0,S1}, S0, {S1}, δ A È δ B È {(S0,ε,q A ), (S0,ε,q B ), (f A,ε,S1), (f B,ε,S1)}) CMSC 330 Spring 2018 22
Reduction: Closure Induction: A* <A> = (Σ A, Q A, q A, {f A }, δ A ) CMSC 330 Spring 2018 23
Reduction: Closure Induction: A* <A> = (Σ A, Q A, q A, {f A }, δ A ) <A*> = (Σ A, Q A È {S0,S1}, S0, {S1}, δ A È {(f A,ε,S1), (S0,ε,q A ), (S0,ε,S1), (S1,ε,S0)}) CMSC 330 Spring 2018 24
Quiz 2: Which NFA mtches *? A. B. C. D. CMSC 330 Spring 2018 25
Quiz 2: Which NFA mtches *? A. B. C. D. CMSC 330 Spring 2018 26
Quiz 3: Which NFA mtches b*? A. B. C. D. CMSC 330 Spring 2018 27
Quiz 3: Which NFA mtches b*? A. B. C. D. CMSC 330 Spring 2018 28
Reduction Complexity Given regulr expression A of size n... Size = # of symbols + # of opertions How mny sttes does <A> hve? Two dded for ech, two dded for ech * O(n) Tht s pretty good! CMSC 330 Spring 2018 31
Reducing NFA to DFA DFA cn reduce NFA cn reduce RE CMSC 330 Spring 2018 33
Reducing NFA to DFA NFA my be reduced to DFA By explicitly trcking the set of NFA sttes Intuition Build DFA where Ø Ech DFA stte represents set of NFA current sttes Exmple S1 S2 ε S3 S1 S1, S2, S3 NFA DFA CMSC 330 Spring 2018 34
Algorithm for Reducing NFA to DFA Reduction pplied using the subset lgorithm DFA stte is subset of set of ll NFA sttes Algorithm Input Ø NFA (Σ, Q, q 0, F n, δ) Output Ø DFA (Σ, R, r 0, F d, d) Using two subroutines Ø ε-closure(d, p) (nd ε-closure(d, S)) Ø move(d, p, ) (nd move(d, S, )) CMSC 330 Spring 2018 35
ε-trnsitions nd ε-closure ε We sy p q If it is possible to go from stte p to stte q by tking only e-trnsitions in δ If $ p, p 1, p 2, p n, q Î Q such tht Ø {p,ε,p 1 } Î δ, {p 1,ε,p 2 } Î δ,, {p n,ε,q} Î δ ε-closure(δ, p) Set of sttes rechble from p using ε-trnsitions lone Ø Set of sttes q such tht p ε q ccording to δ Ø ε-closure(δ, p) = {q p ε q in δ } ε Ø ε-closure(δ, Q) = { q p Î Q, p q in δ } Notes Ø ε-closure(δ, p) lwys includes p Ø We write ε-closure(p) or ε-closure(q) when δ is cler from context CMSC 330 Spring 2018 36
ε-closure: Exmple 1 Following NFA contins ε S1 S2 ε S2 S3 ε S1 S3 ε ε Ø Since S1 S2 nd S2 S3 ε ε S1 S2 S3 ε-closures ε-closure(s1) = ε-closure(s2) = ε-closure(s3) = { S3 } ε-closure( { S1, S2 } ) = { S1, S2, S3 } { S2, S3 } { S1, S2, S3 } È { S2, S3 } CMSC 330 Spring 2018 37
ε-closure: Exmple 2 Following NFA contins ε S1 S3 ε S3 S2 ε S1 S2 ε ε Ø Since S1 S3 nd S3 S2 b S1 S2 S3 ε ε ε-closures ε-closure(s1) = ε-closure(s2) = ε-closure(s3) = { S1, S2, S3 } { S2 } { S2, S3 } ε-closure( { S2,S3 } ) = { S2 } È { S2, S3 } CMSC 330 Spring 2018 38
ε-closure Algorithm: Approch Input: NFA (Σ, Q, q 0, F n, δ), Stte Set R Output: Stte Set R Algorithm Let R = R Repet Let R = R Let R = R È {q p Î R, (p, e, q) Î d} Until R = R // strt sttes // continue from previous // new ε-rechble sttes // stop when no new sttes This lgorithm computes fixed point see note linked from project description CMSC 330 Spring 2018 39
ε-closure Algorithm Exmple Clculte ε-closure(d,{s1}) R R ε ε S1 S2 S3 {S1} {S1} {S1} {S1, S2} {S1, S2} {S1, S2, S3} Let R = R Repet Let R= R Let R = R È {q p Î R, (p, e, q) Î d} Until R = R {S1, S2, S3} {S1, S2, S3} CMSC 330 Spring 2018 40
Clculting move(p,) move(δ,p,) Set of sttes rechble from p using exctly one trnsition on Ø Set of sttes q such tht {p,, q} Î δ Ø move(δ,p,) = { q {p,, q} Î δ } Ø move(δ,q,) = { q p Î Q, {p,, q} Î δ } i.e., cn lift move() to strt from set of sttes Q Notes: Ø move(δ,p,) is Ø if no trnsition (p,,q) Î δ, for ny q Ø We write move(p,) or move(r,) when δ cler from context CMSC 330 Spring 2018 41
move(,p) : Exmple 1 Following NFA Σ = {, b } Move move(s1, ) = move(s1, b) = move(s2, ) = move(s2, b) = move(s3, ) = move(s3, b) = { S2, S3 } Ø Ø { S3 } Ø Ø b S1 S2 S3 move({s1,s2},b) = { S3 } CMSC 330 Spring 2018 42
move(,p) : Exmple 2 Following NFA Σ = {, b } Move move(s1, ) = move(s1, b) = move(s2, ) = move(s2, b) = move(s3, ) = move(s3, b) = { S2 } { S3 } { S3 } Ø Ø Ø S1 S2 S3 b move({s1,s2},) = {S2,S3} ε CMSC 330 Spring 2018 43
NFA DFA Reduction Algorithm ( subset ) Input NFA (Σ, Q, q 0, F n, δ), Output DFA (Σ, R, r 0, F d, d ) Algorithm Let r 0 = e-closure(δ,q 0 ), dd it to R While $ n unmrked stte r Î R Mrk r For ech Î S Let E = move(δ,r,) Let e = e-closure(δ,e) If e Ï R Let R = R È {e} Let d = d È {r,, e} Let F d = {r $ s Î r with s Î F n } // DFA strt stte // process DFA stte r // ech stte visited once // for ech letter // sttes reched vi // sttes reched vi e // if stte e is new // dd e to R (unmrked) // dd trnsition r e // finl if include stte in F n CMSC 330 Spring 2018 44
NFA DFA Exmple 1 Strt = e-closure(δ,s1) = { {S1,S3} } R = { {S1,S3} } r Î R = {S1,S3} move(δ,{s1,s3},) = {S2} Ø e = e-closure(δ,{s2}) = {S2} Ø R = R È {{S2}} = { {S1,S3}, {S2} } Ø d = d È {{S1,S3},, {S2}} move(δ,{s1,s3},b) = Ø b S1 S2 S3 {1,3} NFA DFA {2} ε CMSC 330 Spring 2018 45
NFA DFA Exmple 1 (cont.) R = { {S1,S3}, {S2} } r Î R = {S2} move(δ,{s2},) = Ø move(δ,{s2},b) = {S3} Ø e = e-closure(δ,{s3}) = {S3} Ø R = R È {{S3}} = { {S1,S3}, {S2}, {S3} } Ø d = d È {{S2}, b, {S3}} b S1 S2 S3 {1,3} NFA DFA {2} ε b {3} CMSC 330 Spring 2018 46
NFA DFA Exmple 1 (cont.) R = { {S1,S3}, {S2}, {S3} } r Î R = {S3} Move({S3},) = Ø Move({S3},b) = Ø Mrk {S3}, exit loop F d = {{S1,S3}, {S3}} Ø Since S3 Î F n Done! b S1 S2 S3 {1,3} NFA DFA {2} ε b {3} CMSC 330 Spring 2018 47
Quiz 4: Which DFA is equiv to this NFA? NFA: b S0 S1 S2 ε A. S0 S1 b S1, S2 b B. C. S0 S1 S2, S0 S0 S1 b S2, S0 D. None of the bove CMSC 330 Spring 2018 49 b
Quiz 4: Which DFA is equiv to this NFA? NFA: b S0 S1 S2 ε A. S0 S1 b S1, S2 b B. C. S0 S1 S2, S0 S0 S1 b S2, S0 D. None of the bove CMSC 330 Spring 2018 50 b
Actul Answer b S0 S1 S2 NFA: ε S0 S1 b S2, S0 b S1, S0 CMSC 330 Spring 2018 51
Subset Algorithm s Fixed Point Input: NFA (Σ, Q, q 0, F, δ) Output: DFA M Algorithm Let q 0 = ε-closure(δ, q 0 ) Let F = {q 0 } if q 0 F, or otherwise Let M = (Σ, {q 0 }, q 0, F, ) DFA // strting pproximtion of Repet Let M = M // current DFA pprox For ech q Î sttes(m), Î Σ // for ech DFA stte q nd letter Let s = ε-closure(δ, move(δ, q, )) // new subset from q Let F = {s} if s F, or otherwise, // subset contins finl? M = M È (, {s},, F, {(q,, s)}) // updte DFA Until M = M // reched fixed point CMSC 330 Spring 2018 57
Redux: DFA to NFA Exmple 1 q ' ε-closure(δ,s1) = {S1,S3} F = {{S1,S3}} since {S1,S3} {S3} NFA b S1 S2 S3 {1,3} M = { Σ, {{S1,S3}}, {S1,S3}, {{S1,S3}}, } Q q 0 ' F δ' DFA ε CMSC 330 Spring 2018 58
Redux: DFA to NFA Exmple 1 (cont) M = { Σ, {{S1,S3}}, {S1,S3}, {{S1,S3}}, } q = {S1, S3} = s = {S2} Ø since move(δ,{s1, S3},) = {S2} Ø nd e-closure(δ,{s2}) = {S2} F = Ø Ø Since {S2} {S3} = where s = {S2} nd F = {S3} b S1 S2 S3 {1,3} CMSC 330 Spring 2018 59 NFA DFA M = M È ( { S2}},, { S1,S3},,{S2 } { Σ, {{S1,S3},{S2}}, {S1,S3}, {{S1,S3}}, {({S1,S3},,{S2})} } {2} Q q 0 ' F δ' ε
Redux: DFA to NFA Exmple 1 (cont) M = { Σ, {{S1,S3},{S2}}, {S1,S3}, {{S1,S3}}, {({S1,S3},,{S2})} } q = {S2} = b s = {S3} Ø since move(δ,{s2},b) = {S3} Ø nd e-closure(δ,{s3}) = {S3} F = {{S3}} Ø Ø Since {S3} {S3} = {S3} where s = {S3} nd F = {S3} {1,3} M = M È { S3 {{S3}} { S2 b S3 } NFA b S1 S2 S3 DFA { Σ, {{S1,S3},{S2},{S3}}, {S1,S3}, {{S1,S3},{S3}}, {({S1,S3},,{S2}), ({S2},b,{S3})} } Q q 0 ' F δ' CMSC 330 Spring 2018 60 {2} ε b {3}
Anlyzing the Reduction Cn reduce ny NFA to DFA using subset lg. How mny sttes in the DFA? Ech DFA stte is subset of the set of NFA sttes Given NFA with n sttes, DFA my hve 2 n sttes Ø Since set with n items my hve 2 n subsets Corollry Ø Reducing NFA with n sttes my be O(2 n ) CMSC 330 Spring 2018 61
Reducing DFA to RE DFA cn reduce NFA cn trnsform cn trnsform RE CMSC 330 Spring 2018 62
Reducing DFAs to REs Generl ide Remove sttes one by one, lbeling trnsitions with regulr expressions When two sttes re left (strt nd finl), the trnsition lbel is the regulr expression for the DFA CMSC 330 Spring 2018 63
Other Topics Minimizing DFA Hopcroft reduction Complementing DFA Implementing DFA CMSC 330 Spring 2018 66
Minimizing DFAs Every regulr lnguge is recognizble by unique minimum-stte DFA Ignoring the prticulr nmes of sttes In other words For every DFA, there is unique DFA with minimum number of sttes tht ccepts the sme lnguge b S1 S2 S3 c CMSC 330 Spring 2018 67 S1 c S3 S2 b
J. Hopcroft, An n log n lgorithm for minimizing sttes in finite utomton, 1971 Minimizing DFA: Hopcroft Reduction Intuition Look to distinguish sttes from ech other Ø End up in different ccept / non-ccept stte with identicl input Algorithm Construct initil prtition Ø Accepting & non-ccepting sttes Itertively split prtitions (until prtitions remin fixed) Ø Split prtition if members in prtition hve trnsitions to different prtitions for sme input Two sttes x, y belong in sme prtition if nd only if for ll symbols in Σ they trnsition to the sme prtition Updte trnsitions & remove ded sttes CMSC 330 Spring 2018 68
Splitting Prtitions No need to split prtition {S,T,U,V} All trnsitions on led to identicl prtition P2 Even though trnsitions on led to different sttes S P1 X P2 T Y U V Z CMSC 330 Spring 2018 69
Splitting Prtitions (cont.) Need to split prtition {S,T,U} into {S,T}, {U} Trnsitions on from S,T led to prtition P2 Trnsition on from U led to prtition P3 S P1 X P2 P4 U T Z Y b P3 CMSC 330 Spring 2018 70
Resplitting Prtitions Need to reexmine prtitions fter splits Initilly no need to split prtition {S,T,U} After splitting prtition {X,Y} into {X}, {Y} we need to split prtition {S,T,U} into {S,T}, {U} P4 S U T P1 CMSC 330 Spring 2018 71 b P3 X b P2 Y
Minimizing DFA: Exmple 1 DFA b S T R Initil prtitions b Split prtition CMSC 330 Spring 2018 72
Minimizing DFA: Exmple 1 DFA Initil prtitions Accept { R } = P1 b P2 P1 Reject { S, T } = P2 Split prtition? Not required, minimiztion done move(s,) move(t,) = T P2 = T P2 b S T R b move(s,b) move (T,b) = R P1 = R P1 CMSC 330 Spring 2018 73
Minimizing DFA: Exmple 2 b S T R b CMSC 330 Spring 2018 74
Minimizing DFA: Exmple 2 DFA P2 b S T R P1 Initil prtitions Accept Reject { R } { S, T } = P1 = P2 Split prtition? Yes, different prtitions for B move(s,) move(t,) = T P2 = T P2 b P3 move(s,b) move (T,b) DFA lredy miniml = T P2 = R P1 CMSC 330 Spring 2018 75
Complement of DFA Given DFA ccepting lnguge L How cn we crete DFA ccepting its complement? Exmple DFA Ø Σ = {,b} CMSC 330 Spring 2018 78
Complement of DFA Algorithm Add explicit trnsitions to ded stte Chnge every ccepting stte to non-ccepting stte & every non-ccepting stte to n ccepting stte Note this only works with DFAs Why not with NFAs? CMSC 330 Spring 2018 79
Implementing DFAs (one-off) It's esy to build progrm which mimics DFA cur_stte = 0; while (1) { symbol = getchr(); switch (cur_stte) { cse 0: switch (symbol) { cse '0': cur_stte = 0; brek; cse '1': cur_stte = 1; brek; cse '\n': printf("rejected\n"); return 0; defult: printf("rejected\n"); return 0; } brek; } } cse 1: switch (symbol) { cse '0': cur_stte = 0; brek; cse '1': cur_stte = 1; brek; cse '\n': printf("ccepted\n"); return 1; defult: printf("rejected\n"); return 0; } brek; defult: printf("unknown stte; I'm confused\n"); brek; CMSC 330 Spring 2018 80
Implementing DFAs (generic) More generlly, use generic tble-driven DFA given components (Σ, Q, q 0, F, d) of DFA: let q = q 0 while (there exists nother symbol s of the input string) q := d(q, s); if q Î F then ccept else reject q is just n integer Represent d using rrys or hsh tbles Represent F s set CMSC 330 Spring 2018 81
Running Time of DFA How long for DFA to decide to ccept/reject string s? Assume we cn compute d(q, c) in constnt time Then time to process s is O( s ) Ø Cn t get much fster! Constructing DFA for RE A my tke O(2 A ) time But usully not the cse in prctice So there s the initil overhed But then processing strings is fst CMSC 330 Spring 2018 82
Summry of Regulr Expression Theory Finite utomt DFA, NFA Equivlence of RE, NFA, DFA RE NFA Ø Conctention, union, closure NFA DFA Ø e-closure & subset lgorithm DFA Minimiztion, complement Implementtion CMSC 330 Spring 2018 84