210 likes | 333 Views
L ECTURE 8 Theory of Computation Converting a PDA to a CFG Pumping Lemma for CFLs. Intro to Theory of Computation. CS 464. Sofya Raskhodnikova. A language is generated by a CFG It is recognized by a PDA. . Converting a PDA to a CFG. Given PDA P = (Q, Σ , Γ , , q, F).
E N D
LECTURE 8 • Theory of Computation • Converting a PDA to a CFG • Pumping Lemma for CFLs Intro to Theory of Computation CS 464 Sofya Raskhodnikova Sofya Raskhodnikova; based on slides by Nick Hopper
A language is generated by a CFG It is recognized by a PDA Sofya Raskhodnikova; based on slides by Nick Hopper
Converting a PDA to a CFG Given PDA P = (Q, Σ, Γ, , q, F) Construct a CFG G = (V, Σ, R, S) with L(G)=L(P) First, simplify P so that: (1) It has a single accept state, qaccept (2) It empties the stack before accepting (3) Each transition either pushes a symbol or pops a symbol, but not both Sofya Raskhodnikova; based on slides by Nick Hopper
From PDA to CFG: main idea For each pair of states p and q in P, add a variable Apq to CFG that generates all strings that that can take P from p to q without changing the stack* V = {Apq | p,qQ } S = Aq0qaccept *Starting from any stack S in p, including empty stack, P has stack S at q. Sofya Raskhodnikova; based on slides by Nick Hopper
ε,ε → $ 0,ε→ 0 q1 q0 1,0→ε ε,ε → 0 ε,$ → ε q2 q3 1,0→ε ε,ε → 0 q4 ε,0 → ε q5 Example What strings does Aq0q1 generate? none What strings does Aq1q2 generate? {0n1n | n > 0} What strings does Aq1q3 generate? none Sofya Raskhodnikova; based on slides by Nick Hopper
From PDA to CFG: main idea Apq generates all strings that take P from p to q without changing the stack Let x be such a string • P’s first move on x must be a push • P’s last move on x must be a pop Consider the stack while reading x. Either: New portion of the stack first empties only at the end of x 2. New portion empties before the end of x Sofya Raskhodnikova; based on slides by Nick Hopper
New portion of the stack first empties only at the end of x stack height r s a b input string p q Apq→ aArsb Sofya Raskhodnikova; based on slides by Nick Hopper
2. New portion empties before the end of x stack height input string p r q Apq→ AprArq Sofya Raskhodnikova; based on slides by Nick Hopper
CFG construction V = {Apq | p,qQ } S = Aq0qaccept For each p,q,r,s Q, t Γ and a,b Σε If (r,t) (p,a,ε) and (q, ε) (s,b,t) Then add the rule Apq→ aArsb For each p,q,r Q, add the rule Apq→ AprArq For each p Q, add the rule App→ ε Sofya Raskhodnikova; based on slides by Nick Hopper
ε,ε → $ 0,ε→ 0 q1 q0 1,0→ε ε,ε → 0 ε,$ → ε q2 q3 1,0→ε ε,ε → 0 q4 ε,0 → ε q5 Aqq→ ε Apq→ AprArq Aq0q3 → εAq1q2ε Aq1q2 → 0Aq1q21 Aq1q2 → 0Aq1q11 none What strings does Aq0q1 generate? {0n1n | n > 0} What strings does Aq1q2 generate? What strings does Aq1q3 generate? none Sofya Raskhodnikova; based on slides by Nick Hopper
Equivalence of CFGs & PDAs A language is generated by a CFG It is recognized by a PDA Sofya Raskhodnikova; based on slides by Nick Hopper
Read on your own Ambiguous CFGs. Chomsky normal form for CFGs (Skipping Chapter 2.4 in Sipser). Sofya Raskhodnikova; based on slides by Nick Hopper
Context-free or not? L₁ = { xy | x,y ∈ {0,1}* and x=y} NOT YES L₂ = {xy | x,y ∈ {0,1}*, |x|=|y| and x ≠ y}
Pumping lemma for CFLs Let L be a context-free language Then there exists P such that For every w L with |w| ≥ P there exist uvxyz=w, where: 1. |vy| > 0 2. |vxy| ≤ P 3. uvixyiz L for all i ≥ 0
Examples there exist uvxyz=w, where: • |vy| > 0 • |vxy| ≤ P • uvixyiz L for all i ≥ 0 Example: L = { w 2 {0,1}* | w = wR}. w = 0; u,v,x,y,z = ? w = 010; u,v,x,y,z=? Example: L = { w 2 {a,b}* | #a > #b in w}. w = a; u,v,x,y,z = ? w = aab; u,v,x,y,z=?
T T R R R R R R y v u z u v x y z v x y Pumping lemma:proof idea If string w is long enough, then every parse tree for w must have a path that contains a variable more than once.
Pumping lemma: proof • Let b be the maximum number of symbols on the right-hand side of a rule. • If the height of a parse tree is h, the length of the string generated is at most: bh • Let |V| be the number of variables in G. • Define p= b|V|+2. • Let w be a string of length at least p. • Let T be the parse tree for w with the smallest number of nodes. • T must have height at least |V|+2.
T T R R R R R R y v u z u v x y z v x y Pumping lemma: proof The longest path in T must have ≥ |V|+1 variables Select R to be the variable that repeats among the lowest |V|+1 variables 1. |vy| > 0 2. |vxy| ≤ P
Negating the pumping lemma L is not context-free Let L be a context-free language. Then there exists P such that For every w L with |w| ≥ P for every P, If There is a For every there exist uvxyz=w, where: 1. |vy| > 0 2. |vxy| ≤ P There is an 3. For every i ≥ 0, uvixyiz L
Using the pumping lemma Prove L = {ww | w 2 {0,1}*} is not context-free Assume L is context-free. Then there is a pumping length P. No matter what P is, the string s = 0P1P0P1P has |s| ¸ P and s 2 L. So there should be uvxyz=s with: 1) |vy| > 0, 2) |vxy| · P, 3) 8 i, uvixyiz 2 L. P P P P s = 00…0011…1100…0011…11
{ww | w 2 {0,1}*} is not a CFL: proof No matter what P is, the string s = 0P1P0P1P has |s| ¸ P and s 2 L. vxy cannot be only in the first half, since pumping down would move a 0 to the end of the first half. Then pumping down must remove at least one 1 or one zero, e.g. uxz = 0P1i0j1P where i p or j p. So uxz L, no matter how they are chosen; so L cannot be context-free! vxy cannot be only in the last half, since pumping up would move a 0 to the end of the first half. So there should be uvxyz=s with: 1) |vy| > 0, 2) |vxy| · P, 3) 8 i, uvixyiz 2 L. P P P P s = 00…0011…1100…0011…11 vxy