0% found this document useful (0 votes)
34 views33 pages

Unit-V (Push Down Automata)

This document provides an introduction to pushdown automata (PDA). It defines a PDA as a type of automaton that defines context-free languages. Key points: - A PDA is like a finite automaton but with an additional stack, allowing it to remember an infinite amount of information in a last-in, first-out manner. - Formally, a PDA is defined by a 7-tuple including states, input symbols, stack symbols, transition function, start state, initial stack symbol, and accepting states. - The transition function defines how the PDA changes states and replaces symbols on the stack based on the current state, input symbol, and top stack symbol.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views33 pages

Unit-V (Push Down Automata)

This document provides an introduction to pushdown automata (PDA). It defines a PDA as a type of automaton that defines context-free languages. Key points: - A PDA is like a finite automaton but with an additional stack, allowing it to remember an infinite amount of information in a last-in, first-out manner. - Formally, a PDA is defined by a 7-tuple including states, input symbols, stack symbols, transition function, start state, initial stack symbol, and accepting states. - The transition function defines how the PDA changes states and replaces symbols on the stack based on the current state, input symbol, and top stack symbol.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Unit V

Push Down Automata

Compiled By
Ghanashyam BK
1
Introduction to Push Down Automata
(PDA)
The context free languages have a type of automaton
that defines them.
This automaton is called “pushdown automaton”
which can be thought as a Є-NFA with the addition of
stack.
The presence of a stack means that, the pushdown
automata can remember infinity amount of
information.
However, the pushdown automaton can only access
the information on its stack in a Last-in-first-out way.

2
Introduction to Push Down Automata
(PDA)
 We can define PDA informally as
device shown in figure:
 PDA is an abstract machine
determined by following three
things:
Input tape
Finite stack control
A stack
 Each moves of the machine is
determined by three things;
The current state
Next input symbol
Symbol on the top of stack

3
Introduction to Push Down Automata
(PDA)
The moves consist of;
Changing state | staying on same state
Replacing the stack top by string of zero or more
symbols.
Popping the top symbol off the stack means replacing
it by Є.
Pushing Y on the stack means replacing stack‟s top,
say X, by YX. Assuming the left end of stack
corresponds to stack„s top.
The single move of the machine contains only one
stack operation either push or pop.
4
Introduction to Push Down Automata
(PDA)
Formal Definition:
A PDA is defined by seven tuples (Q, Σ, Г, δ, q0, z0, F)
Where,
Q- Finite set of states.
Σ- Finite set of input symbols | alphabets.
Г- Finite set of stack alphabet
q0- Start state of PDA q0 ε Q
z0- Initial stack symbol; z0 ε Г
F- Set of final states; F is subset of Q
δ- Transition function that maps Q × (Σ U {Є}) × Г  Q
× Г*
5
Introduction to Push Down Automata
(PDA)
 as for finite automata, δ governs the behavior of PDA. Formally, δ
takes as argument a triple δ (q,a,X) where
q is a state.
 ‘a’ is either an input symbol in Σ or Є. ( note Є does not belongs to Σ)
X is a stack symbol.
 The output of δ is a finite set of pairs (p, γ), where p is the new
state and γ is the string on the stack after transition.
 i.e the moves of PDA can be interpreted as;
δ(q, a, z) = {(P1, γ1) (p2, γ2) …………….(pm, γm)}
 here
 q, pi ε Q,
a ε Σ U Є & z ε Г,
 γi ε Г*

6
Representation of PDA
We can use transition diagram to represent a PDA,
where
Any state is represented by a node in graph (diagram)
Any arc labeled with “start” indicates the start state and
doubly circled states are accepting / final states.
The arc corresponds to transition of PDA as:
 Arc labeled as a, x | α means the transition δ(q, a, x) = (p,α) for
arc from state p to q.

7
Example:
A PDA accepting a string over {a, b} such that
number of a’s and b’s are equal. i.e. L={w | w ε {a,
b}* and a’s and b’s are equal}.
The PDA that accepts the above language can be
constructed using the idea that the PDA should push
the input symbol if the top of the stack symbol is same
as it otherwise Pop the stack.

