You are on page 1of 15

PUSH DOWN AUTOMATA

MOTIVATION

Regular language can be charaterized as the language accepted by finite automata. Similarly,
we can characterize the context-free language as the langauge accepted by a class of
machines called "Pushdown Automata" (PDA). A pushdown automation is an extension of
the NFA.
It is observed that FA have limited capability. (in the sense that the class of languages
accepted or characterized by them is small). This is due to the "finite memory" (number of
states) and "no external memory" involved with them. A PDA is simply an NFA augmented
with an "external stack memory". The addition of a stack provides the PDA with a last-in,
first-out memory management capapability. This "Stack" or "pushdown store" can be used to
record a potentially unbounded information. It is due to this memory management capability
with the help of the stack that a PDA can overcome the memory limitations that prevents a
FA to accept many interesting languages .Although, a PDA can store an unbounded amount
of information on the stack, its access to the information on the stack is limited. It can push
an element onto the top of the stack and pop off an element from the top of the stack. To read
down into the stack the top elements must be popped off and are lost. Due to this limited
access to the information on the stack, a PDA still has some limitations and cannot accept
some other interesting languages.

Finite automata are not able to recognize the context free languages like {WCW R
/ W * } because it has to remember the string W for comparing with the string
WR. But finite automata is not capable to remember anything. Also the language
consists of nested parenthesis (useful in programming languages) is
not
accepted by DFA because the storage of information will be done in its current
state only. It requires unbounded memory to count symbols in strings e.g. anbn A
storage in form of stack would do the trick here, hence, Pushdown Automata
(PDA). i.e Push down Automata is a finite automata with a unlimited memory.
Definition
Following are the equivalent definition
a) PDA is a way to represent the language class called context free
languages
b) PDA is a generalization of FSA and PDA changes from state to state
reading input symbols. Unlike FSA
transitions also update the stack
either by popping symbols or by pushing them.

Components

1)
2)
3)
4)

The PDA consists of 4 components


a) Input tape b) Read unit c) control unit 4) stack
Input tape
It is an infinitely long tape on which input is written
The tape is divided into sequences of cells.Each cell begins from the left end
and extends to the right without an end
Each cell of the tape holds one input letter or blank
Input string is written on the tape prior to the beginning of the operation of
the PDA
Read unit
Read unit of PDA reads words from the cells of the input tape beginning
with the first letter in the leftmost cell and then moves to the right
Control unit
It governs the operation of PDA by performing a sequence of transitions
between internal states available to it
Stack
1) A PDA has an infinitely tall PUSHDOWN STACK which follows LIFO
principle
2) Stack always start with STACK empty
3) The operations on stack are PUSH, POP an NOP
PUSH Adds the input alphabet to the top of the stack
POP Removes the top input alphabet from the top of the stack
NOP- does nothing to the stack

DESCRIPTION OF PDA
Transition diagram

In a directed graph for an arc going from the vertex which corresponds to
state p to the vertex that corresponds to state q, the edge labeling can be
represented in following forms
Form 1

(Input symbol, top input symbol of stack)


Operations on stack

Form2

(P, input symbol, top symbol of stack,operation,q)


Operations on stack

Formally a PDA is defined as a seven tuple , M = ( Q, , ,


, q0, Z0, F) ,where

Q- is the set of finite states of PDA


- is a finite set of input symbols
- is the finite set of stack symbols
q0 Q is the initial state
FQ is the final state
Z0 is the initial stack symbol placed on th top of the stack
- is the transition function of PDA and is defined as Q x ( ) x
to Q x *
The language accepted by PDA
There are two ways to describe the acceptance of a language
1) Define the language accepted to be the set of all inputs for which
some sequence of moves causes PDA to empty its stack. This
language is referred to as language accepted by empty stack
2) Designate some state as final state and define the accepted as the
set of all iputs for which the choice of moves causes PDA to enter a
final state
Design strategy of PDAs
a)
b)
c)
d)
e)
f)
g)
h)
i)

