Lecture 12 Examples of CFLSlide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Lecture 12 Examples of CFLExample 1. L={0 1 | n > 0} is a CFL.n nTo show that L is a CFL, we need to construct aCFG G generating L. Let G = (V, Σ, R, S) where V = {S} Σ = {0, 1} R ={S → ε|0S1 }Example 2. Construct CFG to generate L={xx | x is in (0+1)*}.RG = (V, Σ, R, S) whereV = {S}Σ = {0, 1}R = { S → ε | 0S0 | 1S1}Example 3 L={ x (0+1)* | #0(x) = #1(x)}Idea: what relations exist between longer strings and shorter strings in this language?Consider four cases:Case 1. x = 0w1. Then x is in L iff w is in L.If we use S to represent a string in L, then the relation in Case 1 can be representedAs a rule S → 0S1Case 2. x=1w0. Similar to case 1. This casegives rule S → 1S0Case 3. x=0w0. Suppose w = w1w2···wn. Consider the following sequence: #0(0) - #1(0) > 0, #0(0w1) - #1(0w1), …, #0(0w1···wn) - #1 (0w1···wn) < 0.Note that in this sequence, two adjacent numbersHave difference 1. Therefore, there exists I such that #0(0w1···wi) - #1 (0w1···wi) = 0This means that x is the concatenation of two shorterstrings in L. So, we have a rule S → SSCase 4. x=1w1. Similar to Case 3.Based on the above analysis, we have CFGG=(V, Σ, R, S) whereV = {S}Σ = {0, 1}R = {S → ε | 0S1 | 1S0 | SS}Each nonterminal symbol represents a language.Each rule represents a relationship between languages represented by nonterminal symbols.CFL is closed under union.Proof. Suppose A = L(GA) and B = L(GB) where GA = (VA, ΣA, RA, SA) GB = (VB, ΣB, RB, SB)Without loss of generality, assume VA ∩ VB= Ǿ.(Otherwise, we may change some nonterminal symbols.)Then A U B = L(G) for G=(V, Σ, R, S) whereV = VA U VB U {S}Σ = ΣA U ΣBR = RA U RB U {S → SA | SB }Example 4 L = { 0 1 | m ≠ n, m, n > 0} L = {0 1 | m > n > 0} U {0 1 | n > m > 0}Hence, L = L(G) for G = ({S, SA, SB}, {0,1}, R, S)whereR = {S → SA | SB, SA → 0 | 0SA | 0SA1, SB →1 | SB1 | 0SB1}m nm nmnCFL is closed under concatenation.Proof. Suppose A = L(GA) and B = L(GB) where GA = (VA, ΣA, RA, SA) GB = (VB, ΣB, RB, SB)Without loss of generality, assume VA ∩ VB= Ǿ.(Otherwise, we may change some nonterminal symbols.)Then AB = L(G) for G=(V, Σ, R, S) whereV = VA U VB U {S}Σ = ΣA U ΣBR = RA U RB U {S → SASB }Example 5 L = {xx w | x (0+1), w (0+1)*}R+L = {xx | x (0+1) }{0,1}*R+L=L(G) for G = ({S, SA, SB}, {0, 1}, R, S)whereR = { S → SASB, SA→ 00 | 11 | 0SA0 | 1SA1, SB→ ε | 0SB | 1SB }CFL is closed under star-closure.Proof. Suppose L = (G) for G=(V, Σ, R, S). Then L* = L(G*) for G* =(V, Σ, R*, S) where R* = R U { S → ε | SS}. .* So,.*|*Then .* represents * and represents SSSSSLSLSExample 6 L=(0+1)*00L=L(G) for G=({S, A}, {0,1}, R, S)whereR={S → A00, A → ε | AA | 0 | 1 }The role of nonterminat symbol.Every nonterminal symbol A represents a language which can be generated byusing A as start symbol.Example 7. L={a b c d | m+n = p+q, m, n, p, q > 0}mnp qLet S represent L, A represent {b c | n > 0} B represent {a b c | m+n = p, m, n, p > 0} C represent {b c d | n = p+q, n, p, q > 0}nmnnnppqThen we can find relations S → aSd | B | C, B → aBc | A, C → bCd | A, A → bAc | ε.Example 8 L ={x (0+1)*| x ≠ ww for any w (0+1)*}Let us analyze what would happens for x in L.Case 1. |x| = odd.In this case, x is either in language A = {u0v | |u|=|v|, u, v (0+1)*}or B = {u1v | |u|=|v|, u, v (0+1)*}. Case 2. |x| = even. Write x = x1 x2 … xm y1 y2 …ym . There exists i such that xi ≠ yi .Subcase 2.1 x = x1… xi-10xi+1 …xmy1…yi-11yi+1…ym.x is in ABSubcase 2.2 x = x1… xi-11xi+1 …xmy1…yi-10yi+1…ym.x is in BA Thus, L = A+B+AB+BA. So, L=L(G) for G=({L,A,B}, {0,1}, R, L) where R ={ L → A | B | AB | BA, A → 0 | 0A0 | 0A1 | 1A0 | 1A1, B → 1 | 0B0 | 0B1 | 1B0 |
View Full Document