Languages and Finite Automata
Download
Report
Transcript Languages and Finite Automata
Pumping Lemma
for
Context-free Languages
Prof. Busch - LSU
1
Take an infinite context-free language
Generates an infinite number
of different strings
Example:
S ABE | bBd
A Aa | a
B bSD | cc
D Dd | d
E eE | e
Prof. Busch - LSU
2
In a derivation of a “long” enough
string, variables are repeated
A possible derivation:
S ABE AaBE aaBE
aabSDE aabbBdDE
aaabbccdDE aabbccddE
aabbccddeE aabbccddee
Prof. Busch - LSU
3
Derivation Tree
S
B
A
A
aabbccddee
a
a
b
E
S
b
D
B
c
e
d
d
E
e
c
Repeated
variable
Prof. Busch - LSU
4
Derivation Tree
S
B
A
A
aabbccddee
a
a
b
E
S
b
D
B
c
e
d
d
E
e
c
Repeated
variable
Prof. Busch - LSU
5
B bSD bbBdD bbBdd
B
b
S
D
b
B
d
d
*
B bbBdd
Prof. Busch - LSU
6
S ABE AaBE aaBE aaBeE aaBee
S
B
A
A
E
e
a
E
e
a
*
S aaBee
Prof. Busch - LSU
7
B
c
c
B cc
Prof. Busch - LSU
8
Putting all together
S
B
A
A
a
a
b
S
b
S aaBee
e
D
B
c
*
E
d
d
E
e
c
*
B bbBdd
Prof. Busch - LSU
B cc
9
We can remove the middle part
S
B
A
A
a
c
E
e
c
E
e
a
*
0
0
S aa (bb ) cc (dd ) ee
Prof. Busch - LSU
10
*
*
S aaBee
*
B bbBdd
*
S aaBee aaccee
0
B cc
0
0
aa (bb ) cc (dd ) ee
0
aa (bb ) cc (dd ) ee L(G )
Prof. Busch - LSU
11
We can repeated middle part two times
S
B
A
A
a
b
1
a
2
S
e
D
b
B
d
b
S
D
b
B
d
c
*
E
E
e
d
d
c
2
2
S aa (bb ) cc (dd ) ee
Prof. Busch - LSU
12
*
*
S aaBee
*
B cc
B bbBdd
*
S aaBee aabbBddee
*
2
2
*
2
2
aa (bb ) B(dd ) ee aa (bb ) cc (dd ) ee
2
2
aa (bb ) cc (dd ) ee L(G )
Prof. Busch - LSU
13
We can repeat middle part three times
S
B
A
a
A
1
a
2
3
b
S
D
b
B
d
b
S
D
b
B
d
b
S
D
b
B
d
c
*
E
e
d
E
e
d
d
c
3
3
S aa (bb ) cc (dd ) ee
Prof. Busch - LSU
14
*
S aaBee
*
*
B bbBdd
3
B cc
3
S aa (bb ) cc (dd ) ee L(G )
Prof. Busch - LSU
15
Repeat middle part i times
S
B
A
a
A
1
a
b
S
b
B
E
e
D
d
E
e
d
B
i
b
S
D
b
B
d
c
*
d
c
i
i
S aa (bb ) cc (dd ) ee
Prof. Busch - LSU
16
*
*
S aaBee
*
B bbBdd
i
i
For any
i0
B cc
S aa (bb ) cc (dd ) ee L(G )
Prof. Busch - LSU
17
From Grammar
and given string
S ABE | bBd
A Aa | a
B bSD | cc
D Dd | d
E eE | e
aabbccddee L(G)
We inferred that a family of strings is in L(G )
*
i
i
S aa (bb ) cc (dd ) ee L(G )
Prof. Busch - LSU
for any
i0
18
Arbitrary Grammars
Consider now an arbitrary infinite
context-free language L
Let
Take
G be the grammar of L {}
G so that it has no unit-productions
and no
-productions
(remove them)
Prof. Busch - LSU
19
r
Let t
Let
be the number of variables
be the maximum right-hand size
of any production
Example: S ABE | bBd
A Aa | a
B bSD | cc
D Dd | d
E eE | e
Prof. Busch - LSU
r 5
t 3
20
Claim:
Take string w L(G ) with | w | t .
Then in the derivation tree of w
there is a path from the root to a leaf
where a variable of G is repeated
r
Proof:
Proof by contradiction
Prof. Busch - LSU
21
Derivation tree of
S
We will show:
some variable
is repeated
w
| w | m
H
H
Prof. Busch - LSU
22
First we show that the tree of w
has at least r 2 levels of nodes
Suppose the opposite:
At most
r 1
Levels
Prof. Busch - LSU
23
Maximum number of nodes per level
t
Level 0:
1 nodes
Level 1:
t
nodes
nodes
The maximum right-hand side of any production
Prof. Busch - LSU
24
Maximum number of nodes per level
t
nodes
t
Level 0:
1 nodes
Level 1:
t
Level 2:
2nodes
t
nodes
nodes
2 nodes
t
Prof. Busch - LSU
25
Maximum number of nodes per level
Level 0: 1 nodes
At most
r 1
Level
Levels
i : t i nodes
Level
r: t
r
nodes
Maximum possible string length
r
= max nodes at level r = t
Prof. Busch - LSU
26
Therefore,
maximum length of string
w : | w | t
r
| w | t
r
However we took,
Contradiction!!!
Therefore,
the tree must have at least
Prof. Busch - LSU
r2
levels
27
Thus, there is a path from the root
to a leaf with at least r 2 nodes
V1
At least
r2
Levels
V1 S (root)
V2
V3
r 1 Variables
Vr 1
symbol
(leaf)
Prof. Busch - LSU
28
r
Since there are at most
some variable is repeated
V1
V2
V3
Pigeonhole
principle
different variables,
S
H
H
Vr 1
Prof. Busch - LSU
END OF CLAIM PROOF
29
Take now a string
w
with
| w | t
r
S
From claim:
some variable H
is repeated
H
H
subtree of H
Take H to be the deepest, so that
only H is repeated in subtree
Prof. Busch - LSU
30
We can write
S
w uvxyz
yield
u
yield
v
H
H
u, v, x, y, z :
Strings of terminals
Prof. Busch - LSU
x
z
yield
y
yield
yield
31
Example:
S
A
B
A
u aa
v bb
x cc
y dd
z ee
a
u
b
a
v
E
S
b
B
c
e
D
d
d
y
E
e
z
c
x
B correspond s to H
Prof. Busch - LSU
32
Possible derivations
S
S uHz
u
H
z
H vHy
v
H
y
Hx
x
Prof. Busch - LSU
33
Example:
S
B
A
A
a
a
b
S uHz
S aaBee
E
S
b
e
D
B
c
u aa
v bb
x cc
d
d
c
H vHy
B bbBdd
Prof. Busch - LSU
y dd
z ee
E
e
B correspond s to H
Hx
B cc
34
Remove Middle Part
S
S uHz
u
z
H
Hx
x
Yield:
*
S uHz uxz
uxz uv xy z
0
uv 0xy 0z
Prof. Busch - LSU
0
L(G )
35
Repeat Middle part two times
S
S uHz
u
H
z
1
H vHy
v
H
y
H vHy
v
Hx
Yield:
H
x
2
y
uvvxyyz uv xy z
Prof. Busch - LSU
2
2
36
S uHz
H vHy
*
Hx
*
S uHz uvHyz uvvHyyz
*
uvvxyyz
2
2
uv xy z L(G )
Prof. Busch - LSU
37
Repeat Middle part
i
times
S
S uHz
u
H
z
H vHy
v
1
y
i
H
H vHy
H
y
v
Hx
H
x
Yield:
Prof. Busch - LSU
uv xy z
i
i
38
S uHz
H vHy
Hx
S uHz uvHyz uvvHyyz
uv Hy z uv xy z L(G)
i
i
i
i
Prof. Busch - LSU
39
Therefore,
If we know that:
| w | t
r
w uvxyz L(G)
then we also know:
uv xy z L(G )
i
i
For all
since
L(G) L {}
i
i
uv xy z L
Prof. Busch - LSU
40
Observation 1:
S
| vy | 1
Since G has no
unit and
-productions
u
v
z
H
y
H
x
At least one of
v or
y
Prof. Busch - LSU
is not
41
Observation 2:
|vxy | t
S
r 1
u
since in subtree
only variable H
is repeated
v
H
H
z
y
x
Explanation follows….
subtree of H
Prof. Busch - LSU
42
subtree of H
vxy s1s2 sk
H
T1
s1
Tk
sk
T2
s2
Various yields
| s j | t
r
since no variable is repeated in
k
|vxy | | s j | k t t t t
r
r
Tj
r 1
j 1
Maximum right-hand side of any production
Prof. Busch - LSU
43
Thus, if we choose critical length
m t
r 1
t
r
then, we obtain the pumping lemma for
context-free languages
Prof. Busch - LSU
44
The Pumping Lemma:
For any infinite context-free language
there exists an integer
for any string
L
m such that
w L,
| w | m
we can write
w uvxyz
with lengths
| vxy | m and | vy | 1
and it must be that:
i i
uv xy z L,
for all i 0
Prof. Busch - LSU
45
Applications
of
The Pumping Lemma
Prof. Busch - LSU
46
Non-context free languages
{a b c : n 0}
n n n
Context-free languages
{a b : n 0}
n n
Prof. Busch - LSU
47
Theorem: The language
n n n
L {a b c : n 0}
is not context free
Proof:
Use the Pumping Lemma
for context-free languages
Prof. Busch - LSU
48
L {a b c : n 0}
n n n
Assume for contradiction that
L
is context-free
Since L is context-free and infinite
we can apply the pumping lemma
Prof. Busch - LSU
49
L {a b c : n 0}
n n n
Let m be the critical length
of the pumping lemma
Pick any string
We pick:
w L with length | w | m
wa b c
m m m
Prof. Busch - LSU
50
L {a b c : n 0}
n n n
wa b c
m m m
From pumping lemma:
we can write:
w uvxyz
with lengths
| vxy | m and | vy | 1
Prof. Busch - LSU
51
L {a b c : n 0}
n n n
wa b c
w uvxyz
m m m
| vxy | m
| vy | 1
Pumping Lemma says:
uv xy z L
i
i
for all
Prof. Busch - LSU
i0
52
L {a b c : n 0}
n n n
wa b c
w uvxyz
m m m
| vxy | m
| vy | 1
We examine all the possible locations
of string vxy in w
Prof. Busch - LSU
53
L {a b c : n 0}
n n n
wa b c
w uvxyz
m m m
Case 1:
m
| vxy | m
vxy is in a
| vy | 1
m
m
m
a ...aa...aa...a bbb ...bbb ccc...ccc
u vxy
z
Prof. Busch - LSU
54
L {a b c : n 0}
n n n
wa b c
w uvxyz
m m m
v a
| vxy | m
y a
k1
k2
m
| vy | 1
k1 k2 1
m
m
a ...aa...aa...aa...aa...a bbb ...bbb ccc...ccc
u v
x
y
z
Prof. Busch - LSU
55
L {a b c : n 0}
n n n
wa b c
w uvxyz
m m m
v a
k1
| vxy | m
y a
k2
| vy | 1
k1 k2 1
m
m
m k1 k2
a ...aa...aa...aa...aa...a bbb ...bbb ccc...ccc
u v2 x y2
z
Prof. Busch - LSU
56
L {a b c : n 0}
n n n
wa b c
w uvxyz
m m m
| vxy | m
From Pumping Lemma:
| vy | 1
uv xy z L
2
2
k1 k2 1
However:
uv xy z a
2
2
m k1 k2
b c L
m m
Contradiction!!!
Prof. Busch - LSU
57
L {a b c : n 0}
n n n
wa b c
w uvxyz
m m m
Case 2:
| vxy | m
| vy | 1
vxy is in b m
Similar to case 1
m
m
m
aaa...aaa b ...bb ...bb ...b ccc...ccc
u
vxy
Prof. Busch - LSU
z
58
L {a b c : n 0}
n n n
wa b c
w uvxyz
m m m
Case 3:
| vxy | m
vxy is in c
| vy | 1
m
Similar to case 1
m
m
m
aaa...aaa bbb ...bbb c ...cc...cc...c
u
Prof. Busch - LSU
vxy z
59
L {a b c : n 0}
n n n
wa b c
w uvxyz
m m m
Case 4:
| vxy | m
vxy overlaps a
m
| vy | 1
and
b
m
m
m
m
a ...aa...aa b ...bb ...bbb ccc...ccc
u
z
vxy
Prof. Busch - LSU
60
L {a b c : n 0}
n n n
wa b c
w uvxyz
m m m
Sub-case 1:
| vxy | m
| vy | 1
v contains only a
y contains only b
m
m
m
a ...aa...aa...a b ...bb ...bb ...b ccc...ccc
u v
x
y
Prof. Busch - LSU
z
61
L {a b c : n 0}
n n n
wa b c
w uvxyz
m m m
v a
k1
| vxy | m
yb
| vy | 1
k1 k2 1
k2
m
m
m
a ...aa...aa...a b ...bb ...bb ...b ccc...ccc
u v
x
y
Prof. Busch - LSU
z
62
L {a b c : n 0}
n n n
wa b c
w uvxyz
m m m
v a
k1
| vxy | m
yb
| vy | 1
k1 k2 1
k2
m k2
m
m k1
a ...aa...aa...a b ...bb ...bb ...b ccc...ccc
u v2
x
y
Prof. Busch - LSU
2
z
63
L {a b c : n 0}
n n n
wa b c
w uvxyz
m m m
| vxy | m
From Pumping Lemma:
| vy | 1
uv xy z L
2
2
k1 k2 1
However:
uv xy z a
2
2
m k1 m k2 m
b
c L
Contradiction!!!
Prof. Busch - LSU
64
L {a b c : n 0}
n n n
wa b c
w uvxyz
m m m
Sub-case 2:
| vxy | m
| vy | 1
v contains a and b
y contains only b
m
m
m
a ...aa...a b ...bb ...bb ...bb ...b ccc...ccc
u
v
x
y
Prof. Busch - LSU
z
65
L {a b c : n 0}
n n n
wa b c
w uvxyz
m m m
| vxy | m
| vy | 1
By assumption
v a b
k1
m
k2
k1
yb
k1 , k2 1
k3
m
k2
m
k3
a ...aa...a b ...bb ...bb ...bb ...b ccc...ccc
u
v
x
y
Prof. Busch - LSU
z
66
L {a b c : n 0}
n n n
wa b c
w uvxyz
m m m
v a b
k1
k2
| vxy | m
yb
k1 , k2 1
k3
m k3
m
| vy | 1
m
2k3
k1 k2 k1 k2
a ...aa...ab...ba ...ab...bb ...bb ...bb ...b ccc...ccc
u
v
2
x
Prof. Busch - LSU
y
2
z
67
L {a b c : n 0}
n n n
wa b c
w uvxyz
m m m
| vxy | m
From Pumping Lemma:
| vy | 1
uv xy z L
2
2
k1 , k2 1
However:
uv xy z a b a b
2
2
m
k2
k1
c L
m k3
m
Contradiction!!!
Prof. Busch - LSU
68
L {a b c : n 0}
n n n
wa b c
w uvxyz
m m m
Sub-case 3:
| vxy | m
| vy | 1
v contains only a
y contains a and b
Similar to sub-case 2
m
m
m
a ...aa...aa...aa...a b ...bb ...b ccc...ccc
u
v
x
y
Prof. Busch - LSU
z
69
L {a b c : n 0}
n n n
wa b c
w uvxyz
m m m
Case 5:
| vxy | m
vxy overlaps b
m
| vy | 1
and
c
m
Similar to case 4
m
m
m
aaa...aaa bbb...bbb ccc...ccc
z
u
vxy
Prof. Busch - LSU
70
L {a b c : n 0}
n n n
wa b c
w uvxyz
m m m
Case 6:
| vxy | m
vxy overlaps a , b
m
| vy | 1
m and
c
m
Impossible!
m
m
m
aaa...aaa bbb...bbb ccc...ccc
z
u
vxy
Prof. Busch - LSU
71
In all cases we obtained a contradiction
Therefore: the original assumption that
L {a b c : n 0}
n n n
is context-free must be wrong
Conclusion:
L is not context-free
Prof. Busch - LSU
72