0% found this document useful (0 votes)
5 views130 pages

Pushdown Automata: CSE 211 (Theory of Computation)

Uploaded by

2105044
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views130 pages

Pushdown Automata: CSE 211 (Theory of Computation)

Uploaded by

2105044
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 130

CSE 211

Pushdown Automata
CSE 211 (Theory of Computation)

Atif Hasan Rahman

Assistant Professor
Department of Computer Science and Engineering
Bangladesh University of Engineering & Technology

Adapted from slides by


Dr. Muhammad Masroor Ali

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

The context free languages have a type of automaton that


defines them.
This automaton is called “pushdown automaton.”
It is an extension of the nondeterministic finite automaton
with ✏ -transitions.
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.

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

Languages which can not be Recognized by an FA

Consider ww R : w 2 {a, b}⇤ .


It is context-free, since it is generated by the grammar with
rules S ! aSa, S ! bSb, and S ! ✏ .
It would seem that any device that recognizes the strings in
this language
by reading them from left to right must “remember” the
first half of the input string,
so that it can check it — in reverse order — against
the second half of the input.

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

Languages which can . . . an FA

It is not surprising that this function cannot be performed


by a finite automaton.
If, however, the machine is,
capable of accumulating its input string as it is read,
appending symbols one at a time to a stored string,
then it could nondeterministically guess when the
center of the input has been reached,
and thereafter check the symbols off from its memory
one at a time.
The storage device need not be a general-purpose one.
A sort of “stack” or “pushdown store,” allowing read and
write access only to the top symbol, would do nicely.

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

Languages which can . . . an FA

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

Languages which can . . . an FA

(()(())(()(())))
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.

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

Informal Introduction

The pushdown automaton is in essence a


nondeterministic finite automaton with ✏ -transitions
permitted.
It has one additional capability: a stack on which it can
store a string of “stack symbols.”
The presence of a stack means that, unlike the finite
automaton, the pushdown automaton 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, the pushdown automaton can only access the
information on its stack in a last-in-first-out way.

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

Informal Introduction

As a result, there are languages that could be recognized


by some computer program, but are not recognizable by
any pushdown automaton.
In fact, pushdown automata 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
{0n 1n 2n | n 1}, the set of strings consisting of equal
groups of 0’s, 1’s, and 2’s.

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

Informal Introduction

As a result, there are languages that could be recognized


by some computer program, but are not recognizable by
any pushdown automaton.
In fact, pushdown automata 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
{0n 1n 2n | n 1}, the set of strings consisting of equal
groups of 0’s, 1’s, and 2’s.

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

Informal Introduction

We can view the pushdown automaton informally as the device


suggested in figure.

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

Informal Introduction

A “finite-state control” reads inputs, one symbol at a time.

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

Informal Introduction

The pushdown automaton observes the symbol at the top


of the stack.
It bases its transition on its current state, the input symbol,
and the symbol at the top of stack.

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

Informal Introduction

Alternatively, it may make a spontaneous transition, using


✏ as its input instead of an input symbol.

Atif Hasan Rahman Pushdown Automata


Informal Introduction

In one transition, the pushdown automaton:


1. Consumes from the input the symbol that it uses in the
transition.
If ✏ is used for the input, then no input symbol is
consumed.
Informal Introduction

In one transition, the pushdown automaton:


2. Goes to a new state, which may or may not be the same as
the previous state.
Informal Introduction

a
b b
c ) c

In one transition, the pushdown automaton:


3. Replaces the symbol at the top of the stack by any string.
The string could be ✏ , which corresponds to a pop of
the stack.
Informal Introduction

a a
b b
c ) c

In one transition, the pushdown automaton:


3. Replaces the symbol at the top of the stack by any string.
It could be the same symbol that appeared at the top
of the stack previously; i.e., no change to the stack is
made.
Informal Introduction

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.
It could also replace the top stack symbol by one other
symbol.
This in effect changes the top of the stack but does not
push or pop it.
Informal Introduction

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

Let us consider the language


n o
lwwr = ww R | w is in (0 + 1)⇤

This language, often referred to as “w-w-reversed,” is the


even-length palindromes over alphabet {0, 1}.
It is a CFL, generated by the grammar Gpal , with the
productions P ! 0 and P ! 1 omitted.

Atif Hasan Rahman Pushdown Automata


