0% found this document useful (0 votes)
187 views

Unit 4

This document provides information about pushdown automata (PDA). It defines PDA as a finite automata with an extra memory called a stack, which helps PDA recognize context-free languages. The document discusses the formal definition of PDA and how they can accept languages by emptying the stack or reaching a final state. It also provides an example of a PDA that recognizes the language L={anbn | n>=1} and shows the instantaneous descriptions and transitions for the strings "aabb" and "aaabb".

Uploaded by

Prasanna Kumar
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)
187 views

Unit 4

This document provides information about pushdown automata (PDA). It defines PDA as a finite automata with an extra memory called a stack, which helps PDA recognize context-free languages. The document discusses the formal definition of PDA and how they can accept languages by emptying the stack or reaching a final state. It also provides an example of a PDA that recognizes the language L={anbn | n>=1} and shows the instantaneous descriptions and transitions for the strings "aabb" and "aaabb".

Uploaded by

Prasanna Kumar
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/ 153

Course – TFC

Course Instructor
Dr. Umadevi V
Department of CSE, BMSCE
Webpage:https://sites.google.com/site/drvumadevi/

January 1, 2024 CSE, BMSCE 1


Unit-4

Pushdown Automata
Definition of the Pushdown Automaton, The Languages of a PDA,
Equivalence of PDA’s and CFG’s , Deterministic Pushdown
Automata,
The Pumping Lemma for Context Free Languages, Closure
Properties of Context Free Languages

January 1, 2024 CSE, BMSCE 2


Push Down Automata (PDA)

Pushdown Automata is a finite automata with


extra memory called stack which helps Pushdown
automata to recognize Context Free Languages.

January 1, 2024 CSE, BMSCE 3


Schematic Representation of PDA
Rough Slide

January 1, 2024 CSE, BMSCE 4


Schematic Representation of PDA
Rough Slide

 Example: L={anbn, n>=1}


 Valid String “aabb”

Input a a b b 𝞮

z0
Stack

January 1, 2024 CSE, BMSCE 5


Schematic Representation of PDA
Rough Slide

 Example: L={anbn, n>=1}


 Valid String “aabb” Invalid String “aabbb”

𝞮 Input a a b b b 𝞮
Input a a b b

a
a
z0 z0
Stack Stack

January 1, 2024 CSE, BMSCE 6


Schematic Representation of PDA

 Example: L={anbn, n>=1}


 Valid String “aabb” Invalid String “aabbb”

𝞮 Input a a b b b 𝞮
Input a a b b

a a
a a
z0 z0
Stack Stack

January 1, 2024 CSE, BMSCE 7


Formal Definition of PDA
P=(Q, ∑ , Γ , q0, Z0, δ, F)
 Q is the set of states
 ∑ is the set of input alphabet
 Γ is the set of symbols called stack alphabet
 q0 ∈ Q , is the initial state
 Z0 ∈ Γ, is the initial symbol on the stack
 F is the set of final states
 δ is a transition function which maps Q x {Σ ∪ ε} x Γ into Q x Γ*. In a given
state, PDA will read input symbol and stack symbol (top of the stack) and
move to a new state and change the symbol of stack.

δ(State, Input Symbol, Stack Symbol)=(Next State, Stack Symbol)

January 1, 2024 CSE, BMSCE 8


Classification of Push Down Automata
1. Deterministic Push Down Automata (DPDA)
2. Non-Deterministic Push Down Automata (NPDA)

Note: Unlike Finite Automata, DPDA and NPDA are not equivalent. For example,
the language
L={WWR, W ∈(a+b)*) } is accepted by NPDA but not by any DPDA

January 1, 2024 CSE, BMSCE 9


Two ways a PDA accept the language
There are two ways a PDA can accept the language:
a. Empty Stack: If a sentence in a language causes PDA to empty its stack
then the sentence is accepted.
b. Final State: If a sentence in a language causes PDA to move to a state
which is member of a set of final states, then the sentence is accepted.

January 1, 2024 CSE, BMSCE 10


Question
Construct PDA ( or DPDA) to recognize the language L={anbn, n>=1} by empty stack
method. Show the moves made by PDA for the string aabb and aaabb

January 1, 2024 CSE, BMSCE 11


Construct PDA ( or DPDA) to recognize the language L={a nbn, n>=1} by empty stack method.
Rough Slide

Construct PDA ( or DPDA) to recognize the language L={anbn, n>=1} by empty stack
method. Show the moves made by PDA for the string aabb and aaabb

States Stack Top Input Symbols


Symbol
a b 𝞮
q0 z0

a
q1 z0
a

January 1, 2024 CSE, BMSCE 12


Construct PDA ( or DPDA) to recognize the language L={anbn, n>=1} by empty
stack method.

Construct PDA ( or DPDA) to recognize the language L={anbn, n>=1} by empty stack
method. Show the moves made by PDA for the string aabb and aaabb

States Stack Top Input Symbols


Symbol
a b 𝞮
q0 z0 Push a
q0 X X
a Push a Pop a
q0 q1 X
q1 z0 Pop Z0
X X q1
a Pop a
X q1 X
January 1, 2024 CSE, BMSCE 13
Construct PDA ( or DPDA) to recognize the language L={anbn, n>=1} by empty stack
method.

Representing PDA as a generalized


Transition Diagram

a, z0 | az0 b, a | 𝞮

b, a | 𝞮
q0 q1

a, a | aa 𝞮, Z0 | 𝞮
January 1, 2024 CSE, BMSCE 14
Construct PDA ( or DPDA) to recognize the language L={anbn, n>=1} by empty stack
method.

Formal definition of PDA


P=(Q, ∑ , Γ , q0, Z0, δ, F)

Q = { q 0 , q1 }
∑ = {a, b}
Γ ={a, Z0}
q0 = q0
Z0 = Z0
F={}
Push Operation
δ(q0 , a, z0) =(q0 , az0)
δ(q0 , a, a) =(q0 , aa)
δ(q0 , b, a) =(q1 , 𝞮) Pop Operation
δ(q1 , b, a) =(q1 , 𝞮)
δ(q1 , 𝞮, z0) =(q1 , 𝞮)

January 1, 2024 CSE, BMSCE 15


Instantaneous Description of a PDA
Rough Slide

 The current configuration of PDA at any given instant can be described by Instantaneous
Description (ID).
 An ID gives current state of PDA, the remaining string to be processed and the entire contents of
stack.
Instantaneous Description for the string "aabb"

(q0, aabb, z0)

January 1, 2024 CSE, BMSCE 16


Instantaneous Description of a PDA
 The current configuration of PDA at any given instant can be described by Instantaneous
Description (ID).
 An ID gives current state of PDA, the remaining string to be processed and the entire contents of
stack.
Instantaneous Description for the string "aabb"

(q0, aabb, z0) ˫ (q , abb, az )


0 0

˫ (q , bb, aaz )
0 0

˫ (q , b, az )
1 0

˫ (q , 𝞮, z )
1 0

˫ (q , 𝞮, 𝞮)
1

All the Input symbols


are read and Stack is empty,
therefore the string “aabb” is
Accepted.

January 1, 2024 CSE, BMSCE 17


Instantaneous Description of a PDA
Rough Slide

Instantaneous Description for the string "aaabb"

(q0, aaabb, z0)

January 1, 2024 CSE, BMSCE 18


Instantaneous Description of a PDA
Instantaneous Description for the string "aaabb"

(q0, aaabb, z0) ˫ (q , aabb, az )


0 0

˫ (q , abb, aaz )
0 0

˫ (q , bb, aaaz )
1 0

˫ (q , b, aaz )
1 0

˫ (q , 𝞮, az )
1 0

All the Input symbols


are read but the Stack is
not empty, therefore the string
“aaabb” is not accepted.

January 1, 2024 CSE, BMSCE 19


Question
Construct PDA ( or DPDA) to recognize the language L={anb2n, n>=1} by empty stack
method. Give the graphical representation of the PDA obtained. Show the moves made by
PDA for the string aabbbb and aabbb

January 1, 2024 CSE, BMSCE 20


Construct PDA ( or DPDA) to recognize the language L={a nb2n, n>=1} by empty stack method.
Rough Slide

States Stack Top Input Symbols


Symbol
a b 𝞮
q0 z0

a
q1 z0
a

January 1, 2024 CSE, BMSCE 21


