Properties of cfg

Embed Size (px)

DESCRIPTION

 

Citation preview

  • 1. Properties ofContext-Free languages

2. Union Context-free languages are closed under: UnionL1 is context free L1 L2L2 is context free is context-free 3. Example LanguageGrammarn n L1 {a b }S1aS1b |R L2 {ww }S2aS2a | bS2b | Unionn nR S S1 | S2L {a b } {ww } 4. In general: For context-free languagesL1,L2 with context-free grammarsG1,G2 and start variables S1,S2 It is assumed that variable sets of G1 and G2 are disjointThe grammar of the unionL1 L2has new start variableSand additional production SS1 | S2 5. ConcatenationContext-free languagesare closed under:ConcatenationL1 is context freeL1L2L2 is context freeis context-free 6. ExampleLanguage Grammar n nL1 {a b } S1 aS1b | RL2 {ww } S2aS2a | bS2b | Concatenationn n RS S1S2L {a b }{ww } 7. In general: For context-free languages L1, L2 with context-free grammars G1, G2 and start variablesS1, S2 The grammar of the concatenation L1L2 has new start variableS and additional production SS1S2 8. Star OperationContext-free languagesare closed under:Star-operationL is context free * L is context-free 9. Example Language Grammar n nL {a b } SaSb |Star Operation n nL {a b } * S1 SS1 | 10. In general: For context-free language L with context-free grammar G and start variable S The grammar of the star operation L * has new start variableS1 and additional production S1 SS1 | 11. Negative PropertiesofContext-Free Languages 12. Intersection Context-free languages are not closed under:intersectionL1 is context free L1L2L2 is context free not necessarily context-free 13. Example n n m n m mL1 {a b c }L2 {a b c } Context-free: Context-free: SAC SAB AaAb |AaA | CcC | BbBc |Intersectionn n nL1 L2 {a b c } NOT context-free 14. Complement Context-free languages are not closed under:complementL is context freeL not necessarily context-free 15. Examplen n mn m mL1 {a b c } L2 {a b c }Context-free: Context-free:SAC SABAaAb |AaA |CcC | BbBc | Complementn n nL1L2L1 L2 {a b c }NOT context-free 16. Context-freelanguages andRegular Languages 17. The intersection of a context-free language and a regular language is a context-free languageL1 context free L1 L2L2 regular context-free 18. Machine M1Machine M2NPDA for L1DFA forL2 context-free regularConstruct a new NPDA machine Mthat accepts L1 L2M simulates in parallel M1 and M 2 19. NPDAM1 DFA M2a, b caq1q2 p1p2 transition transition NPDAM q1, p1 a, b cq2 , p2 transition 20. NPDAM1DFAM2 q0 p0initial state initial stateNPDAMq0 , p0Initial state 21. Therefore: L ( M1 )L( M 2 ) is context-free(since M is NPDA) L1 L2 is context-free 22. The Pumping LemmaforContext-Free Languages 23. Take an infinite context-free language Generates an infinite number of different stringsExample:S ABA aBbB SbB b 24. SAB AaBb BSb BbA derivation: Variables are repeatedS AB aBbB abbBabbSb abbABb abbaBbBbabbabbBb abbabbbb 25. Derivation tree string abbabbbbSAB aBbSbb ABa B bbb 26. Derivation tree string abbabbbbSAB aBbSbb ABa B bb repeated b 27. B Sb ABbBaBbBbaBbbbS bA B aB b bbBb 28. Repeated Part BS bA Ba B b bB aBbbb 29. Another possible derivation BS bA B B aBbbba Bb b Sb ABaB bbBaBbbb aaBbbbbbb 30. SA Ba B b S bb A BB aBbbba B b bS abbaBbbb 31. SA Ba B b S bb A BBaBbbba B b bS bA Ba B b bS abbaBbbb abbaaBbbbbbb 32. SA Ba B b S bb A Ba B b bS bA Ba B b b B bbS abbaaBbbbbbbabbaabbbbbbb 33. S abbaabbbbbbbTherefore, the string abbaabbbbbbbis also generated by the grammar 34. We know: Bb BaBbbb S abbaBbbbWe also know this string is generated: S abbaBbbb abbaabbbb 35. We know: Bb B aBbbb S abbaBbbbTherefore, this string is also generated: S abbaBbbb abbaaBbbbbbb abbaabbbbbbb 36. We know: B b B aBbbb S abbaBbbbTherefore, this string is also generated: S abbaBbbb abba(a ) B(bbb)bbb 2 2 abba(a ) B(bbb) bbb 2 2 abba(a ) b(bbb) bbb 37. We know:B bB aBbbb S abbaBbbbTherefore, this string is also generated: S abbaBbbbi iabba(a ) B(bbb) bbbi iabba(a ) b(bbb) bbb 38. Therefore, knowing that abbabbbbis generated by grammar G,we also know that ii abba(a) b(bbb) bbb is generated by G 39. In general:We are given an infinitecontext-free grammar G Assume G has no unit-productionsno -productions 40. Take a string w L (G )with length bigger thanm > (Number of productions) x(Largest right side of a production)Consequence: Some variable must be repeated in the derivation of w 41. u , v, x, y, z : strings of terminalsString w uvxyz S uz Last repeated variableA vy repeated A x 42. SPossiblederivations: u z ASuAz v yA vAyAAx x 43. We know: SuAzAvAy A xThis string is also generated:* SuAz uxz 00 uv xy z 44. We know: SuAz AvAyA xThis string is also generated:* * SuAz uvAyz uvxyz1 1 The original w uv xy z 45. We know:SuAzAvAy A xThis string is also generated:* * *S uAz uvAyz uvvAyyz uvvxyyz22 uv xy z 46. We know: S uAz AvAy A xThis string is also generated: ** SuAzuvAyzuvvAyyz * * uvvvAyyyz uvvvxyyyz 33uv xy z 47. We know: SuAzA vAy A xThis string is also generated: S * uAz * uvAyz * uvvAyyz * * uvvvAyyyz * * uvvvvAy yyyz * * uvvvvxy yyyz i i uv xy z 48. Therefore, any string of the formi i uv xy z i 0is generated by the grammar G 49. Therefore, knowing that uvxyz L(G)i i we also know that uv xy z L(G ) 50. S u z A v y A xObservation: | vxy | mSince A is the last repeated variable 51. Suz Avy A xObservation:| vy | 1Since there are no unit or productions 52. The Pumping Lemma:For infinite context-free languageL there exists an integer m such that for any stringw L,| w| m we can write w uvxyz with lengths | vxy | m and | vy | 1 and it must be:i iuv xy z L, for all i 0 53. Applications ofThe PumpingLemma 54. Non-context free languages n n n{a b c : n 0}Context-free languages n n {a b : n 0} 55. Theorem: The languagen n nL {a b c : n 0}is not context freeProof: Use the Pumping Lemma for context-free languages 56. n n n L {a b c : n 0}Assume for contradiction that Lis context-freeSince L is context-free and infinitewe can apply the pumping lemma 57. n n n L {a b c : n 0}Pumping Lemma gives a magic number msuch that:Pick any string w L with length | w | mm m mWe pick: w a b c 58. n n nL {a b c : n 0}m m mw a b cWe can write:w uvxyzwith lengths| vxy | m and | vy | 1 59. n n n L {a b c : n 0} m m mw a b cw uvxyz| vxy | m| vy | 1Pumping Lemma says: i i uv xy z Lfor all i 0 60. n n n L {a b c : n 0}m m mw a b cw uvxyz| vxy | m| vy | 1We examine all the possible locationsof string vxy in w 61. n n nL {a b c : n 0}m m mw a b cw uvxyz | vxy | m | vy | 1mCase 1:vxy is within am m m aaa...aaa bbb...bbb ccc...ccc u vxyz 62. n n nL {a b c : n 0} m m mw a b cw uvxyz | vxy | m | vy | 1Case 1: v and y consist from only am m m aaa...aaa bbb...bbb ccc...cccu vxy z 63. n n nL {a b c : n 0} m m mw a b cw uvxyz | vxy | m | vy | 1Case 1: Repeating v and yk 1 m k mm aaaaaa...aaaaaa bbb...bbb ccc...cccu 2 2 z v xy 64. n n n L {a b c : n 0}m m mw a b cw uvxyz | vxy | m| vy | 1 2 2Case 1: From Pumping Lemma: uv xy z L k 1m k mmaaaaaa...aaaaaa bbb...bbb ccc...cccu22 z v xy 65. n n nL {a b c : n 0} m m mw a b cw uvxyz| vxy | m| vy | 122Case 1: From Pumping Lemma: uv xy zL k 1 22m k m m However:uv xy z a b c LContradiction!!! 66. n n nL {a b c : n 0}m m mw a b cw uvxyz | vxy | m | vy | 1mCase 2:vxy is within bm m m aaa...aaa bbb...bbb ccc...cccuvxyz 67. n n n L {a b c : n 0}m m m w a b c w uvxyz | vxy | m | vy | 1Case 2: Similar analysis with case 1 m m maaa...aaa bbb...bbb ccc...cccu vxyz 68. n n nL {a b c : n 0}m m mw a b cw uvxyz | vxy | m | vy | 1mCase 3:vxy is within cm m m aaa...aaa bbb...bbb ccc...ccc u vxy z 69. n n n L {a b c : n 0}m m m w a b c w uvxyz| vxy | m | vy | 1Case 3: Similar analysis with case 1 m m maaa...aaa bbb...bbb ccc...cccu vxy z 70. n n nL {a b c : n 0}m m mw a b cw uvxyz | vxy | m | vy | 1m mCase 4:vxy overlaps a and bm m m aaa...aaa bbb...bbb ccc...cccu vxy z 71. n n nL {a b c : n 0} m m m w a b c w uvxyz | vxy | m | vy | 1Case 4: Possibility 1: v contains only ay contains only b m m maaa...aaa bbb...bbb ccc...ccc u vxy z 72. n n nL {a b c : n 0} m m mw a b cw uvxyz| vxy | m | vy | 1Case 4: Possibility 1: v contains only ak1 k2 1 y contains only bm k1m k2 m aaa...aaaaaaa bbbbbbb...bbb ccc...cccu2 2zv xy 73. n n n L {a b c : n 0}m m m w a b c w uvxyz| vxy | m | vy | 12 2Case 4: From Pumping Lemma: uv xy z Lk1 k2 1m k1m k2 m aaa...aaaaaaa bbbbbbb...bbb ccc...ccc u2 2 z v xy 74. n n nL {a b c : n 0}m m m w a b c w uvxyz| vxy | m | vy | 12 2Case 4: From Pumping Lemma: uv xy zLk1 k2 12 2m k1 m k2 m However: uv xy za bcL Contradiction!!! 75. n n n L {a b c : n 0} m m m w a b c w uvxyz | vxy | m | vy | 1Case 4: Possibility 2: v contains a and by contains only b m m maaa...aaa bbb...bbb ccc...ccc u vxy z 76. n n n L {a b c : n 0} m m mw a b cw uvxyz| vxy | m | vy | 1Case 4: Possibility 2: v contains a and b k1 k2 k 1y contains only b m k1 k 2m kmaaa...aaaaabbaabb bbbbbbb...bbb ccc...cccu 2v xy 2 z 77. n n n L {a b c : n 0}m m mw a b cw uvxyz| vxy | m | vy | 1 2 2Case 4: From Pumping Lemma: uv xy zL k1 k2 k 1 m k1 k 2m k maaa...aaaaabbaabb bbbbbbb...bbb ccc...cccu2v xy 2 z 78. n n n L {a b c : n 0} m m m w a b c w uvxyz| vxy | m| vy | 1 2 2Case 4: From Pumping Lemma: uv xy zL However: k1 k2 k 1 2 2m k1 k2 m k m uv xy z a b a b c LContradiction!!! 79. n n nL {a b c : n 0} m m m w a b c w uvxyz | vxy | m| vy | 1Case 4: Possibility 3: v contains only ay contains a and b m m maaa...aaa bbb...bbb ccc...ccc u vxy z 80. n n nL {a b c : n 0} m m m w a b c w uvxyz| vxy | m | vy | 1Case 4: Possibility 3: v contains only ay contains a and bSimilar analysis with Possibility 2 81. n n nL {a b c : n 0}m m mw a b cw uvxyz | vxy | m | vy | 1m mCase 5:vxy overlaps b and cm m m aaa...aaa bbb...bbb ccc...cccuvxyz 82. n n n L {a b c : n 0}m m m w a b c w uvxyz | vxy | m | vy | 1Case 5: Similar analysis with case 4 m m maaa...aaa bbb...bbb ccc...ccc uvxyz 83. There are no other cases to consider(since | vxy | m , string vxy cannot m m m overlap a , b and c at the same time) 84. In all cases we obtained a contradictionTherefore: The original assumption thatn n nL {a b c : n 0}is context-free must be wrongConclusion: L is not context-free