Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Positive Properties
of
Context-Free languages
1
Context-free languages
are closed under:
L1 is context free
L2 is context free
Union
L1 L2
is context-free
2
Union Example:
S1 aS1b |
S2 aS2a | bS2b |
n n
L1 {a b }
R
L2 {ww }
Union grammar:
S S1 | S2
n n
R
L {a b } {ww }
3
In general:
Take any two context-free grammars G1,G2
with start variables S1, S2
The union grammar G
has start variable S S1 | S2
and
L(G) L(G1) L(G2 )
is context-free
4
Context-free languages
are closed under:
Concatenation
L1 is context free
L1L2
L2 is context free
is context-free
5
Concatenation Example:
S1 aS1b |
S2 aS2a | bS2b |
n n
L1 {a b }
R
L2 {ww }
Concatenation grammar:
S S1S2
n n
R
L {a b }{ww }
6
In general:
Take any two context-free grammars G1,G2
with start variables S1, S2
The concatenation grammar G
has start variable S S1 | S2
and
L(G) L(G1) L(G2 )
is context-free
7
Context-free languages
are closed under:
L is context free
Star-operation
* is context-free
L
8
Star operation Example:
S aSb |
n n
L {a b }
Star grammar:
S1 SS1 |
n n
L {a b } *
9
In general:
Take any context-free grammars G
with start variable S
The concatenation grammar G1
has start variable S1 SS1 |
and
L(G1) L(G) *
is context-free
10
Negative Properties
of
Context-Free Languages
11
Context-free languages
intersection
are not closed under:
L1 is context free
L2 is context free
L1 L2
not necessarily
context-free
12
Counter Example:
n n m
n m m
L1 {a b c }
L2 {a b c }
Context-free:
Context-free:
S AC
A aAb |
C cC |
n n n
L1 L2 {a b c }
S AB
A aA |
B bBb |
NOT context-free
13
Context-free languages
complement
are not closed under:
L is context free
L
not necessarily
context-free
14
Counter Example:
n n m
n m m
L1 {a b c }
L2 {a b c }
Context-free:
Context-free:
S AC
A aAb |
C cC |
S AB
A aA |
B bBb |
n n n
L1 L2 L1 L2 {a b c }
NOT context-free
15
Intersection
of
Context-free languages
and
Regular Languages
16
The intersection of
a context-free language and
a regular language
is a context-free language
L1 context free
L1 L2
L2 regular
context-free
17
Machine
M1
NPDA for L1
context-free
Machine M 2
DFA for
L2
regular
Construct a new NPDA machine M
that accepts L1 L2
M
simulates in parallel M1 and M 2
18
NPDA M1
q1
a, b c
DFA M 2
q2
a
p1
transition
p2
transition
NPDA M
a
,
b
c
q1, p1
q2 , p2
transition
19
NPDA M1
DFA M 2
q0
p0
initial state
initial state
NPDA M
q0 , p0
Initial state
20
NPDA M1
DFA M 2
p1
q1
final state
p2
final states
NPDA M
q1, p1
q1, p2
final states
21
M
simulates in parallel M1 and M 2
M accepts string w if and only if
M1 accepts string w
and
M 2 accepts string w
L( M ) L( M1) L( M 2 )
22
Therefore:
L( M1) L( M 2 ) is context-free
(since M is NPDA)
L1 L2 is context-free
23
Example Applications of Regular Closure
Prove that:
n n
L {a b : n 100}
is context-free
24
We know:
n n
{a b }
is context-free
25
We also know:
100 100
L1 {a
b
*
is regular
}
100 100
L1 {(a b) } {a
b
}
is regular
26
n n
*
100 100
L1 {(a b) } {a
{a b }
b
is regular
is context-free
n n
{a b } L1
is context-free
n n
L {a b : n 100}
is context-free
n n
{a b } L1
27
}
Another application
Prove that:
L {w : na nb nc }
is not context-free
28
If
Then
L {w : na nb nc } is context-free
n n n
L {a * b * c*} {a b c }
context-free
regular
Is context-free
Impossible!!!
Therefore, L is not context free
29
Decidable Properties
of
Context-Free Languages
30
Membership Question:
for grammar G
find if string w L(G)
31
Membership Question:
for grammar G
find if string w L(G)
Membership Algorithms:
Parsers
Exhaustive search parser
CYK parsing algorithm
32
Empty Language Question:
for grammar G
find if L(G)
33
Empty Language Question:
for grammar G
find if L(G)
Algorithm:
Remove useless variables
Check if start variable S is useless
34
Infinite Language Question:
for grammar G
find if L(G) is infinite
35
Infinite Language Question:
for grammar G
find if L(G) is infinite
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
36
Example:
S AB
A aCb | a
B bB | bb
C cBS
Dependency graph
infinite
A
C
S
B
37
S AB
A aCb | a
B bB | bb
C cBS
S AB aCbB acBSbB acbbSbbb
2
S acbbSbbb (acbb) S (bbb)
i
(acbb) S (bbb)
2
i
38