Construct PDA ( or DPDA) to recognize the language L={anb2n, n>=1} by empty
stack method.
States Stack Top Input Symbols
Symbol
a b 𝞮
q0 z0 Push aa
q0 X X
a Push aa Pop a
q0 q1 X
q1 z0 Pop Z0
X X
Representing PDA as a generalized
Transition Diagram
q1
a Pop a
X q1 X a, z0 | aaz0 b, a | 𝞮

b, a | 𝞮
q0 q1

a, a | aaa 𝞮, Z0 | 𝞮
January 1, 2024 CSE, BMSCE 22
Construct PDA ( or DPDA) to recognize the language L={anb2n, n>=1} by empty stack
method.

Formal definition of PDA


P=(Q, ∑ , Γ , q0, Z0, δ, F)

Q = { q 0 , q1 }
∑ = {a, b}
Γ ={a, Z0}
q0 = q0
Z0 = Z0
F={}

δ(q0 , a, z0) =(q0 ,aaz0) Push Operation


δ(q0 , a, a) =(q0 ,aaa)
δ(q0 , b, a) =(q1 , 𝞮) Pop Operation
δ(q1 , b, a) =(q1 , 𝞮)
δ(q1 , 𝞮, z0) =(q1 , 𝞮)

January 1, 2024 CSE, BMSCE 23


Construct PDA ( or DPDA) to recognize the language L={a nb2n, n>=1} by empty stack method.
Rough Slide

Instantaneous Description for the string "aabbbb"


(q0, aabbbb, z0)

January 1, 2024 CSE, BMSCE 24


Construct PDA ( or DPDA) to recognize the language L={anb2n, n>=1} by empty
stack method.

Instantaneous Description for the string "aabbbb"

(q0, aabbbb, z0) ˫ (q , abbbb, aaz )


0 0

˫ (q , bbbb, aaaaz )
0 0

˫ (q , bbb, aaaz )
1 0

˫ (q , bb, aaz )
1 0

˫ (q , b, az )
1 0

˫ (q , 𝞮, z )
1 0

˫ (q , 𝞮, 𝞮)
1
All the Input symbols
are read and Stack is
empty, therefore the string
“aabbbb” is accepted.

January 1, 2024 CSE, BMSCE 25


Construct PDA ( or DPDA) to recognize the language L={anb2n, n>=1} by empty
stack method.

Instantaneous Description for the string "aabbb"

(q0, aabbb, z0) ˫ (q , abbb, aaz )


0 0

˫ (q , bbb, aaaaz )
0 0

˫ (q , bb, aaaz )
1 0

˫ (q , b, aaz )
1 0

˫ (q , 𝞮, az )
1 0

All the Input symbols


are read but Stack not is
empty, therefore the string
“aabbb” is not accepted.

January 1, 2024 CSE, BMSCE 26


Question

The push down automata indicate the acceptance


of input string in terms of
a) Finial state
b) Empty stack
c) Both (a) and (b)
d) None of these

January 1, 2024 CSE, BMSCE 27


Question

The push down automata indicate the acceptance


of input string in terms of
a) Finial state
b) Empty stack
c) Both (a) and (b)
d) None of these

January 1, 2024 CSE, BMSCE 28


Question

The instantaneous description is PDA shows


a) Present state
b) Stack symbol
c) String to be processed
d) All of these

January 1, 2024 CSE, BMSCE 29


Question

The instantaneous description is PDA shows


a) Present state
b) Stack symbol
c) String to be processed
d) All of these

January 1, 2024 CSE, BMSCE 30


Question
Construct PDA ( or DPDA) to recognize the language L={a2nbn, n>=1} by empty stack
method. Give the graphical representation of the PDA obtained. Show the moves made by
PDA for the string aaaabb and aabbb

January 1, 2024 CSE, BMSCE 31


Question
Construct PDA ( or DPDA) to recognize the language L={a2nbn, n>=1} by empty stack
method. Give the graphical representation of the PDA obtained. Show the moves made by
PDA for the string aaaabb and aabbb

Solution:
Logic

Initial
a a a a b b 𝞮
State q0

Go To Push a Go To Push a Pop a Pop a Pop Z0


State q1 Go To State q1 Go To Go To Be in Be in
State q0 State q0 State q2 State q2 State q2

January 1, 2024 CSE, BMSCE 32


Construct PDA ( or DPDA) to recognize the language L={a 2nbn, n>=1} by empty stack method.
Rough Slide

States Stack Top Input Symbols


Symbol
a b 𝞮
q0 z0

a
q1 z0

a
q2 z0
a

January 1, 2024 CSE, BMSCE 33


Construct PDA ( or DPDA) to recognize the language L={a2nbn, n>=1} by empty
stack method.

States Stack Top Input Symbols


Symbol
a b 𝞮
q0 z0 q1
X X
a q1 Pop a
q2 X
q1 z0 Push a
q0 X X
a Push a
q0 X X
q2 z0 Pop Z0
X X q2
a Pop a
X q2 X
January 1, 2024 CSE, BMSCE 34
Construct PDA ( or DPDA) to recognize the language L={a2nbn, n>=1} by empty
stack method.

Representing PDA as a generalized Transition Diagram

b, a | 𝞮
b, a | 𝞮

a, z0 | z0 q2
q0 q1
a, a | a

a, z0 | az0 𝞮, Z0 | 𝞮
a, a | aa

January 1, 2024 CSE, BMSCE 35


Construct PDA ( or DPDA) to recognize the language L={a2nbn, n>=1} by empty
stack method.

January 1, 2024 CSE, BMSCE 36


Question

The push down automata indicate the acceptance


of input string in terms of
a) Finial state
b) Empty store
c) Both (a) and (b)
d) None of these

January 1, 2024 CSE, BMSCE 37


Question

The push down automata indicate the acceptance


of input string in terms of
a) Finial state
b) Empty store
c) Both (a) and (b)
d) None of these

January 1, 2024 CSE, BMSCE 38


Question

The instantaneous description is PDA shows


a) Present state
b) Stack symbol
c) String to be processed
d) All of these

January 1, 2024 CSE, BMSCE 39


Question

The instantaneous description is PDA shows


a) Present state
b) Stack symbol
c) String to be processed
d) All of these

January 1, 2024 CSE, BMSCE 40


Question
Design Deterministic Push Down Automata for the language
L={WcWR, W ∈ (a+b)*} by empty stack. Show instantaneous
description for the string aabcbaa

Example Strings:
c , aca , bcb , aacaa , abcba , abbcbba

Logic: Push w on to stack, when c is seen switch to different state, for wr do the
Popping operation

January 1, 2024 CSE, BMSCE 41


Design DPDA for the language L={WcWR, W ∈ (a+b)*} by empty stack.
Rough Slide

State Stack Input Symbols


Top
Symbol a b c 𝞮

q0 z0

a
b
q1 Z0
a
b

January 1, 2024 CSE, BMSCE 42


Design DPDA for the language L={WcWR, W ∈ (a+b)*} by empty stack.

State Stack Input Symbols


Top
Symbol a b c 𝞮 Transition Diagram

c, z0 | z0
q0 z0 Push a Push b q1
q0 q0 X c, a | a
c, b | b
a Push a Push b q1 q0 q1
q0 q0 X
b Push a Push b q1
q0 q0 X a,
b,
z0
z0
| az0
| bz0
a, a | 𝞮
b, b | 𝞮
q1 Z0 Pop Z0 a, a | aa 𝞮, Z0 | 𝞮
X X X q1 a, b | ab
b, b | bb
a Pop a
q1 X X X b, a | ba

b Pop b
X q1 X X
January 1, 2024 CSE, BMSCE 43
Design DPDA for the language L={WcWR, W ∈ (a+b)*} by empty stack.
Formal definition of PDA
P=(Q, ∑ , Γ , q0, Z0, δ, F)
Q = { q 0 , q1 }
∑ = {a, b, c}
Γ ={a, b, Z0}
q0 = q0
Z0 = Z0
F={}
δ(q0 , a, z0) =(q0 ,az0)
δ(q0 , b, z0) =(q0 ,bz0) Push Operation
δ(q0 , a, a) =(q0 ,aa)
δ(q0 , a, b) =(q0 ,ab)
δ(q0 , b, b) =(q0 ,bb)
δ(q0 , b, a) =(q0 ,ba)
No action
δ(q0 , c, z0) =(q1 ,z0)
δ(q0 , c, a) =(q1 ,a)
δ(q0 , c, b) =(q1 ,b)
Pop Operation
δ(q1 , a, a) =(q1 , 𝞮)
January 1, 2024 δ(q1 , b, b) =(q1 , 𝞮)
CSE, BMSCE 44
δ(q , 𝞮, z ) =(q , 𝞮)
Design DPDA for the language L={WcWR, W ∈ (a+b)*} by empty stack.
Rough Slide

