Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 11  2004 SDU Lecture7 Pushdown Automaton.

Similar presentations


Presentation on theme: "Lecture 11  2004 SDU Lecture7 Pushdown Automaton."— Presentation transcript:

1 Lecture 11  2004 SDU Lecture7 Pushdown Automaton

2  2004 SDU 2 Push Down Automaton (PDA) Language Acceptor Model for CFLs It is an NFA with a stack. Finite State control Stack input

3  2004 SDU 3 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.

4  2004 SDU 4 PDA (contd.) If PDA in state q can consume u, pop x from stack, change state to p, and push w on stack We show it as u, x  w p q u, x; w

5  2004 SDU 5 Pushdown automata A pushdown automaton is a 6-tuple (Q, , , , q 0, F), where Q, , , F are finite sets, and 1. Q is the set of states, 2.  is the input alphabet, 3.  is the stack alphabet, 4.  : Q        P(Q    ) is the transition function, 5.q 0  Q is the start state, and 6.F  Q is the set of accept states.

6  2004 SDU 6 Pushdown automata (cont.) M = (Q, , , , q 0, F) accepts a string w  * if w can be represented as w 1 …w m with w 1, …, w m   and there exist r 0, …, r m  Q and s 0, …, s m    * satisfying the following conditions: 1.r 0 = q 0 and s 0 =  2.For every i, 0  i  m-1, (r i, b)   (r i-1, w i, a), where s i-1 = at and s i = bt for some a, b    and t   * 3.r m  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}.

7  2004 SDU 7 PDA, example 1 L = { 0 n 1 n | n  0} Design idea: 1.Use a special symbol $ to mark the bottom of the stack (using  wouldn’t work since  is always there) 2.First put onto the stack all the 0s preceding the 1s 3.Then try to match the stacked 0s with the 1s 4.The input string is in L, if and only if, the bottom $ is the top symbol when all the input symbols have been consumed.

8  2004 SDU 8 Example 1 (cont.)  ={0,$}, Q={q 1,q 2,q 3,q 4 }, F = {q 1, q 4 }, q 1 is the start state. We use q 1 to place the $ on the stack, q 2 to read 0s and push them onto the stack (and react to the first 1), and q 3 to read 1s and pop 0s from the stack (and react to the $ by going to accept state q 4 ). As a diagram: q1q1 q4q4 q2q2 q3q3 ,$,$ 0,   0 1,0   , $  

9  2004 SDU 9 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.

10  2004 SDU 10 CFG  PDA Given CFG G = (V, , R, S) Let PDA M = (Q, ,   V  {$}, , q start, {q accept })  Q = {q start, q loop, q accept }  contains transitions for the form 1.  ’ (q start, ,  ) = (q loop, S$) ** 2.  ’ (q loop, , A) = {(q loop, w) | A  w  R } ** 3.For each symbol     (q loop, ,  ) = (q loop,  )  (q loop, , $) = (q accept,  ) q start q loop q accept ,  S$ , $   , 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.

11  2004 SDU 11 CFG  PDA The PDA simulates a leftmost derivation of the string. 1.Place the marker symbol $ and the start variable on the stack. 2.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.

12  2004 SDU 12 Example: S  aSb |  q0q0 q1q1 q2q2 q3q3 q4q4 q5q5 ,  ;$ , ,S ,S;b ,  ;S ,  ;a ,$;  b,b;  a,a;  ,S; 

13  2004 SDU 13 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 1.It has a single accept state, q accept. 2.It empties its stack before accepting. 3.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.

14  2004 SDU 14 PDA  CFG Suppose P = {Q, , , Δ, q 0, {q accept }}. To construct G: The variables of G are {A pq | p, q  Q}. A pq 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.

15  2004 SDU 15 PDA  CFG The start variable is A q0qaccept. 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 A pq  bA rs c 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.

16  2004 SDU 16 PDA  CFG  [Type 2] For each p, q, r  Q put the rule A pq  A pr A rq in G.  [Type 3] Finally, for each p  Q put the rule A pp   in G.

17  2004 SDU 17 PDA  CGF

18  2004 SDU 18 PDA  CFG Statement: A string w is derived by A pq 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 A pq  A pr A rq, then, w = xy, x is derived by A pr with less then k+1 steps, and y is derived by A rq 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 A pq  bA rs c, 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.

19  2004 SDU 19 PDA  CFG <=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 A pq  bA rs c. By induction hypothesis, A rs derives x. And thus A pq 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, A pr derives x, A rq derives y, and thus A pq derives w = xy by first deriving A pr A rq.


Download ppt "Lecture 11  2004 SDU Lecture7 Pushdown Automaton."

Similar presentations


Ads by Google