ر
الجامعة السعودية االلكتونية
ر
االلكتونية الجامعة السعودية
26/12/2021
Theory of Computing
Headline separator Module 6
Pushdown Automata
Contents
1. Definitions of Pushdown Automata
2. Languages of PDA
3. Equivalence of PDAs and CFGs
4. Deterministic Pushdown Automata
Weekly Learning Outcomes
1. Explain Pushdown Automata.
2. Explain equivalence of PDAs and CFGs.
Required Reading
1. Definitions of Pushdown Automata
2. Languages of PDA
3. Equivalence of PDAs and CFGs
4. Deterministic Pushdown Automata
(Introduction to Automata Theory, Languages, and Computation
(2013) Global Edition 3rd Edition)
Recommended Reading
[Link]
[Link]
This Presentation is mainly dependent on the textbook: Introduction to Automata Theory, Languages, and Computation: Global Edition, 3rd edition (2013) PHI
by John E. Hopcroft, Rajeev Motwani and Jeffrey D. Ullman
• Definitions of Pushdown Automata
PDA - The automata for CFLs
• PDA is to CFLs what FA to Regular Languages
• The pushdown automaton is essentially an -NFA with the addition of
a stack.
• The stack can be read, pushed and popped only at the top just like
the stack data structure.
• Two different versions of the pushdown automaton:
• that accepts by entering an accepting state like finite automata do
• another version that accepts by emptying its stack (regardless of its present
state)
• Grammars can be converted to equivalent PDA and vice-versa.
Input -NFA Accept/reject
string
A stack filled with “stack symbols”
7
PDA – Why a Stack?
• The presence of a stack means that unlike the finite automaton the
PDA can remember an infinite amount of information.
• However unlike a general purpose computer which also has the
ability to remember arbitrarily large amounts of information, PDA
can only access the information on its stack in a last-in-first-out
way.
• There are languages that could be recognized by some computer
program but are not recognizable by any PDA.
• In fact PDA recognize all and only the context-free languages.
• There are many languages that are context free including some we
have seen that are not regular languages.
• There are also some simple-to-describe languages that are not
context-free.
• An example of a non-context-free language is {0n1n2n | n ≥ 1}.
8
Pushdown Automata - Definition
The formal definition of a PDA
P = ( Q, ∑, , δ, q0, Z0, F ), where
Q: A finite set of states.
∑: A finite set of input symbols also analogous to the
corresponding component of a finite automaton.
: A finite stack symbols.
δ: The transition function.
q0: The start state.
Z0: Initial stack symbol.
F: The set of accepting states or final states.
9
δ : The Transition Function
old state input symb. Stack top new state(s) new Stack top(s)
δ : Q x ∑ x => Q x
δ(q, a, X) = {(p, Y), …}
1. state transition from q to p
2. a is the next input symbol a X Y
q p
3. X is the current stack top symbol
4. Y is the replacement for X;
it is in * (a string of stack symbols) Y=? Action
i. Set Y = for: Pop(X)
ii. If Y=X: stack top is unchanged i) Y= Pop(X)
iii. If Y=Z1Z2…Zk: X is popped and ii) Y=X Pop(X)
is replaced by Y in reverse order Push(X)
(i.e., Z1 will be the new stack top)
iii) Y=Z1Z2..Zk Pop(X)
Push(Zk)
Push(Zk-1)
…
Push(Z2)
Push(Z1)
10
• Languages of PDA
Example
Let Lwwr = {wwR | w is in (0+1)*}
• CFG for Lwwr : S==> 0S0 | 1S1 |
• PDA for Lwwr :
• P = ( Q, ∑, , δ, q0, Z0, F )
= ({q0, q1, q2}, {0, 1}, {0, 1, Z0 }, δ, q0, Z0, {q2})
12
Initial state of the PDA:
PDA for Lwwr Stack q0
top
Z0
1. δ(q0,0, Z0)={(q0,0Z0)}
2. δ(q0,1, Z0)={(q0,1Z0)} First symbol push on stack
3. δ(q0,0, 0)={(q0,00)}
4. δ(q0,0, 1)={(q0,01)} Grow the stack by pushing
5. δ(q0,1, 0)={(q0,10)} new symbols on top of old
6. δ(q0,1, 1)={(q0,11)} (w-part)
7. δ(q0, , 0)={(q1, 0)}
8. δ(q0, , 1)={(q1, 1)} Switch to popping mode, nondeterministically
9. δ(q0, , Z0)={(q1, Z0)} (boundary between w and wR)
10. δ(q1,0, 0)={(q1, )}
Shrink the stack by popping matching
11. δ(q1,1, 1)={(q1, )}
symbols (wR-part)
12. δ(q1, , Z0)={(q2, Z0)} Enter acceptance state
13
PDA as a state diagram
δ(qi, a, X) = {(qj, Y)}
Next Current Stack
input stack Top
Current symbol top Replacement
state (w/ string Y)
a, X / Y Next
qi qj state
14
PDA for Lwwr: Transition Diagram
Grow stack ∑ = {0, 1}
0, Z0/0Z0 = {Z0, 0, 1}
1, Z0/1Z0 Pop stack for Q = {q0, q1, q2}
0, 0/00 matching symbols
0, 1/01
1, 0/10 0, 0/
1, 1/11 1, 1/
, Z0/Z0 q0 q1 q2
, Z0/Z0 , Z0/Z0
, 0/0
, 1/1 Go to acceptance
Switch to
popping mode
This would be a non-deterministic PDA
15
Example 2: language of balanced parenthesis
Pop stack for ∑ = { (, ) }
matching symbols = {Z0, ( }
Grow stack Q = {q0, q1, q2}
(, Z0 / ( Z0
(, ( / ( ( ), ( /
q0 q1 q2
, Z0 / Z0 ), ( / , Z0 / Z0
, Z0 / Z0 Go to acceptance (by final state)
Switch to when you see the stack bottom symbol
(, ( / ( (
popping mode
(, Z0 / ( Z0
To allow adjacent
blocks of nested paranthesis
16
Example 2: language of balanced parenthesis
(another design)
∑ = { (, ) }
(,Z0 / ( Z0 = {Z0, ( }
(,( / ( ( Q = {q0, q1}
), ( /
start ,Z0/ Z0
q0 q1
,Z0/ Z0
17
PDA’s Instantaneous Description (ID)
A PDA has a configuration at any given instance: (q, w, y)
• q - current state
• w - remainder of the input (i.e., unconsumed part)
• y - current stack contents as a string from top to bottom of
stack
If δ(q, a, X)={(p, A)} is a transition, then the following are also true:
• (q, a, X ) |--- (p, , A)
• (q, aw, XB ) |--- (p, w, AB)
|--- sign is called a “turnstile notation” and represents one
move
|---* sign represents a sequence of moves
18
How does the PDA for Lwwr work on input “1111”?
All moves made by the non-deterministic PDA
(q0,1111,Z0)
(q1,1111,Z0) Path dies…
(q0,111,1Z0)
(q0,11,11Z0) (q1,111,1Z0) Path dies…
(q0,1,111Z0) (q1,11,11Z0)
(q0,,1111Z0) (q1,1,111Z0) (q1,1,1Z0) Acceptance by
final state:
(q1, ,1111Z0) (q1, ,11Z0) (q1, ,Z0) = empty input
Path dies… AND
Path dies… final state
(q2, ,Z0) 19
Acceptance by…
There are two types of PDAs that one can design:
those that accept by final state or by empty stack
• PDAs that accept by final state:
• For a PDA P, the language accepted by P, denoted by L(P)
by final state, is: Checklist:
• {w | (q0,w,Z0) |---* (q,, A) }, s.t., q F - input exhausted?
- in a final state?
• PDAs that accept by empty stack:
• For a PDA P, the language accepted by P, denoted by N(P)
by empty stack, is:
• {w | (q0,w,Z0) |---* (q, , ) }, for any q Q.
Q) Does a PDA that accepts by empty stack Checklist:
need any final state specified in the design? - input exhausted?
21
- is the stack empty?
Example: L of balanced parenthesis
An equivalent PDA that
PDA that accepts by final state accepts by empty stack
(,Z0 / ( Z0
P F: PN: (, ( / ( (
(,Z0 / ( Z0
), ( /
(,( / ( (
,Z0 /
), ( /
start ,Z0/ Z0 start
q0 q1 q0
,Z0/ Z0 ,Z0/ Z0
How will these two PDAs work on the input: ( ( ( ) ) ( ) ) ( )
22
PDAs accepting by final state and empty stack
are equivalent
• PF <= PDA accepting by final state
• PF = (QF, ∑, , δF, q0, Z0, F)
• PN <= PDA accepting by empty stack
• PN = (QN, ∑, , δN, q0, Z0)
• Theorem:
• (PN==> PF) For every PN, there exists a PF s.t. L(PF)=L(PN)
• (PF==> PN) For every PF, there exists a PN s.t. L(PF)=L(PN)
24
PN==> PF construction
How to convert an empty stack PDA into a final state PDA?
• Whenever PN’s stack becomes empty, make PF go to a final
state without consuming any addition symbol
• To detect empty stack in PN: PF pushes a new stack symbol
X0 (not in of PN) initially before simultating PN
PF: PN: , X0 / X0
, X0/Z0X0 , X 0 / X 0
New
p0 q0
, X 0 / X 0 pf
start …
, X0 / X0
, X 0 / X0
PF = (QN U {p0, pf}, ∑, U {X0}, δF, p0, X0, {pf})
25
Example: Matching parenthesis “(” “)”
PN: ( {q0}, {(,)}, {Z0,Z1}, δN, q0, Z0 ) P f: ( {p0,q0 ,pf}, {(,)}, {X0,Z0,Z1}, δf, p0, X0 , pf)
δN: δN(q0,(,Z0) = { (q0,Z1Z0) } δf: δf(p0, ,X0) = { (q0,Z0) }
δN(q0,(,Z1) = { (q0, Z1Z1) } δf(q0,(,Z0) = { (q0,Z1 Z0) }
δN(q0,),Z1) = { (q0, ) } δf(q0,(,Z1) = { (q0, Z1Z1) }
δf(q0,),Z1) = { (q0, ) }
δN(q0, ,Z0) = { (q0, ) } δf(q0, ,Z0) = { (q0, ) }
δf(p0, ,X0) = { (pf, X0 ) }
(,Z0 /Z1Z0 (,Z0/Z1Z0
(,Z1 /Z1Z1 (,Z1/Z1Z1
),Z1 / ),Z1/
,Z0 / ,Z0/
start start
,X0/Z0X0 ,X0/ X0
q0 q0
p0 pf
Accept by empty stack Accept by final state
26
PF==> PN construction
How to convert an final state PDA into an empty stack PDA?
• Main idea:
• Whenever PF reaches a final state, just make an -transition into a new end
state, clear out the stack and accept
• Danger: What if PF design is such that it clears the stack midway without
entering a final state?
➔ to address this, add a new start symbol X0 (not in of PF)
PN = (Q U {p0,pe}, ∑, U {X0}, δN, p0, X0)
PN:
, X0/Z0X0 , any/ , any/
New
p0 q0
, any/ pe
start …
, any/
PF
27
• Equivalence of PDAs and CFGs
CFGs == PDAs ==> CFLs
PDA by PDA by
≡
final state empty stack
?
CFG
29
Converting CFG to PDA
This is same as: “implementing a CFG using a PDA”
Main idea: The PDA simulates the leftmost derivation on a given w, and
upon consuming it fully it either arrives at acceptance (by empty stack)
or non-acceptance.
accept
PDA
OUTPUT
INPUT
w (acceptance by
empty stack)
reject
implements
CFG
30
Converting a CFG into a PDA
This is same as: “implementing a CFG using a PDA”
Main idea: The PDA simulates the leftmost derivation on a given w, and
upon consuming it fully it either arrives at acceptance (by empty stack)
or non-acceptance.
Steps:
1. Push the right hand side of the production onto the stack, with
leftmost symbol at the stack top
2. If stack top is the leftmost variable, then replace it by all its
productions (each possible substitution will represent a distinct
path taken by the non-deterministic PDA)
3. If stack top has a terminal symbol, and if it matches with the next
symbol in the input string, then pop it
State is inconsequential (only one state is needed)
31
Formal construction of PDA from CFG
• Given: G = (V, T, P, S) Note: Initial stack symbol (S)
same as the start variable
• Output: PN = ({q}, T, V U T, δ, q, S)
in the grammar
• δ:
• For all A V , add the following transition(s) in the PDA:
• δ(q, , A) = { (q, ) | “A ==>” P}
• For all a T, add the following
transition(s) in the PDA:
• δ(q, a, a)= { (q, ) }
Before: After:
A
…
…
Before: After: a…
a a pop
…
…
32
Example: CFG to PDA
• G = ( {S, A}, {0, 1}, P, S)
• P:
• S ==> AS | 1,1 /
• A ==> 0A1 | A1 | 01 0,0 /
,A / 01
,A / A1
• PDA = ({q}, {0, 1}, {0, 1, A, S}, δ, q, S) ,A / 0A1
,S /
• δ: ,S / AS
,S / S
• δ(q, , S) = { (q, AS), (q, )} q
• δ(q, , A) = { (q, 0A1), (q, A1), (q, 01) }
• δ(q, 0, 0) = { (q, ) }
• δ(q, 1, 1) = { (q, ) } How will this new PDA work?
Lets simulate string 0011
33
Simulating string 0011 on the new PDA …
Leftmost deriv.:
1,1 /
0,0 /
PDA (δ): ,A / 01 S => AS
,A / A1 => 0A1S
δ(q, , S) = { (q, AS), (q, )}
,A / 0A1
δ(q, , A) = { (q, 0A1), (q, A1), (q, 01) } ,S / => 0011S
δ(q, 0, 0) = { (q, ) } ,S / AS => 0011
δ(q, 1, 1) = { (q, ) }
,S / S
Stack moves (shows only the successful path): q
0 0
A A 1 1
A 1 1 1 1 1 Accept by
S S S S S S S S
empty stack
0 0 1 1
S =>AS =>0A1S =>0011S => 0011
34
Converting a PDA into a CFG
• Main idea: Reverse engineer the productions
from transitions
If δ(q, a, Z) => (p, Y1Y2Y3…Yk):
1. State is changed from q to p;
2. Terminal a is consumed;
3. Stack top symbol Z is popped and replaced with a sequence of k
variables.
• Action: Create a grammar variable called “[qZp]” which includes the
following production:
• [qZp] => a[pY1q1] [q1Y2q2] [q2Y3q3]… [qk-1Ykqk]
36
Example: Bracket matching
• To avoid confusion, we will use b=“(“ and e=“)”
PN: ( {q0}, {b, e}, {Z0, Z1}, δ, q0, Z0 )
0. S => [q0Z0q0]
1. δ(q0, b, Z0) = { (q0, Z1Z0) } 1. [q0Z0q0] => b [q0Z1q0] [q0Z0q0]
2. δ(q0, b, Z1) = { (q0, Z1Z1) } 2. [q0Z1q0] => b [q0Z1q0] [q0Z1q0]
3. [q0Z1q0] => e
3. δ(q0, e, Z1) = { (q0, ) }
4. [q0Z0q0] =>
4. δ(q0, , Z0) = { (q0, ) }
Let A=[q0Z0q0] Simplifying,
Let B=[q0Z1q0]
If you were to directly write a CFG: 0. S => b B S |
0. S => A 1. B => b B B | e
1. A => b B A
S => b S e S | 2. B => b B B
3. B => e
4. A =>
37
Two ways to build a CFG
Build a PDA Construct (indirect)
CFG from PDA
Derive CFG directly (direct)
Similarly… Two ways to build a PDA
Derive a CFG Construct
PDA from CFG (indirect)
Design a PDA directly (direct)
38
• Deterministic Pushdown Automata
This PDA for Lwwr is non-deterministic
Grow stack
0, Z0/0Z0
Why does it have to
1, Z0/1Z0 Pop stack for be nondeterministic?
0, 0/00 matching symbols
0, 1/01
1, 0/10 0, 0/
1, 1/11 1, 1/
q0 q1 q2
, Z0/Z0 , Z0/Z0
, 0/0
, 1/1 Accepts by final state
Switch to To remove guessing,
popping mode impose the user to
insert c in the middle
40
D-PDA for Lwcwr = {wcwR | c is some special
symbol not in w}
Example shows that: Nondeterministic PDAs ≠ DPDAs
Note:
• all transitions have
Grow stack become deterministic
0, Z0/0Z0
Pop stack for
1, Z0/1Z0 matching symbols
0, 0/00
0, 1/01 0, 0/
1, 0/10 1, 1/
1, 1/11
q0 q1 q2
c, Z0/Z0 , Z0/Z0
c, 0/0
c, 1/1 Accepts by
Switch to final state
popping mode
41
Deterministic PDA: Definition
• A PDA is deterministic if and only if:
1. δ(q, a, X) has at most one member for any a ∑ U {}
➔ If δ(q, a, X) is non-empty for some a∑, then δ(q, , X) must be
empty.
42
PDA vs DPDA vs Regular languages
Lwcwr
Lwwr
D-PDA
Regular languages
non-deterministic PDA
43
Main Reference
1. Definitions of Pushdown Automata
2. Languages of PDA
3. Equivalence of PDAs and CFGs
4. Deterministic Pushdown Automata
(Introduction to Automata Theory, Languages, and Computation
(2013) Global Edition 3rd Edition)
Additional References
[Link]
[Link]
Thank You