Instantaneous Description for the string "aabcbaa"


(q0, aabcbaa, z0)

January 1, 2024 CSE, BMSCE 45


Design DPDA for the language L={WcWR, W ∈ (a+b)*} by empty stack.

Instantaneous Description for the string "aabcbaa"

(q0, aabcbaa, z0) ˫ (q , abcbaa, az )


0 0

˫ (q , bcbaa, aaz )
0 0

˫ (q , cbaa, baaz )
0 0

˫ (q , baa, baaz )
1 0

˫ (q , aa, aaz )
1 0

˫ (q , a, az )
1 0

˫ (q , 𝞮, z )
1 0

˫ (q , 𝞮, 𝞮)
1
All the Input symbols
are read and Stack is
empty, therefore the string
“aabcbaa” is accepted.

January 1, 2024 CSE, BMSCE 46


Question
Design Deterministic Push Down Automata for the language L={W, W ∈ (a+b)*
and na(w)==nb(w)} by empty stack. Show instantaneous description for the
string aababb
Example Strings:
ab , ba , baba , aabb , baab , aababb

Logic: Input Stack Top Operation

a Z0 Push a

b Z0 Push b

a a Push a

b a Pop a

a b Pop b

b b Push b

January 1, 2024 CSE, BMSCE 47


Design DPDA for the language L={W, W ∈ (a+b)* and n a(w)==nb(w)} by empty stack.
Rough Slide

State Stack Input Symbols


Top
Symbol a b 𝞮

q0 z0

a
b
q1 Z0
a
b

January 1, 2024 CSE, BMSCE 48


Design DPDA for the language L={W, W ∈ (a+b)* and na(w)==nb(w)} by empty
stack.

State Stack Input Symbols


Top
Symbol a b 𝞮

q0 z0 Push a Push b q1
q0 q0
a Push a Pop a
q0 q0 X
b Pop b Push b
q0 q0 X
q1 Z0 Pop Z0
X X q1
a
X X X
b
January 1, 2024
X X X
CSE, BMSCE 49
Design DPDA for the language L={W, W ∈ (a+b)* and na(w)==nb(w)} by empty
stack.

Instantaneous Description for the string "aababb"

(q0, aababb, z0) ˫ (q , ababb, az )


0 0

˫ (q , babb, aaz )
0 0

˫ (q , abb, az )
0 0

˫ (q , bb, aaz )
0 0

˫ (q , b, az )
0 0

˫ (q , 𝞮, z )
0 0

˫ (q , 𝞮, z )
1 0

˫ (q , 𝞮, 𝞮)
1
All the Input symbols
are read and Stack is
empty, therefore the string
“aababb” is accepted.

January 1, 2024 CSE, BMSCE 50


Question
Design Deterministic Push Down Automata for the language L={W, W ∈ (a+b)*
and na(w) < nb(w)} by final state. Show instantaneous description for the string
abbab
Example Strings:
b, bb , babab , abbb , bbbaa , abbab

Logic: Input Stack Top Operation

a Z0 Push a

b Z0 Push b

a a Push a

b a Pop a

a b Pop b

b b Push b
𝞮 b Move to Final State

January 1, 2024 CSE, BMSCE 51


Design Deterministic Push Down Automata for the language L={W, W ∈ (a+b)*
and na(w) < nb(w)} by final state.

State Stack Input Symbols


Top
Symbol a b 𝞮 Transition Diagram

q0 z0 Push a Push b
q0 q0 X q0
𝞮, b | b
q1
a Push a Pop a
q0 q0 X
b Pop b Push b q1 a, z0 | az0 𝞮, b | b
q0 q0 b, z0 | bz0
a, a | aa
q1 Z0
X X X a,
b,
b
b
|𝞮
| bb
a
X X X b, a |𝞮

b q1
X X
January 1, 2024 CSE, BMSCE 52
Design Deterministic Push Down Automata for the language L={W, W ∈ (a+b)*
and na(w) < nb(w)} by final state.
Formal definition of PDA
P=(Q, ∑ , Γ , q0, Z0, δ, F)
Q = { q 0 , q1 }
∑ = {a, b}
Γ ={a, b, Z0}
q0 = q0
Z0 = Z0
F = {q1}
δ(q0 , a, z0) =(q0 ,az0)
δ(q0 , b, z0) =(q0 ,bz0) Push Operation
δ(q0 , a, a) =(q0 ,aa)
δ(q0 , b, b) =(q0 ,bb)
Pop Operation
δ(q0 , a, b) =(q0 , 𝞮)
δ(q0 , b, a) =(q0 , 𝞮)
No action
δ(q0 , 𝞮, b) =(q1 ,b)
δ(q 1 , for
Note: 𝞮, b) =(q
na(w) ,b)
>1 n b(w)
δ(q0 , 𝞮, a) =(q1 ,a)
δ(q1 , 𝞮, a) =(q1 ,a)
January 1, 2024 CSE, BMSCE 53
Design Deterministic Push Down Automata for the language L={W, W ∈ (a+b)*
and na(w) < nb(w)} by final state.

Instantaneous Description for the string "abbab"

(q0, abbab, z0) ˫ (q , bbab, az )


0 0

˫ (q , bab, z )
0 0

˫ (q , ab, bz )
0 0

˫ (q , b, z )
0 0

˫ (q , 𝞮, bz )
0 0

˫ (q , 𝞮, bz )
1 0

˫ (q , 𝞮, bz )
1 0
All the Input symbols
are read and reached to
Final state, therefore the string
“abbab” is accepted.

January 1, 2024 CSE, BMSCE 54


Question
Design Deterministic Push Down Automata for the language L={aibJck ,
J=i+k ,i,k≥0} by empty stack. Show instantaneous description for the string
abbbcc
Example Strings:
i k J=i+k Strings
aibJck
0 0 0 𝞮

1 1 2 abbc

1 0 1 ab

0 1 1 bc

2 2 4 aabbbbcc

2 1 3 aabbbc

1 2 3 abbbcc

January 1, 2024 CSE, BMSCE 55


Question
Design Deterministic Push Down Automata for the language L={aibJck ,
J=i+k ,i,k≥0} by empty stack. Show instantaneous description for the string
abbbcc
Example Strings:
𝞮 , abbc , ab , bc , aabbbbcc , aabbbc , abbbcc

aibJck
Logic:
aibibkck

ai bi bk ck 𝞮

Push a Pop a Push b Pop b Pop Z0

January 1, 2024 CSE, BMSCE 56


Design Deterministic Push Down Automata for the language L={a ibJck , J=i+k ,i,k≥0} by empty
stack.
Rough Slide
State Stack Top Input Symbols
Symbol
a b c 𝞮

q0 z0
a

q1 Z0

q2 Z0

q3 Z0

January 1, 2024 CSE, BMSCE 57


Design Deterministic Push Down Automata for the language L={aibJck , J=i+k ,i,k≥0} by
empty stack.
State Stack Input Symbols
Top
Symbol a b c 𝞮

q0 z0 Push a , q0 Push b, q1 X q3
a Push a , q0 Pop a, q1 X X

b X X X X

q1 Z0 X Push b, q1 X X

a X Pop a, q1 X X

b X Push b, q1 Pop b, q2 X

q2 Z0 X X X q3

a X X X X

b X X Pop b, q2 X

q3 Z0 X X X Pop z0, q3

a X X X X

b X X X X
January 1, 2024 CSE, BMSCE 58
Design Deterministic Push Down Automata for the language L={aibJck ,
J=i+k ,i,k≥0} by empty stack.

Instantaneous Description for the string "abbbcc"

(q0, abbbcc, z0) ˫ (q , bbbcc, az )


0 0

˫ (q , bbcc, z )
0 0

˫ (q , bcc, bz )
1 0

˫ (q , cc, bbz )
1 0

˫ (q , c, bz )
2 0

