Regulr Expressions (RE) Regulr Expressions (RE) Empty set F A RE denotes the empty set Opertion Nottion Lnguge UNIX Empty string A RE denotes the set {} Alterntion R +r L(r ) L(r ) r r Symol Alterntion Conctention Kleene-* M + N M N M* A RE denotes the set {} If M is RE for the set M nd N is RE for the set N, then M+N is RE for the set M U N If M is RE for the set M nd N is RE for the set N, then M.N is RE for the setm. N If M is RE for the set M, then M* is RE for the setm* Conctention Kleene-* Kleene-+ Exponentition r r r * r + r n L(r ) L(r ) L(r )* L(r ) + L(r ) n (r )(r ) (r ) (r )+ (r ){n} Regulr Expressions (RE) Regulr Expressions (RE) Exmple For the lphet S={, } + is RE denote the set {} U {} * is RE denote the set {}*={,,, }.* is RE denote the set {}.{,,, }={,,, } Notes For RE r, r i = r.r.r i-times Opertions precedence: * >. > + So we cn omit mny prentheses, for exmple: the RE (((*))+) cn e written s *+ We me revite rr* to r + The corresponding set (lnguge) denoted y RE r will e expressed s L(r) Definition Nondeterministic Finite Automt A nondeterministic finite utomton M is defined y 5-tuple M=(Q,S,d,q,F), with Q: finite set of sttes S: finite input lphet d: trnsition function d:q S P(Q) q Q: strt stte F Q: set of finl sttes Definition Nondeterministic Finite Automt A string w is ccepted y n NFA M if nd only if there exists pth strting t q which is leled y w nd ends in finl stte. The lnguge ccepted y n NFA M is the set of ll strings which re ccepted y M nd is denoted y L (M ). L(M)={w: d(q,w) F F}
Definition Nondeterministic Finite Automt A nondeterministic finite utomton hs trnsition rules like: q : : q Nondeterministic trnsition Nondeterministic Finite Automt Nondeterminism ~ Prllelism For ny string w, the nondeterministic utomton cn e in suset Q of severl possile sttes. If the finl set contins finl stte, then the utomton ccepts the string. The utomton processes the input in prllel fshion; its computtionl pth is no longer line, ut more like tree. Nondeterministic Finite Automt We cn write the NFA in two wys. Stte digrph. Tle q δ q q q q {q} {q,q} {q} q Exmple Nondeterministic Finite Automt Write n NFA for the lnguge, over S={,}, ending in q Σ = {, } F = { q} Q = { q, q, q} q ( ) q Check the input Nondeterministic Finite Automt Nondeterministic Finite Automt Quiz Check the input ( ) Exmple Write n NFA for the lnguge, over S={,}, L=( U )* ( U )* q q q q q q Input: q is finl stte hence the input is ccepted
Nondeterministic Finite Automt Nondeterministic Finite Automt Exmple Exmple 4 Write n NFA for the lnguge, over S={,}, L=( U )* ( U ) ( U )* q q q strt Wht lnguge is ccepted y this NFA q q Answer: (+)* Nondeterministic Finite Automt Exmple 5 For exmple, consider the following NFA which reds the input.,, Accepted! Theorem: For every lnguge L tht is ccepted y nondeterministic finite utomton, there is (deterministic) finite utomton tht ccepts L s well. FA nd NFA re equivlent computtionl models. Proof ide: When keeping trck of nondeterministic computtion of n NFA N we use mny fingers to point t the suset Q of sttes of N tht cn e reched on given input string. We cn simulte this computtion with deterministic utomton M with stte spce P(Q). Proof Let L e the lnguge recognized y the NFA N = (Q,S,d,q,F). Define the DFA M = (Q,S,d,q,F ) y. Q = P(Q). d (R,) = { q Q q d(r,) for n r R }. q = {q } 4. F = {R Q R contins finl stte of N} It is esy to see tht the previously descried deterministic finite utomton M ccepts the sme lnguge s N. Exmple Convert the NFA: q, q into DFA Given NFA Constructed DFA Q={q, q } Q =P(Q)={F, {q }, {q }, {q, q }} q q = {q } F={q } F ={{q }, {q, q }} For d see the next slide
Exmple Convert the NFA: q, q into DFA Given NFA Constructed DFA d d(q,)={q,q } d ({q },)={q,q } F F F d(q,)={q } d ({q },)={q } {q } {q,q } {q } d(q,)=f d ({q },)=F {q } F {q,q } d(q,)={q,q } d ({q },)={q,q } {q,q } {q,q } {q,q } {q } {q } d ({q,q },)=d(q,) U d(q,) ={q,q }, d ({q,q },)=d(q,) U d(q,) ={q,q } {q,q } F, Exmple Strt with the NFA: Q: Wht s the ccepted lnguge Q: How mny sttes does the suset construction crete in this cse Exmple A: L = {x {,}* rd it of x from right is } Exmple Strt with {}: A: 6 = 4 sttes. Tht s lot of sttes. Would e nice if only hd to construct useful sttes, I.e. those tht cn e reched from strt stte. Exmple Exmple Brnch out. Notice tht δ(,) = {,}. Brnch out. Notice tht δ ({,},) = {,,}. 4
Exmple Exmple NFA = DFA Brnch out. Note tht δ ({,,},) = {,,,4} Exmple Exmple Exmple Exmple 5
Exmple Exercise Summrizing: Convert the following NFA into n equivlent DFA q q q Therefore, we sved 5% effort y not constructing ll possile sttes unthinkingly. Nondeterministic Finite Automt with empty moves (-NFA) Nondeterministic Finite Automt with empty moves (-NFA) Definition A nondeterministic finite utomton with empty moves (-NFA) M is defined y 5-tuple M=(Q,S,d,q,F), with Q: finite set of sttes S: finite input lphet d: trnsition function d:q (S U {}) P(Q) q Q: strt stte F Q: set of finl sttes Definition A string w is ccepted y -NFA M if nd only if there exists pth strting t q which is leled y w nd ends in finl stte. The lnguge ccepted y -NFA M is the set of ll strings which re ccepted y M nd is denoted y L (M ). L(M)={w: d(q,w) F F} Notes Nondeterministic Finite Automt with empty moves (-NFA) δ : Q ( Σ { }) P( Q) A -trnsition cuses the mchine to chnge its stte non-deterministiclly, without consuming ny input. q Notes Nondeterministic Finite Automt with empty moves (-NFA) A -NFA hs trnsition rules/possiilities like: q Empty string trnsition And q : : q Nondeterministic trnsition 6
Nondeterministic Finite Automt with empty moves (-NFA) Nondeterministic Finite Automt with empty moves (-NFA) Nondeterminism ~ Prllelism For ny string w, the nondeterministic utomton cn e in suset Q of severl possile sttes. If the finl set contins finl stte, then the utomton ccepts the string. We cn write the NFA in two wys. Stte digrph q q q The utomton processes the input in prllel fshion; its computtionl pth is no longer line, ut more like tree.. Tle δ : Q ( Σ { }) P( Q ) δ q q q {q} {q,q} {q} Nondeterministic Finite Automt with empty moves (-NFA) Nondeterministic Finite Automt with empty moves (-NFA) Exmple,, Exmple,,,, q q q 4 q q q 4 This utomton ccepts, ecuse there is possile pth tht leds to finl stte, nmely: q q q q 4 q 4 (note tht q q q q q is not ccepting) The string gets rejected: on the utomton cn only rech: {q,,q }. Nondeterministic Finite Automt with empty moves (-NFA) Exmple strt A -trnsition is tken without consuming ny chrcter from the input. Wht does the NFA ove ccepts *+* Quiz Nondeterministic Finite Automt with empty moves (-NFA) M : M : M : M 4 : q q q, q q q q Wht re d(q,), d(q,), d(q, ) in ech of M, M, M nd in M 4. 7
Answer Nondeterministic Finite Automt with empty moves (-NFA) M: M: M: M4: q q q, q q M: d(q,) = d(q,) = d(q, ) = M: Sme M: d(q,) = d(q,) =, d(q, ) = {q, } M4: d(q,)={q,q }, d(q,)={,q }, d(q, )= q q Quiz Nondeterministic Finite Automt with empty moves (-NFA) q, q q Which of the following strings is ccepted... 4. Nondeterministic Finite Automt with empty moves (-NFA) Answer. is rejected. No pth leled y empty string from strt stte to n ccept stte.. is ccepted. EG the pth q q. is ccepted. EG the pth q q 4. is ccepted. There is only one ccepted pth: q q, q q q q q q q q q Nondeterministic Finite Automt with empty moves (-NFA) Definition Given -NFA stte s, the -closure(s) is the set of sttes tht re rechle through -trnsiton from s. -closure(s)={q: there is pth from s to q leled } Given set of -NFA sttes T, the -closure(t) is the set of sttes tht re rechle through -trnsiton from ny stte s T. -closure(t)=u s T -closure(s) Nondeterministic Finite Automt with empty moves (-NFA) Nondeterministic Finite Automt with empty moves (-NFA) Exmple : q q Exmple : IF -z f -z i 4 5 6 7-9 -9-9 ny 4 5 9 chrcter error ID 8 NUM -closure(q ) = {q,q, } -closure(q ) = {q, } Wht sttes cn e reched from stte without consuming chrcter -closure( ) = { } 8
Nondeterministic Finite Automt with empty moves (-NFA) Exmple IF -z f -z 4 5 6 i 7-9 -9-9 ny 4 5 9 chrcter error Wht sttes cn e reched from stte without consuming chrcter {,4,9,4} form the -closure of stte ID 8 NUM Nondeterministic Finite Automt with empty moves (-NFA) Exmple IF -z f -z 4 5 6 i 7-9 -9-9 ny 4 5 9 chrcter error Wht re ll the stte closures in this NFA closure() = {,4,9,4} closure(5) = {5,6,8} closure(8) = {6,8} closure(7) = {7,8} closure() = {,,} closure() = {,} closure() = {,} ID 8 NUM Nondeterministic Finite Automt with empty moves (-NFA) Definition: Extension of d δ : Q ( U{ }) P( Q) δ is defined s follows:. δ (q,)=-closure(q). δ (q,w)=-closure(t) where T={p: p d(r,) nd r δ (q,w)}, S, w S* * δ : Q P( Q) Nondeterministic Finite Automt with empty moves (-NFA) Exmple: Extension of d q q δ (q, ) = {q, } -NFA NFA -NFA NFA Theorem: For every lnguge L tht is ccepted y -NFA, there is n NFA tht ccepts L s well. -NFA nd NFA re equivlent computtionl models. Proof: Let M=(Q,S,d,q,F) e -NFA, n equivlent NFA, M =(Q,S,d,q,F ) cn e constructed s follows:. F ' = { F U{ q } F If -closure(q ) Otherwise F F. δ ' ( q, ) = δˆ( q, ) 9
-NFA NFA -NFA NFA Exmple: For the -NFA: q q Construct the equivlent NFA Exmple: For the -NFA: q q Construct the equivlent NFA Answer: Given -NFA Constructed NFA Answer: Given -NFA Constructed NFA Q={q,q, } nd S={,} -closure(q )={q,q, } F F Q={q,q, } nd S={,} F = {q,q } Q={q,q, } nd S={,} -closure(q )={q,q, } F F Q={q,q, } nd S={,} F = {q,q } δ ( q,) = δˆ(,) = {q,q, } ' q δ ( q,) = δˆ(,) = {q, } ' q δ ( q,) = δˆ(,) = { } ' q, q q,, δ ( q,) = δˆ(,) = F ' q δ ( q,) = δˆ(,) = {q, } ' q δ ( q,) = δˆ(,) = { } ' q,, q q,, -NFA NFA -NFA NFA Exmple: For the -NFA: Answer: Given -NFA Q={q,q, } nd S={,} -closure(q )={q,q, } F F δ ( q,) = δˆ(,) = F ' q δ ( q,) = δˆ(,) = F ' q δ ( q,) = δˆ(,) = { } ' q q q Construct the equivlent NFA Constructed NFA Q={q,q, } nd S={,} F = {q,q },, q q,, Exmple: For the -NFA: Construct the equivlent NFA Answer: q q,, q q,, RE -NFA Proof: RE -NFA Theorem: Let r e RE, there exist -NFA tht ccepts L(r). The proof works y induction, using the recursive definition of regulr expressions. RE -NFA F q q q q
Proof: M M r+r L(M) U L(M) RE -NFA M r* L(M)* Exmple The -NFA for symol i is: The -NFA for symol f is: RE -NFA For the regulr expression r=if we uild the -NFA s follows: strt strt i f M M r.r L(M) L(M) The -NFA for the regulr expression if is: strt i f IF Exmple RE -NFA Exmple RE -NFA For the regulr expression r=+* uild the equivlent -NFA The -NFA for symol is: The -NFA for symol is: The -NFA for symol * is: For the regulr expression r=+* uild the equivlent -NFA The -NFA for the regulr expression +* is: * The -NFA for the regulr expression +* is: +* Exmple RE -NFA Exmple RE -NFA Q: Find n NFA for the regulr expression ( )*( ( )*)( )* ( )*( ( )*)( )* Note tht: in this exmple e =
Exercise RE -NFA Construct -NFA for the regulr expression: *+(+)*+*