Understand the language properties for which the PDA has to be designed
Determine the set and alphabet set required
Identify the initial ,accepting and dead states of PDA
Decide on the stack symbols required
Determine the initial stack symbol
For each state decide on the transition to be made
For each state transition decide on the stack operations to e performed
Obtain the transition diagram and transition table for PDA
Test the PDA on short strings

1) Design a PDA to accept the language L ={wCw R/ w (0+1)* by an empty


stack
3

Design strategy : As it reads the first half of its input, it remain in its initial
state q0 and pushes all the symbols from the input string onto the stack.
When the machine sees a C symbol in the input string it switches its
state from q0 to q1 without updating the stack. If the input symbol
matches with the top of the stack ,top element is popped out. If the input
symbol does not match with top of the stack then no further operation is
possible. If the stack is emptied after reading entire string then that
language is accepted.
Following are the transitions required
1) Read each symbol of the string and push into the stack before C is
encountered
a) (q0,,z0,nop,q0)
b) (q0, 0, z0, push(0), q0)
c) (q0, 1, z0, push(1), q0)
d) (q0, 1, 1, push(1), q0)
e) (q0, 0, 0, push(0), q0)
f) (q0, 1, 0, push(1), q0)
g) (q0, 0 , 1, push(0), q0)
2) Once C is encountered state changes from q0 to q1 without performing
any operation
a) (q0, C , 1, nop, q1)
b) (q0, C , 0, nop, q1)
c) (q0, C , z0, nop, q1)
3) Read the next input symbol in te second half and pop from the stack if
there is a match
a) (q1, 1 , 1, pop, q1)
b) (q1, 0 , 0, pop, q1)
4) If the stack is empty after reading last symbol ,it moves from q1 to q2
Thus the PDA is ( {q0,q1,q2}, {0,1}, {0,1,z0}, , z0, q2) where is
given
by
the
transition
1,1
push(1)
0,1
push(0)
1,0
push (1)
0,0
push(0)

, z0
nop

q0
0, z 0
push(0)

1,1
pop
C ,0
nop

C ,1
nop

C , z0
nop

0,0
pop

q1

, z0
nop

1, z0
push(1)

diagram
Transition table is given by
Unread input
001C100

Transition
-

Stack
Z0
4

New state
q0

q2

01C100
1C100
C100
100
00
0

(q0,0,z0,push(0),q0)
(q0,0,0,push(0),q0)
(q0,1,0,push(1),q0)
(q0,C,1,nop,q1)
(q1,1,1,pop,q1)
(q1,0,0,pop,q1)
(q1,0,0,pop,q1)
(q1,,z0,nop,q2)

0z0
00z0
100z0
100z0
00z0
0z0
Z0
Z0

q0
q0
q0
q1
q1
q1
q1
q2

Example
To show that w = 001C100 is accepted by PDA
: Q x () x Q x *
(q0,001C100,z0 ) = (q0,0z0)
(q0,01C100,0z0 ) = (q0,00z0)
stack
(q0,1C100,00z0 ) = (q0,100z0)
(q0,C100,100z0 ) = (q1,100z0)
(q1,100,100z0 ) = (q1,00z0)
(q1,00,00z0 ) = (q1,0z0)
(q1,0,0z0 ) = (q1,z0)
is empty
(q1,,z0 ) = (q2,z0)

-first symbol is pushed into the stack


- second symbol is pushed into the
-third symbol is pushed into the stack
-no updation on stack
- top element is popped
-top element is popped
-top element is popped and stack
- moves to accepting state

2) Construct a PDA to accept L = { wwR / w (0+1)*}


Design strategy : As it reads the first half of its input, it remain in its initial
state q0 and pushes all the symbols from the input string onto the stack.
When the machine sees the first symbol of w R it switches its state from
q0 to q1 without updating the stack. If the input symbol matches with the
top of the stack ,top element is popped out. If the input symbol does not
match with top of the stack then no further operation is possible. If the
stack is emptied
after reading entire string then that language is
accepted.
Following are the transitions required
1) Read each symbol of the string and push into the stack before the first
symbol of wR is encountered
a) (q0,,z0,nop,q0)
b) (q0, 0, z0, push(0), q0)
c) (q0, 1, z0, push(1), q0)
d) (q0, 1, 1, push(1), q0)
e) (q0, 0, 0, push(0), q0)
f) (q0, 1, 0, push(1), q0)
g) (q0, 0 , 1, push(0), q0
2) Once the first symbol of w R is encountered, state changes from q0 to
q1 without performing any operation
a) (q0, , 1, nop, q1)
b) (q0, , 0, nop, q1)
c) (q0, , z0, nop, q1)

