# Properties of Context-Free Languages - PowerPoint PPT Presentation

Properties of Context-Free Languages

1 / 34
Properties of Context-Free Languages

## Properties of Context-Free Languages

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. CS 3240 – Chapter 8 Properties of Context-Free Languages

2. Query • Is anbncn context-free? CS 3240 - Properties of Context-Free Languages

3. Non-Context-Free Languages • anbncn is not context-free • Neither is ww • although wwR is! • We will develop a pumping lemma for context-free languages • (oh joy! :-) • as before, can only be used to show that a language is not CF CS 3240 - Properties of Context-Free Languages

4. Properties of Infinite CF Languages • How can you tell by looking at a CFG whether its language is infinite or not? CS 3240 - Properties of Context-Free Languages

5. An Infinite CFL • S → aaB • A → bBb | λ • B → Aa • Consider the derivation:S ⇒ aaB ⇒aaAa ⇒ aabBba ⇒ aabAaba⇒ aabbBbaba ⇒ aabbAababa … CS 3240 - Properties of Context-Free Languages

6. Infinite CFLs • Grammars for infinite CFL must reuse a variable in some derivation • S ⇒* uAz ⇒* uvAyz • That is, A ⇒* vAy • u,v,y,z,are derivable strings of characters and variables • We can repeat the same choices for A again: • S ⇒* uAz ⇒* uvAyz ⇒* uvvAyyz • And again and again… (finally stopping with x) • S ⇒*uvnAynz ⇒* uvnxynz • So for any sufficiently long string, s, we have • s = uvnxynz ⇒ CS 3240 - Properties of Context-Free Languages

7. What Will Force a Variable Repeat? • Hint: think of derivation trees, and the relationship between the depth and the number of leaves in a tree CS 3240 - Properties of Context-Free Languages

8. Reminder: Binary Trees • If a complete binary has depth d, how many leaves does it have? CS 3240 - Properties of Context-Free Languages

9. And My Point? • Consider a path from the root of a tree (S) to a leaf. • It is all variables, except for the leaf • The longer the string, the deeper the path • Eventually a variable must be repeated! CS 3240 - Properties of Context-Free Languages

10. The Pumping Lemma for CFLs • Based on a repeated variable (a type of loop) • For sufficiently-long strings (≥ p = 2v), some variable will be a descendant of itself in the parse • Every string of sufficient length from an infinite CFL can be written as uvxyz, and pumped as uvixyiz, i ≥ 0: • |v| + |y| > 0 • |vxy| <= p

11. anbnan is not context-free • Intuitively: We’ve already used up the stack to coordinate the anbn prefix • Must consider all cases for a proof • CFLPL-8.PDF

12. ww is not Context Free • Use the pumping lemma with apbpapbp

13. Closure Properties of CFLsSection 8.2 • (N)CFLs are closed under: • Union • Concatenation • Kleene Star • “Regular Intersection” (CF ∩ R = CF) • (N)CFLs are not closed under: • intersection • complement CS 3240 - Properties of Context-Free Languages

14. Union of CFLs • Let S1 be the start symbol for L1, and S2 for L2 • Just have a new start symbol point to the OR of the old ones: • S => S1 | S2S1 => …S2 => …

15. Concatenation of CFLs • S => S1S2S1 => …S2 => …

16. Kleene Star of CFLs • Rename the old start variable to S1 • S => S1S | λS1 => …

17. Intersection of CFLs • Let L1 = anbncm • The concatenation of anbnand cm • Let L2 = anbmcm • The concatenation of an and bmcm • These are both context-free • L1∩ L2 = anbncn • We already showed this is not context free

18. “Regular” Intersection • Let R be a regular language and L a context-free language • The R∩Lis context-free • Why? CS 3240 - Properties of Context-Free Languages

19. Regular Intersection Exampleanbn ∩ EVEN-EVEN CS 3240 - Properties of Context-Free Languages

20. Combine the TablesIn the Usual Way CS 3240 - Properties of Context-Free Languages

21. Resulting Intersection jail CS 3240 - Properties of Context-Free Languages

22. Complement of CFLs • Proof by contradiction, derived from the formula for intersection:L1∩ L2 = (L1' + L2')'Since the intersection is not closed, but union is, then the complement cannot be. • (Otherwise we could compute the intersection, which in general is not CF)

23. Complement of CFLsAnother Look • Non-determinism is the problem • Remember NFAs? • To find the complement, we needed to first convert to a DFA, then flip the states • Since some CFLs are inherently non-deterministic, they have no deterministic equivalent to “flip” • But… what does this say about deterministic CFLs? CS 3240 - Properties of Context-Free Languages

24. Closure Properties of DCFLs • DCFLs are closed under: • Complement! • Concatenation • Kleene Star • “Regular Intersection” (CF ∩ R = CF) • DCFLs are not closed under: • Intersection • Union! CS 3240 - Properties of Context-Free Languages

25. DCFLs not Closed under Union! • Consider:L1 = {aibjck | i = j}L2 = {aibjck | j = k} • Each of these is DCF • (Easy to show – your 7.1 homework was similar) • The union is not! • It requires non-determinism • It’s still context-free, but not Deterministic CF

26. Another Interesting FactFYI • DCFLs always have an associated CFG that is unambiguous

27. Closure Properties of CFLsSummary • Closed under Union, Concatenation, Kleene Star • Not closed under intersection, complement • CFL ∩ Regular = CFL • DCFLs are closed under complement • But not union! • Swap those two