2. 2
Pushdown Automata (PDA)
• Informally:
– A PDA is an NFA-ε with a stack.
– Transitions are modified to accommodate stack operations.
• Questions:
– What is a stack?
– How does a stack help?
• A DFA can “remember” only a finite amount of information, whereas a PDA can
“remember” an infinite amount of (certain types of) information, in one memory-stack
3. 3
• Example:
{0n1n | 0=<n}
is not regular, but
{0n1n | 0≤n≤k, for some fixed k} is regular, for any
fixed k.
• For k=3:
L = {ε, 01, 0011, 000111}
0/1
q
0
q
7
0 q
1
1
1
q
2
1
q
5
0 q
3
1
1
q
4
0
1
0
0
0/1
q
6
0
4. 4
• In a DFA, each state remembers a finite amount of information.
• To get {0n1n | 0≤n} with a DFA would require an infinite number of states
using the preceding technique.
• An infinite stack solves the problem for {0n1n | 0≤n} as follows:
– Read all 0’s and place them on a stack
– Read all 1’s and match with the corresponding 0’s on the stack
• Only need two states to do this in a PDA
• Similarly for {0n1m0n+m | n,m≥0}
6. 6
Formal Definition of a PDA
• A pushdown automaton (PDA) is a seven-tuple:
M = (Q, Σ, Г, δ, q0, z0, F)
Q A finite set of states
Σ A finite input alphabet
Г A finite stack alphabet
q0 The initial/starting state, q0 is in Q
z0 A starting stack symbol, is in Г // need not always remain at the bottom of stack
F A set of final/accepting states, which is a subset of Q
δ A transition function, where
δ: Q x (Σ U {ε}) x Г –> finite subsets of Q x Г*
7. 7
• Consider the various parts of δ:
Q x (Σ U {ε}) x Г –> finite subsets of Q x Г*
– Q on the LHS means that at each step in a computation, a PDA must consider its’
current state.
– Г on the LHS means that at each step in a computation, a PDA must consider the
symbol on top of its’ stack.
– Σ U {ε} on the LHS means that at each step in a computation, a PDA may or may
not consider the current input symbol, i.e., it may have epsilon transitions.
– “Finite subsets” on the RHS means that at each step in a computation, a PDA may
have several options.
– Q on the RHS means that each option specifies a new state.
– Г* on the RHS means that each option specifies zero or more stack symbols that
will replace the top stack symbol, but in a specific sequence.
8. 8
• PDA transitions:
δ(q, a, z) = {(p1,γ1), (p2,γ2),…, (pm,γm)}
– Current state is q
– Current input symbol is a
– Symbol currently on top of the stack z
– Move to state pi from q
– Replace z with γi on the stack (leftmost symbol on top)
– Move the input head to the next input symbol
:
q
p
1
p
2
p
m
a/z/ γ1
a/z/ γ2
a/z/ γm
9. 9
• Two types of PDA transitions:
δ(q, ε, z) = {(p1,γ1), (p2,γ2),…, (pm,γm)}
– Current state is q
– Current input symbol is not considered
– Symbol currently on top of the stack z
– Move to state pi from q
– Replace z with γi on the stack (leftmost symbol on top)
– No input symbol is read
:
q
p
1
p
2
p
m
ε/z/ γ1
ε/z/ γ2
ε/z/ γm
17. Exercise
• Construct PDA for acceptance by empty stack.
1. L={a2n bn | n>=0 }
1. L = {0n1n2m3m | n>=1, m>=1}
2. L={0n1m0n | m, n>=1}.
3. L= { a i b j c k | i, j, k ≥ 0 and i + j = k }
• Construct PDA for acceptance by Final state
1. L = { a n b n c m | m, n ≥ 1 }
1. L = {0n1m2m3n | n>=1, m>=1}
2. L={an b2n | n>=0 }
– L={an b3n | n>=1 }
1. L={a3n bn | n>=0 }
17
18. 18
• Definition: Let M = (Q, Σ, Г, δ, q0, z0, F) be a PDA. The language
accepted by empty stack, denoted LE(M), is the set
LE(M) = {w | (q0, w, z0) |—* (p, ε, ε) for some
p in Q}
• Definition: Let M = (Q, Σ, Г, δ, q0, z0, F) be a PDA. The language
accepted by final state, denoted LF(M), is the set
LE(M)= {w | (q0, w, z0) |—* (p, ε, γ) for some p in F and γ in
Г*}
• Definition: Let M = (Q, Σ, Г, δ, q0, z0, F) be a PDA. The language
accepted by empty stack and final state, denoted L(M), is the set
LE(M)= {w | (q0, w, z0) |—* (p, ε, ε) for some p in F}
Language Acceptance
21. Problem 1: CFG - PDA
Convert the grammar S-> aAA, A->aS/bS/a to a PDA that accepts the same language
by empty stack
Solution :
Let G be a CFG and G=(V,T,P,S) where V={S,A} T={a,b}
p: S->aAA, A->aS/bS/a.
To find the equivalent PDA
Q={q0}
∑=T={a,b}
Г=VUT={S,A,a,b}
F=Ø
Transition function for PDA:
– For each variable S,A
δ(q0,Є,S)={(q0,aAA)}
δ(q0,Є,A)={(q0,aS),(q0,bS),(q0,a)}
For each terminal a,b
δ(q0,a,a)={q0,Є}
δ(0,b,b)={q0,Є}
21
22. Problem 2: CFG - PDA
• Convert the grammar S-> 0S1/A, A->1A0/S/є to a PDA that accepts the same language by
empty stack.
Solution :
Let G be a CFG and G=(V,T,P,S) where V={S,A} T={a,b}
p: S->aAA, A->aS/bS/a.
To find the equivalent PDA
Q={q0}
∑=T={0,1}
Г=VUT={S,A,0,1}
F=Ø
Transition function for PDA:
– For each variable S,A
δ(q0,Є,S)={(q0,0S1), (q0,A)}
δ(q0,Є,A)={(q0,1A0),(q0,S),(q0, Є)}
For each terminal 0,1
δ(q0,0,0)={q0,Є}
δ(q0,1,1)={q0,Є}
22
23. Problem 3: CFG – PDA
Construct PDA for the given CFG, and test whether 0104 is acceptable by this PDA.
S → 0BB B → 0S | 1S | 0
23
Solution:
The PDA can be given as:
1.A = {(q), (0, 1), (S, B, 0, 1), δ, q, S, ?}
The production rule δ can be:
R1: δ(q, ε, S) = {(q, 0BB)}
R2: δ(q, ε, B) = {(q, 0S) | (q, 1S) | (q, 0)}
R3: δ(q, 0, 0) = {(q, ε)}
R4: δ(q, 1, 1) = {(q, ε)}
Testing 0104 i.e. 010000 against PDA:
δ(q, 010000, S) ⊢ δ(q, 010000, 0BB)
⊢ δ(q, 10000, BB) R1
⊢ δ(q, 10000,1SB) R3
⊢ δ(q, 0000, SB) R2
⊢ δ(q, 0000, 0BBB) R1
⊢ δ(q, 000, BBB) R3
⊢ δ(q, 000, 0BB) R2
⊢ δ(q, 00, BB) R3
⊢ δ(q, 00, 0B) R2
⊢ δ(q, 0, B) R3
⊢ δ(q, 0, 0) R2
⊢ δ(q, ε) R3
ACCEPT
Thus 0104 is accepted by the PDA.
24. 24
CFG to PDA conversion
Formally, the given PDA is
M = (Q, Σ, Г, δ, q0, z0, F). Define CFG G=(V,
T, P, S), where
V=[p x q] for all p & q Є Q and x Є Σ,
T= Σ,
P=Set of Production rules constructed from δ
And S=Starting Symbol
25. Rules to construct P using δ
R1 – Production Rules for S
S 🡪 [q0 z0 p] for all p Є Q
R2 – Production Rules corresponding to the
transition move for pop operation
(q, a, z) = (p, ϵ)
[q z p] 🡪a
26. Cont…
R3 - Production Rules corresponding to the
transition move for push and read operation
(q, a, z) = (q’, z1z2…….zn)
[q z p] 🡪 a [q’ z1 q1] [q1 z2 q2] ………[qn zn p]
33. Final State to Empty Stack PDA
33
(p0, w,X0) |- (q0, w, Z0X0) |-* (q, ε , αX0) |- ( p, ε ,ε )
Example : Design a PDA to check for well-formed parentheses
34. Empty Stack to Final State PDA
34
(p0, w,X0) |- (q0, w, Z0X0) |-* (q, ε , X0) |- ( pf, ε ,ε )
Example : Design a PDA to check for well-formed parentheses
37. Is Npda more powerful
than DPDA?
• Power of NPDA is more than DPDA. It is
not possible to convert every NPDA to
corresponding DPDA. ... The languages
accepted by DPDA are called DCFL
(Deterministic Context Free Languages)
which are subset of NCFL (Non
Deterministic CFL) accepted by NPDA
37
39. Closure Properties of Context
Free Grammar
Context-free languages are closed under −
•Union- If L1 and L2 are CFL’s then L1ꓴL2 is
also CFL.
•Concatenation- If L1 and L2 are CFL’s then
L1L2 is also CFL.
•Kleene Star- If L1 is CFL then L*1 is also
CFL.
40. Closure under Union
• Begin with two grammars: G1 = (V1, Σ , P1, S1) and G2
= (V2, Σ , P2, S2), generating CFL’s L1 and L2
respectively.
• The new CFG Gx is made as:
– Σ remains the same
– Sx is the new start variable
– Vx = V1 ∪ V2 ∪ {Sx}
– Px = P1 ∪ P2 ∪ {Sx → S1|S2}
• Explanation: All we have done is augment the variable
set with a new start state and then allowed the new start
state to map to either of the two grammars. So, we’ll
generate strings from either L1 or L2, i.e. L1 ꓴ L2
41. Example
• Let L1 = { anbn , n > 0}. Corresponding
grammar G1 will have P: S1 → aAb|ab
• Let L2 = { cmdm , m ≥ 0}. Corresponding
grammar G2 will have P: S2 → cBb| ε
• Union of L1 and L2, L = L1 ∪ L2 = { anbn } ∪
{ cmdm }
• The corresponding grammar G will have the
additional production S → S1 | S2
42. Closure under Concatenation
• Begin with two grammars: G1 = (V1, Σ , P1, S1) and G2 =
(V2, Σ , P2, S2), generating CFL’s L1 and L2 respectively.
• The new CFG Gy is made as:
– Σ remains the same
– Sy is the new start variable
– Vy = V1 ꓴ V2 ꓴ {Sy}
– Py = P1 ꓴ P2 ꓴ {Sx → S1S2}
• Explanation: Again, all we have done is to augment the
variable set with a new start state, and then allowed the
new start state to map to the concatenation of the two
original start symbols. So, we will generate strings that
begin with strings from L1 and end with strings from L2,
i.e. L1L2 .
43. Example
• Let L1 = { anbn , n > 0}. Corresponding
grammar G1 will have P: S1 → aAb|ab
• Let L2 = { cmdm , m ≥ 0}. Corresponding
grammar G2 will have P: S2 → cBb| ε
• Concatenation of the languages L1 and L2, L =
L1L2 = { anbncmdm }
• The corresponding grammar G will have the
additional production S → S1 S2
44. Clouser under Kleene Star
• Begin with two grammars: G1 = (V1, Σ , P1, S1) and
G2 = (V2, Σ , P2, S2), generating CFL’s L1 and L2
respectively.
• The new CFG Gz is made as:
– Σ remains the same
– Sz is the new start variable
– Vz = V1 ꓴ {Sz}
– Pz = P1 ꓴ {Sz → S1Sz | ε}
• Explanation: Again we have augmented the variable
set with a new start state, and then allowed the new
start state to map to either S1Sz or ε. This means we
can generate strings with zero or more strings made
from expanding the variable S1, i.e. L*1 .
45. Example
• Let L = { anbn , n ≥ 0}. Corresponding
grammar G will have P: S → aAb| ε
• Kleene Star L1 = { anbn }*
• The corresponding grammar G1 will have
additional productions S1 → SS1 | ε
46. Context-free languages are not closed under −
•Intersection − If L1 and L2 are context free
languages, then L1 ∩ L2 is not necessarily
context free.
•Intersection with Regular Language − If L1 is a
regular language and L2 is a context free
language, then L1 ∩ L2 is a context free
language.
•Complement − If L1 is a context free language,
then L1’ may not be context free.