We can design an informal pushdown automaton accepting
lwwr , as follows.
1. Start in a state q0 that represents a “guess” that we have
not yet seen the middle.
i.e., we have not seen the end of the string w that is to
be followed by its own reverse.
While in state q0 , we read symbols and store them on
the stack, by pushing a copy of each input symbol
onto the stack, in turn.
#
0101
| {z1011} 11011010
| {z }
w wR
1
1
We are in q0 Stack contains 0
1
0
We can design an informal pushdown automaton accepting
lwwr , as follows.
2. At any time, we may guess that we have seen the middle.
i.e., the end of w.
At this time, w will be on the stack, with the right end
of w at the top and the left end at the bottom.
We signify this choice by spontaneously going to
state q1 .
Since the automaton is nondeterministic, we actually
make both guesses:
we guess we have seen the end of w, 1
but we also stay in state q0 and continue to read 1
inputs and store them on the stack. 0
01011011
| {z } 11011010
# 1
| {z } Stack contains
w wR 1
0
We switch to q1 1
0
We can design an informal pushdown automaton accepting
lwwr , as follows.
3. Once in state q1 , we compare input symbols with the
symbol at the top of the stack.
If they match, we consume the input symbol, pop the
stack, and proceed.
If they do not match, we have guessed wrong; our
guessed w was not followed by w R .
This branch dies, although other branches of the
nondeterministic automaton may survive and 1
eventually lead to acceptance. 0
# 1
01011011
| {z } 1 | 1011010
{z } Stack contains 1
w wR 0
1
We are in q1 0
We can design an informal pushdown automaton accepting
lwwr , as follows.
4. If we empty the stack, then we have indeed seen some
input w followed by w R .
We accept the input that was read up to this point.

#
01011011
| {z } 1101101
| {z 0}
w wR

We are in q1 Stack contains 0


CSE 211 PDA

The Formal Definition of Pushdown Automata

Our formal notation for a pushdown automaton (PDA)


involves seven components.
We write the specification of a PDA P as follows:

P = (Q, ⌃, , , q0 , Z0 , F )

Atif Hasan Rahman Pushdown Automata


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 )
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 transition function.


As for a finite automaton, governs the
behavior of the automaton.
Formally, takes as argument a triple
(q, a, X ), where:
1. q is a state in Q.
2. a is either an input symbol in ⌃ or a = ✏ ,
the empty string, which is assumed not to
be an input symbol.
3. X is a stack symbol, that is, a member
of .
The Formal . . . Automata

P = (Q, ⌃, , , q0 , Z0 , F )

(q, a, X ) is set of (p, )


The output of is a finite set of pairs (p, ), where
p is the new state,
and is the string of stack symbols that replaces X at
the top of the stack.
For instance,
if = ✏ , then the stack is popped,
if = X , then the stack is unchanged,
and if = YZ , then X is replaced by Z , and Y is
pushed onto the stack.
The Formal . . . Automata

P = (Q, ⌃, , , q0 , Z0 , F )

q0 : The start state.


The PDA is in this state before making any
transitions.
Z0 : The start symbol.
Initially, the PDA’s stack consists of one
instance of this symbol, and nothing else.
F : The set of accepting states, or final states.
CSE 211 PDA

Example

Let us design a PDA P to accept the language lww R .


There are a few details that we need to understand in order
to manage the stack properly.
We shall use a stack symbol Z0 to mark the bottom of the
stack.
We need to have this symbol present so that, after we pop
w off the stack and realize that we have seen ww R on the
input, we still have something on the stack to permit us to
make a transition to the accepting state, q2 .

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

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

q1 : We have just seen the midpoint of the


string/crossed the midpoint of the string.
01011011
| {z } 11011010
#
| {z } We switch from q0 to q1
w wR

q2 : We are finished with the string and it is acceptable.


01011011
| {z } 11011010
| {z }
# We switch from q1 to q2
w wR

Atif Hasan Rahman Pushdown Automata


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:
1. i) (q0 , 0, Z0 ) = {(q0 , 0Z0 )} and
ii) (q0 , 1, Z0 ) = {(q0 , 1Z0 )}.
This rule applies initially, when we are in state q0 and we
see the start symbol Z0 at the top of the stack.
We read the first input, and push it onto the stack, leaving
Z0 below to mark the bottom.

#
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:

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)}.
These four similar rules allow us to stay in state q0 and
read inputs, pushing each onto the top of the stack and
leaving the previous top stack symbol alone. 0
#
01 1 1
| 011011
{z } 11011010
| {z } We remain in q0
0 0
w wR
Z0 ) 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:

3. i) (q0 , ✏ , Z0 ) = {(q1 , Z0 )},


ii) (q0 , ✏ , 0) = {(q1 , 0)}, and
iii) (q0 , ✏ , 1) = {(q1 , 1)}.
These three rules allow P to go from state q0 to state q1
spontaneously (on ✏ input), leaving intact whatever symbol
is at the top of the stack. 1 1
1 1
0 0
01011011 1 1
| {z } 11011010
#
| {z } We switch from q0 to q1 1 1
w wR 0 0
1 1
0 0
Z0 ) 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:

4. i) (q1 , 0, 0) = {(q1 , ✏ )}, and


