Lec07 PushdownAutomata
Lec07 PushdownAutomata
Lec07 PushdownAutomata
• If (p,)(q,a,X) where qQ, a{}, X and *. Then for all strings
w* and *, we have a move (transition):
(q,aw,X) ⊢ (p,w,) )
• This move reflects the idea that, by consuming a (which may be ) from the input and
replacing X on top of the stack by we can go from state q to state p.
– Note that what remains on the input, w, and what is below the top of the stack, ,do not
influence the action of the PDA, they are merely carried along.
BASIS:
I ⊢* I for any ID I.
INDUCTION:
If I ⊢* J if there exists ID K such that I ⊢ K and K ⊢* J.
1111
q0
Z0
1111 111
q0 q0
Z0 1
Z0
1111 111 11
q0 q0 q0
Z0 1 1
Z0 1
Z0
BBM401 Automata Theory and Formal Languages 14
Actions of Example PDA
(q0,1111,Z0) ⊢ (q0,111,1Z0) ⊢ (q0,11,11Z0)
⊢ (q1,11,11Z0)
1111 111 11 11
q0 q0 q0 q1
Z0 1 1 1
Z0 1 1
Z0 Z0
BBM401 Automata Theory and Formal Languages 15
Actions of Example PDA
(q0,1111,Z0) ⊢ (q0,111,1Z0) ⊢ (q0,11,11Z0)
⊢ (q1,11,11Z0) ⊢ (q1,1,1Z0)
1111 111 11 11 1
q0 q0 q0 q1 q1
Z0 1 1 1 1
Z0 1 1 Z0
Z0 Z0
BBM401 Automata Theory and Formal Languages 16
Actions of Example PDA
(q0,1111,Z0) ⊢ (q0,111,1Z0) ⊢ (q0,11,11Z0)
⊢ (q1,11,11Z0) ⊢ (q1,1,1Z0) ⊢ (q1,,Z0)
1111 111 11 11 1
q0 q0 q0 q1 q1 q1
Z0 1 1 1 1 Z0
Z0 1 1 Z0
Z0 Z0
BBM401 Automata Theory and Formal Languages 17
Actions of Example PDA
(q0,1111,Z0) ⊢ (q0,111,1Z0) ⊢ (q0,11,11Z0)
⊢ (q1,11,11Z0) ⊢ (q1,1,1Z0) ⊢ (q1,,Z0) ⊢ (q2,,Z0)
1111 111 11 11 1
q0 q0 q0 q1 q1 q1 q2
Z0 1 1 1 1 Z0 Z0
Z0 1 1 Z0
Z0 Z0
BBM401 Automata Theory and Formal Languages 18
PDA Move - Example
• On input 01 the PDA has the following computation sequences:
(q0,01,Z0)
(q1,,10Z0)
• All computations end with an ID whose state is NOT a final state or the input string is NOT
consumed. Thus, 01 is NOT accepted by this PDA.
– (q1,,10Z0) q1 is NOT a final state, and q1 does not have any move.
– (q2,01,Z0) the input (01) is NOT consumed, and q2 does not have any move.
– (q2,1,0Z0) the input (1) is NOT consumed, and q2 does not have any move.
• Starting in the initial ID with w waiting on the input, P consumes w from the input and
enters an accepting state.
– The contents of the stack at that time is irrelevant.
• Since final states are irrelevant for PDAs that accept by empty stack, we do not define
final states for those PDAs.
– That is, a PDA that accepts by empty stack is 6 tuple (not 7 tuple).
• L(P) is the set of inputs w that P can consume and at the same time empty its stack.
– Final states are irrelevant.
,P/0P0
,P/1P1
,P/
0,0/
1,1/
q
(q,,P10) (q,,10)
(q,,0P010) (q,,1P110)
• Since all computations do NOT end with an ID containing empty stack and empty string, the
string 01 is NOT accepted by this PDA.
Theorem:
• If L=L(PF) for some PDA PF = (Q,,,,q0,Z0,F) which accepts by final state, then
there exists another PDA PN which accepts by empty stack such that L=L(PN).
,P/0P0
,P/1P1
,P/
0,0/
P 1,1/
p0 q pf
• We have already shown that (2) and (3) are the same.
• It turns out to be easiest next to show that (1) and (3) are the same, thus Implying the
equivalence of all three.
BBM401 Automata Theory and Formal Languages 34
From Grammars to Pushdown Automata
• Given a CFG G, we construct a PDA that simulates the leftmost derivations of G.
• Any left-sentential form that is not a terminal string can be written as xA, where
– A is the leftmost variable,
– x is whatever terminals appear to the left of A, and
– is the string of terminals and variables that appear to the right of A.
– If a left-sentential form consists of terminals only, then A is .
• Let xA lm x be a derivation step from the left-sentential form xA.
– This derivation step corresponds to the PDA having consumed x and having A
on the top of the stack, and then the PDA on it pops A and pushes into the stack.
P = ({q}, T, V∪T, , q, S)
,P/0P0
,P/1P1 (q,,P) = { (q,0P0), (q,1P1), (q,) }
,P/
0,0/ (q,0,0) = { (q,) }
1,1/
q (q,1,1) = { (q,) }
Proof:
• We have to show that (q,w,S) ⊢* (q,,) if and only if S ⇒* w
• We need to prove something more general:
– We need to show that (q,wx,S) ⊢* (q,x,) for any x if and only if S ⇒*lm w
– After this proof, we can let x==
• Then (q,w,S) ⊢* (q,,) if and only if S ⇒* w
• That is, w is in L(P) if and only if w is in L(G).
Basis: 0 steps
• Then =S, w=, and S ⇒*lm S is surely true.
Induction:
• Consider n moves of P: (q,wx,S) ⊢* (q,x,) and assume the IH for sequences of
n-1 moves.
• There are two cases, depending on whether the last move uses a terminal or a variable
on the top of stack.
Case 1: The move sequence must be of the form (q,yax,S) ⊢* (q,ax,a) ⊢ (q,x,),
where ya = w.
• By the IH applied to the first n-1 steps, S ⇒*lm ya.
• But ya = w, so S ⇒*lm w.
Case 2: The move sequence must be of the form (q,wx,S) ⊢* (q,x,A) ⊢ (q,x,),
where A is a production and = .
• By the IH applied to the first n-1 steps, S ⇒*lm wA.
• Thus, S ⇒*lm w = w.
• Intuition:
– G will have variables generating exactly the inputs that cause P to have the net
effect of popping a stack symbol X while going from state p to state q.
– P never gets below this X while doing so.
Variables of G:
• G’s variables are of the form [pXq] where p∊Q, q∊Q, X∊,
– The variable [pXq] generates all and only the strings w such that
(p,w,X) ⊢* (q,,).
• In addition, G will also have a start symbol S.
• Thus, V = { [pXq] : p∊Q, q∊Q, X∊} ∪ {S}
Productions of G:
• Each production for [pXq] comes from a move of P in state p with stack symbol X.
CASE 2: δ(p,a,X) contains (r,Y) for some state r and a stack symbol Y.
• For each state q∊Q, G has the production [pXq] a[rYq] where a∊∪{ε}.
– We can erase X and go from p to q by reading a (entering state r and replacing X by Y)
and then reading some w that gets P from r to q while erasing Y.
– Note: [pXq] ⇒* aw whenever [rYq] ⇒* w.
There are languages that are not regular, but there are DPDAs for them.
– { wcwR : w∊{0,1}* } is a member of languages of DPDAs but it is not regular.
There are context free languages which are NOT members of languages of DPDAs.
– { wwR : w∊{0,1}* } is NOT a member of languages of DPDAs. i.e. there is NO
DPDA for the language { wwR : w∊{0,1}* }.
– languages of DPDAs context free languages
We can always find an unambiguous grammar for the language of a given DPDA.
– The languages of DPDAs are equal to languages of deterministic context free
grammars.
– Each LR(k) grammar is an unambiguous grammar, but not all unambiguous
grammars are LR(k) grammars.
– The languages of LR(k) grammars (k1) are equal to languages of deterministic context
free grammars (languages of DPDAs).
– LR(k) grammars are widely used in the parsing of programming languages (LR parsers).
Languages of DPDAs
Regular Languages