Properties of cfg

1,878 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,878
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
166
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Properties of cfg

  1. 1. Properties ofContext-Free languages
  2. 2. Union Context-free languages are closed under: UnionL1 is context free L1 L2L2 is context free is context-free
  3. 3. Example Language Grammar n n L1 {a b } S1 aS1b | R L2 {ww } S2 aS2a | bS2b | Union n n R S S1 | S2L {a b } {ww }
  4. 4. In general: For context-free languages L1, L2 with context-free grammars G1, G2 and start variables S1, S2 It is assumed that variable sets of G1 and G2 are disjoint The grammar of the union L1 L2 has new start variable S and additional production S S1 | S2
  5. 5. Concatenation Context-free languages are closed under: ConcatenationL1 is context free L1L2L2 is context free is context-free
  6. 6. ExampleLanguage Grammar n nL1 {a b } S1 aS1b | RL2 {ww } S2 aS2a | bS2b | Concatenation n n R S S1S2L {a b }{ww }
  7. 7. In general: For context-free languages L1, L2 with context-free grammars G1, G2 and start variables S1, S2 The grammar of the concatenation L1L2 has new start variable S and additional production S S1S2
  8. 8. Star Operation Context-free languages are closed under: Star-operationL is context free * L is context-free
  9. 9. Example Language Grammar n nL {a b } S aSb | Star Operation n nL {a b } * S1 SS1 |
  10. 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 variable S1 and additional production S1 SS1 |
  11. 11. Negative Properties of Context-Free Languages
  12. 12. Intersection Context-free languages are not closed under: intersectionL1 is context free L1 L2L2 is context free not necessarily context-free
  13. 13. Example n n m n m mL1 {a b c } L2 {a b c } Context-free: Context-free: S AC S AB A aAb | A aA | C cC | B bBc | Intersection n n nL1 L2 {a b c } NOT context-free
  14. 14. Complement Context-free languages are not closed under: complementL is context free L not necessarily context-free
  15. 15. Example n n m n m mL1 {a b c } L2 {a b c }Context-free: Context-free:S AC S ABA aAb | A aA |C cC | B bBc | Complement n n n L1 L2 L1 L2 {a b c } NOT context-free
  16. 16. Context-free languages andRegular Languages
  17. 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. 18. Machine M1 Machine M2NPDA for L1 DFA for L2 context-free regular Construct a new NPDA machine M that accepts L1 L2M simulates in parallel M1 and M 2
  19. 19. NPDA M1 DFA M2 a, b c aq1 q2 p1 p2 transition transition NPDA M q1, p1 a, b c q2 , p2 transition
  20. 20. NPDA M1 DFA M2 q0 p0initial state initial state NPDA M q0 , p0 Initial state
  21. 21. Therefore: L ( M1 ) L( M 2 ) is context-free (since M is NPDA) L1 L2 is context-free
  22. 22. The Pumping Lemma for Context-Free Languages
  23. 23. Take an infinite context-free language Generates an infinite number of different stringsExample: S AB A aBb B Sb B b
  24. 24. S AB A aBb B Sb B bA derivation: Variables are repeated S AB aBbB abbB abbSb abbABb abbaBbBb abbabbBb abbabbbb
  25. 25. Derivation tree string abbabbbb S A B a B b S b b A B a B b b b
  26. 26. Derivation tree string abbabbbb S A B a B b S b b A B a B b b repeated b
  27. 27. B Sb ABb B aBbBb aBbbb S b A B a B b b b B b
  28. 28. Repeated Part B S b A B a B b b B  aBbbb
  29. 29. Another possible derivation B S b A B B  aBbbb a B b b S b A B a B b bB  aBbbb aaBbbbbbb
  30. 30. S A Ba B b S b b A B B  aBbbb a B b b S  abbaBbbb
  31. 31. S A B a B b S b b A B B  aBbbb a B b b S b A B a B b bS  abbaBbbb  abbaaBbbbbbb
  32. 32. S A B a B b S b b A B a B b b S b A B a B b b B b bS  abbaaBbbbbbb abbaabbbbbbb
  33. 33. S  abbaabbbbbbbTherefore, the string abbaabbbbbbbis also generated by the grammar
  34. 34. We know: B b B  aBbbb S  abbaBbbbWe also know this string is generated: S  abbaBbbb abbaabbbb
  35. 35. We know: B b B  aBbbb S  abbaBbbbTherefore, this string is also generated: S  abbaBbbb abbaaBbbbbbb abbaabbbbbbb
  36. 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. 37. We know: B b B  aBbbb S  abbaBbbbTherefore, this string is also generated: S  abbaBbbb  i i abba(a ) B(bbb) bbb i i abba(a ) b(bbb) bbb
  38. 38. Therefore, knowing that abbabbbbis generated by grammar G,we also know that i i abba(a) b(bbb) bbb is generated by G
  39. 39. In general: We are given an infinite context-free grammar G Assume G has no unit-productions no -productions
  40. 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. 41. u , v, x, y, z : strings of terminalsString w uvxyz S u z Last repeated variable A v y repeated A x
  42. 42. SPossiblederivations: u z AS uAz v yA vAy AA x x
  43. 43. We know: S uAz A vAy A xThis string is also generated: * S uAz uxz 0 0 uv xy z
  44. 44. We know: S uAz A vAy A xThis string is also generated: * * S uAz uvAyz uvxyz 1 1 The original w uv xy z
  45. 45. We know: S uAz A vAy A xThis string is also generated: * * * S uAz uvAyz uvvAyyz uvvxyyz 2 2 uv xy z
  46. 46. We know: S uAz A vAy A xThis string is also generated: * * S uAz uvAyz uvvAyyz * * uvvvAyyyz uvvvxyyyz 3 3 uv xy z
  47. 47. We know: S uAz A vAy A xThis string is also generated: S * uAz * uvAyz * uvvAyyz * * uvvvAyyyz *  * uvvvvAy  yyyz * * uvvvvxy  yyyz i i uv xy z
  48. 48. Therefore, any string of the form i i uv xy z i 0is generated by the grammar G
  49. 49. Therefore, knowing that uvxyz L(G) i i we also know that uv xy z L(G )
  50. 50. S u z A v y A xObservation: | vxy | mSince A is the last repeated variable
  51. 51. S u z A v y A xObservation: | vy | 1Since there are no unit or productions
  52. 52. The Pumping Lemma: For infinite context-free language L there exists an integer m such that for any string w L, | w| m we can write w uvxyz with lengths | vxy | m and | vy | 1 and it must be: i i uv xy z L, for all i 0
  53. 53. Applications ofThe Pumping Lemma
  54. 54. Non-context free languages n n n {a b c : n 0} Context-free languages n n {a b : n 0}
  55. 55. Theorem: The language n n n L {a b c : n 0} is not context freeProof: Use the Pumping Lemma for context-free languages
  56. 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. 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 | m m m m We pick: w a b c
  58. 58. n n n L {a b c : n 0} m m m w a b cWe can write: w uvxyzwith lengths | vxy | m and | vy | 1
  59. 59. n n n L {a b c : n 0} m m m w a b c w uvxyz | vxy | m | vy | 1Pumping Lemma says: i i uv xy z L for all i 0
  60. 60. n n n L {a b c : n 0} m m m w a b c w uvxyz | vxy | m | vy | 1We examine all the possible locationsof string vxy in w
  61. 61. n n n L {a b c : n 0} m m m w a b c w uvxyz | vxy | m | vy | 1 mCase 1: vxy is within a m m m aaa...aaa bbb...bbb ccc...ccc u vxy z
  62. 62. n n n L {a b c : n 0} m m m w a b c w uvxyz | vxy | m | vy | 1Case 1: v and y consist from only a m m m aaa...aaa bbb...bbb ccc...ccc u vxy z
  63. 63. n n n L {a b c : n 0} m m m w a b c w uvxyz | vxy | m | vy | 1Case 1: Repeating v and y k 1 m k m m aaaaaa...aaaaaa bbb...bbb ccc...ccc u 2 2 z v xy
  64. 64. n n n L {a b c : n 0} m m m w a b c w uvxyz | vxy | m | vy | 1 2 2Case 1: From Pumping Lemma: uv xy z L k 1 m k m m aaaaaa...aaaaaa bbb...bbb ccc...ccc u 2 2 z v xy
  65. 65. n n n L {a b c : n 0} m m m w a b c w uvxyz | vxy | m | vy | 1 2 2Case 1: From Pumping Lemma: uv xy z L k 1 2 2 m k m m However: uv xy z a b c L Contradiction!!!
  66. 66. n n n L {a b c : n 0} m m m w a b c w uvxyz | vxy | m | vy | 1 mCase 2: vxy is within b m m m aaa...aaa bbb...bbb ccc...ccc u vxy z
  67. 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 m aaa...aaa bbb...bbb ccc...ccc u vxy z
  68. 68. n n n L {a b c : n 0} m m m w a b c w uvxyz | vxy | m | vy | 1 mCase 3: vxy is within c m m m aaa...aaa bbb...bbb ccc...ccc u vxy z
  69. 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 m aaa...aaa bbb...bbb ccc...ccc u vxy z
  70. 70. n n n L {a b c : n 0} m m m w a b c w uvxyz | vxy | m | vy | 1 m mCase 4: vxy overlaps a and b m m m aaa...aaa bbb...bbb ccc...ccc u vxy z
  71. 71. n n n L {a b c : n 0} m m m w a b c w uvxyz | vxy | m | vy | 1Case 4: Possibility 1: v contains only a y contains only b m m m aaa...aaa bbb...bbb ccc...ccc u vxy z
  72. 72. n n n L {a b c : n 0} m m m w a b c w uvxyz | vxy | m | vy | 1Case 4: Possibility 1: v contains only a k1 k2 1 y contains only b m k1 m k2 m aaa...aaaaaaa bbbbbbb...bbb ccc...ccc u 2 2 z v xy
  73. 73. 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 z L k1 k2 1 m k1 m k2 m aaa...aaaaaaa bbbbbbb...bbb ccc...ccc u 2 2 z v xy
  74. 74. 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 z L k1 k2 1 2 2 m k1 m k2 m However: uv xy z a b c L Contradiction!!!
  75. 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 b y contains only b m m m aaa...aaa bbb...bbb ccc...ccc u vxy z
  76. 76. 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 b k1 k2 k 1 y contains only b m k1 k 2 m k maaa...aaaaabbaabb bbbbbbb...bbb ccc...cccu 2 v xy 2 z
  77. 77. 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 z L k1 k2 k 1 m k1 k 2 m k maaa...aaaaabbaabb bbbbbbb...bbb ccc...cccu 2 v xy 2 z
  78. 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 z L However: k1 k2 k 1 2 2 m k1 k2 m k m uv xy z a b a b c L Contradiction!!!
  79. 79. n n n L {a b c : n 0} m m m w a b c w uvxyz | vxy | m | vy | 1Case 4: Possibility 3: v contains only a y contains a and b m m m aaa...aaa bbb...bbb ccc...ccc u vxy z
  80. 80. n n n L {a b c : n 0} m m m w a b c w uvxyz | vxy | m | vy | 1Case 4: Possibility 3: v contains only a y contains a and b Similar analysis with Possibility 2
  81. 81. n n n L {a b c : n 0} m m m w a b c w uvxyz | vxy | m | vy | 1 m mCase 5: vxy overlaps b and c m m m aaa...aaa bbb...bbb ccc...ccc u vxy z
  82. 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 m aaa...aaa bbb...bbb ccc...ccc u vxy z
  83. 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. 84. In all cases we obtained a contradictionTherefore: The original assumption that n n n L {a b c : n 0} is context-free must be wrongConclusion: L is not context-free

×