Professional Documents
Culture Documents
TOC GTU Study Material Presentations Unit-4 05052020031442AM
TOC GTU Study Material Presentations Unit-4 05052020031442AM
TOC GTU Study Material Presentations Unit-4 05052020031442AM
Theory of Computation
Unit-4
Pushdown
Automata, CFL &
NCFL
Prof. Dixita B. Kagathara
dixita.kagathara@darshan.ac.in
Theory
Unit – 4of: Pushdown
Computation (2160704)
Automata, CFL & NCFL 3 Darshan Institute of Engineering & Technology
Why Pushdown Automata?
Exercise
• Draw Finite automata for language
Not b
Possible??
Finite
Input Stack Accept | Reject
Control
Stack
• A PDA can write an unbounded no. of symbols on the stack and read
these symbols later.
• Writing a symbol on to the stack is called “PUSH” operation.
• Removing a symbol off the stack is called “POP” operation.
Unit – 4of: Pushdown
Theory Automata,
Computation CFL & NCFL 5
(2160704) Darshan Institute of Engineering & Technology
Definition of PDA
• A pushdown Automata(PDA) is a 7-tuple ,
where
is finite set of states
and are finite sets of i/p symbol
is stack alphabet
the initial state, is an element of Q
the initial stack symbol, is an element of
is a set of accepting states, is a subset of Q
the set of finite subsets of
The function is called transition function of .
𝒃
𝒃
𝒃
𝒛𝟎
Unit – 4of: Pushdown
Theory Automata,
Computation CFL & NCFL 10
(2160704) Darshan Institute of Engineering & Technology
How to Represent PDA?
Move No State Input Stack Symbol Move(s)
b, a | ^
𝑞0 𝑞1 FIRST 2 SYMBOLS
OF THE STACK
^
a, a | aa
a, z0|az0 b, a | ^
b, a | ^ ^,z0 | z0
𝑞0 𝑞1 𝑞2
a, a | aa ^,z0 | z0
a, z0|az0
a, b|^ b, z0|bz0
b, a|^ ^,z0 | z0
𝑞0 𝑞1
a, a | aa
b, b | bb
Unit – 4of: Pushdown
Theory Automata,
Computation CFL & NCFL 13
(2160704) Darshan Institute of Engineering & Technology
Design PDA for {anbn+m cm | n,m>=1}
an bn+m cm = an bn bm cm
= a 2 b2 b3 c 3
= aa bb bbb ccc
a, a| aa b, a| ^ b, b| bb
b, z0| bz0
𝑞0
a, z0| az0
𝑞1
b, a| ^
𝑞2 𝑞3
c, b| ^
c, b| ^
𝑞4
^, z0| z0
𝑞5
Unit – 4of: Pushdown
Theory Automata,
Computation CFL & NCFL 16
(2160704) Darshan Institute of Engineering & Technology
Exercise: Design PDA
1. Design PDA for anbncmdm , n,m>=1
2. Design PDA for anbmcmdn , n,m>=1
3. Design PDA for an+mbncm , n,m>=1
𝒂
𝒃
𝒂
𝒛𝟎
Unit – 4of: Pushdown
Theory Automata,
Computation CFL & NCFL 19
(2160704) Darshan Institute of Engineering & Technology
Design PDA for palindrome with middle symbol c.
Move No State Input Stack Symbol Move(s)
b, z0|bz0
a, z0|az0 a, a | aa
𝑞0
b, b | bb b, a | ba
a, b | ab
b, z0|bz0
a, z0|az0 a, a | aa
c,a | a
𝑞0 c,b | b
c,z0 | z0
𝑞1
b, b | bb b, a | ba
a, b | ab
b, z0|bz0 b, b | ^
a, z0|az0 a, a | aa
c,a | a
𝑞0 c,b | b
c,z0 | z0
𝑞1 ^,z0 | z0
𝑞2
b, b | bb b, a | ba
a, b | ab a, a | ^
𝑞0 c,b | b
c,z0 | z0
𝑞1 ^,z0 | z0
𝑞2
b, b | bb b, a | ba
a, b | ab a, a | ^
Move No Resulting State Unread Input Stack
𝒒
𝑞𝟐2
a, a| aa b, a| ^ c, z0| z0
c, z0| z0
𝑞0 a, z0| az0
𝑞1
b, a| ^
𝑞2 𝑞3
a, a| aa b, a| ^ c, z0| z0
c, z0| z0
𝑞0 a, z0| az0
𝑞1
b, a| ^
𝑞2 𝑞3 ^, z0| z0
𝑞7
a, z0| z0 a, z0| z0 b, b| bb c, b| ^
𝑞 4 b,z | bz 𝑞5
0 0 c, b| ^
𝑞6
Unit – 4of: Pushdown
Theory Automata,
Computation CFL & NCFL 26
(2160704) Darshan Institute of Engineering & Technology
Design PDA for {aibjck | i,j,k>=1 & j=i or j=k}
Move No State Input Stack Symbol Move(s)
i=jj=i
J= ^, z0| z0
𝑞7
j=k
a, z0| z0 b, b| bb c, b| ^
a, z0| z0
^, z0| z0
J=
𝑞4 b,z0| bz0
𝑞5 c, b| ^
𝑞6
Unit – 4of: Pushdown
Theory Automata,
Computation CFL & NCFL 27
(2160704) Darshan Institute of Engineering & Technology
Design DPDA for L={anb2n|a,b ∈ Ʃ, n≥0} 𝒂
𝒂
anb2n If n=1 then a 1 b2 = a b b
𝒃
𝒃
𝒃
𝒛𝟎
Unit – 4of: Pushdown
Theory Automata,
Computation CFL & NCFL 28
(2160704) Darshan Institute of Engineering & Technology
Design DPDA for L={anb2n|a,b ∈ Ʃ, n≥0}
Move No State Input Stack Symbol Move(s)
a, z0|aa b, a | ^
b, a | ^ ^,z0 | z0
𝑞0 𝑞1 𝑞2
a, a | aa ^,z0 | z0
b, z0|bz0
a, z0|az0 a, a | aa b, z0| z0
za,0 z0| z0
𝑞0 a,a | a
𝑞1
b,a | a
b, b | bb b, a | ba
a,b | b
a, b | ab
b,b | b
Unit – 4of: Pushdown
Theory Automata,
Computation CFL & NCFL 30
(2160704) Darshan Institute of Engineering & Technology
Design PDA for even odd length palindrome
Move No State Input Stack Symbol Move(s)
^,b | b
b, z0|bz0 ^,a | a
^,z0 | z0
a, z0|az0 a, a | aa a,z0 | z0
𝑞0 b,z0|z0
𝑞1
a,a | a
b,a | a
b, b | bb b, a | ba
a, b | ab a,b | b
b,b | b
^,b | b
b, z0|bz0 ^,a | a
^,z0 | z0 b, b | ^
a, z0|az0 a, a | aa a,z0|z0
𝑞0 b,z0|z0
𝑞1 ^,z0 | z0
𝑞2
a,a | a
b,b | b
b, b | bb b, a | ba
a, b | ab a,b | b a, a | ^
b,a | a
{, [ | {[
{, z0|{z0 [, [|[[ [, { | [{
𝑞0 [, z0|[z0
𝑞1
{, { | {{
{, [ | {[
{, z0|{z0 [, [|[[ [, { | [{
𝑞0 [, z0|[z0
}, { | ^
𝑞1
^,z0 | z0 {, { | {{
], [ | ^
Unit – 4of: Pushdown
Theory Automata,
Computation CFL & NCFL 34
(2160704) Darshan Institute of Engineering & Technology
Design PDA to accept string with more a’s than b’s.
Move No State Input Stack Symbol Move(s)
a, b | ^ a, a | aa
b, z0| bz0 ^, a | a
a, z0|az0
𝑞0 𝑞1
b, a | ^
b, b | bb
Unit – 4of: Pushdown
Theory Automata,
Computation CFL & NCFL 36
(2160704) Darshan Institute of Engineering & Technology
CFG to PDA
Theory
Unit – 4of: Pushdown
Computation (2160704)
Automata, CFL & NCFL 40 Darshan Institute of Engineering & Technology
Steps to convert CFG to PDA
• From given CFG G=(V,Σ,S,P), we can construct a PDA.
• The PDA’s δ is given by :
1. For each non terminal A, include a transition
Include a transition δ(q, 𝜖, A){(q, α)|A α is a production in G}
2. For each terminal a, include a transition
Include a transition δ(q, a, a){(q, 𝜖)}
Theory
Unit – 4of: Pushdown
Computation (2160704)
Automata, CFL & NCFL 44 Darshan Institute of Engineering & Technology
Rule to convert PDA to CFG
1. Add the following production for the start symbol S.
S[q0 z qi]
q0 initial state
z initial stake symbol
qi ∈ Q
2. For each transition of the form δ( qi, a, B) (qj, C)
where qi, qj ∈ Q
a ∈ (Ʃ U ^)
B, C ∈ (┌ U ^)
Then for each q∈ Q, add the production
[qi, B, q]a[qj, C, q]
Unit – 4of: Pushdown
Theory Automata,
Computation CFL & NCFL 45
(2160704) Darshan Institute of Engineering & Technology
Rule to convert PDA to CFG
3. For transition of the form δ( qi, a, B) (qj,C1C2)
where qi, qj ∈ Q
a ∈ (Ʃ U ^)
B, C1, C2 ∈ (┌ U ^)
Then for each p1, p2 ∈ Q, add the production
[qi, B, p1]a[qj C1 p2] [p2 C2 p1]
Theory
Unit – 4of: Pushdown
Computation (2160704)
Automata, CFL & NCFL 56 Darshan Institute of Engineering & Technology
Intersection and complement of CFL
• Theorem:-There are CFLs L1 and L2 so that L1∩ L2 is not a CFL, and
there is a CFL L so that L’ is not a CFL.
• We observed that
L= { aibjck| i < j and i < k }
• is not context-free. However, although no PDA can test both
conditions i < j and i< k simultaneously, it is easy enough to build
two PDAs that test the conditions separately. In other words,
although the intersection L of the two languages
L1 = {a i bjck | i < j}
and
L2 = {aibjck | i < k}
• is not a CFL, both languages themselves are.
Unit – 4of: Pushdown
Theory Automata,
Computation CFL & NCFL 57
(2160704) Darshan Institute of Engineering & Technology
Intersection and complement of CFL
• Another way to verify that L1 is a CFL is to check it is generated by
the grammar with productions
SABC AaAb|˄ BbB|b CcC|˄
• Similarly, L2is generated by the CFG with productions
SAC AaAc|B BbB|˄ CcC|c
• The second statement in the theorem follows from the first and
the formula
L1∩ L2 =( L1’U L2’)’
• If complements of CFLs were always CFLs, then for any CFLs L1 and
L2, the language L1’and L2’ would be CFLs, so would their union,
and so would its complement. We know now that this is not the
Unit
Theory
case.
– 4of: Pushdown Automata,
Computation CFL & NCFL 58 Darshan Institute of Engineering & Technology
(2160704)
Pumping lemma for CFG
Theory
Unit – 4of: Pushdown
Computation (2160704)
Automata, CFL & NCFL 59 Darshan Institute of Engineering & Technology
Pumping lemma for CFG
• Suppose L is a context-free language. Then there is an integer n so
that for every u ∈ L with |u| ≥ n, there are strings v, w, x, y, and z
satisfying,
u = vwxyz
|wy| > 0
|wxy| ≤ n
for every m ≥ 0, vwmxymz ∈ L
Theory
Unit – 4of: Pushdown
Computation (2160704)
Automata, CFL & NCFL 61 Darshan Institute of Engineering & Technology