Download presentation
Presentation is loading. Please wait.
1
Today Chapter 2: (Pushdown automata) Non-CF languages CFL pumping lemma Closure properties of CFL
2
Pushdown Automata Pushdown automata are for context-free languages what finite automata are for regular languages. 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.)
3
Informal Description PDA (1) input w = 00100100111100101 internal state set Q xyyzxxyyzx stack The PDA M reads w and stack element. Depending on - input w i , - stack s j , and - state q k Q the PDA M: - jumps to a new state, - pushes an element (nondeterministically)
4
Informal Description PDA (2) input w = 00100100111100101 internal state set Q xyyzxxyyzx stack After the PDA has read complete input, M will be in state Q If possible to end in accepting state F Q, then M accepts w
5
Formal Description PDA A Pushdown Automata M is defined by a six tuple (Q, , , ,q 0,F), with Q finite set of states finite input alphabet finite stack alphabet q 0 start state Q F set of accepting states Q transition function : Q P (Q )
6
PDA for L = { 0 n 1 n | n 0 } Example 2.9: The PDA first pushes “ $ 0 n ” on stack. Then, while reading the 1 n string, the zeros are popped again. If, in the end, $ is left on stack, then “accept” q1q1 q3q3 q2q2 q4q4 , $ , $ 1, 0 0, 0
7
Machine Diagram for 0 n 1 n q1q1 q3q3 q2q2 q4q4 , $ , $ 1, 0 0, 0 On w = 000111 (state; stack) evolution: (q 1 ; ) (q 2 ; $) (q 2 ; 0$) (q 2 ; 00$) (q 2 ; 000$) (q 3 ; 00$) (q 3 ; 0$) (q 3 ; $) (q 4 ; ) This final q 4 is an accepting state
8
Machine Diagram for 0 n 1 n q1q1 q3q3 q2q2 q4q4 , $ , $ 1, 0 0, 0 On w = 0101 (state; stack) evolution: (q 1 ; ) (q 2 ; $) (q 2 ; 0$) (q 3 ; $) (q 4 ; ) … But we still have part of input “01”. There is no accepting path.
9
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 M G 2) Given a PDA M, make a CFG G M
10
Non-CF Languages The language L = { a n b n c n | n 0 } does not appear to be context-free. Informal: The problem is that every variable can (only) act ‘by itself’ (context-free). The problem of A * vAy : If S * uAz * uvAyz * uvxyz L, then S * uAz * uvAyz * … * uv i Ay i z * uv i xy i z L as well, for all i=0,1,2,…
11
“Pumping Lemma for CFLs” Idea: If we can prove the existence of derivations for elements of the CFL L that use the step A * vAy, then a new form of ‘v-y pumping’ holds: A * vAy * v 2 Ay 2 * v 3 Ay 3 * …) Observation: We can prove this existence if the parse-tree is tall enough.
12
Remember Parse Trees Parse tree for S AbbcBa * cbbccccaBca cbbccccacca S b b ac A c c ca A c c c B B
13
Pumping a Parse Tree A A uvxyz If 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
14
A Tree Tall Enough Let L be a context-free language, and let G be its grammar with maximal b symbols on the right side of the rules: A X 1 …X b A parse tree of depth h produces a string with maximum length of b h. 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 on a ‘top-down path’ that occurs more than once.
15
uvxyz L A A uvxyz By repeating the A–A part we get… S
16
uv 2 xy 2 z L A u v x y z R A A v x y … while removing the A–-A gives… S
17
uxz L A uz In general uv i xy i z L for all i=0,1,2,… S x
18
Pumping Lemma for CFL For every context-free language L, there is a pumping length p, such that for every string s L and |s| p, we can write s=uvxyz with 1) uv i xy i z L for every i {0,1,2,…} 2) |vy| 1 3) |vxy| p Note that 1) implies that uxz L 2) says that vy cannot be the empty string Condition 3) is not always used
19
Formal Proof of Pumping Lemma Let G=(V, ,R,S) be the grammar of a CFL. Maximum size of rules is b 2: A X 1 …X b A string s requires a minimum tree-depth log b |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 * uvxyz It follows that uv i xy i z 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’
20
Pumping a n b n c n (Ex. 2.20) Assume that B = {a n b n c n | n 0} is CFL Let p be the pumping length, and s = a p b p c p B P.L.: s = uvxyz = a p b p c p, with uv i xy i z B for all i 0 Options for |vxy|: 1) The strings v and y are uniform (v=a…a and y=c…c, for example). Then uv 2 xy 2 z will not contain the same number of a’s, b’s and c’s, hence uv 2 xy 2 z B 2) v and y are not uniform. Then uv 2 xy 2 z will not be a…ab…bc…c Hence uv 2 xy 2 z B
21
Pumping a n b n c n (cont.) Assume that B = {a n b n c n | n 0} is CFL Let p be the pumping length, and s = a p b p c p B P.L.: s = uvxyz = a p b p c p, with uv i xy i z B for all i 0 We showed: No options for |vxy| such that uv i xy i z B for all i. Contradiction. B is not a context-free language.
22
Example 2.21 (Pumping down) Proof that C = {a i b j c k | 0 i j k } is not context-free. Let p be the pumping length, and s = a p b p c p C P.L.: s = uvxyz, such that uv i xy i z C for every i 0 Two options for 1 |vxy| p: 1) vxy = a*b*, then the string uv 2 xy 2 z has not enough c’s, hence uv 2 xy 2 z C 2) vxy = b*c*, then the string uv 0 xy 0 z = uxz has too many a’s, hence uv 0 xy 0 z C Contradiction: C is not a context-free language.
23
D = { ww | w {0,1}* } (Ex. 2.22) Carefully take the strings s D. Let p be the pumping length, take s=0 p 1 p 0 p 1 p. Three options for s=uvxyz with 1 |vxy| p: 1) If a part of y is to the left of | in 0 p 1 p |0 p 1 p, then second half of uv 2 xy 2 z starts with “1” 2) Same reasoning if a part of v is to the right of middle of 0 p 1 p |0 p 1 p, hence uv 2 xy 2 z D 3) If x is in the middle of 0 p 1 p |0 p 1 p, then uxz equals 0 p 1 i 0 j 1 p D (because i or j < p) Contradiction: D is not context-free.
24
Pumping Problems Using the CFL pumping lemma is more difficult than 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?
25
Union Closure Properties Lemma: Let A 1 and A 2 be two CF languages, then the union A 1 A 2 is context free as well. Proof: Assume that the two grammars are G 1 =(V 1, ,R 1,S 1 ) and G 2 =(V 2, ,R 2,S 2 ). Construct a third grammar G 3 =(V 3, ,R 3,S 3 ) by: V 3 = V 1 V 2 { S 3 } (new start variable) with R 3 = R 1 R 2 { S 3 S 1 | S 2 }. It follows that L(G 3 ) = L(G 1 ) L(G 2 ).
26
Intersection & Complement? Let again A 1 and A 2 be two CF languages. One can prove that, in general, the intersection A 1 A 2, and the complement Ā 1 = * \ A 1 are not context free languages. One proves this with specific counter examples of languages (see homework).
27
Homework Exercise 2.2 (a, b) Exercise 2.7 Problem 2.18 (a, b, c, d)
28
Practice Problems Exercise 2.10 Problem 2.19 Problem 2.20 Problem 2.23
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.