ii) (q1 , 1, 1) = {(q1 , ✏ )}.
Now, in state q1 , we can match input symbols against the
top symbols on the stack, and pop when the symbols
match.
1
# 0 0
01011011
| {z } 1101
| {z1010} We remain in q1 1 1
w wR 0 0
Z0 ) 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:

5. i) (q1 , ✏ , Z0 ) = {(q2 , Z0 )}.


Finally, if we expose the bottom-of-stack marker Z0 and we
are in state q1 , then we have found an input of the form
ww R .
We go to state q2 and accept.

01011011
| {z } 11011010
# We switch from
| {z } Z0 ) Z0
w wR q1 to q2
No “Mixing and Matching”

There may be several pairs that are options for a PDA in


some situation.
For instance, suppose (q, a, X ) = {(p, YZ ), (r , ✏ )}.
When making a move of the PDA, we have to chose one
pair in its entirety.
We cannot pick a state from one and a stack-replacement
string from another.
Thus, in state q, with X on the top of the stack, reading
input a, we could go to state p and replace X by YZ .
Or we could go to state r and pop X .
However, we cannot go to state p and pop X .
And we cannot go to state r and replace X by YZ .
No “Mixing and Matching”

There may be several pairs that are options for a PDA in


some situation.
For instance, suppose (q, a, X ) = {(p, YZ ), (r , ✏ )}.
When making a move of the PDA, we have to chose one
pair in its entirety.
We cannot pick a state from one and a stack-replacement
string from another.
Thus, in state q, with X on the top of the stack, reading
input a, we could go to state p and replace X by YZ .
Or we could go to state r and pop X .
However, we cannot go to state p and pop X .
And we cannot go to state r and replace X by YZ .
No “Mixing and Matching”

There may be several pairs that are options for a PDA in


some situation.
For instance, suppose (q, a, X ) = {(p, YZ ), (r , ✏ )}.
When making a move of the PDA, we have to chose one
pair in its entirety.
We cannot pick a state from one and a stack-replacement
string from another.
Thus, in state q, with X on the top of the stack, reading
input a, we could go to state p and replace X by YZ .
Or we could go to state r and pop X .
However, we cannot go to state p and pop X .
And we cannot go to state r and replace X by YZ .
No “Mixing and Matching”

There may be several pairs that are options for a PDA in


some situation.
For instance, suppose (q, a, X ) = {(p, YZ ), (r , ✏ )}.
When making a move of the PDA, we have to chose one
pair in its entirety.
We cannot pick a state from one and a stack-replacement
string from another.
Thus, in state q, with X on the top of the stack, reading
input a, we could go to state p and replace X by YZ .
Or we could go to state r and pop X .
However, we cannot go to state p and pop X .
And we cannot go to state r and replace X by YZ .
CSE 211 PDA

A Graphical Notation for PDA’s

The list of facts is not too easy to follow.


A diagram, generalizing the transition diagram of a finite
automaton, will make aspects of the behavior of a given
PDA clearer.
We shall therefore introduce and subsequently use a
transition diagram for PDA’s.

Atif Hasan Rahman Pushdown Automata


A Graphical Notation for PDA’s
We shall introduce and subsequently use a transition diagram
for PDA’s in which:
a) The nodes correspond to the states of the PDA.
b) An arrow labeled Start indicates the start state
c) And doubly circled states are accepting, as for finite
automata.
d) The arcs correspond to transitions of the PDA in the
following sense.
An arc labeled a, X /↵ from state q to state p means
that (q, a, X ) contains the pair (p, ↵ ), perhaps
among other pairs.
That is, the arc label tells what input is used, and also
gives the old and now tops of the stack.
The only thing that the diagram does not tell us is which
stack symbol is the start symbol.
Conventionally, it is Z0 , unless we indicate otherwise.
A Graphical Notation for PDA’s
We shall introduce and subsequently use a transition diagram
for PDA’s in which:
a) The nodes correspond to the states of the PDA.
b) An arrow labeled Start indicates the start state
c) And doubly circled states are accepting, as for finite
automata.
d) The arcs correspond to transitions of the PDA in the
following sense.
An arc labeled a, X /↵ from state q to state p means
that (q, a, X ) contains the pair (p, ↵ ), perhaps
among other pairs.
That is, the arc label tells what input is used, and also
gives the old and now tops of the stack.
The only thing that the diagram does not tell us is which
stack symbol is the start symbol.
Conventionally, it is Z0 , unless we indicate otherwise.
A Graphical Notation for PDA’s
We shall introduce and subsequently use a transition diagram
for PDA’s in which:
a) The nodes correspond to the states of the PDA.
b) An arrow labeled Start indicates the start state
c) And doubly circled states are accepting, as for finite
automata.
d) The arcs correspond to transitions of the PDA in the
following sense.
An arc labeled a, X /↵ from state q to state p means
that (q, a, X ) contains the pair (p, ↵ ), perhaps
among other pairs.
That is, the arc label tells what input is used, and also
gives the old and now tops of the stack.
The only thing that the diagram does not tell us is which
stack symbol is the start symbol.
Conventionally, it is Z0 , unless we indicate otherwise.
A Graphical Notation for PDA’s
We shall introduce and subsequently use a transition diagram
for PDA’s in which:
a) The nodes correspond to the states of the PDA.
b) An arrow labeled Start indicates the start state
c) And doubly circled states are accepting, as for finite
automata.
d) The arcs correspond to transitions of the PDA in the
following sense.
An arc labeled a, X /↵ from state q to state p means
that (q, a, X ) contains the pair (p, ↵ ), perhaps
among other pairs.
That is, the arc label tells what input is used, and also
gives the old and now tops of the stack.
The only thing that the diagram does not tell us is which
stack symbol is the start symbol.
Conventionally, it is Z0 , unless we indicate otherwise.
CSE 211 PDA