3) Read the next input symbol in the second half and pop from the stack if
there is a match
c) (q1, 1 , 1, pop, q1)
d) (q1, 0 , 0, pop, q1)
4) If the stack is empty after reading last symbol ,it moves from q1 to q2
Thus the PDA is ( {q0,q1,q2},{0,1},{0,1,z0},,z0,q2) where is given
by the transition
1,1
push(1)
0,1
push(0)
1,0
push (1)
0,0
push(0)

, z0
nop

q0
0, z 0
push(0)

1,1
pop
,0
nop

,1
nop

, z0
nop

0,0
pop

q1

, z0
nop

1, z0
push(1)

Transition table is given by


Unread input
0110
110
10
10
0

Transition
(q0,0,z0,push(0),
q0)
(q0,1,0,push(1),q
0)
(q0,,0,nop,q0)
(q1,1,1,pop,q1)
(q1,0,0,pop,q1)
(q1,,z0,nop,q2)

Stack
Z0
0z0

New state
q0
q0

10z0

q0

10z0
0z0
z0
z0

q1
q1
q1
q2

Example
To show that w = 0110 is accepted by PDA
: Q x () x Q x *
(q0,0110,z0 ) = (q0,0z0)
-first symbol is pushed into the stack
(q0,110,0z0 ) = (q0,01z0)
- second symbol is pushed into the stack
(q0,110,01z0 ) = (q1,01z0) - no updation on stack
(q1,10,10z0 ) = (q1,0z0)
- top element is popped
(q1,0,0z0 ) = (q1,z0)
- top element is popped
(q1,,z0 ) = (q2,z0)
- moves to accepting state
q1,,z0 ) = (q2,z0)
- moves to accepting state
3) Obtain a PDA to accept the language L = { a nbn / n 1}
Design strategy : As it reads the input symbol a, it remain in its
initial state q0 and pushes the symbols from the input string onto the
stack. Once an input symbol b is encountered it switches its state from
q0 to q1 and top element is popped out. This process continues for every
b. If the stack is emptied after reading entire string then that language is
accepted.
6

q2

Following are the transitions required


1) Read each a of the string and push into the stack until b is
encountered
a) (q0, a, z0, push(a), q0)
b) (q0, a, a, push(a), q0)
2) Once b
is encountered, state changes from q0 to q1 without
performing pop operation
a) (q0, b , a, pop, q1)
b) (q1, b, a, pop, q1)
3) If the stack is emptied after reading last symbol ,it moves from q1 to
q2
Thus the PDA is ( {q0,q1,q2},{a,b},{a,z0}, , z0 ,q2) where is given
by the transition
a, a
push (a )

b, a
pop

a, z0
push( a)

b, a
pop

q0

q1

, z0
nop

Transition table is given by


Unread input
aabb
abb
bb
b

Transition
(q0,a,z0,push(a),q
0)
(q0,a,a,push(a),q
0)
(q0,b,a,pop,q1)
(q1,b,a,pop,q1)
(q1,,z0,pop,q1)

Stack
Z0
az0

New state
q0
q0

aaz0

q0

az0
z0
z0

q1
q1
q2

Example
To show that w = 0110 is accepted by PDA
: Q x () x Q x *
(q0,aabb,z0 ) = (q0,az0)
-first symbol a is pushed into the stack
(q0,abb,az0 ) = (q0,aaz0)
- second symbol is pushed into the stack
(q0,bb,aaz0 ) = (q1,az0) - top element is popped out
(q1,b,az0 ) = (q1,z0)
- top element is popped
(q1,,z0 ) = (q2,z0)
- - moves to accepting state
4) Design a PDA to accept the language L = { w (a,b) */na = nb }
Transition diagram is