˫ (q , 𝞮, z )
2 0

˫ (q , 𝞮, z )
3 0

˫ (q , 𝞮, 𝞮)
3

All the Input symbols


are read and stack is
empty, therefore the string
“abbbcc” is accepted.

January 1, 2024 CSE, BMSCE 59


Question
Construct PDA ( or DPDA) to recognize the language L={anbn, n>=1} by final state
method.

January 1, 2024 CSE, BMSCE 60


Construct PDA ( or DPDA) to recognize the language L={a nbn, n>=1} by final state method.
Rough Slide

States Stack Top Input Symbols


Symbol
a b 𝞮
q0 z0

a
q1 z0
a
q2 z0
a

January 1, 2024 CSE, BMSCE 61


January 1, 2024 CSE, BMSCE 62
Question
Design Non-deterministic Push Down Automata for the language L={WWR, W ∈
(a+b)*} by empty stack. Show instantaneous description for the string abba

or

Design Non-deterministic Push Down Automata for the language L={x, x ∈


(a+b)* and x is a palindrome} by empty stack. Show instantaneous description
for the string abba

January 1, 2024 CSE, BMSCE 63


Question
Design Non-deterministic Push Down Automata for the language L={WWR, W ∈
(a+b)*} by empty stack. Show instantaneous description for the string abba
Example Strings:
𝞮 , aa , bb , aaaa , abaaba , bbaabb , abbbba

January 1, 2024 CSE, BMSCE 64


Question
Design Non-deterministic Push Down Automata for the language L={WWR, W ∈
(a+b)*} by empty stack. Show instantaneous description for the string abba
Example Strings:
𝞮 , aa , bb , aaaa , abaaba , bbaabb
Logic:

Input Stack Operation


Top
a Z0 Push a, q0

b Z0 Push b, q0

a a Push a, q0 ; Pop a , q1

b a Push b, q0

a b Push a, q0

b b Push b, q0 ; Pop b, q1

January 1, 2024 CSE, BMSCE 65


Design NPDA for the language L={WWR, W ∈ (a+b)*} by empty stack.
Rough Slide

Logic
Instantaneous Description for the string "abba" Input Stack Operation
Top
(q0, abba, z0)
a Z0 Push a, q0

b Z0 Push b, q0

a a Push a, q0 ; Pop a , q1

b a Push b, q0

a b Push a, q0

b b Push b, q0 ; Pop b, q1
𝞮 Z0 Change state to q2
𝞮 Z0 Pop Z0 , q2

January 1, 2024 CSE, BMSCE 66


Design NPDA for the language L={WWR, W ∈ (a+b)*} by empty stack.
Logic
Instantaneous Description for the string "abba" Input Stack Operation
Top
(q0, abba, z0) ˫ (q , bba, az )
0 0 a Z0 Push a, q0

˫ (q , ba,baz )
0 0
b Z0 Push b, q0

a a Push a, q0 ; Pop a , q1
(q0, a, bbaz0) (q1, a, az0) b a Push b, q0

a b Push a, q0
(q0, 𝞮, abbaz0)
(q1, 𝞮, z0) b b Push b, q0 ; Pop b, q1
Rejected
(q1, 𝞮, 𝞮)
Accepted

January 1, 2024 CSE, BMSCE 67


Design NPDA for the language L={WWR, W ∈ (a+b)*} by empty stack.
Rough Slide

State Stack Top Input Symbols


Symbol
a b 𝞮

q0 z0

q1 Z0

a
b

q2 Z0
a
b

January 1, 2024 CSE, BMSCE 68


Design NPDA for the language L={WWR, W ∈ (a+b)*} by empty stack.

State Stack Top Input Symbols


Symbol
a b 𝞮

q0 z0 Push a , q0 Push b , q0 q2

a Push a , q0 Push b , q0 X
Pop a , q1
b Push a , q0 Push b , q0 X
Pop b , q1
q1 Z0 X X q2

a Pop a , q1 X X
b X Pop b , q1 X

q2 Z0 X X Pop Z0 , q2
a X X X
b X X X
January 1, 2024 CSE, BMSCE 69
Design NPDA for the language L={WWR, W ∈ (a+b)*} by empty stack.

Formal definition of PDA


