applications of regular closure
DESCRIPTION
Applications of Regular Closure. The intersection of a context-free language and a regular language is a context-free language. Regular Closure. context free. regular. context-free. An Application of Regular Closure. Prove that:. is context-free. - PowerPoint PPT PresentationTRANSCRIPT
1
Applications of
Regular Closure
2
The intersection of a context-free language and a regular languageis a context-free language
1L context free
2L regular
21 LL
context-free
Regular Closure
3
An Application of Regular Closure
Prove that: }100:{ nbaL nn
is context-free
4
}{ nnbaWe know:
is context-free
5
}{ 1001001 baL is regular
}{}){( 100100*1 babaL is regular
We also know:
6
}{}){( 100100*1 babaL
regular
}{ nnbacontext-free
1}{ Lba nn is context-free
Lnba
Lbann
nn
}100:{
}{ 1
context-free
(regular closure)
7
Another Application of Regular Closure
Prove that: }:{ cba nnnwL
is not context-free
8
}:{ cba nnnwL
}{*}**{ nnn cbacbaL
context-free regular context-free
If is context-free
Then
Impossible!!!
Therefore, is not context free L
(regular closure)
9
Decidable Propertiesof
Context-Free Languages
10
Membership Question:
for context-free grammarfind if string
G)(GLw
Membership Algorithms: Parsers
• Exhaustive search parser
• CYK parsing algorithm
11
Empty Language Question:
for context-free grammar find if
G)(GL
Algorithm:
S
1. Remove useless variables
2. Check if start variable is useless
12
Infinite Language Question:
for context-free grammar find if is infinite
G)(GL
Algorithm:
1. Remove useless variables
2. Remove unit and productions
3. Create dependency graph for variables
4. If there is a loop in the dependency graph then the language is infinite
13
Example:
cBSC
bbbBB
aaCbA
ABS
|
|
S
A
B
C
Dependency graph Infinite language
14
cBSC
bbbBB
aaCbA
ABS
|
|
acbbSbbbacBSbBaCbBABS
ii bbbSacbb
bbbSacbbacbbSbbbS
)()(
)()( 22
15
The Pumping Lemmafor
Context-Free Languages
16
Take an infinite context-free language
Example:
bB
SbB
aBbA
ABS
Generates an infinite numberof different strings
17abbabbbbabbabbBb
abbaBbBbabbABbabbSb
abbBaBbBABS
bB
SbB
aBbA
ABS
A derivation:
18
S
A B
bBa
b
bS
A B
bBa
b
b
Derivation tree string abbabbbb
19
S
A B
bBa
b
bS
A B
bBa
b
b
repeated
Derivation tree string abbabbbb
20
B
bS
A B
bBa
b
b
aBbbbaBbBb
ABbSbB
bB
21
B
bS
A B
bBa b
aBbbbB
Repeated Part
22
B
bS
A B
ba bB
bS
A B
bBa b
aaBbbbbbbaBbbbB
Another possible derivation
aBbbbB
23
S
A B
bBa
b
bS
A B
ba bB
abbaBbbbS
aBbbbB
24bbabbaaBbbbbabbaBbbbS
S
A B
bBa
b
bS
A B
ba bB
bS
A B
bBa b
aBbbbB
25bbabbaabbbbbbbabbaaBbbbbS
S
A B
bBa
b
bS
A B
ba bB
bS
A B
bBa b bBb
26
bbabbaabbbbbS
Therefore, the string
bbabbaabbbbb
is also generated by the grammar
27
abbaBbbbS
aBbbbB bBWe know:
We also know this string is generated:
abbaabbbb
abbaBbbbS
28
abbaBbbbS
aBbbbB bBWe know:
Therefore, this string is also generated:
bbabbaabbbbb
bbabbaaBbbbb
abbaBbbbS
29
abbaBbbbS
aBbbbB bBWe know:
Therefore, this string is also generated:
bbbbbbbaabba
bbbbbbBaabba
bbbbbbBaabba
abbaBbbbS
22
22
)()(
)()(
)()(
30
Therefore, this string is also generated:
bbbbbbbaabba
bbbbbbBaabba
abbaBbbbS
ii
ii
)()(
)()(
abbaBbbbS
aBbbbB bBWe know:
31
Therefore, knowing that
abbabbbb
is generated by grammar , we also know that
bbbbbbbaabba ii )()(
is generated by
G
G
32
In general:
We are given an infinite context-free grammar G
Assume has no unit-productions no -productions
G
33
Take a string with length bigger than
Some variable must be repeatedin the derivation of
)(GLw
(Number of productions) x(Largest right side of a production)
m >
w
Consequence:
34
S
A
A
x
Last repeated variable
u z
v y
uvxyzw
terminalsof strings :,,,, zyxvu
String
repeated
35
S
A
A
x
u z
v y
uAzS
vAyA
xA
Possiblederivations:
36
uAzS vAyA
xA
We know:
This string is also generated:
uxzuAzS*
zxyuv 00
37
This string is also generated:
uvxyzuvAyzuAzS**
The original zxyuvw 11
uAzS vAyA
xA
We know:
38
This string is also generated:
uvvxyyzuvvAyyzuvAyzuAzS***
zxyuv 22
uAzS vAyA
xA
We know:
39
This string is also generated:
uvvvxyyyzuvvvAyyyz
uvvAyyzuvAyzuAzS**
**
zxyuv 33
uAzS vAyA
xA
We know:
40
This string is also generated:
yyyzvxyuvvv
yyyzvAyuvvv
uvvvAyyyz
uvvAyyzuvAyzuAzS
zxyuv ii
uAzS vAyA
xA
We know:
*
***
*****
41
Therefore, any string of the form
zxyuv ii
is generated by the grammar G
0i
42
knowing that )(GLuvxyz
we also know that )(GLzxyuv ii
Therefore,
43
S
A
A
x
u z
v y
mvxy ||Observation:
Since is the last repeated variableA
44
S
A
A
x
u z
v y
1|| vyObservation:
Since there are no unit or productions
45
The Pumping Lemma:
there exists an integer such that m
for any string mwLw || ,
we can write
For infinite context-free language L
uvxyzw
with lengths 1||and || vymvxy
and it must be:
0 allfor , iLzxyuv ii
46
Applicationsof
The Pumping Lemma
47
Context-free languages
}0:{ nba nn
Non-context free languages
}0:{ ncba nnn
48
Theorem:The language
}0:{ ncbaL nnn
is not context free
Proof: Use the Pumping Lemmafor context-free languages
49
}0:{ ncbaL nnn
Assume for contradiction that
is context-free
Since is context-free and infinitewe can apply the pumping lemma
L
L
50
Pumping Lemma gives a magic numbersuch that:
m
Pick any string with length Lw mw ||
We pick:mmm cbaw
}0:{ ncbaL nnn
51
}0:{ ncbaL nnn
mmm cbaw
We can write: uvxyzw
with lengths andmvxy || 1|| vy
52
}0:{ ncbaL nnn
Pumping Lemma says:
Lzxyuv ii for all 0i
mmm cbawuvxyzw mvxy || 1|| vy
53
}0:{ ncbaL nnn
We examine all the possible locationsof string in vxy w
mmm cbawuvxyzw mvxy || 1|| vy
54
}0:{ ncbaL nnn
Case 1: vxy is withinma
ccccccbbbbbbaaaaaa .........
vxy
m m m
u z
mmm cbawuvxyzw mvxy || 1|| vy
55
}0:{ ncbaL nnn
Case 1: and consist from only v y a
ccccccbbbbbbaaaaaa .........
vxy
m m m
u z
mmm cbawuvxyzw mvxy || 1|| vy
56
}0:{ ncbaL nnn
Case 1:
ccccccbbbbbbaaaaaaaaaaaa .........22xyv
km m m
u z
Repeating andv y
mmm cbawuvxyzw mvxy || 1|| vy
1k
57
}0:{ ncbaL nnn
Case 1:
ccccccbbbbbbaaaaaaaaaaaa .........22xyv
km m m
u z
From Pumping Lemma: Lzxyuv 22
mmm cbawuvxyzw mvxy || 1|| vy
1k
58
}0:{ ncbaL nnn
Case 1:
Lcbazxyuv mmkm 22
From Pumping Lemma: Lzxyuv 22
However:
Contradiction!!!
mmm cbawuvxyzw mvxy || 1|| vy
1k
59
}0:{ ncbaL nnn
Case 2: vxy is withinmb
ccccccbbbbbbaaaaaa .........
vxy
m m m
u z
mmm cbawuvxyzw mvxy || 1|| vy
60
}0:{ ncbaL nnn
Case 2:
ccccccbbbbbbaaaaaa .........
vxy
m m m
u z
Similar analysis with case 1
mmm cbawuvxyzw mvxy || 1|| vy
61
}0:{ ncbaL nnn
Case 3: vxy is withinmc
ccccccbbbbbbaaaaaa .........
vxy
m m m
u z
mmm cbawuvxyzw mvxy || 1|| vy
62
}0:{ ncbaL nnn
Case 3:
ccccccbbbbbbaaaaaa .........
vxy
m m m
u z
Similar analysis with case 1
mmm cbawuvxyzw mvxy || 1|| vy
63
}0:{ ncbaL nnn
Case 4: vxy overlaps and ma
ccccccbbbbbbaaaaaa .........
vxy
m m m
u z
mb
mmm cbawuvxyzw mvxy || 1|| vy
64
}0:{ ncbaL nnn
Case 4:
ccccccbbbbbbaaaaaa .........
vxy
m m m
u z
Possibility 1: contains onlycontains onlyvy
ab
mmm cbawuvxyzw mvxy || 1|| vy
65
}0:{ ncbaL nnn
ccccccbbbbbbbbbbaaaaaaaaaa .........22xyv
1km 2km m
u z
mmm cbawuvxyzw mvxy || 1|| vy
Case 4:Possibility 1: contains onlycontains onlyvy
ab121 kk
66
}0:{ ncbaL nnn
Case 4:
ccccccbbbbbbbbbbaaaaaaaaaa .........22xyv
1km 2km m
u z
From Pumping Lemma: Lzxyuv 22
mmm cbawuvxyzw mvxy || 1|| vy
121 kk
67
}0:{ ncbaL nnn
Case 4:From Pumping Lemma: Lzxyuv 22
Lcbazxyuv mkmkm 2122However:
Contradiction!!!
mmm cbawuvxyzw mvxy || 1|| vy
121 kk
68
}0:{ ncbaL nnn
Case 4:
ccccccbbbbbbaaaaaa .........
vxy
m m m
u z
Possibility 2: contains andcontains onlyvy
ab
mmm cbawuvxyzw mvxy || 1|| vy
b
69
}0:{ ncbaL nnn
Case 4:
ccccccbbbbbbbbbbbaaaaabbaabaaa .........
22xyv
m km m
u z
Possibility 2: contains andcontains onlyvy
ab
mmm cbawuvxyzw mvxy || 1|| vy
b
1k 2k
121 kkk
70
}0:{ ncbaL nnn
mmm cbawuvxyzw mvxy || 1|| vy
Case 4:From Pumping Lemma: Lzxyuv 22
ccccccbbbbbbbbbbbaaaaabbaabaaa .........
22xyv
m km m
u z
1k 2k
121 kkk
71
}0:{ ncbaL nnn
mmm cbawuvxyzw mvxy || 1|| vy
Case 4:From Pumping Lemma: Lzxyuv 22
Lcbabazxyuv mkmkkm 2122
However:
Contradiction!!!
121 kkk
72
}0:{ ncbaL nnn
Case 4:
ccccccbbbbbbaaaaaa .........
vxy
m m m
u z
Possibility 3: contains onlycontains andvy
ab
mmm cbawuvxyzw mvxy || 1|| vy
a
73
}0:{ ncbaL nnn
Case 4:Possibility 3: contains onlycontains andvy
ab
mmm cbawuvxyzw mvxy || 1|| vy
a
Similar analysis with Possibility 2
74
}0:{ ncbaL nnn
Case 5: vxy overlaps and mb
ccccccbbbbbbaaaaaa .........
vxy
m m m
u z
mc
mmm cbawuvxyzw mvxy || 1|| vy
75
}0:{ ncbaL nnn
Case 5:
mmm cbawuvxyzw mvxy || 1|| vy
Similar analysis with case 4
ccccccbbbbbbaaaaaa .........
vxy
m m m
u z
76
There are no other cases to consider
(since , string cannot overlap , and at the same time)
vxymvxy ||
ma mb mc
77
In all cases we obtained a contradiction
Therefore: The original assumption that
}0:{ ncbaL nnn
is context-free must be wrong
Conclusion: is not context-freeL