Example
The PDA of previous example is represented by the diagram
shown.

Atif Hasan Rahman Pushdown Automata


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.

P = ({q0 , q1 , q2 }, {0, 1}, {0, 1, Z0 }, , q0 , Z0 , {q2 })

is defined by the following rules:


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.

P = ({q0 , q1 , q2 }, {0, 1}, {0, 1, Z0 }, , q0 , Z0 , {q2 })

is defined by the following rules:


1. i) (q0 , 0, Z0 ) =
{(q0 , 0Z0 )} and
ii) (q0 , 1, Z0 ) =
{(q0 , 1Z0 )}.
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.

P = ({q0 , q1 , q2 }, {0, 1}, {0, 1, Z0 }, , q0 , Z0 , {q2 })

is defined by the following rules:

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.

P = ({q0 , q1 , q2 }, {0, 1}, {0, 1, Z0 }, , q0 , Z0 , {q2 })

is defined by the following rules:

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.

P = ({q0 , q1 , q2 }, {0, 1}, {0, 1, Z0 }, , q0 , Z0 , {q2 })

is defined by the following rules:

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.

P = ({q0 , q1 , q2 }, {0, 1}, {0, 1, Z0 }, , q0 , Z0 , {q2 })

is defined by the following rules:

5. i) (q1 , ✏ , Z0 ) =
{(q2 , Z0 )}.
CSE 211 PDA

Instantaneous Descriptions of a PDA

To this point, we have only an informal notion of how a


PDA “computes.”
The PDA goes from configuration to configuration, in
response to input symbols (or sometimes ✏ ).
In the finite automaton, the state is the only thing that we
need to know about the automaton.
The PDA’s configuration involves both the state and the
contents of the stack.
Being arbitrarily large, the stack is often the more important
part of the total configuration of the PDA at any time.
It is also useful to represent as part of the configuration the
portion of the input that remains.

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

Instantaneous Descriptions of a PDA

To this point, we have only an informal notion of how a


PDA “computes.”
The PDA goes from configuration to configuration, in
response to input symbols (or sometimes ✏ ).
In the finite automaton, the state is the only thing that we
need to know about the automaton.
The PDA’s configuration involves both the state and the
contents of the stack.
Being arbitrarily large, the stack is often the more important
part of the total configuration of the PDA at any time.
It is also useful to represent as part of the configuration the
portion of the input that remains.

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

Instantaneous Descriptions of a PDA

To this point, we have only an informal notion of how a


PDA “computes.”
The PDA goes from configuration to configuration, in
response to input symbols (or sometimes ✏ ).
In the finite automaton, the state is the only thing that we
need to know about the automaton.
The PDA’s configuration involves both the state and the
contents of the stack.
Being arbitrarily large, the stack is often the more important
part of the total configuration of the PDA at any time.
It is also useful to represent as part of the configuration the
portion of the input that remains.

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

Instantaneous Descriptions of a PDA

To this point, we have only an informal notion of how a


PDA “computes.”
The PDA goes from configuration to configuration, in
response to input symbols (or sometimes ✏ ).
In the finite automaton, the state is the only thing that we
need to know about the automaton.
The PDA’s configuration involves both the state and the
contents of the stack.
Being arbitrarily large, the stack is often the more important
part of the total configuration of the PDA at any time.
It is also useful to represent as part of the configuration the
portion of the input that remains.

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

Instantaneous Descriptions of a PDA

Thus, we shall represent the configuration of a PDA by a


triple (q, w, ), where
1. q is the state,
2. w is the remaining input, and
3. is the stack contents.
Conventionally, we show the top of the stack at the left end
of and the bottom at the right end.
Such a triple is called an instantaneous description, or ID,
of the pushdown automaton.

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

Instantaneous Descriptions of a PDA

Thus, we shall represent the configuration of a PDA by a


