190 likes | 284 Views
Lecture7 Pushdown Automaton. Push Down Automaton (PDA). Language Acceptor Model for CFLs It is an NFA with a stack. input. Finite State control. Stack. PDA (contd.). In one move the PDA can : change state, consume a symbol from the input tape or ignore it
E N D
Push Down Automaton (PDA) • Language Acceptor Model for CFLs • It is an NFA with a stack. input Finite State control Stack
PDA (contd.) • In one move the PDA can : • change state, • consume a symbol from the input tape or ignore it • pop a symbol from the stack or ignore it • push a symbol onto the stack or not • A string is accepted provided the machine when started in the start state consumes the string and reaches a final state.
PDA (contd.) • If PDA in state q can consume u, pop xfrom stack, change state to p, and push w on stack • We show it as u, x w u, x; w p q
Pushdown automata • A pushdown automaton is a 6-tuple (Q, , , , q0, F), where • Q, , , F are finite sets, and • Q is the set of states, • is the input alphabet, • is the stack alphabet, • : Q P(Q )is the transition function , • q0 Q is the start state, and • F Q is the set of accept states.
Pushdown automata (cont.) • M = (Q, , , , q0, F) accepts a string w* if w can be represented as w1…wm with w1, …, wm and there exist r0, …, rm Q and s0, …, sm * satisfying the following conditions: • r0 = q0 and s0 = • For every i, 0 i m-1, (ri, b) (ri-1, wi, a),where si-1 = at and si = bt for some a, b and t * • rm F • Here the stack is read from top to bottom (or left to right). • Let M be a PDA, the language recognized by M is defined as L(M)={w | w is accepted by M}.
PDA, example 1 • L = { 0n1n | n 0} • Design idea: • Use a special symbol $ to mark the bottom of the stack (using wouldn’t work since is always there) • First put onto the stack all the 0s preceding the 1s • Then try to match the stacked 0s with the 1s • The input string is in L, if and only if, the bottom $ is the top symbol when all the input symbols have been consumed.
0, 0 ,$ q2 q1 1,0 q4 1,0 q3 , $ Example 1 (cont.) ={0,$}, Q={q1,q2,q3,q4}, F = {q1, q4}, q1 is the start state. We use q1 to place the $ on the stack, q2 to read 0s and push them onto the stack (and react to the first 1), and q3 to read 1s and pop 0s from the stack (and react to the $ by going to accept state q4). As a diagram:
PDAs Recognize CFLs • Theorem. Each context-free language is recognized by a PDA. • Proof. Let L be produced by a grammar G = (V, , R, S). Idea. Construct a PDA that performs leftmost derivations. A step of a derivation using A w is implemented by popping the left-hand side and pushing the right-hand side of the rule onto the stack. The input string is checked whether can be produced by the grammar by comparing the consumed symbol with the produced symbol.
CFG PDA • Given CFG G = (V, , R, S) • Let PDA M = (Q, , V {$}, , qstart, {qaccept}) • Q = {qstart, qloop, qaccept} • contains transitions for the form • ’(qstart, , ) = (qloop, S$) ** • ’(qloop, , A) = {(qloop, w) | A w R} ** • For each symbol (qloop, , ) = (qloop, ) (qloop, , $) = (qaccept, ) qstart , S$ , qloop , A w , $ **’(q, a, b)=(p, w) is a shorthand for a series of transitions, which consumes a, pops b, and pushing the string w, and changes state from q to p. qaccept
CFG PDA • The PDA simulates a leftmost derivation of the string. • Place the marker symbol $ and the start variable on the stack. • Repeat the following steps forever (a) If the top of stack is a variable symbol A, non-deterministically select one of the rules for A and substitute A by the string on the right-hand side of the rule. (b) If the top of stack is terminal symbol , read the next symbol from the input and compare it to . If they match, repeat. If they do not match, reject on this branch of the non-determinism. (c) If the top of stack is the symbol $, enter the accept state. Doing so accepts the input if it has all been read.
Example: SaSb | , ;S q4 q3 b,b; a,a; ,S; ,S;b , ;a ,;$ ,$; ,,S q2 q0 q1 q5
Idea of PDA CFG • Each PDA P has an equivalent CFG G. • First, we simplify our task by modifying P slightly to give it the following three features • It has a single accept state, qaccept. • It empties its stack before accepting. • Each transition either pushes a symbol onto the stack (a push move) or pops one off the stack (a pop move), but does not do both at the same time.
PDA CFG • SupposeP = {Q, , , Δ, q0, {qaccept}}. • To construct G: • The variables of G are {Apq| p, q Q}. Apq generates all the strings that can take P from p with an empty stack to q with an empty stack. • Two possibilities occur during P’s computation on x. Either the symbol popped at the end is the symbol pushed at the beginning or not. First, simulated by Type 1 rules on next slide and the second by Type 2 rules.
PDA CFG • The start variable is Aq0qaccept. Now we describe G’s rules. • [Type 1] For each p, q, r, s Q, d , and b, c , if ((p, b, ), (r, d)) is in Δ and ((s, c, d), (q, )) is in Δ,put the rule Apq bArsc in G. • In other words, find pairs of transitions in the PDA such that the first transition in the pair pushes a symbol t and the second transition pops the same symbol t. Each such pair of transitions gives a Type 1 rule. The states p, q, r, s, and the symbols a, b are determined by looking at the transitions in the pair.
PDA CFG • [Type 2] For each p, q, r Q put the rule Apq AprArq in G. • [Type 3] Finally, for each p Q put the rule App in G.
PDACFG • Statement: A string w is derived by Apq if and only if w takes P from p with empty stack to q with empty stack. • Proof.=>By induction on # of steps of derivations. For 1 step derivation, w = , and p = q, obviously true. Suppose it is true for k steps. For k+1 steps, if the first derivation is ApqAprArq, then, w = xy, x is derived by Apr with less then k+1 steps, and y is derived by Arq with less than k+1 steps. By induction hypothesis, x takes P from q with empty stack to r with empty stack, y takes P from r with empty stack to q with empty stack. Then w takes P from p with empty stack to q with empty stack. If the first derivation is ApqbArsc, then w=bxc, and because of the existence of this rule, there must be transitions ((p, b, ), (r, d)) and ((s, c, d), (q, )) in Δ. Thus by induction hypothesis, x takes P from r with empty stack to s with empty stack, and since at states (p, b, ) and (s, c, d), P pushes and pops the same symbol, bxc takes P from p with empty stack to q with empty stack.
PDACFG <=By induction on # of state transitions. For 0 transition, it must hold that p = q, and w = , obviously true. Suppose it is true for k transitions. For k+1 transitions, suppose w takes P from p with empty stack to q with empty stack, the first transition must push some symbol d. If the first pushed symbol d is popped out of the stack at the last transition, suppose w = bxc, where b and c are the two symbols consumed by P at the first and last transitions, then x takes P from r with empty stack to s with empty stack, where r and s are the second and the last second states. There must be a rue ApqbArsc. By induction hypothesis, Ars derives x. And thus Apq derives w. If the first pushed symbol is popped out of the stack in the middle of the transitions, then there must be a middle state r, such that d is popped out just before P entering state r. Then w = xy, where x takes P from p with empty stack to r with empty stack, and y takes P from r with empty stack to q with empty stack. Then by induction hypothesis, Apr derives x, Arq derives y, and thus Apq derives w = xy by first deriving AprArq.