today chapter 2: (pushdown automata) non-cf languages cfl pumping lemma closure properties of cfl

28
Today Chapter 2: (Pushdown automata) Non-CF languages CFL pumping lemma Closure properties of CFL

Post on 22-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Today Chapter 2: (Pushdown automata) Non-CF languages CFL pumping lemma Closure properties of CFL

Today

•Chapter 2:• (Pushdown automata)

• Non-CF languages

• CFL pumping lemma

• Closure properties of CFL

Page 2: Today Chapter 2: (Pushdown automata) Non-CF languages CFL pumping lemma Closure properties of CFL

Pushdown Automata

Pushdown automata are for context-free languages what finite automata are for regularlanguages.

PDAs are recognizing automata that have a single stack (= memory): Last-In First-Out pushing and popping

Difference: PDAs are inherently nondeterministic.(They are not practical machines.)

Page 3: Today Chapter 2: (Pushdown automata) Non-CF languages CFL pumping lemma Closure properties of CFL

Informal Description PDA (1)

input w = 00100100111100101

internal state set Q

xyyzx

stack

The PDA M reads w and stack element.Depending on - input wi ,

- stack sj , and

- state qk Qthe PDA M: - jumps to a new state, - pushes an element

(nondeterministically)

Page 4: Today Chapter 2: (Pushdown automata) Non-CF languages CFL pumping lemma Closure properties of CFL

Informal Description PDA (2)

input w = 00100100111100101

internal state set Q

xyyzx

stack

After the PDA has read complete input, M will be in state Q

If possible to end in accepting state FQ,then M accepts w

Page 5: Today Chapter 2: (Pushdown automata) Non-CF languages CFL pumping lemma Closure properties of CFL

Formal Description PDA

A Pushdown Automata M is defined by asix tuple (Q,,,,q0,F), with• Q finite set of states• finite input alphabet• finite stack alphabet• q0 start state Q• F set of accepting states Q• transition function

: Q P (Q )

Page 6: Today Chapter 2: (Pushdown automata) Non-CF languages CFL pumping lemma Closure properties of CFL

PDA for L = { 0n1n | n0 }

Example 2.9: The PDA first pushes “ $ 0n ” on stack.Then, while reading the 1n string, thezeros are popped again.If, in the end, $ is left on stack, then “accept”

q1

q3

q2

q4

, $

, $

1, 0

1, 0

0, 0

Page 7: Today Chapter 2: (Pushdown automata) Non-CF languages CFL pumping lemma Closure properties of CFL

Machine Diagram for 0n1n

q1

q3

q2

q4

, $

, $

1, 0

1, 0

0, 0

On w = 000111 (state; stack) evolution:(q1; ) (q2; $) (q2; 0$) (q2; 00$) (q2; 000$) (q3; 00$) (q3; 0$) (q3; $) (q4; ) This final q4 is an accepting state

Page 8: Today Chapter 2: (Pushdown automata) Non-CF languages CFL pumping lemma Closure properties of CFL

Machine Diagram for 0n1n

q1

q3

q2

q4

, $

, $

1, 0

1, 0

0, 0

On w = 0101 (state; stack) evolution:(q1; ) (q2; $) (q2; 0$) (q3; $) (q4; ) …But we still have part of input “01”.There is no accepting path.

Page 9: Today Chapter 2: (Pushdown automata) Non-CF languages CFL pumping lemma Closure properties of CFL

PDAs versus CFL

Theorem 2.12: A language L is context-free if and only of there is a pushdown automata M that recognizes L.

Two step proof: 1) Given a CFG G, construct a PDA MG

2) Given a PDA M, make a CFG GM

Page 10: Today Chapter 2: (Pushdown automata) Non-CF languages CFL pumping lemma Closure properties of CFL

Non-CF Languages

The language L = { anbncn | n0 } does notappear to be context-free.

Informal: The problem is that every variablecan (only) act ‘by itself’ (context-free).

The problem of A * vAy :If S * uAz * uvAyz * uvxyz L, then S * uAz * uvAyz * … * uviAyiz * uvixyiz L as well, for all i=0,1,2,…

Page 11: Today Chapter 2: (Pushdown automata) Non-CF languages CFL pumping lemma Closure properties of CFL

“Pumping Lemma for CFLs”

Idea: If we can prove the existence of derivationsfor elements of the CFL L that use the step A * vAy, then a new form of ‘v-y pumping’ holds: A * vAy * v2Ay2 * v3Ay3 * …)

Observation: We can prove this existence if the parse-tree is tall enough.

Page 12: Today Chapter 2: (Pushdown automata) Non-CF languages CFL pumping lemma Closure properties of CFL

Remember Parse Trees

Parse tree for S AbbcBa * cbbccccaBca cbbccccacca

S

b b acA

c c caA

c c

c

B

B

Page 13: Today Chapter 2: (Pushdown automata) Non-CF languages CFL pumping lemma Closure properties of CFL

Pumping a Parse Tree

A

A

u v x y zIf s = uvxyz L is long, then its parse-tree is tall.Hence, there is a path on which a variable A repeats itself. We can pump this A–A part.

S

Page 14: Today Chapter 2: (Pushdown automata) Non-CF languages CFL pumping lemma Closure properties of CFL

A Tree Tall Enough

Let L be a context-free language, and letG be its grammar with maximal b symbols on the right side of the rules: A X1…Xb

A parse tree of depth h produces a stringwith maximum length of bh. Long strings implies tall trees. Let |V| be the number of variables of G.If h = |V|+2 or bigger, then there is a variable ona ‘top-down path’ that occurs more than once.

Page 15: Today Chapter 2: (Pushdown automata) Non-CF languages CFL pumping lemma Closure properties of CFL

uvxyz L

A

A