triple (q, w, ), where
1. q is the state,
2. w is the remaining input, and
3. is the stack contents.
Conventionally, we show the top of the stack at the left end
of and the bottom at the right end.
Such a triple is called an instantaneous description, or ID,
of the pushdown automaton.

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

Instantaneous Descriptions of a PDA

Thus, we shall represent the configuration of a PDA by a


triple (q, w, ), where
1. q is the state,
2. w is the remaining input, and
3. is the stack contents.
Conventionally, we show the top of the stack at the left end
of and the bottom at the right end.
Such a triple is called an instantaneous description, or ID,
of the pushdown automaton.

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

Instantaneous Descriptions of a PDA

For finite automata, the ˆ notation was sufficient to


represent sequences of instantaneous descriptions
through which a finite automaton moved.
The ID for a finite automaton is just its state.
However, for PDA’s we need a notation that describes
changes in the state, the input, and stack.
Thus, we adopt the “turnstile” notation for connecting pairs
of ID’s that represent one or many moves of a PDA.

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

Instantaneous Descriptions of a PDA

Let P = (Q, ⌃, , , q0 , Z0 , F )be a PDA.


Define `P , or just ` when P is understood, as follows.
Suppose (q, a, X ) contains (p, ↵ ).
Then for all strings w in ⌃⇤ and in ⇤ :
(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.
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, perhaps to influence events
later.

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

Instantaneous Descriptions of a PDA

Let P = (Q, ⌃, , , q0 , Z0 , F )be a PDA.


Define `P , or just ` when P is understood, as follows.
Suppose (q, a, X ) contains (p, ↵ ).
Then for all strings w in ⌃⇤ and in ⇤ :
(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.
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, perhaps to influence events
later.

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

Instantaneous Descriptions of a PDA

Let P = (Q, ⌃, , , q0 , Z0 , F )be a PDA.


Define `P , or just ` when P is understood, as follows.
Suppose (q, a, X ) contains (p, ↵ ).
Then for all strings w in ⌃⇤ and in ⇤ :
(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.
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, perhaps to influence events
later.

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

Instantaneous Descriptions of a PDA

Let P = (Q, ⌃, , , q0 , Z0 , F )be a PDA.


Define `P , or just ` when P is understood, as follows.
Suppose (q, a, X ) contains (p, ↵ ).
Then for all strings w in ⌃⇤ and in ⇤ :
(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.
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, perhaps to influence events
later.

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
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 )}

From the initial ID, there


are two choices of move.
The first guesses that the
middle has not been seen
and leads to ID
(q0 , 111, 1Z0 ).
In effect, a 1 has been
removed from the input
and pushed onto the stack.
P = ({q0 , q1 , q2 }, {0, 1}, {0, 1, Z0 }, , q0 , Z0 , {q2 }) 1111
is defined by the following rules:

3. i) (q0 , ✏ , Z0 ) = {(q1 , Z0 )},


ii) (q0 , ✏ , 0) = {(q1 , 0)}, and
iii) (q0 , ✏ , 1) = {(q1 , 1)}.

The second choice from


the initial ID guesses that
the middle has been
reached.
Without consuming input,
the PDA goes to state q1 ,
leading to the ID
(q1 , 1111, Z0 ).
P = ({q0 , q1 , q2 }, {0, 1}, {0, 1, Z0 }, , q0 , Z0 , {q2 }) 1111
is defined by the following rules:

5. i) (q1 , ✏ , Z0 ) = {(q2 , Z0 )}.

Since the PDA may accept


if it is in state q1 and sees
Z0 on top of its stack, the
PDA goes from there to ID
(q2 , 1111, Z0 ).
That ID is not exactly an
accepting ID, since the
input has not been
completely consumed.
P = ({q0 , q1 , q2 }, {0, 1}, {0, 1, Z0 }, , q0 , Z0 , {q2 }) 1111
is defined by the following rules:

5. i) (q1 , ✏ , Z0 ) = {(q2 , Z0 )}.

Had the input been ✏ rather


than 1111, the same
sequence of moves would
have led to ID (q2 , ✏ , Z0 ),
which would show that ✏ is
accepted.
P = ({q0 , q1 , q2 }, {0, 1}, {0, 1, Z0 }, , q0 , Z0 , {q2 }) 1111
is defined by the following rules:

3. i) (q0 , ✏ , Z0 ) = {(q1 , Z0 )},


ii) (q0 , ✏ , 0) = {(q1 , 0)}, and
iii) (q0 , ✏ , 1) = {(q1 , 1)}.

The PDA may also guess


that it has seen the middle
after reading one 1, that is,
when it is in the ID
(q0 , 111, 1Z0 ).
That guess also leads to
failure, since the entire
input cannot be consumed.
P = ({q0 , q1 , q2 }, {0, 1}, {0, 1, Z0 }, , q0 , Z0 , {q2 }) 1111
is defined by the following rules:

The correct guess, that the


