5//6 Grmmr Automt nd Lnguges Regulr Grmmr Context-free Grmmr Context-sensitive Grmmr Prof. Mohmed Hmd Softwre Engineering L. The University of Aizu Jpn Regulr Lnguges Context Free Lnguges Context Sensitive Lnguges Lnguges 2 Regulr Lnguges Content Equivlence etween Regulr Grmmrs nd Regulr Lnguges Pumping Lemm (PL) Exmples Regulr Lnguges L is Regulr Lnguge if nd only if there exist finite utomton M = (Q, Σ, δ, q, F ) such tht: L = L(M) = { w Σ* : δ (q,w) F} 3 4
5//6 Equivlence etween Regulr Grmmrs nd Regulr Lnguges Theorem If L is regulr lnguge then there is right-liner grmmr G =(V,T,S,P ) such tht L=L(G). Proof. L is regulr implies (y def.) there exist finite utomton M = (Q, Σ, δ, q, F ) such tht L(M)=L. Now we construct the equivlent grmmr G s follows: Vriles re the sttes: V = Q Strt symol is strt stte: S = q Sme lphet of terminls T=Σ A trnsition δ(q,)=q 2 ecomes the rule q àq 2 Accept sttes q F define the λ-productions q à λ Accepted pths give rise to terminting derivtions nd vice vers. L(G)=L(M). 5 Equivlence etween Regulr Grmmrs nd Regulr Lnguges Exmple The DFA ove cn e simulted y the grmmr x à x y y à x z z à x z λ 6 Equivlence etween Regulr Grmmrs nd Regulr Lnguges Equivlence etween Regulr Grmmrs nd Regulr Lnguges Exmple x à x y y à x z z à x z λ Exmple x à x y y à x z z à x z λ x x y 7 8 2
5//6 Equivlence etween Regulr Grmmrs nd Regulr Lnguges Equivlence etween Regulr Grmmrs nd Regulr Lnguges Exmple x à x y y à x z z à x z λ Exmple x à x y y à x z z à x z λ x y x x y x x 9 Equivlence etween Regulr Grmmrs nd Regulr Lnguges Exmple x à x y y à x z z à x z λ x y x x y Equivlence etween Regulr Grmmrs nd Regulr Lnguges Exmple x à x y y à x z z à x z λ x y x x y z 2 3
5//6 Equivlence etween Regulr Grmmrs nd Regulr Lnguges Exmple x à x y y à x z z à x z λ x y x x y z Equivlence etween Regulr Grmmrs nd Regulr Lnguges Theorem 2 If G =(V,T,S,P) is right-liner grmmr then L(G) is regulr lnguge. Proof. : Define FA M = (Q, Σ, δ, q, F ) s follows Strt stte q correspond to strt symol S A non-finl stte q i corresponds to vrile symol V i Sme lphet of terminls Σ = Τ For every rule V i à m V j, define trnsition δ(q i, m )=q j For every rule V i à m, define trnsition δ(q i, m )=q f finl stte ACCEPT! 3 Terminting derivtions give rise to ccepted pths nd vice vers. So L(M)=L(G). Hence (y def.) L(G) is regulr lnguge. 4 Equivlence etween Regulr Grmmrs nd Regulr Lnguges Theorem 2 Construct n FA tht is equivlent to the right-liner grmmr: Answer: S à A A à S Aà S A q f Equivlence etween Regulr Grmmrs nd Regulr Lnguges Comments THEOREM nd THEOREM 2 show tht right-liner grmmrs nd regulr lnguges re equivlent. Similrly we cn show tht left-liner grmmrs nd regulr lnguges re equivlent. 5 Hence we conclude tht Regulr Grmmrs nd Regulr Lnguges re equivlent. 6 4
5//6 Regulr Lnguges Regulr Lnguges Cn every CFG e converted into right liner grmmr? How we cn identify non-regulr lnguges? A: NO! This would men tht ll context free lnguges re regulr. For exmple: S à λ S cnnot e converted ecuse { n n } is not regulr. 7 A: By using technique clled Pumping Lemm 8 Pumping Lemm (PL) Pumping Lemm (PL) Motivtion Motivtion Consider the lnguge L = * = {,,,, } The string is sid to e pumple in L ecuse cn tke the underlined portion, nd pump it up (i.e. repet) s much s desired while lwys getting elements in L. Consider the lnguge L = * = {,,,, } Which of the following re pumple?. 2. 3. A:. Pumple:,,,, etc. 2. Pumple: 9 3. not pumple ecuse most of * not in L 2 5
5//6 Pumping Lemm (PL) Pumping Lemm (PL) Motivtion Motivtion Define L 2 y the following utomton: Is pumple? A: Pumple:,. Underlined sustrings correspond to cycles in the FA! Let L3 = {,,, λ} Which strings re pumple? A: None! When pumping ny string non-trivilly, lwys result in infinitely mny possile strings. So no pumping cn go on inside finite set. Cycles in the FA cn e repeted ritrrily often, hence pumple. 2 Pumping Lemm give criterion for when strings cn e pumped. 22 Pumping Lemm (PL) Pumping Lemm (PL) Motivtion q q q2 q3, We hve: Becuse: L(M ) q q q q q q3 Motivtion Note, q q q q q q3 So, L(M ) L(M ) Also, q q q q q q3 So, We note tht: i j i, j Ν : ( ) ( ) L( M ) 23 24 6
5//6 Theorem Pumping Lemm (PL) Given n (infinite) regulr lnguge L, there is numer p (clled the pumping numer) such tht ny string in L of length p is pumple within its first p letters. In other words, for ll u L with u p we cn write: u = xyz (x is prefix, z is suffix) y (mid-portion y is non-empty) xy p (pumping occurs in first p letters) xy i z L for ll i (cn pump y-portion) To prove the Pumping Lemm we need to know the Pigeonhole Principle 25 Pigeonhole principle The pigeonhole principle is very simple, yet powerful method for identifying nonregulr lnguges. It sttes tht: given n ojects nd m oxes, if n>m then t lest one ox must hve more thn one oject. n ojects: n > m m oxes: Pumping Lemm (PL) This Box hs more thn one oject 26 Pumping Lemm (PL) Pigeonhole principle fundmentl oservtion Given sufficiently long string, the sttes of DFA must repet in n ccepting computtion. These cycles cn then e used to predict (generte) infinitely mny other strings in (of) the lnguge. Proof Now consider n ccepted string u. By ssumption L is regulr so let M e the FA ccepting it. Let p = Q = no. of sttes in M. Suppose u p. Pumping Lemm (PL) The pth leled y u visits p+ sttes in its first p letters. Thus (y pigeonhole principle) u must visit some stte twice. Pigeon-Hole Principle 27 The su-pth of u connecting the first nd second visit of the vertex is loop, nd gives the climed string y tht cn e pumped within the first p letters. 28 7
5//6 Notes: Pumping Lemm (PL) It is necessry condition. Every regulr lnguge stisfies it. If lnguge violtes it, it is not regulr. RL => PL not PL => not RL It is not sufficient condition. Not every non-regulr lnguge violtes it. not RL =>? PL or not PL (no conclusion) Notes: Pumping Lemm (PL) For ll sufficiently long strings (u) There exists non-null prefix (xy) nd sustring (y) For ll repetitions of the sustring (y), we get strings in the lnguge. u L : u k x,y,z : ( xyz = u) ( xy p) ( y ) i ( i : i xy z L) 29 3 Pumping Lemm (PL) Proving non-regulrity If there exists n ritrrily long string u L, nd for ech decomposition u = xyz, there exists n i such tht xy i z L, then L is non-regulr. Negtion of the necessry condition: u L : u p x,y,z : ( xyz = u) ( xy p) ( y ) i ( i : i xy z L) Proving non-regulrity Pumping Lemm (PL) In generl, to prove tht L isn t regulr:. Assume L were regulr 2. Therefore it hs pumping no. p 3. Find string pttern involving the length p in some clever wy, nd which cnnot e pumped. This is the hrd prt. 4. (2)àß(3) <contrdiction> Therefore our ssumption () ws wrong nd conclude tht L is not regulr lnguge 3 32 8
5//6 Exmple Pumping Lemm (PL) Show tht L={ n n n =,,2, } is not regulr.. Assume L were regulr 2. Therefore it hs pumping no. p, sy p=2 3. But consider the string u= 2 2. we hve u =4 > p=2, let x=, y=, nd z= è u=xyz, y, nd xy p(=2). PLè xy i z L for ll i Tking i=è xz L è L 4. <contrdiction> Therefore our ssumption () ws wrong nd conclude tht L is not regulr lnguge 33 Exercise Show tht the following lnguges re not regulr: L p = p { p is prime numer} L = c c { c is composite numer} L = { ω {, }* # ' s inω = # 's inω} L pl ={x * x =x R } Pumping Lemm (PL) 34 9