u v x y z

By repeating the A–A part we get…

S

Page 16: Today Chapter 2: (Pushdown automata) Non-CF languages CFL pumping lemma Closure properties of CFL

uv2xy2z L

A

u v x y zRA

A

v x y… while removing the A–-A gives…

S

Page 17: Today Chapter 2: (Pushdown automata) Non-CF languages CFL pumping lemma Closure properties of CFL

uxz L

A

u z

In general uvixyiz L for all i=0,1,2,…

S

x

Page 18: Today Chapter 2: (Pushdown automata) Non-CF languages CFL pumping lemma Closure properties of CFL

Pumping Lemma for CFL

For every context-free language L, there is a pumping length p, such that for every string sL and |s|p, we can write s=uvxyz with

1) uvixyiz L for every i{0,1,2,…}2) |vy| 13) |vxy| p

Note that 1) implies that uxz L 2) says that vy cannot be the empty string Condition 3) is not always used

Page 19: Today Chapter 2: (Pushdown automata) Non-CF languages CFL pumping lemma Closure properties of CFL

Formal Proof of Pumping Lemma

Let G=(V,,R,S) be the grammar of a CFL.Maximum size of rules is b2: A X1…Xb

A string s requires a minimum tree-depth logb|s|.If |s| p=b|V|+2, then tree-depth |V|+2, hence there is a path and variable A where A repeats itself: S * uAz * uvAyz * uvxyzIt follows that uvixyiz L for all i=0,1,2,…Furthermore: |vy| 1 because tree is minimal |vxy| p because bottom tree with p leaves has a ‘repeating path’

Page 20: Today Chapter 2: (Pushdown automata) Non-CF languages CFL pumping lemma Closure properties of CFL

Pumping anbncn (Ex. 2.20)

Assume that B = {anbncn | n0} is CFLLet p be the pumping length, and s = apbpcp BP.L.: s = uvxyz = apbpcp, with uvixyiz B for all i0Options for |vxy|:1) The strings v and y are uniform (v=a…a and y=c…c, for example). Then uv2xy2z will not contain the same number of a’s, b’s and c’s, hence uv2xy2zB2) v and y are not uniform. Then uv2xy2z will not be a…ab…bc…c Hence uv2xy2zB

Page 21: Today Chapter 2: (Pushdown automata) Non-CF languages CFL pumping lemma Closure properties of CFL

Pumping anbncn (cont.)

Assume that B = {anbncn | n0} is CFLLet p be the pumping length, and s = apbpcp BP.L.: s = uvxyz = apbpcp, with uvixyiz B for all i0

We showed: No options for |vxy| such that uvixyiz B for all i. Contradiction.

B is not a context-free language.

Page 22: Today Chapter 2: (Pushdown automata) Non-CF languages CFL pumping lemma Closure properties of CFL

Example 2.21 (Pumping down)

Proof that C = {aibjck | 0ijk } is not context-free.Let p be the pumping length, and s = apbpcp CP.L.: s = uvxyz, such that uvixyiz C for every i0Two options for 1 |vxy| p:1) vxy = a*b*, then the string uv2xy2z has not enough c’s, hence uv2xy2zC2) vxy = b*c*, then the string uv0xy0z = uxz has too many a’s, hence uv0xy0zC

Contradiction: C is not a context-free language.

Page 23: Today Chapter 2: (Pushdown automata) Non-CF languages CFL pumping lemma Closure properties of CFL

D = { ww | w{0,1}* } (Ex. 2.22)

Carefully take the strings sD. Let p be the pumping length, take s=0p1p0p1p.Three options for s=uvxyz with 1 |vxy| p:1) If a part of y is to the left of | in 0p1p|0p1p, then second half of uv2xy2z starts with “1”2) Same reasoning if a part of v is to the right of middle of 0p1p|0p1p, hence uv2xy2z D3) If x is in the middle of 0p1p|0p1p, then uxz equals 0p1i 0j1p D (because i or j < p)

Contradiction: D is not context-free.

Page 24: Today Chapter 2: (Pushdown automata) Non-CF languages CFL pumping lemma Closure properties of CFL

Pumping Problems

Using the CFL pumping lemma is more difficultthan the pumping lemma for regular languages.

You have to choose the string s carefully,and divide the options efficiently.

Additional CFL properties would be helpful(like we had for regular languages).

What about closure under standard operations?

Page 25: Today Chapter 2: (Pushdown automata) Non-CF languages CFL pumping lemma Closure properties of CFL

Union Closure Properties

Lemma: Let A1 and A2 be two CF languages,then the union A1A2 is context free as well.

Proof: Assume that the two grammars areG1=(V1,,R1,S1) and G2=(V2,,R2,S2).Construct a third grammar G3=(V3,,R3,S3) by: V3 = V1 V2 { S3 } (new start variable) with R3 = R1 R2 { S3 S1 | S2 }.

It follows that L(G3) = L(G1) L(G2).

Page 26: Today Chapter 2: (Pushdown automata) Non-CF languages CFL pumping lemma Closure properties of CFL

Intersection & Complement?

Let again A1 and A2 be two CF languages.

One can prove that, in general, the intersection A1 A2 , and the complement Ā1= * \ A1 are not context free languages.

One proves this with specific counter examples of languages (see homework).

Page 27: Today Chapter 2: (Pushdown automata) Non-CF languages CFL pumping lemma Closure properties of CFL

Homework

• Exercise 2.2 (a, b)

• Exercise 2.7

• Problem 2.18 (a, b, c, d)

Page 28: Today Chapter 2: (Pushdown automata) Non-CF languages CFL pumping lemma Closure properties of CFL

Practice Problems

• Exercise 2.10

• Problem 2.19

• Problem 2.20

• Problem 2.23