34
CS 3240 – Chapter 8

CS 3240 – Chapter 8. Is a n b n c n context-free? CS 3240 - Properties of Context-Free Languages2

Embed Size (px)

Citation preview

Page 1: CS 3240 – Chapter 8.  Is a n b n c n context-free? CS 3240 - Properties of Context-Free Languages2

CS 3240 – Chapter 8

Page 2: CS 3240 – Chapter 8.  Is a n b n c n context-free? CS 3240 - Properties of Context-Free Languages2

Is anbncn context-free?

CS 3240 - Properties of Context-Free Languages 2

Page 3: CS 3240 – Chapter 8.  Is a n b n c n context-free? CS 3240 - Properties of Context-Free Languages2

anbncn is not context-freeNeither 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 3

Page 4: CS 3240 – Chapter 8.  Is a n b n c n context-free? CS 3240 - Properties of Context-Free Languages2

How can you tell by looking at a CFG whether its language is infinite or not?

CS 3240 - Properties of Context-Free Languages 4

Page 5: CS 3240 – Chapter 8.  Is a n b n c n context-free? CS 3240 - Properties of Context-Free Languages2

S → aaBA → bBb | λB → Aa

Consider the derivation:S ⇒ aaB ⇒aaAa ⇒ aabBba ⇒ aabAaba⇒ aabbBbaba ⇒ aabbAababa …

CS 3240 - Properties of Context-Free Languages 5

Page 6: CS 3240 – Chapter 8.  Is a n b n c n context-free? CS 3240 - Properties of Context-Free Languages2

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 6

Page 7: CS 3240 – Chapter 8.  Is a n b n c n context-free? CS 3240 - Properties of Context-Free Languages2

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 7

Page 8: CS 3240 – Chapter 8.  Is a n b n c n context-free? CS 3240 - Properties of Context-Free Languages2

If a complete binary has depth d, how many leaves does it have?

CS 3240 - Properties of Context-Free Languages 8

Page 9: CS 3240 – Chapter 8.  Is a n b n c n context-free? CS 3240 - Properties of Context-Free Languages2

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 9

Page 10: CS 3240 – Chapter 8.  Is a n b n c n context-free? CS 3240 - Properties of Context-Free Languages2

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

Page 11: CS 3240 – Chapter 8.  Is a n b n c n context-free? CS 3240 - Properties of Context-Free Languages2

Intuitively: We’ve already used up the stack to coordinate the anbn prefix

Must consider all cases for a proof CFLPL-8.PDF

Page 12: CS 3240 – Chapter 8.  Is a n b n c n context-free? CS 3240 - Properties of Context-Free Languages2

Use the pumping lemma with apbpapbp

Page 13: CS 3240 – Chapter 8.  Is a n b n c n context-free? CS 3240 - Properties of Context-Free Languages2

(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 13

Page 14: CS 3240 – Chapter 8.  Is a n b n c n context-free? CS 3240 - Properties of Context-Free Languages2

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 | S2

S1 => …S2 => …

Page 15: CS 3240 – Chapter 8.  Is a n b n c n context-free? CS 3240 - Properties of Context-Free Languages2

S => S1S2

S1 => …S2 => …

Page 16: CS 3240 – Chapter 8.  Is a n b n c n context-free? CS 3240 - Properties of Context-Free Languages2

Rename the old start variable to S1

S => S1S | λS1 => …

Page 17: CS 3240 – Chapter 8.  Is a n b n c n context-free? CS 3240 - Properties of Context-Free Languages2

Let L1 = anbncm

The concatenation of anbn and cm

Let L2 = anbmcm

The concatenation of an and bmcm

These are both context-freeL1 ∩ L2 = anbncn

We already showed this is not context free

Page 18: CS 3240 – Chapter 8.  Is a n b n c n context-free? CS 3240 - Properties of Context-Free Languages2

Let R be a regular language and L a context-free language

The R∩L is context-freeWhy?

CS 3240 - Properties of Context-Free Languages 18

Page 19: CS 3240 – Chapter 8.  Is a n b n c n context-free? CS 3240 - Properties of Context-Free Languages2

CS 3240 - Properties of Context-Free Languages 19

a,λ b,X

± s s/X f/λ

+ f Φ f/λ

a b

± A B C

B A D

C D A

D C B

Page 20: CS 3240 – Chapter 8.  Is a n b n c n context-free? CS 3240 - Properties of Context-Free Languages2

CS 3240 - Properties of Context-Free Languages 20

a,λ b,X

± sA s/X; B f/λ; C

sB s/X; A f/λ; D

fC ; D f/λ; A

fD ; C f/λ; B

+ fA ; B f/λ; C

fB ; A f/λ; D

Page 21: CS 3240 – Chapter 8.  Is a n b n c n context-free? CS 3240 - Properties of Context-Free Languages2

CS 3240 - Properties of Context-Free Languages 21

jail

Page 22: CS 3240 – Chapter 8.  Is a n b n c n context-free? CS 3240 - Properties of Context-Free Languages2

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)