q2

b, b
push(b)

b, a
pop

a, a
push (a )

a, b
pop

, z0
nop

q0
b, z0
push( a)

q2

a, z0
push( a)

You can write the transitin table and example


5) Design a PDA to accept the language of nested ,balanced paranthesis
Transition diagram is
), (
pop

(, z0
push (()

(, (
push (a )

), (
pop

q0

, z0
nop

6) Design a PDA to accept L = {w/w (a,b) * such that na(w) > nb(w)
b, a
pop

b, b
push (b)
a, a
push (a)

a, b
pop

q0
b, z 0
push(a )

, a
nop

q2

a, z 0
push(a )

DETERMINISTIC PDA(DPDA) AND NON DETERMINISTIC PDA( NPDA)


DPDA : A PDA is deterministic if each input string can only be processed
by the machine in only one way , i.e for the same input symbol and same
stack symbol there must be only one choice
Formally a PDA , M = ( Q, , , , q0, Z0, F) is deterministic
1) (q,a,z) has only one element
2) If (q,,z) is not empty then (q,a,z) should be empty
Example 1) : The pda for the nested parenthesis is deterministic
Sol : The transitions required are
1) (q0, (, z0, push( ( ) , q0)
8

q2

2)
3)
4)
5)

(q0,
(q0,
(q1,
(q1,

(,
),
),
,

(, push( ( ) , q0)
(, pop , q1)
(, pop , q1)
z0, nop , q2)

We check the following 2 conditions


1) For the same input symbol and the same stack symbol there must be
only one choice. Since there is only transition for the same input
symbol and same stack symbol , condition 1 is satisfied
2) If (q, , z) is defined then (q, a, z) is not defined. Here q1, , z0,
nop , q2) is defined , but (q1, (, z0, nop , q2) is not defined
Both conditions are satisfied and hence it is deterministic
Example 2 : The PDA for the language
deterministic
Sol The transitions required are
a) (q0, a, z0, push(a), q0)
c) (q0, a, a, push(a), q0)
d) (q0, b , a, pop, q1)

L ={ a nbn / n 1} is

e) (q1, b, a, pop, q1)


f) (q1, , z0, nop, q2)
For the same input symbol and the same stack symbol there must
be only one choice. Since there is only transition for the same input
symbol and same stack symbol , condition 1 is satisfied
If (q, , z) is defined then (q, a, z) is not defined. Here (q1, ,
z0, nop , q2) is defined , but q1, a, z0, nop , q2) is not defined
Both conditions are satisfied and hence it is deterministic
NPDA : A PDA is nondeterministic, if there is some string that can be
processed in more than one way. There are two types of nondeterministic
PDA
a) First kind of non determinism occurs when a state emits two or more
edges labeled with same input symbol and same stack symbol

a, b
push ( x)

q1

a, b
push ( y )

q2

q3

b) Second kind of non determinism occurs when a state emits two edges
labelled with the same stack symbol ,where one input symbol is and
9

other input symbol is different from . Here there are two choices
based on different inputs

a, z 0
push ( xq)2
q1

, z 0
pop

q3

Example : The PDA for the language L = { ww R / w (a,b)* is non


deterministic
The transition required are
a)
b)
c)
d)
e)
f)
g)

(q0,,z0,nop,q0)
(q0, 0, z0, push(0), q0)
(q0, 1, z0, push(1), q0)
(q0, 1, 1, push(1), q0)
(q0, 0, 0, push(0), q0)
(q0, 1, 0, push(1), q0)
(q0, 0 , 1, push(0), q0)

h) (q0, , 1, nop, q1)


i) (q0, , 0, nop, q1)
j) (q0, , z0, nop, q1)
k) (q1, 1 , 1, pop, q1)
l) (q1, 0 , 0, pop, q1)
m) (q1,,z0,nop,q2)
Here for the same input symbol and same stack there exist more than
one choice

