Theorem 7.16: Every CFL is a member of P

Theorem 7.16: Every CFL is a member of P
Proof: Let G be a Chomsky normal form
grammar for language L. The following O(n3)
algorithm decides whether G accepts w, where
n is the length of w :
3
O(n ) Parsing Algorithm
(Dynamic Programming)
1. If w = ε and S → ε is a rule, accept.
2. For i = 1 to n:
// examine each substring of length one
3.
For each variable A:
4.
Test whether A → b is a rule, where b = wi.
5.
If so, place A in table (i, i).
6. For l = 2 to n:
// l is the length of the substring
7.
For i = 1 to n – l + 1:
// i is the start position of the substring
8.
Let j = i + l – 1
// j is the end position of the substring
9.
For k = i to j – 1
// k is the split position
10.
For each rule A → BC
11.
If table (i, k) contains B and table (k+1, j) contains
C, put A in table (i, j)
12. If S is in table (1, n), accept; otherwise, reject
Why is it O(n3) ?
Each of steps 6, 7, 9 runs at most n times:
1. If w = ε and S → ε is a rule, accept.
2. For i = 1 to n:
// examine each substring of length one
3.
For each variable A:
4.
Test whether A → b is a rule, where b = wi.
5.
If so, place A in table (i, i).
6. For l = 2 to n:
// l is the length of the substring
7.
For i = 1 to n – l + 1:
// i is the start position of the substring
8.
Let j = i + l – 1
// j is the end position of the substring
9.
For k = i to j – 1
// k is the split position
10.
For each rule A → BC
11.
If table (i, k) contains B and table (k+1, j) contains
C, put A in table (i, j)
12. If S is in table (1, n), accept; otherwise, reject
Example



Consider L = anbn
Familiar grammar is S → ASB | ε
Convert to CNF:
S → CB | ε
C → AD | a
D → CB | b
A→a
B→b


Try w = aabb
|w| = 4, so table will be 4 × 4:
2. For i = 1 to n:
3.
For each variable A:
4.
Test whether A → b is a rule, where b = wi.
5.
If so, place A in table (i, i).
C→a
D→b
A→a
B→b
A,C
A,C
B,D
B,D
6. For l = 2 to n:
// l is the length of the substring
7.
For i = 1 to n – l + 1: // i is the start position of the substring
8.
Let j = i + l – 1 // j is the end position of the substring
9.
For k = i to j – 1 // k is the split position
l
2
2
2
3
3
3
3
4
4
4
i
1
2
3
1
1
2
2
1
1
1
j
2
3
4
3
3
4
4
4
4
4
k
1
2
3
1
2
2
3
1
2
3
10.
11.
i
1
For each rule A → BC
If table (i, k) contains B and table (k+1, j) contains
C, put A in table (i, j)
k
1
k+1
2
j
2
table(i, k)
A,C
table(k+1, j)
A,C
A,C
A,C
B,D
B,D
S → CB
C → AD
D → CB
10.
11.
i
2
For each rule A → BC
If table (i, k) contains B and table (k+1, j) contains
C, put A in table (i, j)
k
2
k+1
3
j
3
table(i, k)
A,C
table(k+1, j)
B,D
A,C
A,C C
B,D
B,D
S → CB
C → AD
D → CB
10.
11.
i
2
For each rule A → BC
If table (i, k) contains B and table (k+1, j) contains
C, put A in table (i, j)
k
2
k+1
3
j
3
table(i, k)
A,C
table(k+1, j)
B,D
A,C
A,C C,S,
D
B,D
B,D
S → CB
C → AD
D → CB
10.
11.
i
3
For each rule A → BC
If table (i, k) contains B and table (k+1, j) contains
C, put A in table (i, j)
k
3
k+1
4
j
4
table(i, k)
B,D
table(k+1, j)
B,D
A,C
A,C C,S,
D
B,D
B,D
S → CB
C → AD
D → CB
10.
11.
i
1
For each rule A → BC
If table (i, k) contains B and table (k+1, j) contains
C, put A in table (i, j)
k
1
k+1
2
j
3
table(i, k)
A,C
A,C
table(k+1, j)
C,S,D
C
A,C C,S,
D
B,D
B,D
S → CB
C → AD
D → CB
10.
11.
i
1
For each rule A → BC
If table (i, k) contains B and table (k+1, j) contains
C, put A in table (i, j)
k
2
k+1
3
j
3
table(i, k)
C
A,C
table(k+1, j)
B,D
C,S,
D
A,C C,S,
D
B,D
B,D
S → CB
C → AD
D → CB
10.
11.
i
2
For each rule A → BC
If table (i, k) contains B and table (k+1, j) contains
C, put A in table (i, j)
k
2
k+1
3
j
4
table(i, k)
A,C
A,C
table(k+1, j)
-
C,S,
D
A,C C,S,
D
B,D
B,D
S → CB
C → AD
D → CB
10.
11.
i
2
For each rule A → BC
If table (i, k) contains B and table (k+1, j) contains
C, put A in table (i, j)
k
3
k+1
4
j
4
table(i, k)
C,S,D
A,C
table(k+1, j)
B,D
C,S,
D
A,C C,S, S,D
D
B,D
B,D
S → CB
C → AD
D → CB
10.
11.
i
1
For each rule A → BC
If table (i, k) contains B and table (k+1, j) contains
C, put A in table (i, j)
k
1
k+1
2
j
4
table(i, k)
A,C
A,C
table(k+1, j)
S,D
C,S, C
D
A,C C,S, S,D
D
B,D
B,D
S → CB
C → AD
D → CB
10.
11.
i
1
For each rule A → BC
If table (i, k) contains B and table (k+1, j) contains
C, put A in table (i, j)
k
2
k+1
3
j
4
table(i, k)
C
A,C
table(k+1, j)
-
C,S, C
D
A,C C,S, S,D
D
B,D
B,D
S → CB
C → AD
D → CB
10.
11.
i
1
For each rule A → BC
If table (i, k) contains B and table (k+1, j) contains
C, put A in table (i, j)
k
3
k+1
4
j
4
table(i, k)
C,S,D
A,C
table(k+1, j)
B,D
C,S, C,S
D
,D
A,C C,S, S,D
D
B,D
B,D
S → CB
C → AD
D → CB
12. If S is in table (1, n), accept; otherwise, reject
A,C
C,S, C,S
D
,D
A,C C,S, S,D
D
B,D
B,D