P=(Q, ∑ , Γ , q0, Z0, δ, F)
Transition Diagram
Q = { q0 , q1 , q2}
a, a | 𝞮 ∑ = {a, b}
𝞮, Z0| z0 Γ ={a, b, Z0}
b, b | 𝞮
q0 q1 q2 q0 = q0
Z0 = Z0
F={}
a, z0 | az0 a, a | 𝞮 𝞮, Z0| 𝞮 δ(q0 , a, z0) =(q0 ,az0)
b, b | 𝞮 δ(q0 , b, z0) =(q0 ,bz0)
b, z0 | bz0
δ(q0 , a, b) ={(q0 ,ab)
a, a | aa
δ(q0 , b, a) ={(q0 ,ba)
a, b | ab
b, b | bb
δ(q0 , a, a) ={(q0 , aa), (q1, 𝞮)}
b, a | ba
δ(q0 , b, b) ={(q0 ,bb), (q1, 𝞮)}

δ(q1 , a, a) =(q1 , 𝞮)
δ(q1 , b, b) =(q1 , 𝞮)

δ(q1 , 𝞮, z0) =(q2 , Z0)


January 1, 2024 CSE, BMSCE 70
δ(q2 , 𝞮, z0) =(q2 , 𝞮)
Equivalence of PDA and CFG

- Converting CFG to PDA


- Converting PDA to CFG

January 1, 2024 CSE, BMSCE 71


Procedure for converting PDA to CFG
1. The input symbols of PDA will be the terminals of CFG
2. If PDA moves from state qi to qj on consuming the input a ∈ Σ when Z is top of the
stack, then the non-terminals of CFG are the triplets of the form (qiZqj)
3. If q0 is the start state and qf is the final state then (q0Zqf) is the start symbol of the
CFG
4. The productions of CFG can be obtained from the transitions of PDA as shown below:
a. For each transition of the form δ(qi , a, Z) = (qj, AB) introduce the
productions of the form
(qiZqk) -> a(qjAql)(qlBqk) where qk and ql will take all possible values from Q
b. For each transition of the form δ(qi , a, Z) = (qj, ε ) introduce the production
(qiZqj) -> a
Note: Using this procedure, we may introduce lot of useless symbols, which in any way can be
eliminated.

January 1, 2024 CSE, BMSCE 72


Question

Obtain CFG that generates the language accepted by


the PDA
P=(Q, ∑ , Γ , q0, Z0, δ, F)
Q = { q 0 , q1 }
∑ = {a, b}
Γ ={A, Z}
q0 = q0
Z0 = Z
F = {q1}
δ(q0 , a, Z)=(q0, AZ)
a, A | 𝞮
δ(q0 , b, A)=(q0, AA)
q0 q1
δ(q0 , a, A)=(q1, 𝞮)

a, z | AZ
b, A | AA
January 1, 2024 CSE, BMSCE 73
Question

Obtain CFG that generates the language accepted by the PDA M=({q0 , q1 }, {a,
b}, {A, Z}, q0 , Z , {q1} ,
{ δ(q0 , a, Z)=(q0, AZ),
δ(q0 , b, A)=(q0, AA),
δ(q0 , a, A)=(q1, 𝞮))

Solution:
For δ of the form Resulting Production
δ(qi , a, Z) = (qj, ε ) (qiZqj) → a

δ(q0 , a, A)=(q1, ε) (q0Aq1) → a

January 1, 2024 CSE, BMSCE 74


Obtain CFG that generates the language accepted by the PDA M=({q0 , q1 }, {a, b}, {A, Z}, q0 , Z , {q1} ,
{δ(q0 , a, Z)=(q0, AZ), δ(q0 , b, A)=(q0, AA), δ(q0 , a, A)=(q1, 𝞮))

Solution (Contd…):
For δ of the form Resulting Production
δ(qi , a, Z) = (qj, AB) (qiZqk) → a(qjAql)(qlBqk)
where qk and ql will take all possible
values from Q
δ(q0 , a, Z)=(q0, AZ) (q0Zq0) → a(q0Aq0)(q0Zq0) | a(q0Aq1)(q1Zq0)

(q0Zq1) → a(q0Aq0)(q0Zq1) | a(q0Aq1)(q1Zq1)

75

January 1, 2024 CSE, BMSCE


Obtain CFG that generates the language accepted by the PDA M=({q0 , q1 }, {a, b}, {A, Z}, q0 , Z , {q1} ,
{δ(q0 , a, Z)=(q0, AZ), δ(q0 , b, A)=(q0, AA), δ(q0 , a, A)=(q1, 𝞮))

Solution (Contd…):
For δ of the form Resulting Production
δ(qi , a, Z) = (qj, AB) (qiZqk) → a(qjAql)(qlBqk)
where qk and ql will take all possible
values from Q
δ(q0 , a, Z)=(q0, AZ) (q0Zq0) → a(q0Aq0)(q0Zq0) | a(q0Aq1)(q1Zq0)

(q0Zq1) → a(q0Aq0)(q0Zq1) | a(q0Aq1)(q1Zq1)

δ(q0 , b, A)=(q0, AA) (q0Aq0) → b(q0Aq0)(q0Aq0) | b(q0Aq1)(q1Aq0)

(q0Aq1) → b(q0Aq0)(q0Aq1) | b(q0Aq1)(q1Aq1)


Start Symbol of the grammar is (q0Zq1)

76

January 1, 2024 CSE, BMSCE


Obtain CFG that generates the language accepted by the PDA M=({q0 , q1 }, {a, b}, {A, Z}, q0 , Z , {q1} ,
{δ(q0 , a, Z)=(q0, AZ), δ(q0 , b, A)=(q0, AA), δ(q0 , a, A)=(q1, 𝞮))

Solution (Contd…):
G=(V, T, P, S)
V= {(q0Zq1) , (q0Aq1) , (q0Zq0), (q0Aq0) ,
(q1Zq0) , (q1Zq1) , (q1Aq0) , (q1Aq1) }
T={a, b}
P={
(q0Aq1) → a
(q0Zq0) → a(q0Aq0)(q0Zq0) | a(q0Aq1)(q1Zq0)
(q0Zq1) → a(q0Aq0)(q0Zq1) | a(q0Aq1)(q1Zq1)
(q0Aq0) → b(q0Aq0)(q0Aq0) | b(q0Aq1)(q1Aq0)
(q0Aq1) → b(q0Aq0)(q0Aq1) | b(q0Aq1)(q1Aq1)
}
S=(q0Zq1)

January 1, 2024 CSE, BMSCE 77


Question

Obtain CFG that generates the language accepted by


the PDA
P=(Q, ∑ , Γ , q0, Z0, δ, F)
Q = { q0 , q1 , q2 }
∑ = {a, b}
Γ ={A, Z}
q0 = q0
Z0 = Z
F = {q2}
δ(q0 , a, Z)=(q0, AZ)
δ(q0 , a, A)=(q0, A)
δ(q0 , b, A)=(q1, 𝞮)
δ(q1 , 𝞮, Z)=(q2, 𝞮)

January 1, 2024 CSE, BMSCE 78


Converting PDA to CFG: M=({ q0 , q1 , q2 }, {a, b} , {A, Z}, q0 , Z, {q2}, {
δ(q0 , a, Z)=(q0, AZ), δ(q0 , a, A)=(q0, A), δ(q0 , b, A)=(q1, 𝞮), δ(q0 , 𝞮, Z)=(q2, 𝞮)}

Solution:
To obtain CFG from the PDA, all transitions should be of the form
δ(qi , a, Z) = (qj, AB)
or
δ(qi , a, Z) = (qj, ε )

In the given transitions except the second transition, all transitions are in the required form. So, let us
take the second transition
δ(q0 , a, A)=(q0, A)
This transition indicates that when input symbol a is encountered and top stack symbol is A, the PDA
remains in the state q0 and contents of the stack are not altered. This can be interpreted as delete A
from stack and insert A onto the stack. So once A is deleted from the stack we enter into a new state
q3. But in state q3 without consuming any input we add A onto the stack. The corresponding
transitions are
δ(q0 , a, A)=(q3, 𝞮)
δ(q3 , 𝞮, Z)=(q0, AZ)

January 1, 2024 CSE, BMSCE 79


Converting PDA to CFG: M=({ q0 , q1 , q2 }, {a, b} , {A, Z}, q0 , Z, {q2}, {
δ(q0 , a, Z)=(q0, AZ), δ(q0 , a, A)=(q0, A), δ(q0 , b, A)=(q1, 𝞮), δ(q1 , 𝞮, Z)=(q2, 𝞮)}

Solution (Contd…):
So the given PDA can be written using the following transitions
δ(q0 , a, Z)=(q0, AZ)
δ(q0 , a, A)=(q3, 𝞮)
δ(q3 , 𝞮, Z)=(q0, AZ)
δ(q0 , b, A)=(q1, 𝞮)
δ(q1 , 𝞮, Z)=(q2, 𝞮)

For δ of the form Resulting Production


δ(qi , a, Z) = (qj, ε ) (qiZqj) → a
δ(q0 , a, A)=(q3, 𝞮) (q0Aq3)→ a
δ(q0 , b, A)=(q1, 𝞮) (q0Aq1) → b
δ(q1 , 𝞮, Z)=(q2, 𝞮) (q1Zq2) → 𝞮

January 1, 2024 CSE, BMSCE 80


Converting PDA to CFG: M=({ q0 , q1 , q2 }, {a, b} , {A, Z}, q0 , Z, {q2}, {
δ(q0 , a, Z)=(q0, AZ), δ(q0 , a, A)=(q0, A), δ(q0 , b, A)=(q1, 𝞮), δ(q1 , 𝞮, Z)=(q2, 𝞮)}

Solution (Contd…):
So the given PDA can be written using the following transitions
δ(q0 , a, Z)=(q0, AZ) , δ(q0 , a, A)=(q3, 𝞮), δ(q3 , 𝞮, Z)=(q0, AZ)
δ(q0 , b, A)=(q1, 𝞮) , δ(q1 , 𝞮, Z)=(q2, 𝞮)
For δ of the form Resulting Production
δ(qi , a, Z) = (qj, AB) (qiZqk) → a(qjAql)(qlBqk)
where qk and ql will take all possible
values from Q
δ(q0 , a, Z)=(q0, AZ) (q0Zq0) → a(q0Aq0)(q0Zq0) | a(q0Aq1)(q1Zq0) |
a(q0Aq2)(q2Zq0) | a(q0Aq3)(q3Zq0)

(q0Zq1) → a(q0Aq0)(q0Zq1) | a(q0Aq1)(q1Zq1) |


a(q0Aq2)(q2Zq1) | a(q0Aq3)(q3Zq1)

(q0Zq2) → a(q0Aq0)(q0Zq2) | a(q0Aq1)(q1Zq2) |


a(q0Aq2)(q2Zq2) | a(q0Aq3)(q3Zq2)

(q0Zq3) → a(q0Aq0)(q0Zq3) | a(q0Aq1)(q1Zq3) |


January 1, 2024 a(qBMSCE
CSE,
0Aq2)(q2Zq3) | a(q0Aq3)(q3Zq3)
81
Converting PDA to CFG: M=({ q0 , q1 , q2 }, {a, b} , {A, Z}, q0 , Z, {q2}, {
δ(q0 , a, Z)=(q0, AZ), δ(q0 , a, A)=(q0, A), δ(q0 , b, A)=(q1, 𝞮), δ(q1 , 𝞮, Z)=(q2, 𝞮)}

Solution (Contd…):
So the given PDA can be written using the following transitions
δ(q0 , a, Z)=(q0, AZ) , δ(q0 , a, A)=(q3, 𝞮), δ(q3 , 𝞮, Z)=(q0, AZ)
δ(q0 , b, A)=(q1, 𝞮) , δ(q1 , 𝞮, Z)=(q2, 𝞮)
For δ of the form Resulting Production
δ(qi , a, Z) = (qj, AB) (qiZqk) → a(qjAql)(qlBqk)
where qk and ql will take all possible values from Q
δ(q3 , 𝞮, Z)=(q0, AZ) (q3Zq0) → (q0Aq0)(q0Zq0) | (q0Aq1)(q1Zq0) |
(q0Aq2)(q2Zq0) | (q0Aq3)(q3Zq0)

(q3Zq1) → (q0Aq0)(q0Zq1) | (q0Aq1)(q1Zq1) |


(q0Aq2)(q2Zq1) | (q0Aq3)(q3Zq1)

(q3Zq2) → (q0Aq0)(q0Zq2) | (q0Aq1)(q1Zq2) |


(q0Aq2)(q2Zq2) | (q0Aq3)(q3Zq2)

(q3Zq3) → (q0Aq0)(q0Zq3) | (q0Aq1)(q1Zq3) |


(q0Aq2)(q2Zq3) | (q0Aq3)(q3Zq3)
Start Symbol of the grammar is (q0Zq2)

January 1, 2024 CSE, BMSCE 82


January 1, 2024 CSE, BMSCE 83
January 1, 2024 CSE, BMSCE 84
January 1, 2024 CSE, BMSCE 85
Greibach Normal Form (GNF)
In Greibach Normal Form (GNF), there is a restriction on the terminals and
variables appearing on the right hand side of the production.

A CFG G = (V, T, P, S) is said to be in GNF if every production is of the form


A → a⍺
where a ∈ T and ⍺ ∈ V ∗ , i.e., ⍺ is a string of zero or more variables.
The first symbol on the right hand side of the production must be a terminal and
it can be followed by zero or more Variables.
If Grammar is in GNF, it can be easily converted into Push Down Automata
(PDA), which accepts Context Free Languages

January 1, 2024 CSE, BMSCE 86


Procedure for converting CFG to PDA
Given any grammar, first obtain the grammar in GNF (Greibach Normal Form) and then
obtain PDA.

The steps to be followed to convert a grammar G=(V,T,P,S) to its equivalent PDA are
1. Convert the grammar to GNF
2. Let q0 be the start state and z0 is initial symbol on stack. Without consuming any input,
push the start symbol S onto the stack and change state to q1
The transition for this can be δ(q0, ε, z0)=(q1,Sz0)
3. For each production of the form A -> aβ introduce the transition
δ(q1 , a, A)=(q1, β)
4. Finally, in state q1 , without consuming any input change the state to qf which is an
accepting state. The transition can be of the form δ(q1 , ε, z0)=(qf, z0)

January 1, 2024 CSE, BMSCE 87


Question
Convert following Grammar to PDA. Show the instantaneous description for the
string “aabba”
S → aABC
A → aB | a
B → bA | b
C →a

January 1, 2024 CSE, BMSCE 88


Convert following Grammar to PDA.
S → aABC ; A → aB | a ; B → bA | b ; C → a

Solution:
Step1: Convert the grammar to GNF
The given grammar is in GNF

Step2: Let q0 be the start state and z0 is initial symbol on stack. Without consuming any
input, push the start symbol S onto the stack and change state to q1
The transition for this can be δ(q0, ε, z0)=(q1,Sz0)

January 1, 2024 CSE, BMSCE 89


Convert following Grammar to PDA.
S → aABC ; A → aB | a ; B → bA | b ; C → a
Rough Slide

Solution (Contd…):
Step3: For each production of the form A -> aβ introduce the transition δ(q1 , a, A)=(q1,
β)

Production Transition
S → aABC
A → aB
A →a
B → bA
B →b
C →a

January 1, 2024 CSE, BMSCE 90


Convert following Grammar to PDA.
S → aABC ; A → aB | a ; B → bA | b ; C → a

Solution (Contd…):
Step3: For each production of the form A -> aβ introduce the transition δ(q1 , a, A)=(q1,
β)

Production Transition
S → aABC δ(q1 , a, S)=(q1, ABC)
A → aB δ(q1 , a, A)=(q1, B)
A →a δ(q1 , a, A)=(q1, 𝞮)
B → bA δ(q1 , b, B)=(q1, A)
B →b δ(q1 , b, B)=(q1, 𝞮)
C →a δ(q1 , a, C)=(q1, 𝞮)
Step4:Finally, in state q1 , without consuming any input change the state to qf which is an
accepting state. The transition can be of the form δ(q1 , ε, z0)=(qf, z0)

January 1, 2024 CSE, BMSCE 91


Convert following Grammar to PDA.
S → aABC ; A → aB | a ; B → bA | b ; C → a

Formal definition of PDA

P=(Q, ∑ , Γ , q0, Z0, δ, F)


Q = { q 0 , q1 , qf }
∑ = {a, b}
Γ ={S, A, B, C, Z0}
q0 = q0
Z0 = Z0
F = {qf}

δ(q0, ε, z0)=(q1,Sz0)
δ(q1 , a, S)=(q1, ABC)
δ(q1 , a, A)=(q1, B)
δ(q1 , a, A)=(q1, 𝞮)
δ(q1 , b, B)=(q1, A)
δ(q1 , b, B)=(q1, 𝞮)
δ(q1 , a, C)=(q1, 𝞮)
δ(q1 , ε, z0)=(qf, z0)

January 1, 2024 CSE, BMSCE 92


Convert following Grammar to PDA.
S → aABC ; A → aB | a ; B → bA | b ; C → a
Rough Slide

Instantaneous Description for the string "aabba"

Transitions of PDA
δ(q0, ε, z0)=(q1,Sz0)

δ(q1 , a, S)=(q1, ABC)

δ(q1 , a, A)=(q1, B)
δ(q1 , a, A)=(q1, 𝞮)

δ(q1 , b, B)=(q1, A)
δ(q1 , b, B)=(q1, 𝞮)

δ(q1 , a, C)=(q1, 𝞮)

δ(q1 , ε, z0)=(qf, z0)

January 1, 2024 CSE, BMSCE 93


Convert following Grammar to PDA.
S → aABC ; A → aB | a ; B → bA | b ; C → a

Instantaneous Description for the string "aabba"

(q0, aabba, z0) ˫ (q , aabba, Sz )


1 0 Transitions of PDA

˫ (q , abba, ABCz )
1 0
δ(q0, ε, z0)=(q1,Sz0)

δ(q1 , a, S)=(q1, ABC)


(q1, bba, BBCz0) (q1, bba, BCz0)
δ(q1 , a, A)=(q1, B)
δ(q1 , a, A)=(q1, 𝞮)
(q1, ba, BCz0) (q1, ba, ABCz0)
δ(q1 , b, B)=(q1, A)
δ(q1 , b, B)=(q1, 𝞮)
(q1, a, Cz0) (q1, a, ACz0)
δ(q1 , a, C)=(q1, 𝞮)
(q1, 𝞮, z0)
δ(q1 , ε, z0)=(qf, z0)
(qf, 𝞮, z0) Accepted
All Input Symbols are read and the final state is reached,
therefore the string “aabba” is accepted
January 1, 2024 CSE, BMSCE 94
Question
Convert following Grammar to PDA.
S → aABB | aAA
A → aBB | a
B → bBB | A
C →a

January 1, 2024 CSE, BMSCE 95


January 1, 2024 CSE, BMSCE 96
January 1, 2024 CSE, BMSCE 97
Pumping Lemma for CFL

January 1, 2024 CSE, BMSCE 98


Pumping Lemma for Context Free Language (CFL)

Let L be context free language and is infinite. Let z is sufficiently


long string and z ∈ L ,
so that |z| >=n where n is some positive integer. If the string z can
be decomposed into combination of strings
z=uvwxy
Such that |vwx| <= n , |vx| >=1 ,
then uviwxiy ∈ L for i=0,1,2,3,……

January 1, 2024 CSE, BMSCE 99


Using Pumping Lemma to show certain languages are not
CFL

The general strategy used to prove that a given language is not CFL
is shown below:
1. Assume that the language L is infinite and it is context free
2. Select string say z and break it into sub strings u, v, w, x and y
such that |z|>=n,
z=uvwxy where |vwx| <= n , |vx| >=1
3. Find any i such that uviwxiy ∉ L . According to pumping lemma
uviwxiy ∈ L . So, the result is a contradiction to the assumption
that the language is context free. Therefore, the given language L
is not context free.

January 1, 2024 CSE, BMSCE 100


Question
Show that the language L={anbncn, n≥1} is not Context Free Language (CFL).

January 1, 2024 CSE, BMSCE 101


Question
Show that the language L={anbncn, n≥1} is not Context Free Language (CFL).
Solution:
Step1: Let L be CFL and let z= anbncn ∈ L
Step2: Note that |z|=| anbncn |=3n >= n
Split z into uvwxy such that |vwx| ≤ n and |vx| ≥ 1

January 1, 2024 CSE, BMSCE 102


Question
Show that the language L={anbncn, n≥0} is not Context Free Language (CFL).
Solution:
Step1: Let L be CFL and let z= anbncn ∈ L
Step2: Note that |z|=| anbncn |=3n >= n
Split z into uvwxy such that |vwx| ≤ n and |vx| ≥ 1

|vx| =|ajal|= (j+ l) ≥ 1

|vwx| =|ajakal|=(j+k+ l) ≤ n
January 1, 2024 CSE, BMSCE 103
Question
Show that the language L={anbncn, n≥0} is not Context Free Language (CFL).
Solution (Contd…):
Step3: For all i, uviwxiy ∈ L.

Hence L={anbncn, n≥0} is not CFL

January 1, 2024 CSE, BMSCE 104


Question
Show that the language L={WW , W ∈ (a+b)* }is not Context Free Language (CFL).

January 1, 2024 CSE, BMSCE 105


Show that the language L={WW , W ∈ (a+b)*} is not Context Free Language (CFL).
Rough Slide

Solution:
Step1: Let L be CFL and Let z= anbnanbn ∈ L
Step2: Note that |z|=| anbnanbn |=4n >= n
Split z into uvwxy such that |vwx| ≤ n and |vx| ≥ 1

January 1, 2024 CSE, BMSCE 106


January 1, 2024 CSE, BMSCE 107
January 1, 2024 CSE, BMSCE 108
Question
Show that the language L={W , W ∈ (0+1)* , |w| is a perfect square }is not Context Free
Language (CFL).

January 1, 2024 CSE, BMSCE 109


Show that the language L={W , W ∈ (0+1)* , |w| is a perfect square }is not Context Free Language
(CFL).
Rough Slide

Solution:
Step1: Let L be CFL and Let z= 0n2 ∈ L
Step2: Note that |z|=| 0n2|=n2 ≥ n
Split z into uvwxy such that |vwx| ≤ n and |vx| ≥ 1
Step 3: For all i, uviwxiy ∈ L.

January 1, 2024 CSE, BMSCE 110


Show that the language L={W , W ∈ (0+1)* , |w| is a perfect square }is not Context Free Language
(CFL).
Rough Slide

Solution:
Step1: Let L be CFL and Let z= 0n2 ∈ L
Step2: Note that |z|=| 0n2|=n2 ≥ n
Split z into uvwxy such that |vwx| ≤ n and |vx| ≥ 1
Step 3: For all i, uviwxiy ∈ L.
Let |vx| = m , m ≤ n.
But for i=2,
n2 < |uv2wx2y| = n2+m ≤ (n2 + n) < (n+1)2

And hence the length is nit a perfect square and therefore string is not in L. This
contradiction allows us to conclude that our assumption was wrong and L is not CFL.

January 1, 2024 CSE, BMSCE 111


Question
Show that the language L={an!, n≥0} is not Context Free Language (CFL).
Solution:
Step1: Let L be CFL and it is infinite, Let z= an! ∈ L
Step2: Note that |z|=| an!|=n! > n
Split z into uvwxy such that |vwx| ≤ n and |vx| ≥ 1

an! = an!-(j+k+l )ajakal


v=aj , w=ak , x=al

|vx| ≥ 1, |aj+al| =(j+ l )≥ 1 |vwx| ≤ n, |aj+ak+al| =(j+k+ l ) ≤ n

January 1, 2024 CSE, BMSCE 112


Question
Show that the language L={an!, n≥0 is not Context Free Language (CFL).
Solution (Contd..):
Step3: For all i, uviwxiy ∈ L.
But for i=2,

an!-(j+k+l)a2jaka2l = an!+j+l
|an!+j+l|= n!+j+l ≤ n!+n
< n!+n!(n)
< n!(n+1)

n! < (n!+j+l) < (n+1)!

Since, (n!+j+l) lies in between n! and (n+1)! ,


the string generated by uv2wx2y = an!+j+l ∉ L
which is contradiction, hence L is not CFL
January 1, 2024 CSE, BMSCE 113
Question

Pumping lemma for context free language breaks


the strings into
a) Two parts
b) Three parts
c) Five parts
d) Six parts

January 1, 2024 CSE, BMSCE 114


Answer

Pumping lemma for context free language breaks


the strings into
a) Two parts
b) Three parts
c) Five parts
d) Six parts

January 1, 2024 CSE, BMSCE 115


Question
In pumping lemma for context free language
a) We start by assuming the given language is context free and
then we get contradict
b) We first convert the given language into regular language and
then apply steps on
c) Both (a) and (b)
d) None of these