Example 2 : The PDA for the language L = { w (a ,b) * / na = nb } is


nondeterministic

CONTEXT FREE GRAMMAR (CFG)


Most compilers and interpreters contain a component called parser, that
extracts the meaning of a program. Construction of parser is possible
through CFG. An important application of CFG could be found in the
specification and compilation of programming languages

10

CFG is used to specify language and the language specified by a CFG is


called context free language(CFL). A CFG consists of terminals, non
terminals, a start symbol and productions. The terminals are nothing but
the tokens of the language used to constructs of the language. Non
terminals are the variables that denote a set of strings. Production in a
grammar specify the manner in which the terminals and set of strings
defined by the non terminals can be combined to form set of strings
CFG is formally defined as a 4 tuple G = (V,T,P,S)
4. Write a CFG, which generates palindrome for binary numbers
Grammar will generate palindrome for binary numbers, that is 00, 010, 11,
101, 11100111.
Let CFG be

G=(Vn,Vt,P,S)

Where

Vn=set of non-terminal={S}
Vt= set of terminals={0,1}

And production rule P is defined as


S 0S0/1S1
S 0/1/
Obviously this grammar generates palindrome for binary numbers, it can
be seen by the following derivation.
S 0S0
01S10
010S010
0101010
Which is a palindrome.
5. Write a CFG for the regular expression
r=0*1(0+1)*
Let us analyse regular expression
r = 0* 1 (0 +1)*
Clearly regular expression is the set of strings which starts with any
number of 0s , followed by a one and end with any combinations of 0s
and 1s.
Let CFG be

G=(Vn,Vt,P,S)
11

Where

Vn={S,A,B}
Vt={0,1}

And produce P are defined as


SA1B
A0A/
B0B/1B/
Let us see the derivation of the string 00101
SA1B
0A10B
00A101B
00101
So clearly G is CFG for regular expression r.
6. write a CFG which generates string having equal number of as and bs
Let CFG be

G=(Vn,Vt,P,S)

V= {S}
V={a,b}
Where P is defined as
SaSbS/bSas/
Let us derive a string
W= bbabaabbaa
SbSaS
bbSaSaS
bbaSbaSbSaSaS
bbaSbaaSbSbSaSa
bbabaaSbSbSaSaS
bbabaabaSbSaSaS
bbabaabbSaSaS
12

bbabaabbaSaS
bbabaabbaaS
bbabaabbaa
7. Design a CFG, which can generate string, having any combinations of
as and bs, except null string.
Let CFG be

G=(Vn,Vt,P,S)
Vn={S}
Vt={a,b}

Productions P are defined as


(P1,say)SaS
(P2,say)SbS
(P3,say)Sa
(P4,say)Sb
We can produce the word baba as follows:

baS(by P1)
baaS(by P1)
baab (by P4)
8. Design CFG for regular expressions
r=(a+b)*aa(a+b)*
Let CFG be

G=(Vn,Vt,P,S)
Vn={S,T}
Vt={a,b}

Where P is defined as
STaaT

(say P1)

TaT

(say P2)

TbT

(say P3)

(Say P4)
13

For example to generate baabaab, wew can proceed as follows:


STaaT
bTaaT
baTaaT
baaTaaT
baabTaaT
baabaaT
baabaaT
baabaabT
baabaab
baabaab
There are other sequences that can also derive the word baabaab.
9.Design a CFG for the regular expression r=(a+b)*.
Let CFG be

G=(Vn,Vt,P,S)
V={S}
V={a,b}

Pare defined as
SaS
SbS
Sa
Sb
S
The word ab can be generated by the derivation
SaS
abS
ab
ab
14

Or by the derivation

SaS

ab
10.the difference between even palindrome and odd palindrome is that
when weare finally ready to get rid of S in the even palindromeworking
string, we must replace it with . If we were force to replace it with an a
or b instead , we would create a central letter and the result would be a
grammar for odd palindrome as follows;
SaSa
SbSb
Sa
Sb
If we allow the option of turning the central S into either or a letter, we
would have a grammar for the entire

15

You might also like