Transcript Examples

Pumping Lemma
Problem:
– Let B be the language of all palindromes over {0,1} containing an
equal number of 0s and 1s. Show that B is not CFL
Solution:
– We assume that B is a CFL and obtain a contradiction.
– Let p be the pumping constant of B that is guaranteed to exist by
the pumping lemma. Select the string s = 0p1p1p0p .
– Clearly s is a member of B and of length at least p . We can
show that no matter how we divide s into uvxyz, one of the
conditions of the lemma is violated.
• Pumping lemma, | vxy |<= p , we can only place vxy in the following
ways:
1. vxy completely falls in the first 0p
–
If we pump v and y , then the new string is no longer a palindrome, and the
number of 0s will be greater than the number of 1s, which is a contradiction.
2. vxy falls between 0p and 1p
–
In this case, v will only contain 0s while y will only contain 1s. So if we pump s ,
the new string is no longer palindrome, which is a contradiction.
3. vxy completely falls in the first 1p
–
Similar to (1), after pumping s , the number of 1s will be greater than the number
of 0s, which is a contradiction.
4. vxy falls between 1p and 1p
–
After pumping s , the number of 1s will be greater than the number of 0s, which
is a contradiction.
5. vxy completely falls in the second 1p
–
This case is same with (3).
6. vxy falls between 1p and 0p
–
Similar to (2), v will only contain 1s while y will only contain 0s. So if we pump s ,
the new string is no longer palindrome, which is a contradiction.
7. vxy completely falls in the second 0p
–
This case is same with (1).
Hence, B is not context free.
• Let L = {w  {0; 1}* : w = wR}.
• (a) Show that L is context-free by giving a context-free
grammar for L.
• (b) Show that L is context-free by giving a pushdown
automaton for L.
• (c) Show that L is not regular.
CFG
• L is a symmetric language. Consider a context-free
grammar for L : (V, Σ, R, S) , where
i. V = {S}
ii. Σ = {0,1}
iii. Rules:
S  0S0 |1S1| 0 |1|ε
iv. S = S V
PDA
Pumping Lemma
• Assume that L regular. Let p be the pumping constant
given by the pumping lemma.
• Let s be the string 1p-1101p.
• s can be split into three pieces, s = xyz , where for any i
>= 0 the string xyiz is in L .
• According to the pumping lemma condition, we must
have | xy |<= p . If this is the case, then y must consist
only of 1’s, so xyyz  L . Therefore s cannot be pumped.
• This is a contradiction.
Pushdown Automaton -- PDA
Input String
Stack
States
Initial Stack Symbol
stack
head
Stack
Stack
$
z
bottom special symbol
Appears at time 0
top
The States
Input
symbol
Pop
symbol
a,
b

c
q1
q2
Push
symbol
q1
a, b  c
q2
input

a


a
stack
b
h
e
$
top
Replace
c
h
e
$

q1
a,  c
q2
input

a


stack
b
h
e
$
top
Push
a
c
b
h
e
$

q1
a,b  
q2
input

a


a
stack
b
h
e
$
top
Pop
h
e
$

q1
a,  
q2
input

a


a
stack
b
h
e
$
top
No Change
b
h
e
$

Empty Stack
q1
a, $  
q2
input

a


stack
$
a

empty
top
Pop
The automaton HALTS
No possible transition after q2
A Possible Transition
q1
a, $  b
q2
input

a


stack
$
top
Pop
b
a

Non-Determinism
PDAs are non-deterministic
Allowed non-deterministic transitions
a, b  c
q2
q1
a, b  c
q1
q3
 ,b  c q
2
  transition
Example PDA
PDA
M
L(M )  {a b : n  0}
n
a,  a
n
b,a  

,



q0
q1 b,a   q2
 ,$  $
q3
L(M )  {a b : n  0}
n n
Basic Idea:
1. Push the a’s
on the stack
2. Match the b’s on input
with a’s on stack
a,  a
b,a  

,



q0
q1 b,a   q2
3. Match
found
 ,$  $
q3
Input
a a a b b b
$
Stack
a,  a
b,a  

,   q b,a   q
q0
2
1
 ,$  $
q3
Input
a a a b b b
$
Stack
a,  a
b,a  

,



q0
q1 b,a   q2
 ,$  $
q3
Input
a a a b b b
a
$
Stack
a,  a
b,a  

,



q0
q1 b,a   q2
 ,$  $
q3
Input
a
a
$
a a a b b b
Stack
a,  a
b,a  

,



q0
q1 b,a   q2
 ,$  $
q3
a
a
a
$
Input
a a a b b b
Stack
a,  a
b,a  

,



q0
q1 b,a   q2
 ,$  $
q3
a
a
a
$
Input
a a a b b b
Stack
a,  a
b,a  

,



q0
q1 b,a   q2
 ,$  $
q3
Input
a
a
$
a a a b b b
Stack
a,  a
b,a  

,



q0
q1 b,a   q2
 ,$  $
q3
Input
a
$
a a a b b b
Stack
a,  a
b,a  

,



q0
q1 b,a   q2
 ,$  $
