400 likes | 564 Views
CSCI 2670 Introduction to Theory of Computing. September 23, 2004. Agenda. Yesterday Pushdown automata Today Quiz More on pushdown automata Pumping lemma for CFG’s. Example. Find for the PDA that accepts all strings in {0,1} * with the same number of 0’s and 1’s
E N D
CSCI 2670Introduction to Theory of Computing September 23, 2004
Agenda • Yesterday • Pushdown automata • Today • Quiz • More on pushdown automata • Pumping lemma for CFG’s September 23, 2004
Example • Find for the PDA that accepts all strings in {0,1}* with the same number of 0’s and 1’s • Need to keep track of “equilibrium point” with a $ on the stack • If stack top is not $, it contains the symbol currently dominating in the string September 23, 2004
Example • Find for the PDA that accepts all strings in {0,1}* with the same number of 0’s and 1’s • Push a symbol on the stack as its read if • It matches the top of the stack, or • The top of stack is $ • Pop the symbol off the top of the stack if you read a 0 and the top of stack is 1 or vice versa September 23, 2004
1,$1$ 1,1 11 1,00 0 1,0$ $ ε,$ ε ε,ε$ 0,$0$ 0,0 00 0,11 1 0,1$ $ Example September 23, 2004
1,$1$ 1,1 11 1,0 ε ε,$ ε ε,ε$ 0,$0$ 0,0 00 0,1 ε Example This PDA is equivalent to the one on the previous slide September 23, 2004
Equivalence of PDA’s and CFG’s Theorem: A language is context free if and only if some pushdown automaton recognizes it Proved in two lemmas – one for the “if” direction and one for the “only if” direction September 23, 2004
CFG’s are recognized by PDA’s Lemma: If a language is context free, then some pushdown automaton recognizes it Proof idea: Construct a PDA following CFG rules September 23, 2004
Constructing the PDA • You can read any symbol in when that symbol is at the top of the stack • Transitions of the form a,aε • The rules will be pushed onto the stack – when a variable A is on top of the stack and there is a rule Aw, you pop A and push w • You can go to the accept state only if the stack is empty September 23, 2004
State control State control a b a b x B z t A t Idea of PDA construction for AxBz September 23, 2004
ε, εB ε,Az ε, εx Actual construction for AxBz In an abuse of notation, we say (q,ε,A)=(q,xBz) September 23, 2004
Constructing the PDA • Q = {qstart, qloop, qaccept}E, where E is the set of states used for replacement rules onto the stack • (the PDA alphabet) is the set of terminals in the CFG • (the stack alphabet) is the union of the terminals and the variables and {$} (or some suitable placeholder) September 23, 2004
Constructing the PDA • is comprised of several rules • (qstart,ε,ε)=(qloop,S$) • Start with placeholder on the stack and with the start variable • (qloop,a,a)=(qloop,ε) for every a • Terminals may be read off the top of the stack • (qloop,ε,A)=(qloop,w) for every rule Aw • Implement replacement rules • (qloop,ε,$)=(qaccept,ε) • Accept when the stack is empty September 23, 2004
ε,SSS ε,S(S) ε,S() ε, ε S$ ε,$ε qstart qloop qaccept (,(ε ),)ε Example • S SS | (S) | () September 23, 2004
Example • Read (()()) ε,SSS ε,S(S) ε,S() S $ ε, ε S$ ε,$ε qstart qloop qaccept (,(ε ),)ε September 23, 2004
Example • Read (()()) ( S ) $ ε,SSS ε,S(S) ε,S() ε, ε S$ ε,$ε qstart qloop qaccept (,(ε ),)ε September 23, 2004
Example • Read (()()) S ) $ ε,SSS ε,S(S) ε,S() ε, ε S$ ε,$ε qstart qloop qaccept (,(ε ),)ε ( September 23, 2004
Example SS ) $ • Read (()()) ε,SSS ε,S(S) ε,S() ε, ε S$ ε,$ε qstart qloop qaccept (,(ε ),)ε ( September 23, 2004
Example ( ) S ) $ • Read (()()) ε,SSS ε,S(S) ε,S() ε, ε S$ ε,$ε qstart qloop qaccept (,(ε ),)ε ( September 23, 2004
Example ) S ) $ • Read (()()) ε,SSS ε,S(S) ε,S() ε, ε S$ ε,$ε qstart qloop qaccept (,(ε ),)ε (( September 23, 2004
Example S ) $ • Read (()()) ε,SSS ε,S(S) ε,S() ε, ε S$ ε,$ε qstart qloop qaccept (,(ε ),)ε (() September 23, 2004
( ) ) $ Example • Read (()()) ε,SSS ε,S(S) ε,S() ε, ε S$ ε,$ε qstart qloop qaccept (,(ε ),)ε (() September 23, 2004
) ) $ Example • Read (()()) ε,SSS ε,S(S) ε,S() ε, ε S$ ε,$ε qstart qloop qaccept (,(ε ),)ε (()( September 23, 2004
) $ Example • Read (()()) ε,SSS ε,S(S) ε,S() ε, ε S$ ε,$ε qstart qloop qaccept (,(ε ),)ε (()() September 23, 2004
$ Example • Read (()()) ε,SSS ε,S(S) ε,S() ε, ε S$ ε,$ε qstart qloop qaccept (,(ε ),)ε (()()) September 23, 2004
Example • Read (()()) ε,SSS ε,S(S) ε,S() ε, ε S$ ε,$ε qstart qloop qaccept (,(ε ),)ε (()()) September 23, 2004
y x z The pumping lemma for RE’s • The pumping lemma for RE’s depends on the structure of the DFA and the fact that a state must be revisited • Only a finite number of states September 23, 2004
T R R u v x y z v & y will be repeated simultaneously The pumping lemma for CFG’s • What might be repeated in a CFG? • The variables September 23, 2004
T R T R R R R u v x y z z u y y v v x The pumping lemma for CFG’s September 23, 2004
T R T R R x u v x y z u z The pumping lemma for CFG’s September 23, 2004
The pumping lemma for CFL’s Theorem: If A is a context-free language, then there is a number p (the pumping length) where, if s is any string in A of length at least p, then s may be divided into five pieces s=uvxyz satisfying the conditions: • For each i 0, uvixyiz A • |vy| > 0 • |vxy| p September 23, 2004
Finding the pumping length of a CFL • Let b equal the longest right-hand side of any rule (assume b > 1) • Each node in the parse tree has at most b children • At most bh nodes are h steps from the start node • Let p equal b|V|+2, where |V| is the number of variables (could be huge!) • Tree height is at least |V|+2 September 23, 2004
Proving the pumping lemma for CFG’s • Let s be any string in A with length at least p = b|V|+2 and let be a parse tree for s with the fewest possible nodes • The height of is at least |V|+2 • The longest path in has a length of at least |V|+2 • Leaf is a terminal and at least |V|+1 variables • Some variable appears twice in the path September 23, 2004
T R R u v x y z } At most |V|+1 steps Proving the pumping lemma for CFG’s • Let R be the last variable to appear twice on this path • The last two occurrences of R are both at most |V|+1 steps from the leaf Question: What do we know about uvixyiz? Answer: It is accepted by the PDA September 23, 2004
T R R u v x y z } At most |V|+1 steps Proving the pumping lemma for CFG’s • Let R be the last variable to appear twice on this path • The last two occurrences of R are both at most |V|+1 steps from the leaf Question: What do we know about |vy|? Answer: It is non-empty (because tree has fewest possible nodes) September 23, 2004
T R R u v x y z } At most |V|+1 steps Proving the pumping lemma for CFG’s • Let R be the last variable to appear twice on this path • The last two occurrences of R are both at most |V|+1 steps from the leaf Question: What do we know about |vxy|? Answer: It has at most p symbols (because at most |V|+1 steps) September 23, 2004
Example • Show A is not context free, where A={an|n is prime} Proof: Assume A is context-free and let p be the pumping length of A. Let w=an for any np. By the pumping lemma, w=uvxyz such that |vxy|p, |vy|>0, and uvixyizA for all i=0,2,1…. September 23, 2004
Example (cont.) • Show A is not context free, where A={an|n is prime} Clearly, vy=ak for some k Consider the string uvn+1xyn+1z This string add n copies of ak to w – i.e., this is an+nk Since the exponent is n(1+k) this in not in A, which contradicts the pumping lemma. Therefore, A is not context free. September 23, 2004
Have a fun weekend! September 23, 2004