Pushdown Automata: CSE 211 (Theory of Computation)
Pushdown Automata: CSE 211 (Theory of Computation)
Pushdown Automata
CSE 211 (Theory of Computation)
Assistant Professor
Department of Computer Science and Engineering
Bangladesh University of Engineering & Technology
(()(())(()(())))
To take another example, the set of strings of balanced
parentheses is also nonregular.
However, computer programmers are familiar with a simple
algorithm for recognizing this language:
Start counting at zero,
add one for every left parenthesis,
and subtract one for every right parenthesis.
If the count either goes negative at any time, or ends
up different from zero, then the string should be
rejected as unbalanced;
otherwise it should be accepted.
Now, a counter can be considered as a special case of a
stack, on which only one kind of symbol can be written.
Informal Introduction
Informal Introduction
Informal Introduction
Informal Introduction
Informal Introduction
Informal Introduction
Informal Introduction
a
b b
c ) c
a a
b b
c ) c
a d
b b
c ) c
e
a d
b b
c ) c
In one transition, the pushdown automaton:
3. Replaces the symbol at the top of the stack by any string.
Finally, the top stack symbol could be replaced by two
or more symbols.
This has the effect of (possibly) changing the top stack
symbol, and then pushing one or more new symbols
onto the stack.
CSE 211 PDA
Example
#
01011011
| {z } 1101101
| {z 0}
w wR
P = (Q, ⌃, , , q0 , Z0 , F )
P = (Q, ⌃, , , q0 , Z0 , F )
The components have the following meanings:
Q: A finite set of states, like the states of a finite
automaton.
⌃: A finite set of input symbols, also analogous to the
corresponding component of a finite automaton.
: A finite stack alphabet.
This component, which has no
finite-automaton analog, is the set of symbols
that we are allowed to push onto the stack.
The Formal . . . Automata
P = (Q, ⌃, , , q0 , Z0 , F )
The components have the following meanings:
Q: A finite set of states, like the states of a finite
automaton.
⌃: A finite set of input symbols, also analogous to the
corresponding component of a finite automaton.
: A finite stack alphabet.
This component, which has no
finite-automaton analog, is the set of symbols
that we are allowed to push onto the stack.
The Formal . . . Automata
P = (Q, ⌃, , , q0 , Z0 , F )
The components have the following meanings:
Q: A finite set of states, like the states of a finite
automaton.
⌃: A finite set of input symbols, also analogous to the
corresponding component of a finite automaton.
: A finite stack alphabet.
This component, which has no
finite-automaton analog, is the set of symbols
that we are allowed to push onto the stack.
The Formal . . . Automata
P = (Q, ⌃, , , q0 , Z0 , F )
P = (Q, ⌃, , , q0 , Z0 , F )
P = (Q, ⌃, , , q0 , Z0 , F )
Example
Example-continued
The three states:
q0 : We are yet to see the midpoint of the string.
#
01
| 011011
{z } 11011010
| {z } We remain in q0
w wR
#
01011011 0
| {z } 11011010
| {z } We are in q0
Z0
w wR ) Z0
Thus, our PDA for lwwr can be described as
P = ({q0 , q1 , q2 }, {0, 1}, {0, 1, Z0 }, , q0 , Z0 , {q2 })
where is defined by the following rules:
01011011
| {z } 11011010
# We switch from
| {z } Z0 ) Z0
w wR q1 to q2
No “Mixing and Matching”
Example
The PDA of previous example is represented by the diagram
shown.
2. i) (q0 , 0, 0) =
{(q0 , 00)},
ii) (q0 , 0, 1) =
{(q0 , 01)},
iii) (q0 , 1, 0) =
{(q0 , 10)}, and
iv) (q0 , 1, 1) =
{(q0 , 11)}.
a) The nodes correspond to the states of the PDA.
b) An arrow labeled Start indicates the start state. and doubly circled states are
accepting, as for finite automata.
c) The arcs correspond to transitions of the PDA. An arc labeled a, X /↵ from state
q to state p means that (q, a, X ) contains the pair (p, ↵ ), perhaps among other
pairs.
3. i) (q0 , ✏ , Z0 ) =
{(q1 , Z0 )},
ii) (q0 , ✏ , 0) =
{(q1 , 0)}, and
iii) (q0 , ✏ , 1) =
{(q1 , 1)}.
a) The nodes correspond to the states of the PDA.
b) An arrow labeled Start indicates the start state. and doubly circled states are
accepting, as for finite automata.
c) The arcs correspond to transitions of the PDA. An arc labeled a, X /↵ from state
q to state p means that (q, a, X ) contains the pair (p, ↵ ), perhaps among other
pairs.
4. i) (q1 , 0, 0) =
{(q1 , ✏ )}, and
ii) (q1 , 1, 1) =
{(q1 , ✏ )}.
a) The nodes correspond to the states of the PDA.
b) An arrow labeled Start indicates the start state. and doubly circled states are
accepting, as for finite automata.
c) The arcs correspond to transitions of the PDA. An arc labeled a, X /↵ from state
q to state p means that (q, a, X ) contains the pair (p, ↵ ), perhaps among other
pairs.
5. i) (q1 , ✏ , Z0 ) =
{(q2 , Z0 )}.
CSE 211 PDA
Example
Let us consider the action of
the PDA of previous example
on the input 1111.
Since q0 is the start state and
Z0 is the start symbol, the
initial ID is (q0 , 1111, Z0 ).
On this input, the PDA has an
opportunity to guess wrongly
several times.
The entire sequence of ID’s
that the PDA can reach from
the initial ID (q0 , 1111, Z0 ) is
shown.
Arrows represent the `
relation.
Atif Hasan Rahman Pushdown Automata
CSE 211 PDA
Example
Let us consider the action of
the PDA of previous example
on the input 1111.
Since q0 is the start state and
Z0 is the start symbol, the
initial ID is (q0 , 1111, Z0 ).
On this input, the PDA has an
opportunity to guess wrongly
several times.
The entire sequence of ID’s
that the PDA can reach from
the initial ID (q0 , 1111, Z0 ) is
shown.
Arrows represent the `
relation.
Atif Hasan Rahman Pushdown Automata
P = ({q0 , q1 , q2 }, {0, 1}, {0, 1, Z0 }, , q0 , Z0 , {q2 }) 1111
is defined by the following rules:
1. i) (q0 , 0, Z0 ) = {(q0 , 0Z0 )} and
ii) (q0 , 1, Z0 ) = {(q0 , 1Z0 )}
(q0 , 1111, Z0 )
` (q0 , 111, 1Z0 )
` (q0 , 11, 11Z0 )
` (q1 , 11, 11Z0 )
` (q1 , 1, 1Z0 )
` (q1 , ✏ , Z0 )
` (q2 , ✏ , Z0 )
P = ({q0 , q1 , q2 }, {0, 1}, {0, 1, Z0 }, , q0 , Z0 , {q2 }) 1111
is defined by the following rules:
(q0 , 1111, Z0 )
` (q0 , 111, 1Z0 )
` (q0 , 11, 11Z0 )
` (q1 , 11, 11Z0 )
` (q1 , 1, 1Z0 )
` (q1 , ✏ , Z0 )
` (q2 , ✏ , Z0 )
P = ({q0 , q1 , q2 }, {0, 1}, {0, 1, Z0 }, , q0 , Z0 , {q2 }) 1111
is defined by the following rules:
(q0 , 1111, Z0 )
` (q0 , 111, 1Z0 )
` (q0 , 11, 11Z0 )
` (q1 , 11, 11Z0 )
` (q1 , 1, 1Z0 )
` (q1 , ✏ , Z0 )
` (q2 , ✏ , Z0 )
P = ({q0 , q1 , q2 }, {0, 1}, {0, 1, Z0 }, , q0 , Z0 , {q2 }) 1111
is defined by the following rules:
(q0 , 1111, Z0 )
` (q0 , 111, 1Z0 )
` (q0 , 11, 11Z0 )
` (q1 , 11, 11Z0 )
` (q1 , 1, 1Z0 )
` (q1 , ✏ , Z0 )
` (q2 , ✏ , Z0 )
P = ({q0 , q1 , q2 }, {0, 1}, {0, 1, Z0 }, , q0 , Z0 , {q2 }) 1111
is defined by the following rules:
(q0 , 1111, Z0 )
` (q0 , 111, 1Z0 )
` (q0 , 11, 11Z0 )
` (q1 , 11, 11Z0 )
` (q1 , 1, 1Z0 )
` (q1 , ✏ , Z0 )
` (q2 , ✏ , Z0 )
Instantaneous Descriptions of a PDA
There are three important principles about ID’s and their
transitions that we shall need in order to reason about PDA’s:
1. If a sequence of ID’s (computation) is legal for a PDA P,
then the computation formed by adding the same
additional input string to the end of the input (second
component) in each ID is also legal.
2. If a computation is legal for a PDA P, then the computation
formed by adding the same additional stack symbols below
the stack in each ID is also legal.
3. If a computation is legal for a PDA P, and some tail of the
input is not consumed, then we can remove this tail from
the input in each ID, and the resulting computation will still
be legal.
Intuitively, data that P never looks at cannot affect its
computation.
Instantaneous Descriptions of a PDA
There are three important principles about ID’s and their
transitions that we shall need in order to reason about PDA’s:
1. If a sequence of ID’s (computation) is legal for a PDA P,
then the computation formed by adding the same
additional input string to the end of the input (second
component) in each ID is also legal.
2. If a computation is legal for a PDA P, then the computation
formed by adding the same additional stack symbols below
the stack in each ID is also legal.
3. If a computation is legal for a PDA P, and some tail of the
input is not consumed, then we can remove this tail from
the input in each ID, and the resulting computation will still
be legal.
Intuitively, data that P never looks at cannot affect its
computation.
Instantaneous Descriptions of a PDA
There are three important principles about ID’s and their
transitions that we shall need in order to reason about PDA’s:
1. If a sequence of ID’s (computation) is legal for a PDA P,
then the computation formed by adding the same
additional input string to the end of the input (second
component) in each ID is also legal.
2. If a computation is legal for a PDA P, then the computation
formed by adding the same additional stack symbols below
the stack in each ID is also legal.
3. If a computation is legal for a PDA P, and some tail of the
input is not consumed, then we can remove this tail from
the input in each ID, and the resulting computation will still
be legal.
Intuitively, data that P never looks at cannot affect its
computation.
Instantaneous Descriptions of a PDA
There are three important principles about ID’s and their
transitions that we shall need in order to reason about PDA’s:
1. If a sequence of ID’s (computation) is legal for a PDA P,
then the computation formed by adding the same
additional input string to the end of the input (second
component) in each ID is also legal.
2. If a computation is legal for a PDA P, then the computation
formed by adding the same additional stack symbols below
the stack in each ID is also legal.
3. If a computation is legal for a PDA P, and some tail of the
input is not consumed, then we can remove this tail from
the input in each ID, and the resulting computation will still
be legal.
Intuitively, data that P never looks at cannot affect its
computation.
ID’s for Finite Automata?
Example
Example-continued
Example-continued
Example-continued
Example
Example-continued
Example-continued
(1) (q, a, Z0 ) = (q, aZ0 ),
(2) (q, a, a) = (q, aa),
(3) (q, a, b) = (q, ✏ ),
(4) (q, b, Z0 ) = (q, bZ0 ),
(5) (q, b, b) = (q, bb),
(6) (q, b, a) = (q, ✏ ),
(7) (q, ✏ , Z0 ) = (f , Z0 ).
Example-continued
(1) (q, a, Z0 ) = (q, aZ0 ),
(2) (q, a, a) = (q, aa),
(3) (q, a, b) = (q, ✏ ),
(4) (q, b, Z0 ) = (q, bZ0 ),
(5) (q, b, b) = (q, bb),
(6) (q, b, a) = (q, ✏ ),
(7) (q, ✏ , Z0 ) = (f , Z0 ).
Example-continued
(1) (q, a, Z0 ) = (q, aZ0 ),
(2) (q, a, a) = (q, aa),
(3) (q, a, b) = (q, ✏ ),
(4) (q, b, Z0 ) = (q, bZ0 ),
(5) (q, b, b) = (q, bb),
(6) (q, b, a) = (q, ✏ ),
(7) (q, ✏ , Z0 ) = (f , Z0 ).
Example-continued
(1) (q, a, Z0 ) = (q, aZ0 ),
(2) (q, a, a) = (q, aa),
(3) (q, a, b) = (q, ✏ ),
(4) (q, b, Z0 ) = (q, bZ0 ),
(5) (q, b, b) = (q, bb),
(6) (q, b, a) = (q, ✏ ),
(7) (q, ✏ , Z0 ) = (f , Z0 ).
Language of a PDA
Language of a PDA
P = ({q}, T , V [ T , , q, S)
where is defined by
1 For each variable A,
Example
Theorem
If PDA P is constructed from CFG G by the construction above,
then N(P) = L(G)
Proof
By induction
Skipped
Example
S ! [q0 Z0 q0 ]
(q0 , (, Z0 ) = {(q0 , (Z0 )},
S ! [q0 Z0 q1 ]
(q0 , (, () = {(q0 , (()},
[q0 Z0 q0 ] ! ( [q0 ( q0 ][q0 Z0 q0 ]
(q0 , ), () = {(q0 , ✏ )},
(q0 , ✏ , Z0 ) = {(q1 , ✏ )}. [q0 Z0 q0 ] ! ( [q0 ( q1 ][q1 Z0 q0 ]
The last transition modified [q0 Z0 q1 ] ! ( [q0 ( q0 ][q0 Z0 q1 ]
to empty the stack [q0 Z0 q1 ] ! ( [q0 ( q1 ][q1 Z0 q1 ]
...
Theorem
Let P = (Q, ⌃, , , q0 , Z0 ) be a PDA. Then there is context free
grammar G such that L(G) = N(P)
Proof - skipped
Example
Consider L = {0n 1n 2n |n 1}
|vwx| n and v and x are not both ✏
vwx cannot include both 0’s and 2’s
If vwx has no 2’s then uwy , which should be in L, has
fewer 0’s or 1’s than 2’s
If vwx has no 0’s then uwy has n 0’s but fewer 1’s or 2’s
We get a contradiction, so L cannot be context-free.
Theorem
Suppose we have a parse tree according to a Chomsky Normal
Form grammar G = (V , T , P, S) and suppose that the yield of
the tree is a terminal string w. If the length of the longest path
is n then |w| 2n 1 .
Proof - skipped
Proof
Since k m, there are at least m + 1 occurrences of
variables A0 , A1 , . . . Ak on the path
As there are only m different variables in V at least two of
the last m + 1 variables on the path must be same
Suppose Ai = Aj . Then it is possible to divide the tree as
shown
Proof
Ai = Aj = A
Replace the subtree
rooted at Ai which has
yield vwx by the subtree
rooted at Aj which has
yield w (Fig b)
Corresponds to the case
i = 0 in the pattern of
strings uv i wx i y
Replace the subtree
rooted at Aj by the subtree
rooted at Ai (Fig c)
The yield of this tree is
uv 2 wx 2 y