q3
Input
a a a b b b
$
Stack
a,  a
b,a  
accept

,



q0
q1 b,a   q2
 ,$  $
q3
• A string is accepted if there is a
computation such that:
– All the input is consumed
– The last state is an accepting state
– At the end of the computation,
– we do not care about the stack contents
– (the stack can be empty at the last state)
L  {a b : n  0}
n n
is the language accepted by the PDA:
a,  a
q0
b,a  
 ,   q b,a   q
2
1
 ,$  $
q3
aab
a,  a
b,a  

,



q0
q1 b,a   q2
 ,$  $
q3
a a b
$
a,  a
b,a  

,   q b,a   q
q0
2
1
 ,$  $
q3
a a b
$
a,  a
b,a  

,



q1 b,a   q2
q0
 ,$  $
q3
a
a a b
$
a,  a
b,a  

,



q1 b,a   q2
q0
 ,$  $
q3
a
a
a a b
$
a,  a
b,a  

,



q1 b,a   q2
q0
 ,$  $
q3
a
a
a a b
$
a,  a
b,a  

,



q1 b,a   q2
q0
 ,$  $
q3
a
a
a a b
$
reject
a,  a
b,a  

,



q1 b,a   q2
q0
 ,$  $
q3
Another PDA example

L(M )  {vv : v {a, b} }
R
a, a  
b, b  
a,   a
b,   b
q0
 ,  
 ,$  $
q1
q2

L(M )  {vv : v {a, b} }
R
1. Push v
on stack
2. Guess
middle
of input
a, a  
b, b  
a,   a
b,   b
q0
3. Match vR on input
with v on stack
 ,  
4. Match
found
 ,$  $
q1
q2
a b b a
$
a, a  
b, b  
a,   a
b,   b
q0
 ,  
 ,$  $
q1
q2
a b b a
a, a  
b, b  
a,   a
b,   b
q0
a
$
 ,  
 ,$  $
q1
q2
b
a
$
a b b a
a, a  
b, b  
a,   a
b,   b
q0
 ,  
 ,$  $
q1
q2
a b b a
Guess the middle
of string
a, a  
b, b  
a,   a
b,   b
q0
b
a
$
 ,  
 ,$  $
q1
q2
b
a
$
a b b a
a, a  
b, b  
a,   a
b,   b
q0
 ,  
 ,$  $
q1
q2
a b b a
a, a  
b, b  
a,   a
b,   b
q0
a
$
 ,  
 ,$  $
q1
q2
a b b a
$
a, a  
b, b  
a,   a
b,   b
q0
 ,  
accept
 ,$  $
q1
q2
Rejection Example:
Input
a b b b
$
a, a  
b, b  
a,   a
b,   b
q0
 ,  
 ,$  $
q1
q2
a b b b
a, a  
b, b  
a,   a
b,   b
q0
a
$
 ,  
 ,$  $
q1
q2
b
a
$
a b b b
a, a  
b, b  
a,   a
b,   b
q0
 ,  
 ,$  $
q1
q2
a b b b
Guess the middle
of string
a, a  
b, b  
a,   a
b,   b
q0
b
a
$
 ,  
 ,$  $
q1
q2
b
a
$
a b b b
a, a  
b, b  
a,   a
b,   b
q0
 ,  
 ,$  $
q1
q2
There is no possible transition.
a b b b
a
$
a, a  
b, b  
a,   a
b,   b
q0
Input is not
consumed
 ,  
 ,$  $
q1
q2
Another computation on same string:
a b b b
$
a, a  
b, b  
a,   a
b,   b
q0
 ,  
 ,$  $
q1
q2
a b b b
a, a  
b, b  
a,   a
b,   b
q0
a
$
 ,  
 ,$  $
q1
q2
b
a
$
a b b b
a, a  
b, b  
a,   a
b,   b
q0
 ,  
 ,$  $
q1
q2
b
b
a
$
a b b b
a, a  
b, b  
a,   a
b,   b
q0
 ,  
 ,$  $
q1
q2
b
b
b
a
$
a b b b
a, a  
b, b  
a,   a
b,   b
q0
 ,  
 ,$  $
q1
q2
a b b b
final state
is not reached
a, a  
b, b  
a,   a
b,   b
q0
b
b
b
a
$
 ,  
 ,$  $
q1
q2
abbb L(M )
a, a  
b, b  
a,   a
b,   b
q0
 ,  
 ,$  $
q1
q2
Another PDA example
L(M )  {w {a, b} :
in everyprefixv, na (v)  nb (v)}
*
a,   a
b, a  
M
q0
aab
abbb (r)
Pushing Strings
Input
symbol
Pop
symbol
a,
b

w
q1
q2
Push
string
Example:
q1
a, b  cdf
q2
a
a
b
h
e
$
top
Push
c
d
f
h
e
$
pushed
string
Another PDA example
L(M )  {w {a, b} : na (w)  nb (w)}
*
a, $  0$
a, 0  00
a,1  
b, $ 1$
b, 1 11
b,0  
q1
, $  $
abbbaa
q2