CSI 3104 /Winter 2011: Introduction to Forml Lnguges Chpter 7: Kleene s Theorem Chpter 7: Kleene s Theorem Regulr expressions, Finite Automt, trnsition grphs re ll the sme!! Dr. Neji Zgui CSI3104-W11 1
Chpter 7: Kleene s Theorem Method of proof Let A,B,C e sets such tht A B, B C, C A. Then A=B=C. Remrk: Regulr expressions, finite utomt, nd trnsition grphs ech define set of lnguges. Dr. Neji Zgui CSI3104-W11 2
Chpter 7: Kleene s Theorem Kleene s Theorem Any lnguge tht cn e defined y regulr expression, or finite utomton, or trnsition grph cn e defined y ll three methods. Dr. Neji Zgui CSI3104-W11 3
Chpter 7: Kleene s Theorem Proof of Kleen s theorem: It is enough to prove ech of the lemms elow. Lemm 1: Every lnguge tht cn e defined y finite utomton cn lso e defined y trnsition grph. Lemm 2: Every lnguge tht cn e defined y trnsition grph cn lso e defined y regulr expression. Lemm 3: Every lnguge tht cn e defined y regulr expression cn lso e defined y finite utomton. FA TG RE FA Dr. Neji Zgui CSI3104-W11 4
Chpter 7: Kleene s Theorem Lemm 1: Every lnguge tht cn e defined y finite utomton cn lso e defined y trnsition grph. Proof: By definition, every finite utomton is trnsition grph. Lemm 2: Every lnguge tht cn e defined y trnsition grph cn lso e defined y regulr expression. Proof: By constructive lgorithm, tht works 1. for every trnsition grph 2. in finite numer of steps Dr. Neji Zgui CSI3104-W11 5
Chpter 7: Kleene s Theorem 1st step: trnsform to trnsition grph with single strt stte. 1 2 5 3 4 L 1 L 3 4 2 L 5 Dr. Neji Zgui CSI3104-W11 6
Chpter 7: Kleene s Theorem 2nd step: nd single finl stte 9 1 L 12 2 Result is n equivlent trnsition grph, of the form: L L L L L L 9 L Dr. Neji Zgui CSI3104-W11 7
Chpter 7: Kleene s Theorem 3. Comine edges tht hve the sme strting nd ending stte. x r 3 r 1 r 2 x r 1 r 2 r 3 r 1 3 7 r 1 r 2 r 2 3 7 Dr. Neji Zgui CSI3104-W11 8
Chpter 7: Kleene s Theorem 3. Eliminte sttes one y one: ypss nd stte elimintion opertion r 1 1 r 2 3 3 r 1 1 r 3 3 r 2 r 1 1 r 2 3 3 r 1 1 r 2 *r 3 3 Dr. Neji Zgui CSI3104-W11 9
Chpter 7: Kleene s Theorem Bypss stte 2 r 1 r 2 r 3 r 4 1 2 4 3 r 1 r 2 *r 3 3 r 5 5 1 r 1 r 2 *r 4 4 r 1 r 2 *r 5 5 Dr. Neji Zgui CSI3104-W11 10
Chpter 7: Kleene s Theorem L 1 4 L L 3 2 5 L ypss stte 2, pths through stte 2: 1 2 4, 1 2 5, 3 2 4, 3 2 5 Dr. Neji Zgui CSI3104-W11 11
Chpter 7: Kleene s Theorem L 1 4 L L 3 2 5 L L 1 * * 4 L L 3 * 5 L * Dr. Neji Zgui CSI3104-W11 12
Chpter 7: Kleene s Theorem 7 9 7 r 1 r 2 r 3 2 r 1 r 3 *r 4 r 1 r 3 *r 5 r 1 r 3 *r 6 r 2 r 3 *r 4 r 2 r 3 *r 5 r 4 r 5 r 6 1 1 4 5 1 1 4 Even with mny pths through stte 2, lwys n equivlent GTG. 9 r 2 r 3 *r 6 5 Dr. Neji Zgui CSI3104-W11 13
Chpter 7: Kleene s Theorem Specil cses r r 2 3 r 1 2 4 3 r 1 3 2 1, 1 2 1 r 1 r 2 * r 3 r 4 r 2 * r 1 3 3 Dr. Neji Zgui CSI3104-W11 14
Chpter 7: Kleene s Theorem r 1 1 r 8 r 2 r 4 r 5 2 r 3 r 7 r 6 3 3 2 1, 3 2 3, 1 2 1, 1 2 3 r 9 r 8 r 2 r 4 *r 5 r 1 r 2 r 4 *r 3 1 3 r 7 r 6 r 4 *r 5 r 9 r 6 r 4 *r 3 Dr. Neji Zgui CSI3104-W11 15
Chpter 7: Kleene s Theorem 3. Comine edges r 1 r 2 r 35 r 1 r 2 r 35 Dr. Neji Zgui CSI3104-W11 16
Chpter 7: Kleene s Theorem Exmple 1,, ()()*() Dr. Neji Zgui CSI3104-W11 17
Chpter 7: Kleene s Theorem, L EXAMPLE2: EVEN-EVEN,, L, 1 2 L L L L 1 1 ()()*() ()()()*() [()()()*()]* Dr. Neji Zgui CSI3104-W11 18
Chpter 7: Kleene s Theorem Exmple 3 1 r 4 4 r 10 r 1 r 2 r 5 r 8 2 r 3 r 7 3 r 9 r 6 5 Dr. Neji Zgui CSI3104-W11 19
Chpter 7: Kleene s Theorem Trnsition Grph Regulr Expression Algorithm (nd proof) 1. Add (if necessry) unique strt stte without incoming edges nd unique finl stte without outgoing edges. For ech stte tht is not strt stte or finl stte, repet steps 2 nd 3. 2. Do ypss nd stte elimintion opertion. 3. Comine edges tht hve the sme strting nd ending stte. 4. Comine the edges etween the strt stte nd the finl stte. The lel on the only remining edge is the regulr expression result. If there is none, the regulr expression is. Dr. Neji Zgui CSI3104-W11 20
Chpter 7: Kleene s Theorem Lemm 3: Every lnguge tht cn e defined y regulr expression cn lso e defined y finite utomton. Proof: By constructive lgorithm strting from the recursive definition of regulr expressions. Dr. Neji Zgui CSI3104-W11 21
Chpter 7: Kleene s Theorem Rememer: Given n lphet, the set of regulr expressions is defined y the following rules. 1. For every letter in, the letter written in old is regulr expression. Λ is regulr expression. 2. If r 1 nd r 2 re regulr expressions, so is r 1 r 2. 3. If r 1 nd r 2 re regulr expressions, so is r 1 r 2. 4. If r 1 is regulr expression, so is r 1 *. 5. Nothing else is regulr expression. Dr. Neji Zgui CSI3104-W11 22
Chpter 7: Kleene s Theorem Build finite utomton tht ccepts the lnguge: ()*()()* Rule: 1. The letter 1 2. The letter 1 3. The word (using 1) 3 4. The word (using 2) 3 5. The expression (using 3 nd 4) 2 6. The expression (using 1 nd 2) 2 7. The expression ()* (using 6) 4 8. The expression ()*() (using 7 nd 5) 3 9. The expression ()*()()* (using 8 nd 7) 3 Dr. Neji Zgui CSI3104-W11 23
Chpter 7: Kleene s Theorem Rule 1 ll ll The lnguge Λ ll Only the letter x ll except x x ll Dr. Neji Zgui CSI3104-W11 24
Chpter 7: Kleene s Theorem Rule 2: r1r2, Exmple 1 x x x 2 1 3 y 1 y 2 y 3 y 4, x 1 x 2 x 1 x 2 x 3 x 1 x 3 x 3 x 3 y 1 y 3 y 2 y 2 y 4 y 1 y 3 y 1 y 4 y 4 y 2 y 3 All word contining Dr. Neji Zgui CSI3104-W11 25 EVEN-EVEN
Chpter 7: Kleene s Theorem 11 9 1 3 6 12 10 5 2 7 4 Dr. Neji Zgui CSI3104-W11 26 Result: r1r2 8
Chpter 7: Kleene s Theorem Exmple 2. x 1 y 1 (words ending in ) x 2 y 2 (words ending in ) x 1 x 2 x 1 x 2 x 2 x 1 y 1 y 1 y 2 y 2 y 1 y 2 Dr. Neji Zgui CSI3104-W11 27
Chpter 7: Kleene s Theorem Lemm 3: Every lnguge tht cn e defined y regulr expression cn lso e defined y finite utomton. Proof: By constructive lgorithm strting from the recursive definition of regulr expressions 1. There is n FA tht ccepts only the empty word (Λ) nd n FA tht ccepts only single letter. 2. If there is n FA tht ccepts the lnguge defined y r 1 nd n FA tht ccepts the lnguge defined y r 2, then there is n FA tht ccepts the lnguge r 1 r 2. 3. If there is n FA tht ccepts the lnguge defined y r 1 nd n FA tht ccepts the lnguge defined y r 2, then there is n FA tht ccepts the lnguge defined y their conctention r 1 r 2. 4. If there is n FA tht ccepts the lnguge defined y r then there is n FA tht ccepts the lnguge defined y r*. Thus for every regulr expression, we cn construct FA. Dr. Neji Zgui CSI3104-W11 28
Chpter 7: Kleene s Theorem Algorithm 1: r 1 r 2 Input: FA 1: lphet: sttes: x 1, x 2, x 3, strt stte: x 1 FA 2: lphet: sttes: y 1, y 2, y 3, strt stte: y 1 plus finl sttes nd trnsitions The new FA: lphet: sttes: z 1, z 2, z 3, strt stte: x 1 or y 1 trnsitions: if z i = x j or y k nd x j x new nd y k y new (for input p) then z new = (x new or y new ) for input p. If x new or y new is finl stte, then z new is finl stte. Dr. Neji Zgui CSI3104-W11 29
Chpter 7: Kleene s Theorem x 1 x 2 (words ending in ) y 1 y 2 (words ending in ) x 1 x 2 x 1 x 2 x 2 x 1 y 1 y 1 y 2 y 2 y 1 y 2 x 1 or y 1 x 1 or y 2 lgorithm 1 x 2 or y 1 Dr. Neji Zgui CSI3104-W11 30
Chpter 7: Kleene s Theorem Algorithm 2: Put ll pirs (x i or x j ) in the trnsition tle x 1 or y 1 x 1 or y 2 x 1 or y 1 x 1 or y 2 x 2 or y 1 x 2 or y 2 x 2 or y 1 x 2 or y 2 Dr. Neji Zgui CSI3104-W11 31
Chpter 7: Kleene s Theorem Rule 3: r1r2, Exmple 1,, second letter is, odd numer of s Dr. Neji Zgui CSI3104-W11 32
Chpter 7: Kleene s Theorem,,, Dr. Neji Zgui CSI3104-W11 33
Chpter 7: Kleene s Theorem x 1 y 1 Exmple 2 x 2 y 2 z 1 z 2 x 3, r 1 : ll with r 2 : words ending in r1r2 z 3 z 4 Dr. Neji Zgui CSI3104-W11 34
Chpter 7: Kleene s Theorem z 1 z 2 z 3 z 4 We strt y creting the sttes z1=x1nd z2=x2 (z2,)= x3 we continue on AF1 OR y1 we move to AF2, since x3 is finl stte in AF1 (z2, ) = x3 or y1= z3 (z3, ) = x3 or y1= z3 (z3, ) = x3 or y1 or y2 = z4 (z4, )= x3 or y1=z3 (z4, )=x3 or y1 or y2 = z4 Dr. Neji Zgui CSI3104-W11 35
Chpter 7: Kleene s Theorem Rule 3: Conctention: Summry of the Algorithm 1. Add stte z for every stte of the first utomton tht is possile to go through efore rriving t finl stte. 2. For ech finl stte x, dd stte z = (x or y 1 ), where y 1 is the strt stte of the second utomton. 3. Strting from the sttes dded t step 2, dd sttes: x (stte such tht execution continues on 1 st utomton) z= OR y (stte such tht execution moves on 2 nd utomton} 4. Lel every stte tht contins finl stte from the second utomton s finl stte. Dr. Neji Zgui CSI3104-W11 36
Chpter 7: Kleene s Theorem x 1 Exmple 3 x 2 x 3, r 1 : no doule, y 1 y 2 r 2 : odd numer of letters, Dr. Neji Zgui CSI3104-W11 37
Chpter 7: Kleene s Theorem x 1 or y 1 x 2 or y 2 or y 1 z 1 z 3 z 2 z 4, x 1 or y 2 or y 1 x 3 or y 1 or y 2 r 1 r 2 : ll words except L Dr. Neji Zgui CSI3104-W11 38
Chpter 7: Kleene s Theorem Exmple 4, x 1 x 3, r 1 : words strting with x 2 y 1 y 2 r 2 : words ending in Dr. Neji Zgui CSI3104-W11 39
Chpter 7: Kleene s Theorem z 1 z 3 z 1 r 1 r 2 z 2, z 2 z 4 z 4 z 6 r 2 r 1 z 3 z 5 z 7 Dr. Neji Zgui CSI3104-W11 40
Chpter 7: Kleene s Theorem Rule 4: r*, Exmple 1 x 1 x 2 x 3,, r: * * r*: words without doule, nd tht do not strt with. x 4 Dr. Neji Zgui CSI3104-W11 41
Chpter 7: Kleene s Theorem z1=x1 ± (z1, ) = x1 or x2= z2 (z1,) = x4= z3 (z2,)= x1 or x2 =z2 (z2, )= x1 or x3 or x4= z4 (z3, )=z3 (z3,) = z3 (z4,)= x1 or x2 or x4= z5 (z4, )= x4 = z3 (z5,)= x1 or x2 or x4= z5 (z5, )= x1 or x3 or x4= z4 r* z 2 z 1, z 4 z 5 z 3 Dr. Neji Zgui CSI3104-W11 42
Chpter 7: Kleene s Theorem Exmple 2 x 1 Words ending in x 2 z1=x1 _ (z1, ) = x1 ou x2= z2 (z1,) = z1 (z2,)= x1 ou x2 =z2 (z2, )= x1=z1 x 1 x 1 ou x 2 x 1 x 1 ou x 2 Prolem with Λ Dr. Neji Zgui CSI3104-W11 43
Chpter 7: Kleene s Theorem Rule 4: Kleene Str: Algorithm Given: n FA whose sttes re {x 1, x 2, x 3, } For every suset of sttes, crete stte of the new FA. Remove ny suset tht contins finl stte ut not the strt stte. Mke the trnsition tle for ll the new sttes. Add stte. Connect it to the sme sttes s the originl strt stte ws connected to using the sme trnsitions. The finl sttes must e those tht contin t lest one finl stte from the originl FA. Dr. Neji Zgui CSI3104-W11 44
Chpter 7: Kleene s Theorem Exmple 3 x 1 x 2 Words with n odd numer of s. z 1 z 2, z 3 Λ nd words with t lest one. Dr. Neji Zgui CSI3104-W11 45
Chpter 7: Kleene s Theorem A nondeterministic finite utomton (NFA) is: 1. finite set of sttes, one of which is designted s the strt stte, nd some (mye none) of which re designted the finl sttes 2. n lphet of input letters 3. finite set of trnsitions tht show how to go to new stte, for some pirs of stte nd letters Remrk: Every finite utomton is nondeterministic finite utomton. Every nondeterministic finite utomton is trnsition grph. Dr. Neji Zgui CSI3104-W11 46
Chpter 7: Kleene s Theorem 2 Exmples of Nondeterministic Finite Automt,,, ()*()*()* x 1 x x 4 2 x 3 Dr. Neji Zgui CSI3104-W11 47
Chpter 7: Kleene s Theorem Theorem: Every lnguge tht cn e defined y nondeterministic finite utomton cn lso e defined y deterministic finite utomton. Proof (1): Every nondeterministic finite utomton is trnsition grph. By lemm 2: trnsition grph regulr expression By lemm 3: regulr expression finite utomton Proof (2): By constructive lgorithm (See Mnuel pge 135) Dr. Neji Zgui CSI3104-W11 48
Chpter 7: Kleene s Theorem Algorithm: nondeterministic utomton deterministic utomton (FA) Given: nondeterministic utomton whose sttes re {x 1, x 2, x 3, } 1. For every suset of sttes, crete stte of the new FA. 2. Mke the trnsition tle for ll the new sttes (or just the new sttes tht cn e entered). 3. Add stte. Add trnsitions tht loop ck to itself for ll letters of the lphet. For ech new stte, if there is no trnsition for letter p, dd one tht goes to the stte. 4. The finl sttes must e those tht contin t lest one finl stte from the originl nondeterministic finite utomton. Dr. Neji Zgui CSI3104-W11 49
Chpter 7: Kleene s Theorem x 1 x x 4 2 x 3 x 1 x 2 x 3 or x 4 x 4,, Dr. Neji Zgui CSI3104-W11 50
Chpter 7: Kleene s Theorem, x 1 x 2 x 3, (x1,)=x1 (x1,)=x1 or x2 (x1 or x2, )=x1 (x1 or x2,)=x2 or x3 (x2 or x3,)=x2 or x3 (x2 or x3,)=x2 or x3 x 1 x 1 or x 2 x 2 or x 3, Dr. Neji Zgui CSI3104-W11 51
Chpter 7: Kleene s Theorem Lemm 3: Every lnguge tht cn e defined y regulr expression cn lso e defined y finite utomton. Proof: By constructive lgorithm strting from the recursive definition of regulr expressions, we uild nondeterministic finite utomton. Then, y the most recent theorem, it is possile to then uild finite utomton. Dr. Neji Zgui CSI3104-W11 52
Chpter 7: Kleene s Theorem Rule 1: Λ nd the letters in Dr. Neji Zgui CSI3104-W11 53
Chpter 7: Kleene s Theorem x 1 x 2 x 4 y y 2 1, x 3 Rule 2: r1r2, x 1 y 1 y 2 Dr. Neji Zgui CSI3104-W11 54 x 2 x 4 x 3
Chpter 7: Kleene s Theorem Rule 3: r1r2 w 1 w 2 x 1 x 2 x 3,, w x 1 x 2 x 3 2 w 1 Dr. Neji Zgui CSI3104-W11 55