Download presentation
Presentation is loading. Please wait.
Published byNorah Robertson Modified over 9 years ago
1
CS 3240 – Chapter 8
2
Is a n b n c n context-free? CS 3240 - Properties of Context-Free Languages2
3
a n b n c n is not context-free Neither is ww although ww R 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 Languages3
4
How can you tell by looking at a CFG whether its language is infinite or not? CS 3240 - Properties of Context-Free Languages4
5
S → aaB A → bBb | λ B → Aa Consider the derivation: S ⇒ aaB ⇒ aaAa ⇒ aabBba ⇒ aabAaba ⇒ aabbBbaba ⇒ aabbAababa … CS 3240 - Properties of Context-Free Languages5
6
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 ⇒ * uv n Ay n z ⇒ * uv n xy n z So for any sufficiently long string, s, we have s = uv n xy n z CS 3240 - Properties of Context-Free Languages6 ⇒
7
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 Languages7
8
If a complete binary has depth d, how many leaves does it have? CS 3240 - Properties of Context-Free Languages8
9
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 Languages9
10
Based on a repeated variable (a type of loop) For sufficiently-long strings (≥ p = 2 v ), 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 uv i xy i z, i ≥ 0: |v| + |y| > 0 |vxy| <= p
11
Intuitively: We’ve already used up the stack to coordinate the a n b n prefix Must consider all cases for a proof CFLPL-8.PDF CFLPL-8.PDF
12
Use the pumping lemma with a p b p a p b p
13
(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 Languages13
14
Let S 1 be the start symbol for L 1, and S 2 for L 2 Just have a new start symbol point to the OR of the old ones: S => S 1 | S 2 S 1 => … S 2 => …
15
S => S 1 S 2 S 1 => … S 2 => …
16
Rename the old start variable to S 1 S => S 1 S | λ S 1 => …
17
Let L 1 = a n b n c m The concatenation of a n b n and c m Let L 2 = a n b m c m The concatenation of a n and b m c m These are both context-free L 1 ∩ L 2 = a n b n c n We already showed this is not context free
18
Let R be a regular language and L a context- free language The R ∩ L is context-free Why? CS 3240 - Properties of Context-Free Languages18
19
CS 3240 - Properties of Context-Free Languages19 a,λb,X ± ss/Xf/λ + fΦf/λ ab ± ABC BAD CDA DCB
20
CS 3240 - Properties of Context-Free Languages20 a,λb,X ± sAs/X; Bf/λ; C sBs/X; Af/λ; D fC; Df/λ; A fD; Cf/λ; B + fA; Bf/λ; C fB; Af/λ; D
21
CS 3240 - Properties of Context-Free Languages21 jail
22
Proof by contradiction, derived from the formula for intersection: L 1 ∩ L 2 = (L 1 ' + L 2 ')' 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
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 Languages23
24
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 Languages24
25
Consider: L 1 = {a i b j c k | i = j} L 2 = {a i b j c k | 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
DCFLs always have an associated CFG that is unambiguous
27
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
28
Unanswerable questions Answerable questions
29
Do 2 arbitrary CFGs generate the same language? Is a CFG ambiguous? Is a given NCFL’s complement also CF? Is the intersection of 2 given CFLs CF? Do 2 CFLs have a common word?
30
Is a non-terminal ever used in a productive derivation? Draw the connectivity graph ✔ Does a CFG generate any words? Substitute each “terminating production” (RHS is all terminals) throughout and see what happens ▪ “back substitution method” Is a CFL finite or infinite? Procedure to detect useful, repeated variables
31
CS 3240 - Properties of Context-Free Languages31 S → aA | bB | λ A → a | aCA | bDA | bBa | aAa B → b | aAb | aCB | bDB | bBb C → aCC | bDC D → aCD | bDD First remove useless variables…
32
CS 3240 - Properties of Context-Free Languages32 S → aA | bB | λ A → a | bBa | aAa B → b | aAb | bBb Pick a non-empty, terminal rule: A → a Back-substitute that rule: S → aa | bB | λ A → a | bBa | aaa B → b | aab | bBb Keep going until we have S →, or we can’t continue. We have S → aa. STOP.
33
CS 3240 - Properties of Context-Free Languages33 S → aA | bB | λ A → a | bBa | aAa B → b | aAb | bBb All variables are useful. Let’s see if A is repeated, for instance. First, mark all A’s on the right: S → aA | bB | λ A → a | bBa | aAa B → b | aAb | bBb Now mark all variables affected on the left: S → aA | bB | λ A → a | bBa | aAa B → b | aAb | bBb Since A was marked on the left, it is repeated.
34
CS 3240 - Properties of Context-Free Languages34 S ➞ aA | SBMark on left: A ➞ baB | λ B ➞ bB | bAS ➞ aA | SB A ➞ baB | λ Mark A’s on right:B ➞ bB | bA S ➞ aA | SBA was marked on left. DONE. A ➞ baB | λ B ➞ bB | bA Now mark corresponding variables on left: S ➞ aA | SB A ➞ baB | λ B ➞ bB | bA Repeat marking on right: S ➞ aA | SB A ➞ baB | λ B ➞ bB | bA
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.