8
Example (Contd…)
For this, let us construct a PDA as;
P = {Q, Σ, Г, δ, q0, z0, F} be the PDA recognizing the
given language. where, let us suppose
 Q = {q0, q1, q2}
 Σ = {a, b}
 Г = {a, b, z0}
 z0 = z0
 q0 = q0
 F = {q2}

9
Example (Contd…)
Now δ is defined as;
δ(q0, Є, Є) = (q1, z0) //initialize the stack to indicate
the bottom of stack.
δ(q1, a, z0) = (q1, az0)
δ(q1, b, z0) = (q1, bz0)
δ(q1, a, a) = (q1, aa)
δ(q1, b, b) = ( q1, bb)
δ(q1, a, b) = (q1, Є)
δ(q1, b, a) = (q1, Є)
δ(q1, Є, z0) = (q2, Є) //indicate the acceptance of
string.

10
Example (Contd…)
So the graphical notation for the PDA constructed in
example 1 can be constructed as;

11
Example (Contd…)
Let us trace for w= aabbbaab
The execution of PDA on the given string can be
traced as shown on the table;

12
Exercise
Trace as above for string aababb
Trace as above for string ababa (note this string will be
not accepted.i.e. PDA will not reach to final state)

13
Instantaneous Description for PDA (ID)
Any configuration of a PDA can be described by a
triplet (q, w, γ).
 Where,
 q- is the state.
 w- is the remaining input.
 γ- is the stack contents.

Such a description by triple is called an instantaneous


description of PDA (ID)

14
Notation for Instantaneous Description
we need a notation that describes changes in the state,
the input and stack.
Let P = {Q, Σ, Г, δ, q0, z0, F} be a PDA.
Then, we define a relation ├, “yields as (q, aw, zα)├ (p,
w, βα) if δ(q, a, z) contains (p, β, and may be Є).
This move reflects the idea that, by consuming “a” from
the input, and replacing z on the top of stack by β, we
can go from state q to state p.
Note that what remains on the input w, and what is
below the top of stack, β, do not influence the action of
the PDA.
15
Instantaneous Description for PDA (ID)
For the PDA described earlier accepting language of
equal a’s and b’s, the accepting sequence of ID‟s for
string 1001 can be shown as;
(q0,1001, Є) ├ (q1,1001,z0)
├(q1, 001, 1z0)
├(q1, 01, z0)
├(q1, 1, 0z0)
├(q1, Є, z0)
├(q2, Є, Є) Accepted
Therefore (q0, 1001, z0) ├* (q2, Є, Є)

16
Deterministic PDA
A pushdown automata P = (Q, Σ, Г, δ, q0, z0, F) is
deterministic pushdown automata if there is no
configuration for which P has a choice of more than
one moves.
i.e. P is deterministic if following two conditions are
satisfied;
For any q ε Q, x ε Г, If δ(q, a, x) ≠Φ for some a ε Σ then
δ(q, Є, x) = Φ i.e. if δ(q, a, x) is non-empty for some a,
then δ(q, Є, x) must be empty.
For any q ε Q, a ε Σ U {Є} and x ε Г, δ(q, a, x) has at
most one element.

17
Example 1
 A DPDA accepting language L = {w c w R | w ε (0+1)*} is
constructed as;
P = ({q0, q1, q2}, {0, 1}, {0, 1, z0}, δ, q0, z0, {q2}) where δ is defined
as
 δ(q0, Є, Є) = (q0, z0) //initialize the stack
 δ(q0, 0, z0) = (q0, 0z0)
 δ(q0, 1, z0) = (q0, 1z0)
 δ(q0, 0, 0) = (q0, 00)
 δ(q0, 1, 1) = (q0, 11)
 δ(q0, 0, 1) = (q0, 01)
 δ(q0, 1, 0) = (q0, 10)
 δ(q0, C, 0) = (q1, 0) //change the state when centre is reached.
 δ(q0, C, 1) = (q1, 1) // change the state when centre is reached
 δ(q0, C, z0) = (q1, z0)// change the state when centre is reached
 δ(q1, 0, 0) = (q1, Є)
 δ(q1, 1, 1) = (q1, Є)
18
 δ(q1, Є, z0) = (q2, Є)