middle is reached after
reading two 1’s, gives us a
sequence of ID’s.
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:

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)}.

(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:

3. i) (q0 , ✏ , Z0 ) = {(q1 , Z0 )},


ii) (q0 , ✏ , 0) = {(q1 , 0)}, and
iii) (q0 , ✏ , 1) = {(q1 , 1)}.

(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:

4. i) (q1 , 0, 0) = {(q1 , ✏ )}, and


ii) (q1 , 1, 1) = {(q1 , ✏ )}.

(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:

5. i) (q1 , ✏ , Z0 ) = {(q2 , Z0 )}.

(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?

One might wonder why we did not introduce for finite


automata a notation like the ID’s we use for PDA’s.
Although a FA has no stack, we could use a pair (q, w),
where q is the state and w the remaining input, as the ID
of a finite automaton.
While we could have done so, we would not glean any
more information from reachability among ID’s than we
obtain from the ˆ notation.
That is, for any finite automaton, we could show that
ˆ(q, w) = p if and only if (q, wx) ` (p, x) for all strings x.
ID’s for Finite Automata?

One might wonder why we did not introduce for finite


automata a notation like the ID’s we use for PDA’s.
Although a FA has no stack, we could use a pair (q, w),
where q is the state and w the remaining input, as the ID
of a finite automaton.
While we could have done so, we would not glean any
more information from reachability among ID’s than we
obtain from the ˆ notation.
That is, for any finite automaton, we could show that
ˆ(q, w) = p if and only if (q, wx) ` (p, x) for all strings x.
CSE 211 PDA

Example

Design a PDA which accepts a string of balanced


parenthesis.
The string must always start with a left parenthesis.
So the PDA accepts all of the following:
(()(())(())) ()()() ✏
But it does not accept any of the following:
()) )()(

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

Example-continued

The principle involved here should be very simple.


Whenever we see a left parenthesis, we push it onto the
stack.
Every left parenthesis can be paired with a unique
subsequent right parenthesis.
Whenever we see a right parenthesis, we try to match it
with one in the stack.

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

Example-continued

The states involved are,


q0 : We are about to start scanning or we are in
the middle of the string.
q1 : We have finished scanning the whole string.
While in q1 , if the stack is empty, the string is accepted.

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

Example-continued

So, the PDA is as follows:

P = ({q0 , q1 }, {(, )}, {(, ), Z0 }, , q0 , Z0 , q1 )

The are as follows:


For the initial condition (or when the parentheses so
far has been perfectly matched),
(q0 , (, Z0 ) = {(q0 , (Z0 )}.
For storing the left parentheses, (q0 , (, () = {(q0 , (()}.
For left-right matches, (q0 , ), () = {(q0 , ✏ )}.
For acceptance, (q0 , ✏ , Z0 ) = {(q1 , Z0 )}.

Atif Hasan Rahman Pushdown Automata


(q0 , (()(())(())), Z0 ) ` (q0 , ()(())(())), (Z0 )
` (q0 , )(())(())), ((Z0 )
` (q0 , (())(())), (Z0 )
` (q0 , ())(())), ((Z0 )
` (q0 , ))(())), (((Z0 )
(q0 , (, Z0 ) = {(q0 , (Z0 )}, ` (q0 , )(())), ((Z0 )
(q0 , (, () = {(q0 , (()}, ` (q0 , (())), (Z0 )
(q0 , ), () = {(q0 , ✏ )}, ` (q0 , ())), ((Z0 )
(q0 , ✏ , Z0 ) = {(q1 , Z0 )}.
` (q0 , ))), (((Z0 )
` (q0 , )), ((Z0 )
` (q0 , ), (Z0 )
` (q0 , ✏ , Z0 )
` (q1 , ✏ , Z0 )
(q0 , ()()(), Z0 ) ` (q0 , )()(), (Z0 )
` (q0 , ()(), Z0 )
` (q0 , )(), (Z0 )
` (q0 , (), Z0 )
` (q0 , ), (Z0 )
(q0 , (, Z0 ) = {(q0 , (Z0 )}, ` (q0 , ✏ , Z0 )
(q0 , (, () = {(q0 , (()}, ` (q1 , ✏ , Z0 )
(q0 , ), () = {(q0 , ✏ )},
(q0 , ✏ , Z0 ) = {(q1 , Z0 )}.
(q0 , ✏ , Z0 ) ` (q1 , ✏ , Z0 )

(q0 , (, Z0 ) = {(q0 , (Z0 )},


(q0 , (, () = {(q0 , (()},
(q0 , ), () = {(q0 , ✏ )},
(q0 , ✏ , Z0 ) = {(q1 , Z0 )}.
(q0 , ()), Z0 ) ` (q0 , )), (Z0 )
` (q0 , ), Z0 )
????

(q0 , (, Z0 ) = {(q0 , (Z0 )},


(q0 , (, () = {(q0 , (()},
(q0 , ), () = {(q0 , ✏ )},
(q0 , ✏ , Z0 ) = {(q1 , Z0 )}.
(q0 , )()()(, Z0 ) ` ????

(q0 , (, Z0 ) = {(q0 , (Z0 )},


(q0 , (, () = {(q0 , (()},
(q0 , ), () = {(q0 , ✏ )},
(q0 , ✏ , Z0 ) = {(q1 , Z0 )}.
CSE 211 PDA

Example

This pushdown automaton accepts the language


{w 2 {a, b}⇤ : w has the same number of a’s and b’s}.
Either a string of a’s or a string of b’s is kept by P on its
stack.
A stack of a’s indicates the excess of a’s over b’s thus far
read.
If in fact P has read more a’s than b’s; a stack of b’s
indicates the excess of b’s over a’s.
In either case, P keeps a special symbol Z0 on the bottom
of the stack as a marker.

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

Example-continued

Let P = (Q, ⌃, , , q0 , Z0 , F ) where


Q = {q, f },
⌃ = {a, b},
= {a, b, Z0 },
Z0 = Z0 ,
F = f,
is listed below,
(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 ).

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

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 ).

In state q, when P reads an a,


it either starts up a stack of a’s from the bottom, while
keeping the bottom marker (transition 1),
or pushes an a onto a stack of a’s (transition 2),
or pops a b from a stack of b’s (transition 3).

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

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 ).

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

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 ).

When reading a b from the input, the machine acts


analogously,
pushing a b onto a stack of b’s (transition 4),
or pushing a b onto a stack consisting of just a bottom
marker (transition 5),
and popping an a from a stack of a’s (transition 6).

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

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 ).

Finally, when Z0 is the topmost (and therefore the only)


symbol on the stack, the machine may pass to a final state
(transition 7).
If at this point all the input has been read, then the
configuration (f , ✏ , Z0 ) has been reached, and the input
string is accepted.
Atif Hasan Rahman Pushdown Automata
CSE 211 PDA

Language of a PDA

The language accepted by P, denoted L(P), is the set of


all strings accepted by P.
Two approaches
1 acceptance by final state
2 acceptance by empty stack
Two methods are equivalent.
A language L has a PDA that accepts it by final state if and
only if L has a PDA that accepts it by empty stack.

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

Language of a PDA

The language accepted by P, denoted L(P), is the set of


all strings accepted by P.
Two approaches
1 acceptance by final state
2 acceptance by empty stack
Two methods are equivalent.
A language L has a PDA that accepts it by final state if and
only if L has a PDA that accepts it by empty stack.

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

Equivalence of PDA’s and CFG’s

Following three classes of languages are equivalent


The context free languages, i.e. the languages defined by
CFG’s.
The languages that are accepted by final state by some
PDA.
The languages that are accepted by empty stack by some
PDA.

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

Acceptance by Final State and Empty Stack

Let P = (Q, ⌃, , , q0 , Z0 , F ) be a PDA


L(P), the language accepted by P by final state, is

{w|(q0 , w, Z0 ) `⇤P (q, ✏ , ↵ )}

for some state q 2 F and any stack string ↵.


N(P), the language accepted by P by empty stack, is

{w|(q0 , w, Z0 ) `⇤P (q, ✏ , ✏ )}

for any state q. Since the set of accepting states is


irrelevant we can omit F .

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

From Empty Stack to Final State

If L = N(PN ) for some PDA PN = (Q, ⌃, , , q0 , Z0 , F ),


then there is a PDA PF such that L = L(PF ).
We use a new symbol X0 2
/
X0 is the start symbol and a marker on the bottom of the
stack.
We also need a new start state p0 whose sole function is to
push Z0 and enter state q0 .
We need another state, pf , which is accepting state of PF
Whenever PF sees X0 on the top of the stack, it transfers
to pf

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

From Empty Stack to Final State

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

From Final State to Empty Stack

If L = L(PF ) for some PDA PF = (Q, ⌃, , , q0 , Z0 , F ), then


there is a PDA PN such that L = N(PN ).
From each accepting state of PF , add a transition on ✏ to a
new state p.
When in state p, PN pops its stack and does not consume
any input.
To avoid a situation where PF accidentally empties its
stack, PN must use a marker X0 on the bottom of the stack.
We also need a new start state p0 whose sole function is to
push start symbol of PF and enter start state of PF .

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

From Final State to Empty Stack

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

From Grammar to PDA (Empty Stack)

Given a CFG G, we construct a PDA that simulates the


leftmost derivations of G.
Consider S =) xA↵ =) w.
⇤ ⇤

A is the leftmost variable


x is whatever terminals appear to its left
↵ is the string of terminals and variables that appear to its
right
The PDA has matched x with inputs
A↵ is in the stack
Suppose the PDA is an ID (q, y , A↵), it guesses the
production to use A ! and moves to (q, y , ↵)

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

From Grammar to PDA (Empty Stack)

Let G = (V , T , P, S) be a CFG. Construct the PDA that


accepts L(G) by empty stack as follows.

P = ({q}, T , V [ T , , q, S)

where is defined by
1 For each variable A,

(q, ✏, A) = {(q, )|A ! is a production of G}

2 For each terminal a, (q, a, a) = {(q, ✏ )}

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

Example

Consider the grammar

The transition function is

Atif Hasan Rahman Pushdown Automata


(q, a + a ⇤ b, E) ` (q, a + a ⇤ b, E + E)
a+a⇤b ` (q, a + a ⇤ b, I + E)
` (q, a + a ⇤ b, a + E)
` (q, + a ⇤ b, +E)
E =) E + E
` (q, a ⇤ b, E)
=) I + E
` (q, a ⇤ b, E ⇤ E)
=) a + E
` (q, a ⇤ b, I ⇤ E)
=) a + E ⇤ E
` (q, a ⇤ b, a ⇤ E)
=) a + I ⇤ E
` (q, ⇤ b, ⇤E)
=) a + a ⇤ E
` (q, b, E)
=) a + a ⇤ I
` (q, b, I)
=) a + a ⇤ b
` (q, b, b)
` (q, ✏ , ✏ )
CSE 211 PDA

From Grammar to PDA (Empty Stack)

Theorem
If PDA P is constructed from CFG G by the construction above,
then N(P) = L(G)

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

Proof

By induction
Skipped

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

From PDA (Empty Stack) to Grammar


We are interested in the net popping of one symbol off the
stack, while consuming some input
A PDA may change state as it pops stack symbols, so we
also note the state that it enters when it finally pops a level
off its stack

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

From PDA (Empty Stack) to Grammar

Our equivalent grammar uses variables each of which


represents an event consisting of
The net popping of some symbol X from the stack
A change in state from some p at the beginning to q when
X has finally been replaced by ✏ on the stack
We represent such a variable by the composite symbol
[pXq]

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

From PDA (Empty Stack) to Grammar


We shall construct G = (V , ⌃, R, S) where the set of variables
V consists of
The special symbol S, which is the start symbol
All symbols of the form [pXq] where p and q are states in
Q, and X is a stack symbol
The productions of G are as follows
For all states p, G has the production S ! [q0 Z0 p]
Let (q, a, X ) contain the pair (r , Y1 Y2 . . . Yk ), where
1 a is either a symbol in ⌃ or a = ✏
2 k can be any number, including 0 in which case the pair is
(r , ✏ )
Then for all lists of states r1 , r2 , . . . , rk , G has the production

[qXrk ] ! a[rY1 r1 ][r1 Y2 r2 ] . . . [rk 1 Yk rk ]

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

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 ]
...

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

From PDA (Empty Stack) to Grammar

Theorem
Let P = (Q, ⌃, , , q0 , Z0 ) be a PDA. Then there is context free
grammar G such that L(G) = N(P)

Proof - skipped

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

Pumping Lemma for CFL

Both CFG and PDA describe context free language


Some simple languages are not context free
L = {0n 1n 2n |n 1} - cannot match three groups
L = {0i 1j 2i 3j |i 1 and j 1} - cannot match two pairs if
they interleave
L = {ww|w is in {0, 1}⇤ } - cannot match two strings of
arbitrary length if alphabet size greater than one
Pumping lemma for context-free languages is used to
show certain languages are not context free
It says that in any sufficiently long string in a CFL, it is
possible to find at most two short, nearby substrings, that
we can pump in tandem

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

Pumping Lemma for CFL

Theorem (The pumping lemma for context-free languages)


Let L be a CFL. Then there exists a constant n such that if z is
any string in L such that |z| is at least n, then we can write
z = uvwxy, subject to the following conditions
1 |vwx|  n. That is, the middle portion is not too long
2 vx 6= ✏ . Since v and x are the pieces to be pumped,this
condition says that at least one of the strings we pump
must not be empty
3 For all i 0, uv i wx i y is in L. That is, the two strings v and
x may be pumped any number of times including 0 and the
resulting string will still be a member of L

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

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.

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

Size of Parse Trees

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

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

Pumping Lemma for CFL Proof


If G has m variables, choose n = 2m
Suppose that z in L is of length at least n
Any parse tree whose longest path is of length  m must
have a yield of length  2m 1 = n/2
It cannot have yield z because z is too long. Any parse
tree with yield z has a path of length at least m + 1
Longest path in the tree for z is of length k + 1 where
k m

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

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

Atif Hasan Rahman Pushdown Automata


CSE 211 PDA

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

Atif Hasan Rahman Pushdown Automata

You might also like