1 positive properties of context-free languages. 2 context-free languages are closed under: union is...
Post on 19-Dec-2015
228 views
TRANSCRIPT
1
Positive Propertiesof
Context-Free languages
2
Context-free languages are closed under: Union
1L is context free
2L is context free
21 LL
is context-free
3
Union Example:
|11 baSS
|| 222 bbSaaSS
Union grammar:
}{1nnbaL
}{2RwwL
21 | SSS }{}{ Rnn wwbaL
4
In general:
Take any two context-free grammarswith start variables 21, SS
The union grammar has start variable 21 | SSS
G
21,GG
and )()()( 21 GLGLGL
is context-free
5
Context-free languages are closed under: Concatenation
1L is context free
2L is context free
21LL
is context-free
6
Concatenation Example:
|11 baSS
|| 222 bbSaaSS
Concatenation grammar:
}{1nnbaL
}{2RwwL
21SSS }}{{ Rnn wwbaL
7
In general:
Take any two context-free grammarswith start variables 21, SS
The concatenation grammar has start variable 21 | SSS
G
21,GG
and )()()( 21 GLGLGL is context-free
8
Context-free languages are closed under: Star-operation
L is context free *L is context-free
9
Star operation Example:
|aSbS
Star grammar:
}{ nnbaL
|11 SSS *}{ nnbaL
10
In general:
Take any context-free grammarswith start variable S
The concatenation grammar has start variable
1G
G
and *)()( 1 GLGL is context-free
|11 SSS
11
Negative Propertiesof
Context-Free Languages
12
Context-free languagesare not closed under: intersection
1L is context free
2L is context free
21 LL
not necessarilycontext-free
13
Counter Example:
}{1mnn cbaL
|
|
cCC
aAbA
ACS
Context-free:
}{2mmn cbaL
|
|
bBbB
aAA
ABS
Context-free:
}{21nnn cbaLL NOT context-free
14
Context-free languagesare not closed under: complement
L is context free L not necessarilycontext-free
15
Counter Example:
}{1mnn cbaL
|
|
cCC
aAbA
ACS
Context-free:
}{2mmn cbaL
|
|
bBbB
aAA
ABS
Context-free:
}{2121nnn cbaLLLL
NOT context-free
16
Intersectionof
Context-free languagesand
Regular Languages
17
The intersection of a context-free language and a regular languageis a context-free language
1L context free
2L regular
21 LL
context-free
18
1Lfor for 2LNPDA
1M
DFA
2M
Construct a new NPDA machinethat accepts
Machine Machine
M
21 LL
context-free regular
M simulates in parallel and 1M 2M
19
1M 2M
1q 2qcba ,
transition
1p 2pa
transition
NPDA DFA
11, pq cba ,
transition
MNPDA
22 , pq
20
1M 2M
0q
initial state
0p
initial state
NPDA DFA
Initial state
MNPDA
00 , pq
21
1M 2M
1q
final state
1p
final states
NPDA DFA
final states
MNPDA
11, pq
2p
21, pq
22
M simulates in parallel and 1M 2M
M accepts string w if and only if
accepts string and w1M
accepts string w2M
)()()( 21 MLMLML
23
Therefore: )()( 21 MLML
(since is NPDA) M
is context-free
21 LL is context-free
24
Example Applications of Regular Closure
Prove that: }100:{ nbaL nn
is context-free
25
}{ nnbaWe know:
is context-free
26
}{ 1001001 baL is regular
}{}){( 100100*1 babaL is regular
We also know:
27
}{}){( 100100*1 babaL
is regular
}{ nnba
is context-free
1}{ Lba nn
is context-free
1}{
}100:{
Lba
nbaLnn
nn
is context-free
28
Another application
Prove that: }:{ cba nnnwL
is not context-free
29
}:{ cba nnnwL
}{*}**{ nnn cbacbaL
context-free regular Is context-free
If is context-free
Then
Impossible!!!
Therefore, is not context free L
30
Decidable Propertiesof
Context-Free Languages
31
Membership Question:
for grammarfind if string
G)(GLw
32
Membership Question:
for grammarfind if string
G)(GLw
Membership Algorithms:
Parsers
Exhaustive search parser
CYKparsing algorithm
33
Empty Language Question:
for grammar find if
G)(GL
34
Empty Language Question:
for grammar find if
G)(GL
Algorithm:
Check if start variable is useless S
Remove useless variables
35
Infinite Language Question:
for grammar find if is infinite
G)(GL
36
Infinite Language Question:
for grammar find if is infinite
G)(GL
Algorithm:
• Remove useless variables• Remove unit and lambda productions
• Create the dependency graph for variables
• If there is a loop in the dependency graph the language is infinite
37
Example:
cBSC
bbbBB
aaCbA
ABS
|
|
S
A
B
C
Dependency graph infinite
38
cBSC
bbbBB
aaCbA
ABS
|
|
acbbSbbbacBSbBaCbBABS
ii bbbSacbb
bbbSacbbacbbSbbbS
)()(
)()( 22