January 1, 2024 CSE, BMSCE 116


Answer
In pumping lemma for context free language
a) We start by assuming the given language is context free
and then we get contradict
b) We first convert the given language into regular language and
then apply steps on
c) Both (a) and (b)
d) None of these

January 1, 2024 CSE, BMSCE 117


Closure properties of CFL

January 1, 2024 CSE, BMSCE 118


Closure properties of CFL
 Closure properties consider operations on CFL that are
guaranteed to produce a CFL
 The CFL’s are closed under union, concatenation, closure (star),
reversal, homomorphism, inverse homomorphism and
substitution.
 CFL’s are not closed under intersection and complementation

119
CFLs are closed under Union Operation
 Let L1 and L2 be two CFL’s generated by CFGs G1=(V1, T1, S1, P1) and G2
=(V2, T2, S2, P2)

120
CFLs are closed under Union Operation
 Let L1 and L2 be two CFL’s generated by CFGs G1=(V1, T1, S1, P1) and G2
=(V2, T2, S2, P2)
 To get grammar for L1  L2 ?
 Add new start symbol S and rules S  S1 | S2
 We get grammar G = (V, T, P, S) where
V = V1  V2  { S }, where S  V1  V2
P = P 1  P2  { S  S1 | S 2 }

121
CFLs are closed under Union Operation
 Let L1 and L2 be two CFL’s generated by CFGs G1=(V1, T1, S1, P1) and G2
