Presentation is loading. Please wait.

Presentation is loading. Please wait.

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(n 3 ) algorithm decides whether.

Similar presentations


Presentation on theme: "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(n 3 ) algorithm decides whether."— Presentation transcript:

1 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(n 3 ) algorithm decides whether G accepts w, where n is the length of w :

2 O(n 3 ) 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 = w i. 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

3 Why is it O(n 3 ) ? 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 = w i. 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 Each of steps 6, 7, 9 runs at most n times:

4 Example Consider L = a n b n Familiar grammar is S → ASB |  Convert to CNF: S 0 → AB | CB |  C → AS S → CB | AB A → a B → b Try w = aabb |w| = 4, so table will be 4  4:

5 A A B B 2. For i = 1 to n: 3. For each variable A: 4.Test whether A → b is a rule, where b = w i. 5.If so, place A in table (i, i). A → a B → b

6 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 li jk

7 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) i kk+1jtable(i, k)table(k+1, j) 2 233AB A A S0,SS0,S B B S 0 → AB S 0 → CB C → AS S → AB S → CB

8 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) i kk+1jtable(i, k)table(k+1, j) 1 123AS AC A S0,SS0,S B B S 0 → AB S 0 → CB C → AS S → AB S → CB

9 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) i kk+1jtable(i, k)table(k+1, j) 1 344CB ACS0S0 A S0,SS0,S B B S 0 → AB S 0 → CB C → AS S → AB S → CB

10 12.If S is in table (1, n), accept; otherwise, reject ACS0S0 A S0,SS0,S B B


Download ppt "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(n 3 ) algorithm decides whether."

Similar presentations


Ads by Google