This document discusses pushdown automata and context-free grammars. It begins by defining a pushdown automaton as a finite state machine with an input tape and stack. There are two ways for a PDA to accept a string - by reaching a final state or emptying the stack. Algorithms are provided for constructing a PDA from a CFG and vice versa. A two-stack PDA is also introduced, which has the computational power of a Turing machine. Examples are given to illustrate PDA constructions.
1. THEORY OF AUTOMATAAND
FORMAL LANGUAGES
UNIT-IV
ABHIMANYU MISHRA
ASSISTANT PROF.(CSE)
JETGI
Abhimanyu Mishra(CSE) JETGI12/31/2016
2. PUSHDOWN AUTOMATA INTRODUCTION
“The pushdown automata is essentially a finite automata with control of both
an input tape and a stack to store what it has read. A stack is “FIRST IN FIRST
OUT” list, that is, symbols can be entered or removed only at the top of the
list. When a new symbol is entered at the top, the symbol previously at the top
becomes second and so on”.
Basically a pushdown automaton is −
"Finite state machine" + "a stack“
A pushdown automaton has three components −
(i) an input tape,
(ii) a control unit
(iii) a stack with infinite size.
12/31/2016 Abhimanyu Mishra(CSE) JETGI
3. Diagram
a b c b a c ……………..
Input Tape
a
c
b
a
12/31/2016 Abhimanyu Mishra(CSE) JETGI
Finite Control
4. DEFINATION OF PUSHDOWN AUTOMATA
A pushdown automata is a system, which is mathematically defined as
follows:
P = ( Q,∑,┌,δ,s,F)
Where Q : non-empty finite set of states
∑ : non-empty finite set of input symbols
┌ : is finite set of pushdown symbols
s : is the initial state, s ∈ Q
F : is the set of final states and F Q
δ : It is a transition function or transition relation.
12/31/2016 Abhimanyu Mishra(CSE) JETGI
5. THERE ARE TWO DIFFERENT WAYS TO DEFINE PDA
ACCEPTABILITY.
(i) Final State Acceptability
In final state acceptability, a PDA accepts a string when, after reading the
entire string, the PDA is in a final state. From the starting state, we can make
moves that end up in a final state with any stack values. The stack values are
irrelevant as long as we end up in a final state.
For a PDA (Q,∑,┌,δ,s, F) the language accepted by the set of final states F is −
L(PDA) = {w | (q0, w, I) ⊢* (q, ε, x), q ∈ F}
for any input stack string x
12/31/2016 Abhimanyu Mishra(CSE) JETGI
6. (ii) Empty Stack Acceptability
Here a PDA accepts a string when, after reading the entire string, the PDA has
emptied its stack.
For a PDA (Q, ∑,┌,δ,s, F), the language accepted by the empty stack is −
L(PDA) = {w | (q0, w, I) ⊢* (q, ε, ε), q ∈ Q}
12/31/2016 Abhimanyu Mishra(CSE) JETGI
7. Example: Design a PDA which accepts the language
L ={ w ∈{a,b}*/w has equal no of a’ and b’s}
Now let us assume that PDA be P
P = (Q,∑,┌,δ,s,F)
Q = { s,q,f}
∑ = {a,b}
┌ = {a,b,c}
F = {f} , and δ is as follows
Now let us assume stack have c value in initial in stack,
12/31/2016 Abhimanyu Mishra(CSE) JETGI
8. (i) ((s, ∈, ∈),(q,c))
(ii) ((q,a,c),(q,ac))
(iii) (q,a,a),(q,aa))
(iv) ((q,a,b),(q, ∈))
(v) ((q,b,c),(q,bc))
(vi) ((q,b,b),(q,bb))
(vii) ((q,b,a),(q, ∈))
(viii) ((q, ∈,c),(f, ∈))
Now if PDA reads symbol ’a’ from the input tape and if stack is empty or has
‘a’ on the stack top then push ‘a’ into the stack, Now if reads ‘a’ from input and
‘b’ is on top of the stack then. It simply pops the symbol ‘b’ and pushes
nothing.
So final state is defined as, PDA is in state f and stack is empty
12/31/2016 Abhimanyu Mishra(CSE) JETGI
9. PUSHDOWN AUTOMATAAND CONTEXT-FREE-GRAMMARS
It should be clear now that PDA can recognize any language for which there
exists a CFG. “ That is class of language accepts by pushdown automata is
exactly the class of context-free languages”.
(i) Algorithm to find PDA corresponding to a given CFG
(ii)Algorithm to find CFG corresponding to a given PDA
12/31/2016 Abhimanyu Mishra(CSE) JETGI
10. (I) ALGORITHM TO FIND PDA CORRESPONDING TO A GIVEN
CFG
Input : A CFG, G = (Vn,Vt,P,S)
Output: Equivalent PDA, P= (Q, ∑, ┌, δ, q0, F)
Steps:
(i) Convert of the production of CFG to GNF
(ii) The PDA has only one state q0
(iii) The start symbol of CFG will be the start symbol in the PDA
(iv) All non-terminals of the CFG will be the stack symbols of the PDA
and all the terminals of the CFG will be the input symbols of the
PDA.
(v) For each production in the form A → aX where a is terminal and
A, X are combination of terminal and non-terminals make a
transition δ (q, a, A).
12/31/2016 Abhimanyu Mishra(CSE) JETGI
11. Example
Construct a PDA from the following CFG.
G = ({S, X}, {a, b}, P, S)
where the productions are −
S → XS | ε , A → aXb | Ab | ab
Solution
Let the equivalent PDA,
P = ({q}, {a, b}, {a, b, X, S}, δ, q, S)
where δ −
δ (q, ε , S) = {(q, XS), (q, ε )}
δ(q, ε , X) = {(q, aXb), (q, Xb), (q, ab)}
δ(q, a, a) = {(q, ε )}
δ(q, 1, 1) = {(q, ε )}
12/31/2016 Abhimanyu Mishra(CSE) JETGI
12. (II) ALGORITHM TO FIND CFG CORRESPONDING TO A GIVEN
PDA
Input : A CFG, G = (Vn,Vt,P,S)
Output : Equivalent PDA, P = (Q, ∑, ┌, δ, q0, F)such that the non-
terminals of the grammar G will be {Xwx | w,x ∈ Q} and the start
state will be Aq0,F.
Steps:
(i) For every w, x, y, z ∈ Q, m ∈┌ and a, b ∈ ∑, if δ (w, a, ε) contains (y,
m) and (z, b, m) contains (x, ε), add the production rule Xwx → a
Xyzb in grammar G.
(ii) For every w, x, y, z ∈ Q, add the production rule Xwx → XwyXyx in
grammar G.
(iii) For w ∈ Q, add the production rule Xww → ε in grammar G.
12/31/2016 Abhimanyu Mishra(CSE) JETGI
13. Example:
Design a PDA for the following CFG, G = (Vn,Vt,P,S) with Vn ={S}, Vt
={(,)} and P is defined δ as follows
S → (S) | ε|SS
Solution:
Lets corresponding PDA will be
P = (Q, ∑, ┌, δ, q0, F)
Q={p,q}
∑ = {(,)}
┌ = (S,(,)} that is terminal and non-terminal in grammar
S =p
F = {q}, and δ is as,
12/31/2016 Abhimanyu Mishra(CSE) JETGI
14. (1) ((p, ε , ε) , {(q, S))
(2) ((q, ε , S) ,{(q, ε )) because S → ∈ is a rule of CFG
(3) ((q, ε, S) ,(q, SS))
(4) ((q, ε,S) , (q, (S))
(5) ((q, (,(),(q, ε))
(6) (((q,),)),(q,(q,ε)) for each a ∈ Vt
12/31/2016 Abhimanyu Mishra(CSE) JETGI
15. Now apply these transition relation on the string w=( )( )
12/31/2016 Abhimanyu Mishra(CSE) JETGI
S.N State Unread
input
Stack Transiti
on used
1 q ( ) ( ) ∈ -
2 q ( ) ( ) S (1)
3 q ( ) ( ) SS (3)
4 q ( ) ( ) (S)SS (4)
5 q ) ( ) S)S (5)
6 q )( ) )S (2)
7 q ( ) S (6)
8 q ( ) (S) (4)
9 q ) S) (5)
10 q ) ) (2)
11 q ∈ ∈ (6)
17. Sol: CFG using above construction production rule is as follows:
S→[q0 $ q0 ]/ [q0 $ q1 ] rule (i)
[q0 a q0 ] → a
[q0 b q1 ] → b
[q1 $ q1 ] → ∈ rule(ii)
Now remaining productions are:-
[q0 $ q0 ] → a [q0 a q0 ]/ [q0 $ q0 ]
[q0 $ q1 ] → a [q0 a q0 ]/ [q0 $ q1 ]
[q0 $ q1 ] → a [q0 a q1 ]/ [q1 $ q1 ]
[q0 $ q1 ] → b [q0 b q0 ]/ [q0 $ q0 ]
[q0 $ q1 ]→ b [q0 b q0 ]/ [q0 $ q1 ]
[q0 $ q1 ] → b [q0 b q1 ]/ [q1 $ q1 ]
12/31/2016 Abhimanyu Mishra(CSE) JETGI
18. [q0 a q0 ] → a [q0 a q0 ]/ [q0 a q0 ]
[q0 a q1 ] → a [q0 a q0 ]/ [q0 a q1 ]
[q0 a q1 ] → a [q0 a q1 ]/ [q1 a q1 ]
[q0 a q0 ] → b [q0 b q0 ]/ [q0 a q0 ]
[q0 a q1 ] → b [q0 b q0 ]/ [q0 a q0 ]
[q0 a q1 ] → b [q0 b q1 ]/ [q1 a q1 ]
[q0 b q0 ] → a [q0 a q0 ]/ [q0 b q0 ]
[q0 b q1 ] → a [q0 a q0 ]/ [q0 b q1 ]
[q0 b q1 ] → a [q0 a q1 ]/ [q1 b q1 ]
12/31/2016 Abhimanyu Mishra(CSE) JETGI
19. [q0 b q0 ] → b [q0 b q0 ]/ [q0 b q0 ]
[q0 b q0 ] → b [q0 b q0 ]/ [q0 b q1 ]
[q0 b q0 ] → b [q0 b q1 ]/ [q1 $ q1 ]
[q0 $ q1 ] → c [q0 b q1 ]
[q0 a q1 ] → c [q1 a q1 ]
[q0 b q0 ] → c [q1 b q1 ]
12/31/2016 Abhimanyu Mishra(CSE) JETGI
20. Problems?
1. Construct PDA for language L = {(ab)n /n>=1}.
2. Construct a PDA to accept the language L = { set of all words in a and
b with an even numbers of a’s}.
3. Construct a PDA for the regular expression
r = 0*1+
12/31/2016 Abhimanyu Mishra(CSE) JETGI
21. TWO STACK PDA
A two-stack PDA is a sixtuple (Q, , , , q0, F), where Q, , , q0, and F are
the same as in a one-stack PDA. The transition function as follow:
: Q ( { }) ( { }) ( { }) to the set of all subsets
of Q ( { }) ( { })
The two stacks of the PDA are independent.
one stack: two stack:
A/B A/B/C
Two-Stack PDA accepts any language that accepted by a Turing Machine.
12/31/2016 Abhimanyu Mishra(CSE) JETGI
22. TWO STACK PDA
(i) A Turing machine can accept languages not accepted
by any PDA with one stack.
(ii) The strength of pushdown automata can be increased by
adding other stacks.
(iii) Actually, a PDA with two stacks have the same computation
control as a Turing Machine.
12/31/2016 Abhimanyu Mishra(CSE) JETGI
23. Diagram of TWO STACK PDA
INPUT TAPE
TAPE HEAD HEAD MOVES
STACK 1 STACK 2
12/31/2016 Abhimanyu Mishra(CSE) JETGI
Finite Control
24. Theorem 8.13 (Hopcroft and Ullman [1])
If a language L is accepted by a Turing machine, L is accepted by a Two-
Stack machine.
The idea is that the first stack can hold what is to the left of the head, while the
second stack holds what is to the right of the head, neglecting all the infinite
blank symbols beyond the leftmost and rightmost of the head.
The proof is taken from (Hopcroft and Ullman [1])
12/31/2016 Abhimanyu Mishra(CSE) JETGI
25. Let’s L be L(M) for some one tape TM M, two-stack machine S, simulating a
one-tape TM M as the following:
S begins with a bottom-of-stack marker on each stack, this marker considered
the start symbol for the stacks, and must not appear elsewhere on the stacks.
The marker indicates that the stack is empty.
The proof is taken from (Hopcroft and Ullman [1])
12/31/2016 Abhimanyu Mishra(CSE) JETGI
26. Suppose that w$ is on the input of S. S copies the input w onto its first stack,
and stops to copy when reading the end marker on the input.
S pops each symbol in turn from its first stack and pushes it onto its second stack.
The first stack of S is empty. The second stack holds w, with the left end of w is
at the top.
12/31/2016 Abhimanyu Mishra(CSE) JETGI