Example 1
The graphical notation for the above PDA is:

19
Example 2
 A DPDA accepting strings of balanced ordered parenthesis P;
 Q = (q0, q1, q2), Σ = {{,}, (,), [,]}, Г = Σ U {z0}, δ, q0, z0, {q2}
Where δ is defined as:
 δ(q0, Є, Є) = (q0, z0)
 δ(q0, {, z0) = (q1, {z0)
 δ(q0, [, z0) = (q1, [z0)
 δ(q0, (, z0) = (q1, (z0)
 δ(q1, {, { ) = (q1, {{ )
 δ(q1, [, [ ) = (q1, [[ )
 δ(q1, (, ( ) = (q1, (( )
 δ(q1, }, { ) = (q1, Є)
 δ(q1, ], [ ) = (q1, Є)
 δ(q1, ), ( ) = (q1, Є)
 δ(q1, ), { ) = (q1, ({ )
 δ(q1, {, [ ) = (q1, {[ )
 δ(q1, Є, z0) = (q2, Є)
20
Example 2
Evaluate or trace for the string [{ }]
(q0, [{ }], z0) ├ (q1, { }], [z0)
├ (q1,}], {[z0)
├ (q1,], [z0)
├ (q1, Є, z0)
├ (q2, Є, Є) Accepted

1. Try for the string [{( ) }].


2. try for the string [( ) ( ) ]
3. Try for the string {[ } ]. Note: this should be rejected.

21
Example 3
Construct a PDA accepting Language L = {wwR | w
ε ( 0+1)* and wR is reversal of w}

22
Language of PDA
We can define acceptance of any string by a PDA in
two ways;
Acceptance by final state:
 Given a PDA P, the language accepted by final state, L(P) is;
w | (q, w, z0) ├* (P, Є, γ)} where P ε F and γ ε Г*.
Acceptance by empty stack:
 Given a PDA P, the language accepted by empty stack, L(P), is
 {w | (q, w, z0) ├* (P, Є, Є)} where P ε Q.

23
Conversion of CFG to PDA
 Given a CFG, G = (V, T, P and S), we can construct a push down
automaton, M which accepts the language generated by the
grammar G, i.e. L(M) = L (G).
 The machine can be defined as;
M = ({q}, T, V U T, δ, q, S, Φ)
Where,
Q = {q} is only the state in the PDA.
Σ=T
Г = V U T (i.e. PDA uses terminals and variables of G as stack symbols)
z0 = S (initial stack symbol is start symbol in grammar)
F=Φ
q0=q And
δ can be defined as;
i. δ(q, Є, A) = {q, α) / A α is a production P of G.
ii. δ(q, a, a) = { (q, Є), for all a ε T}
24 Here all transition occurs on the only one state q.
Conversion of CFG to PDA
Alternatively, we can define push down automata for the
CFG with two states p & q, where p being start state.
Here idea is that the stack symbol initially is supposed to
be Є, and at first PDA starts with state P and reading Є, it
inserts start symbol ‘S’ of CFG into stack and changes
the state to q.
Then all transitions occur in state q.
i.e. the PDA can be defined as;
M = ( {p, q}, T, V U T, δ, p, {q}); stack top is Є.
Then δ can be defined as;
 δ(p, Є, Є) = {(q, S) / S is start symbol of grammar G.
 δ(q, Є, A) = {(q, α) / Aα is a production P of G.
 δ(q, a, a) = {(q, Є), for all a ε Σ.
25
Example
 Let G = (V, T, P and S) where, P is
ET | E + T
T F | T*F
F a | (E)
 We can define a PDA equivalent to this grammar as;
M = ({q0}, {a, *, +, (, )},{a, *, +, (, ), E, T, F}, δ, q0, E, Φ}
Where δ can be defined as;
 δ(q0, Є, E) = {(q0, T), (q0,E + T)}
 δ(q0, Є, T) = {(q0, F), (q0,T*F)}
 δ(q0, Є, F) = {(q0, a), (q0,(E))}
 δ(q0, a, a) = {(q0, Є)}
 δ(q0, *, *) = {(q0, Є)}
 δ(q0, +, +) = {(q0, Є)}
 δ(q0, (, ( ) = {(q0, Є)}
 δ(q0, ),) ) = {(q0, Є)}
26
Example
 with this PDA, M, let us trace out acceptance of a + (a*a);
(q0, a + (a*a), E) ├ (q0, a + (a*a), E + T)
├ (q0, a + (a*a), T + T)
├ (q0, a + (a*a), F + T)
├ (q0, a + (a*a), a + T)
├ (q0, + (a*a), + T)
├ (q0, (a*a), T)
├ (q0, (a*a), F)
├ (q0, (a*a), (E))
├ (q0, a*a), E))
├ (q0, a*a), T))
├ (q0, a*a), T*F)
├ (q0, a*a), F*F)
├ (q0, a*a), a*F)
├ (q0, *a), *F)
├ (q0, a), F))
├ (q0, a), a))
├ (q0,),))
27
├ (q0, Є, Є) Accepted (acceptance by empty stack).
Example
In CFG
E E + T
E +T
 T+T
F + T
a + T
a + F
 a + (E)
 a + (T*F)
 a + (F*F)
 a + (a*F)
 a + (a*a)

28
Conversion of PDA to CFG
Given a PDA M = (Q, Σ, Г, δ, q0, z0, F); F = Φ, we can
obtain an equivalent CFG, G = (V, T, P and S) which
generates the same language as accepted by the PDA
M.
The set of variables in the grammar consists of;
The special symbol S, which is start symbol.
All the symbols of the form [p x q]; p,q ε Q and X ε Г,
where p and q are state in Q
 i.e. V = {S} U {[p x q]}
The terminal in the grammar T = Σ

29
Conversion of PDA to CFG
 The production of G is as follows;
For all states q ε Q, S [q0, z0, q] is a production of G.
For any states p, q ε Q, x ε Г and a ε Σ U {Є} if δ(q, a, x) = (p, Є)
then [p x q] a
For any states p, q ε Q, x ε Г and a ε Σ U {Є}, if δ(q, a, x) = (p, Y1,
Y2,……..Yk);
 where Y1, Y2, ……Yk ε Г and k ≥ 0, Then for all lists of states P 1, P2,
…………..Pk,
 G has the production [q x p k ]  a [ pY1P1][P1Y2P2]…………..[Pk-1YkPk].

 This last production says that one way to pop x and go from state
q to state Pk is to read “a” (which may be Є), then use some input
to pop Y1 off the stack while going from state P to P 1, then read
some more input that pops Y2 off the stack and goes from P1 t oP2
30 and so on………
Example
Let a PDA that recognizes a language L = {anbn | n >0}
be defined as;
δ(q0, a, z0) =(q1, az0)
δ(q0, a, a) =(q1, aa)
δ(q0, b, a) =(q1, Є)
δ(q0, Є, z0) =(q1, Є)
The graphical representation is

31
Example
 Let G = (V, T, P and S) be the equivalent CFG for the given PDA
where,
V= {S} U {[p × q] / p, q ε Q, x ε Г}
S= is the start state.
T= Σ And
P is defined by following production;
 S[q0 z0 q0] | [q0 z0 q1]
 i.e.
S[q0 z0 r2]; for r2 in {q0, q1}
 From the fact that δ(q0, a, z0) contains (q1, az0), we get production
 [q0 z0 r2] a[q1 ar1][r1 z0r2]; for ri in{q0, q1}
 From the fact that δ(q1, a, a) contains (q1, aa), we get productions
 [q1 ar2] a[q1 ar1][r1 a r2] for ri in {q0, q1}
 From the fact that δ(q1, b, a) contains (q1, Є), we get
 [q1aq1]b
 From the fact that δ(q1, Є, z0) contains (q1, Є), we get
32  [q1 z0 q1] Є
Exercise
Convert the PDA P = ({p, q}), {0, 1}, {x, z0}, δ, q ,z0)
to a CFG if δ is given by
δ(q, 1, z0) = (q, xz0)
δ(q, 1, x) = (q, xx)
δ(q, 0, x) = (q, x)
δ(q, Є, z0) = (q, Є)
δ(q, 1, x) = (p, Є)
δ(q, 0, z0) = (q, z0)

33

You might also like