9/6/28 Stereotypicl computer CISC 49 Theory of Computtion Finite stte mchines & Regulr lnguges Professor Dniel Leeds dleeds@fordhm.edu JMH 332 Centrl processing unit (CPU) performs ll the instructions Memory stores dt nd instructions for CPU Input collects informtion from the world Output provides informtion to the world Input Output CPU 2 Super-simple computers Smll numer of potentil inputs Smll numer of potentil outputs/ctions Thermostt Elevtor Vending mchine Automtic door Automtic door Desired ehvior Person pproches entrywy, door opens Person goes through entrywy, door stys open Person is no longer ner entrywy, door closes Noody ner entrywy, door stys closed Two sttes: Open, Closed Two inputs: Front-sensor, Bck-sensor 3 Finite stte mchine 4
9/6/28 Grph nd tle representtions Neither Front, Bck, Both Front, Bck, Both More finite stte mchine pplictions Text prsing Closed Neither Open Front Bck Neither Both Closed Open Open Closed Open Open Open Open Closed Open 5 Trffic light Pc-Mn Electronic locks 6 Coding comintion lock q q 3, A finite utomton M with 3 sttes Strt stte q; ccept stte q2 (doule circle) Exmple ccepted string: Wht re ll strings tht this model will ccept? String ending with or string end with followed y even numer of s 8 Forml definition of Finite Stte Automton Finite stte utomton is 5-tuple Q, Σ, δ, q, F Q is finite set clled sttes Σ is finite set clled the lphet δ: Q Σ Q is the trnsition function q Q is the strt stte F Q is the set of ccept sttes 9 2
9/6/28 Descrie M using forml definition q q 3 M = Q, Σ, δ, q, F, Q = q,, q 3 δ = Σ =, Strt stte: q F = q q 3 q q 3 Lnguge of M If A is set of ll strings ccepted y M, A is lnguge of M L(M)=A A mchine my ccept mny strings, ut only one lnguge M ccepts string M recognizes lnguge Descrie L(M)=A A={w w ends with or w end with one followed y even numer of s} 3 Descrie M2 using forml definition Descrie M2 using forml definition q q M2 = Q,,, δ, q, M2 = Q,,, δ, q, Q = Strt stte: δ = Q = {q, } Strt stte: q δ = q q q2 q2 q q2 4 5 3
9/6/28 Wht is the lnguge of M2? L(M2)={w } Wht is the lnguge of M2? L(M2)={w w ends with t lest one } 6 7 CFG prctice G -> AB A -> xxa y B -> wbw z Wht is the lnguge of M4? (pge 38, Ex..) q s r G 2 -> D D -> nd o L(M4)={w w ends nd egins with sme letter (either or )} r 2 8 2 4
9/6/28 Perform modulo rithmetic Let Σ={RESET,,, 2} Construct M5 to ccept string only if the sum of ech input symol is multiple of 3, nd RESET sets the sum ck to (.3, pge 39),RES 2 More modulo rithmetic Generlize M5 to ccept if sum of symols is multiple of i insted of 3 q, q, q2, q3,, q i,,,2, RESET, δ, q, F q q 2,RES 2 q 3, RES 22 23 More modulo rithmetic Generlize M5 to ccept if sum of symols is multiple of i insted of 3 q, q, q2, q3,, q i,,,2, RESET, δ, q, F δ qj, RESET = q δ qj, = qj δ qj, = qk for k = j+ mod i δ qj, 2 = qk for k = j+2 mod i 24 Definition of M ccepting string Let M = Q, Σ, δ, q, F e finite utomton nd let w = w w 2 w n Then M ccepts w if sequence of sttes r, r,, r n in Q exists with 3 conditions r =q δ r i, w i+ = r i+ for i =,,, n r n F 25 5
9/6/28 Regulr lnguges Definition: lnguge is clled regulr lnguge if some finite utomton recognizes it equivlently All of the strings in regulr lnguge re ccepted y some finite utomton Designing finite utomt (FAs) Determine wht you need to rememer How mny sttes needed for your tsk? Set strt nd finish sttes Assign trnsitions Check your solution Should ccept w L Should reject w L Be creful out ε! 26 27 FA design prctice! FA to ccept lnguge where numer of s is odd (pge 43) FA to ccept string with sustring, FA to ccept string with s sustring (pge 44) q q q 3 q 4 FA to ccept string with sustring (next pge!) 29 3 6
9/6/28 Regulr opertions Let A nd B e lnguges. We define 3 regulr opertions: Union: A B = x x A or x B Conctention: A B = xy x A nd y B Str: A = x x 2 x k k nd ech x i A Repet string or more times Exmples of regulr opertions Let A = good, d nd B = oy, girl Wht is: A B = good, d, oy, girl A B = goodoy, goodgirl, doy, dgirl A = ε, good, d, goodgood, goodd, dgood, dd, 32 34 Express CFG s RegEx? Closure G 2 -> D D -> nd o n * o A collection of ojects is closed under n opertion if pplying tht opertion to memers of the collection returns n oject in the collection G -> AB A -> xxa y B -> wbw z None ville Regulr lnguges re closed under,, 36 37 7
9/6/28 Closure of Union Theorem.25: The clss of regulr lnguges is closed under the union opertion Proof y construction Let s consider two lnguges L: strt with, end with L2: strt with, end with Construct mchines for ech lnguges Construct mchines M3 to recognize L U L2 38 39 Exmple union A = {w w strts with ends with } M q q B = {w w strts with ends with } M2 r r r 2 q rej, r rej, 4 Exmple union Simulte M nd M2 sttes (q,r ) (q,r rej) (,r rej ) (q rej,r ) (q rej,r 2 ) 4 8
9/6/28 Closure of Union Proof y Construction Let us ssume M recognizes lnguge L Define M s M = Q, Σ, δ, q, F Let us ssume M2 recognizes lnguge L2 Define M2 s M2 = R, Σ, δ 2, r, F 2 Proof y construction: Construct M3 to recognize L3 = L L2 Let M3 e defined s M3 = S, Σ, δ 3, s, F 3 Closure of Union Proof y Construction Let M3 e defined s M3 = S, Σ, δ 3, s, F 3 Use ech stte of M3 to simulte eing in stte of M nd nother stte in M2 simultneously M3 sttes: S = q i, r j q i Q nd r j R Strt stte: s = q, r Accept stte: F 3 = q i, r j q i F or r j F 2 42 Trnsition function: δ 3 q i, r j, x = δ 3 q i, x, δ 3 q j, x 43 Closure of Conctention Theorem.26: The clss of regulr lnguges is closed under the conctention opertion If A nd A2 re regulr lnguges, then so is A A2 Chllenge: How do we know when M ends nd M2 egins? Determinism vs. non-determinism Determinism: Single trnsition llowed given current stte nd given input Non-determinism: multiple trnsitions llowed for current stte nd given input trnsition permitted for null input ε,,,ε q q 3 q 4 44 45 9
9/6/28 NFA in ction When there is choice, follow ll pths like cloning If there is no forwrd rrow, pth termintes nd clone dies (no ccept) NFA will ccept if t lest one pth termintes t ccept Alterntive thought: Mgiclly pick est pth from the set of options 46 The lnguge of M, q List some ccepted strings t third entry, we re in sttes {q,q 3, nd q 4 } Wht is L(M)? {w w contins or },,ε q 3 q 4 48 NFA construction prctice Build n NFA tht ccepts ll strings over {,} with in the third position from the end NFA -> DFA Build n NFA tht ccepts ll strings over {,} with in the third position from the end Cn we construct DFA for this? 49 5
9/6/28 Forml definition of Nondeterministic Finite Automton Similr to DFA: 5-tuple Q, Σ, δ, q, F Q is finite set clled sttes Σ is finite set clled the lphet δ: Q Σε P(Q) is the trnsition function q Q is the strt stte F Q is the set of ccept sttes 52 Descrie M using forml definition, M = Q, Σ, δ, q, F Q = Σ = Strt stte: F = q q δ =,, q 3 q q q 3 ε 53 Consider NFA N q q Convert NFA N to DFA M q q N Lnguge: L(N)={w w egins with, ends with, every in w is preceded y } 54
9/6/28 Equivlence of NFAs nd DFAs NFAs nd DFAs recognize the sme clss of lnguges Two mchines re equivlent if they recognize the sme lnguge Every NFA hs n equivlent DFA Union Closure with NFAs Proofs y construction fewer sttes! Any NFA proof pplies to DFA Given two regulr lnguges A nd A 2 recognized y N nd N2 respectively, construct N to recognize A A 2 59 6 Let s consider two lnguges L: strt with, end with L2: strt with, end with Let s consider two lnguges L: strt with, end with N, q q Construct mchines for ech lnguges Construct mchines N3 to recognize L U L2 L2: strt with, end with N2, r r r 2 62 63 2
9/6/28 N3 recognizes L U L2 ε N, q q Closure under conctention Given two regulr lnguges A nd A 2 recognized y N nd N2 respectively, construct N to recognize A A 2 s ε N2, r r r 2 64 66 Closure under str Str: L Prove if A is regulr, A is lso regulr s N, q q ε 69 7 3
9/6/28 Closure of regulr lnguges under str Let N = Q, Σ, δ, q, F N3 = Q 3, Σ, δ 3, s, F 3 Q 3 = Q s Strt stte: s F = F 3 s recognize L will recognize L * iff δ 3 q, = δ q, q s if q Q if q = s nd = ε if q F nd = ε Regulr expressions A regulr expression is description of set of possile strings using single chrcters nd possily including regulr opertions Exmples: 7 72 Regulr expressions forml definition R is regulr expression if R is, for some in lphet Σ ε R R2, where R nd R2 re regulr expressions R R2, where R nd R2 re regulr expressions R, where R is regulr expression Exmples of Regulr Expressions Σ Σ ε ε This is recursive definition 74 75 4
9/6/28 FA cn recognize ny Regulr Expression Theorem: A lnguge is regulr if nd only if some regulr expression descries it Prove: If lnguge is descried y regulr expression, then it is regulr Prove: If lnguge is regulr, then it is descried y regulr expression Prove if lnguge descried regulr expression, it is regulr (recognized y FSA) Ech regulr expression is either Cse : ϵσ Cse 2: ε Cse 3: Cse 4: R R2 Theorem.45 Cse 5: R R2 Theorem.47 Cse 6: R Proven on slide 5 Cse : Cse 2: Cse 3: q q q q 77 78 Converting from FSA to Regulr Expression, 2 79 5