Upcoming SlideShare
×

# Pda

302 views
194 views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
302
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
7
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Pda

1. 1. Pushdown Automata and Context-free Grammars 218
2. 2. Context Free Grammars and Pushdown AutomataTheorem: The following are equivalent • Context-free languages, i.e., languages deﬁned by context-free grammars. • Languages accepted by empty stack by some PDA. • Languages accepted by ﬁnal state by some PDA.We have already shown the equivalence of the two acceptance modes of aPDAWe will now show • For every grammar G there is a PDA P such that L(G) = N (P ) • For every PDA P there is a grammar G such that N (P ) = L(G) 219
3. 3. From Grammars to PDAsGiven a CFG G we will construct a PDA P that simulates the leftmostderivations of G.Each leftmost derivation consists of a sequence of left sentential forms,each of which is derived from the previous one by using one of theproductions.Question: If P simulates a left most derivation of G, then how does Prepresent a left sentential form β of variables and terminals? 220
4. 4. Tail of Sentential FormsDeﬁnition: Any left sentential form (in fact any sentential form) is of theform xAα where x is a string of terminals, A is a variable, and α is astring of terminals and variables. Aα is called the tail of this leftsentential form. • If the left sentential form consists only of terminals then the tail is ǫ. 221
5. 5. Representing Sentential FormsConsider a leftmost derivation string read Þ ß S |⇒lm aAB |⇒lm aCB |⇒lm abcdBdcB|A left sentential form xAα is represented by • The tail Aα being on the stack • x being consumed from the inputIf w = xy is the input to the PDA, then the instantaneous description q, y, Aα represents the sentential form xAα. 222
6. 6. Construction DetailsSuppose the PDA P is in q, y, Aα . In order to simulate the next step ofthe leftmost derivation, P chooses a production A → β, and moves to q, y, βα by replacing A by β on the stack.Note: The state of P does not seem to play any role.Does q, y, βα represent the next left sentential form in the derivation?No! β may have a preﬁx of terminals (or maybe a string of all terminals).P therefore will then pop the terminals at the begining of βα to exposethe next non-terminal. P will match the terminals popped with thesymbols in the input.If P successfully chose the right production rule in each step, then at thelast step of the derivation when we have w, a string of terminals, allsymbols on the stack have either been expanded (because they werevariables) or have been matched with the input. So stack is empty. 223
7. 7. Formal ConstructionLet G = (V, T, R, S) be a CFG. The PDA P = ({q}, T, V ∪ T, δ, q, S)where the transition function is deﬁned as follows: 1. For each variable A, δ(q, ǫ, A) = {(q, β) | A → β is a production of R} 2. For each terminal a, δ(q, a, a) = {(q, ǫ)} 224
8. 8. ExampleConsider a grammar with rules S → aAB, A → C|a, B → b|A, C → bBd.A run of P looks like Input Left Stack Derivation abbdb S| S abbdb aAB | aAB bbdb AB | − bbdb CB | aCB bbdb bBdB | abBdB bdb BdB | − bdb bdB | abbdB db dB | − b B| − b b| abbdb ǫ ǫ| − 225
9. 9. Correctness: L(G) ⊆ N (P )Suppose w ∈ L(G). Consider its leftmost derivation S = x1 α1 ⇒lm x2 α2 ⇒lm · · · ⇒lm xn αnwhere αi is the tail of the ith left sentential form. Observe that x1 = ǫand α1 = S, xn = w and αn = ǫ.Let w = xi yi for each i. We will show that q, w, S ⊢∗ q, yi , αi for eachi, by induction. • Base Case: Let i = 1. Trivial. • Induction Step: We assume q, w, S ⊢∗ q, yi , αi . Since αi is a tail, it begins with a variable A. Moreover, the step xi αi ⇒lm xi+1 αi+1 involves replacing A by the body β of one of its productions. Rule (1) of P ’s transition allows one to replace A by β, and rule (2) allows P to match the terminals on the top with input symbols. Thus we can reach q, yi+1 , αi+1 .Since αn = yn = ǫ, we know that q, w, S ⊢∗ q, ǫ, ǫ . Hence w ∈ N (P ). 226
10. 10. Correctness: N (P ) ⊆ L(G) ∗We will show that if q, x, A ⊢∗ q, ǫ, ǫ then A ⇒ x • Observe that if q, x, A ⊢∗ q, ǫ, ǫ then q, xy, Aα ⊢∗ q, y, α . In other words, we will prove that if P in a sequence of moves, whose net eﬀect of popping A, consumes x from the input then A derives x.If we show the above result then for w ∈ N (P ), we know ∗ q, w, S ⊢∗ q, ǫ, ǫ . So S ⇒ w, which means w ∈ L(G). 227
11. 11. Correctness (continued) ∗To show that if q, x, A ⊢∗ q, ǫ, ǫ then A ⇒ x. We do this by inductionon the number of moves of P . • Base Case: If P makes only one move then it must be the case that x = ǫ and A → ǫ is a production. Hence A ⇒ x. • Induction Step: Let P take n > 1 moves. The ﬁrst move must be of type (1) where A is replaced by the body of one if its productions, say Y1 Y2 · · · Yk . The next n − 1 moves consume symbols from x and have the net eﬀect of popping Y1 through Yk in order. Divide x into x1 x2 · · · xk where xi is portion of the input that is consumed while Yi is popped. 228
12. 12. Proof continued Now for every i, q, xi xi+1 · · · xk , Yi Yi+1 · · · Yk ⊢∗ Y1 q, xi+1 · · · xk , Yi+1 · · · Yk Y2 In fact, because our assumptionStack is that the symbols Yi+1 , . . . Yk are not touched while xi is consumed, Yk we have q, xi , Yi ⊢∗ q, ǫ, ǫ in less than x1 x2 xk n − 1 steps. Now if Yi is a terminal, then input ∗ xi = Yi and Yi ⇒ xi . If Yi is a variable, then from the hypothesis, we can conclude ∗ Yi ⇒ xi . Thus the derivation is, ∗ ∗ ∗ ∗ A ⇒ Y1 Y2 · · · Yk ⇒ x1 Y2 · · · Yk ⇒ x1 x2 · · · Yk ⇒ · · · ⇒ x1 x2 · · · xk 229