=(V2, T2, S2, P2)
 To get grammar for L1  L2 ?
 Add new start symbol S and rules S  S1 | S2
 We get grammar G = (V, T, P, S) where
V = V1  V2  { S }, where S  V1  V2
P = P 1  P2  { S  S1 | S 2 }
 Example:
 L 1 = { a n b n | n  0 } , L 2 = { b n an | n  0 }
 G1 : S1  aS1b |  G2 : S2  bS2a | 
 L1  L2 is G = ({S1, S2 , S}, {a, b}, P, S) where
P = {P1  P2  {S  S1 | S2 }}
 Union of two CFLs results in CFL.

122
CFLs are closed under Concatenation Operation
 Let L1 and L2 be two CFL’s generated by CFGs G1=(V1, T1, S1, P1) and G2
=(V2, T2, S2, P2)
 To get grammar for L1L2 ?
 Add new start symbol S and rule S  S1S2
 We get G = (V, T, P, S) where
V = V1  V2  { S }, where S  V1  V2
P = P 1  P 2  { S  S 1 S2 }
 Example:
 L1 = { anbn | n  0 } with G1: S1  aS1b | 
 L2 = { bmam | m  0 } with G2 : S2  bS2a | 
 L1L2 = { anb{n+m}am | n, m  0 } with G = ({S, S1, S2}, {a, b},
{S  S1S2, S1  aS1b | , S2  bS2a}, S)
 Concatenation of Two CFLs results in a CFL

123
CFLs are closed under Kleene’s-Closure or Star-
Closure Operation

 Let L1 be the CFL generated by CFG G1=(V1, T1, S1, P1)


 Kleene Star Closure L(G)=(L(G1))*
 Example:
 L1 = {anbn | n  0}
(L1)*= { a{n1}b{n1} ... a{nk}b{nk} | k  0 and ni  0 for all i }
 To get the grammar for (L1)*
 Add new start symbol S and rules S  SS1 | .
 We get G = (V, T, P, S) where
V = V1  { S }, where S  V1
P = P1  { S  SS1 | }

124
Context-free languages are not closed under Intersection

 Consider two CFL’s


L1={anbncm, n  0, m  0} and
L2={anbmcm, n  0, m  0}
 But L1 ∩ L2 ={anbncn, n  0}, which is not CFL
 Thus family of CFL’s is not closed under Intersection operation.

January 1, 2024 CSE, BMSCE 125