Page 23: CS 3240 – Chapter 8.  Is a n b n c n context-free? CS 3240 - Properties of Context-Free Languages2

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 23

Page 24: CS 3240 – Chapter 8.  Is a n b n c n context-free? CS 3240 - Properties of Context-Free Languages2

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 24

Page 25: CS 3240 – Chapter 8.  Is a n b n c n context-free? CS 3240 - Properties of Context-Free Languages2

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

Page 26: CS 3240 – Chapter 8.  Is a n b n c n context-free? CS 3240 - Properties of Context-Free Languages2

DCFLs always have an associated CFG that is unambiguous

Page 27: CS 3240 – Chapter 8.  Is a n b n c n context-free? CS 3240 - Properties of Context-Free Languages2

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

Page 28: CS 3240 – Chapter 8.  Is a n b n c n context-free? CS 3240 - Properties of Context-Free Languages2

Unanswerable questionsAnswerable questions

Page 29: CS 3240 – Chapter 8.  Is a n b n c n context-free? CS 3240 - Properties of Context-Free Languages2

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?

Page 30: CS 3240 – Chapter 8.  Is a n b n c n context-free? CS 3240 - Properties of Context-Free Languages2

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

Page 31: CS 3240 – Chapter 8.  Is a n b n c n context-free? CS 3240 - Properties of Context-Free Languages2

CS 3240 - Properties of Context-Free Languages 31

S → aA | bB | λA → a | aCA | bDA | bBa | aAaB → b | aAb | aCB | bDB | bBbC → aCC | bDCD → aCD | bDD

First remove useless variables…

Page 32: CS 3240 – Chapter 8.  Is a n b n c n context-free? CS 3240 - Properties of Context-Free Languages2

CS 3240 - Properties of Context-Free Languages 32

S → aA | bB | λA → a | bBa | aAaB → b | aAb | bBb

Pick a non-empty, terminal rule: A → aBack-substitute that rule:

S → aa | bB | λA → a | bBa | aaaB → b | aab | bBb

Keep going until we have S → <terminal string>, or we can’t continue. We have S → aa. STOP.

Page 33: CS 3240 – Chapter 8.  Is a n b n c n context-free? CS 3240 - Properties of Context-Free Languages2

CS 3240 - Properties of Context-Free Languages 33

S → aA | bB | λA → a | bBa | aAaB → 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 | aAaB → b | aAb | bBb

Now mark all variables affected on the left:

S → aA | bB | λA → a | bBa | aAaB → b | aAb | bBb

Since A was marked on the left, it is repeated.

Page 34: CS 3240 – Chapter 8.  Is a n b n c n context-free? CS 3240 - Properties of Context-Free Languages2

CS 3240 - Properties of Context-Free Languages 34

S aA | SB➞ Mark on left:A baB | λ➞B bB | bA➞ S a➞ A | SB

A ba➞ B | λMark A’s on right: B b➞ B | bA

S a➞ A | SB A was marked on left. DONE.A baB | λ➞B bB | b➞ A

Now mark corresponding variables on left:

S a➞ A | SBA baB | λ➞B bB | b➞ A

Repeat marking on right:

S a➞ A | SBA ba➞ B | λB b➞ B | bA