Intersection
 The CFL’s are not closed under intersection
 Example:
 L = {0n1n2n|n  1} is not context-free.
 L1 = {0n1n2i |n  1,i 1 }, L2 = {0i1n2n |n  1,i 1 } are CFL’s with
corresponding grammars for L1: S->AB; A->0A1 | 01; B->2B | 2 ,
and for L2: S ->AB; A->0A | 0; B->1B2 | 12.
 However, L = L1  L2
 Thus intersection of CFL’s is not CFL

126
Context-free languages are not closed under Complement

 If the family of CFL’s were closed under Complementation then the right side
of the above expression would be a CFL for any L1 and L2. But this contradicts
what we have just shown in previous slide that the intersection of two CFLs is
not necessarily Context free.
 Consequently, the family of CFL is not closed under Complementation

January 1, 2024 CSE, BMSCE 127


Set Difference

 L1 and L2 are CFLs. L1 - L2 is not necessarily a CFL


Proof:
 L1 = * - L
 * is regular and is also CFL
 But * - L = LC
 If CFLs were closed under set difference, then * - L = LC would
always be a CFL.
 But CFL’s are not closed under complementation

128
Reversal
 The CFL’s are closed under reversal
 This means then if L is a CFL, so LR is a CFL
 It is enough to reverse each production of a CFL for L, i.e.,
substitute A by AR
 Example:
 L = { anbn | n  0 } with P : S  aSb | 
 LR = {bnan | n  0 } with PR : S  bSa | 

129
Homomorphism
 Closure under homomorphism of CFL L for every a
 Suppose L is a CFL over alphabet  and h is a homomorphism
on .
 Let s be a substitution that replaces every a  , by h(a). i.e., s(a)
= {h(a)}.
 Then h(L) = s(L).
 h(L) ={h(a1)…h(ak) | k  0} where h(ak) is a homomorphism for
every ak  .

130
Inverse homomorphism
 To recall: If h is a homomorphism, and L is any language, then h -
1
(L), called an inverse homomorphism, is the set of all strings w
such that h(w)L
 The CFL’s are closed under inverse homomorphism.
 Theorem: If L is a CFL and h is a homomorphism, then h-1(L) is a
CFL

131
Inverse homomorphism – proof

Buffer
a h(a)
Input h

Accept/
PDA
Reject

Stack

132
Proof Contd...
 After input a is read, h(a) is placed in a buffer.
 Symbols of h(a) are used one at a time and fed to PDA being
simulated.
 Only when the buffer is empty does the PDA read another of its
input symbol and apply homomorphism to it.

133
Proof Contd...
 Suppose h applies to symbols of alphabet Σ and produces
strings in T*.
 Let PDA P = (Q, T, Γ, δ, q0, Z0, F) that accept CFL L by
final state.
 Construct a new PDA P = (Q, Σ, Γ, δ, (q0, ), Z0, F X {})
to simulate language of h-1(L), where
 Q is the set of pairs (q, x) such that
 q is a state in Q
 x is a suffix of some string h(a) for some input string a in Σ

134
Proof Contd...
 δ is defined by
 δ((q, ), a, X) = {((q, h(a)),a,X)}
 If δ(q, b, X) = {(p, )} where bT or b =  then δ((q, bx), , X) = {((p, x), )}
 The start state of P’ is (q0, )
 The accepting state of P is (q, ), where q is an accepting state of P.
 (q0,h(w),Z0)|-*P (p,,) iff ((q0,),w,Z0) |-*P ((p, ), , )
 P accepts h(w) if and only if P accepts w, because of the way the accepting states of
P are defined.
 Thus L(P)=h-1(L(P))

135
Intersection with RL

 Theorem: If L is CFL and R is a regular language, then L 


R is a CFL.

FA Accept/

AND Reject

PDA

Stack
136
Intersection with RL Proof
 P=(QP, , , P, qP, Z0, FP) be PDA to accept CFL by final
state
 A=(QA, , A, qA, FA) be a DFA for RL
 Construct PDA P = (Q, , , , qo, Z0, F) where
 Q = Qp X QA
 qo= (qp, qA)
 F = (FP X FA)
  is in the form ((q, p), a, X) = ((r, s), ) such that
1. s = A(p, a)
2. (r, ) is in P(q, a, X)

137
Proof Contd…
 For each move of PDA P, we make the same move in PDA P and
also we carry along the state of DFA A in a second component of
P.
 P accepts a string w iff both P and A accept w.
 w is in L  R.
 The moves ((qp, qA), w, Z) |-*P ((q, p), , ) are possible iff (qp,
w, Z) |-*P (q, , ) moves and p = *(qA, w) transitions are
possible.

138
Set Difference with RL
 For a CFL’s L, and a regular language R.
L - R is a CFL.
Proof:
 R is regular and RC is also regular
 L - R = L  RC
 Complement of Regular Language (RC ) is regular
 Intersection of a CFL and a regular language is CFL
 CFL and Regular language are closed under intersection.

139
Complementation
 LC is not necessarily a CFL
 Proof:
 Assume that CFLs were closed under complement.
 If L is a CFL then LC is a CFL
 Since CFLs are closed under union, L1C  L2C is a CFL
 And by our assumption (L1C  L2C) C is a CFL
 But (L1C  L2C) C = L1  L2 which we just showed isn’t
necessarily a CFL.
 Contradiction!

140
Substitution

 Each symbol in the strings of one language is replaced by


an entire CFL language
 Useful in proving some other closure properties of CFL
 Example: S(0) = {anbn| n 1}, S(1) = {aa, bb} is a
substitution on alphabet  ={0, 1}.

141
Substitution
 Theorem: If a substitution s assigns a CFL to every symbol in the alphabet
of a CFL L, then s(L) is a CFL.
 Proof
 Let G = (V, , P, S) be grammar for L
 Let Ga= (Va, Ta, Pa, Sa) be the grammar for each a   with VVa
=
 G= (V, T, P, S) for s(L) where
 V = V  Va
 T = union of Ta for all a  
 P consists of
 All productions in any Pa for a  
 In productions of P, each terminal a is replaced by S a
 A detailed proof that this construction works is in the reader.
 Intuition: this replacement allows any string in La to take the place
of any occurrence of a in any string of L.

142
Example (1)

 L = {0n1n| n 1}, generated by the grammar


S0S1|01,
 s(0) = {anbm|m n}, generated by the grammar SaSb|A;
AaA| ab,
 s(1)={ab, abc}, generated by the grammar S  abA, A  c
|
 Rename second and third S’s to S0 and S1 respectively.
Rename second A to B. Resulting grammars are:
S0S1 | 01
S0aS0b | A; AaA | ab
S1abB; Bc | 

143
Example(1) Contd...
 In the first grammar replace 0 by S0 and 1 by S1. The combined
grammar:

G = ({S, S0, S1, A, B}, {a, b}, P, S),

where P = {S  S0SS1 | S0S1, S0 aS0b | A, A aA | ab,


S1abB, B c | }

144
Application of Substitution
 Closure under union of CFL’s L1 and L2
 Closure under concatenation of CFL’s L1 and L2
 Closure under Kleene’s star (closure * and positive
closure +) of CFL’s L1
 Closure under homomorphism of CFL Li for every
ai 

145
Thanks for Listening

January 1, 2024 CSE, BMSCE 146


Closure properties of CFG’s

January 1, 2024 CSE, BMSCE 147


Closure properties of CFG’s

January 1, 2024 CSE, BMSCE 148


Question

The context free languages are closed under:


a) Intersection
b) Complement
c) Kleene’s-Closure
d) None of the mentioned

January 1, 2024 CSE, BMSCE 149


Question

The context free languages are closed under:


a) Intersection
b) Complement
c) Kleene’s-Closure
d) None of the mentioned

January 1, 2024 CSE, BMSCE 150


Question

Context free languages are not closed under:


a) Intersection
b) Complement
c) All of the mentioned

January 1, 2024 CSE, BMSCE 151


Question

Context free languages are not closed under:


a) Intersection
b) Complement
c) All of the mentioned

January 1, 2024 CSE, BMSCE 152


Applications of Context Free Grammars (CFG)

YACC (Yet Another Compiler-Compiler)


Mark up language (HTML)
XML (Extensible Markup Language)

January 1, 2024 CSE, BMSCE 153

You might also like