Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Simplification of Context-Free Grammars Some useful substitution rules. Removing useless productions. Removing -productions. Removing unit-productions.

Similar presentations


Presentation on theme: "1 Simplification of Context-Free Grammars Some useful substitution rules. Removing useless productions. Removing -productions. Removing unit-productions."— Presentation transcript:

1 1 Simplification of Context-Free Grammars Some useful substitution rules. Removing useless productions. Removing -productions. Removing unit-productions.

2 2 Some Useful Substitution Rules G = (V, T, S, P) A  x 1 Bx 2  P B  y 1 | y 2 |... | y n  P L(G) = L(G^) G^ = (V, T, S, P^) A  x 1 y 1 x 2 | x 1 y 2 x 2 |... | x 1 y n x 2  P^

3 3 Example G = ({A, B}, {a, b}, A, P) A  a | aaA | abBc B  abbA | b G^ = (V, T, S, P^) A  a | aaA | ababbAc | abbc

4 4 Some Useful Substitution Rules G = (V, T, S, P) A  Ax 1 | Ax 2 |... | Ax n  P A  y 1 | y 2 |... | y m  P L(G) = L(G^) G^ = (V  {Z}, T, S, P^) A  y i | y i Z (i =1, m)  P^ Z  x i | x i Z (i =1, n)  P^

5 5 Example G = ({A, B}, {a, b}, A, P) A  Aa | aBc | B  Bb | ba A  aBc | aBcZ | Z | A  aBc | aBcZ | Z | Z  a | aZZ  a | aZ B  Bb | baB  ba | baY Y  b | bY

6 6 Removing Useless Productions S  aSb | | A A  aA S  A is redundant as A cannot be transformed into a terminal string.

7 7 Removing Useless Productions G = (V, T, S, P) A  V is useful iff there is w  L(G) such that: S  * xAy  * w A production is useless it it involves any useless variable.

8 8 Example G = ({S, A, B}, {a, b}, S, P) S  A A  aA | B  bA

9 9 Example G = ({S, A, B, C}, {a, b}, S, P) S  aS | A | C S  aS | A S  aS | A A  a A  a A  a B  aa C  aCb

10 10 Example G = ({S, A, B, C}, {a, b}, S, P) S  aS | A | CS  aS | A S  aS | A A  aA  aA  a B  aaB  aa C  aCb dependency graph SAB

11 11 Theorem Let G = (V, T, S, P) be a context-free grammar. Then there exists an equivalent grammar G^ = (V^, T^, S, P^) that does not contain any useless variables or productions.

12 12 Theorem Let G = (V, T, S, P) be a context-free grammar. Then there exists an equivalent grammar G^ = (V^, T^, S, P^) that does not contain any useless variables or productions. Proof: ?

13 13 Theorem Proof: Construct (V 1, T, S, P 1 ) such that V 1 contains only variables A for which A  * w  T*. 1. Set V 1 to . 2. Repeat until no more variables are added to V 1 : For every A  T for which P has a production of the form A  x 1 x 2... x n (x i  T*  V 1 ) add A to V 1. 3. Take P 1 as all the productions in P with symbols in (V 1  T)*.

14 14 Theorem Proof: Draw the variable dependency graph for G 1 and find all variables that cannot be reached from S. Remove those variables and the productions involving them. Eliminate any terminal that does not occur in a useful production.  G^ = (V^, T^, S, P^)

15 15 Removing -Productions Any production of a context-free grammar of the form: A  is called a -production. Any variable A for which the derivation: A  * is possible is called nullable.

16 16 Example S  aS 1 b S  aS 1 b | ab S 1  aS 1 b | S 1  aS 1 b | ab

17 17 Theorem Let G = (V, T, S, P) be a CFG such that  L(G). Then there exists an equivalent grammar G^ having no -productions.

18 18 Theorem Proof: Find the set V N of all nullable variables of G: 1. For all productions A , put A into V N. 2. Repeat until no more variables are added to V N : For all productions B  A 1 A 2... A n (A i  V N ) add B to V N.

19 19 Theorem Proof: For each production in P of the form: A  x 1 x 2... x m (m  1, x i  V  T) put into P^ that production as well as all those generated by replacing null variables with in all possible combination. Exception: if all x i are nullable, then A  is not put into P^.

20 20 Example S  ABaC S  ABaC | BaC | AaC | ABa | aC | Aa |Ba | a A  BC A  B | C | BC B  b | B  b C  D | C  D D  d V N = {A, B, C}

21 21 Removing Unit-Productions Any production of a context-free grammar of the form: A  B is called a unit-production.

22 22 Theorem Let G = (V, T, S, P) be a CFG without -productions. Then there exists an equivalent grammar G^ = (V, T, S, P^) that does not have any unit-productions.

23 23 Theorem Proof: 1. Put into P^ all non-unit-productions of P. 2. Repeat until no more productions are added to P^: For every A and B  V such that A  * B and B  y 1 | y 2 |... | y n  P^ add A  y 1 | y 2 |... | y n to P^.

24 24 Example S  Aa | B B  A | bb A  a | bc | B

25 25 Example S  Aa | B S  Aa B  A | bb A  a | bc A  a | bc | B B  bb S  * A S  a | bc | bb S  * B A  bb A  * B B  a | bc B  * A

26 26 Theorem Let L be a context-free language that does not contain. Then there exists a CFG that generates L and does not have any useless productions, -productions, or unit-productions.

27 27 Theorem Let L be a context-free language that does not contain. Then there exists a CFG that generates L and does not have any useless productions, -productions, or unit-productions. Proof: 1. Remove -productions. 2. Remove unit-productions. 3. Remove useless-productions

28 28 Two Important Normal Forms Chomsky normal form. Greibach normal form.

29 29 Chomsky Normal Form A context-free grammar G = (V, T, S, P) is in Chomsky normal form iff all productions are of the form: A  BC or A  a where A, B, C  V and a  T.

30 30 Theorem Any context-free grammar G = (V, T, S, P) such that  L(G) has an equivalent grammar G^ = (V^, T, S, P^) in Chomsky normal form.

31 31 Theorem Proof: First, construct an equivalent grammar G 1 = (V 1, T, S, P 1 ). V 1 = V  {B a | a  T}P 1 = {B a  a | a  T} Remove all terminals from productions of length  1: 1. Put all productions A  a into P 1. 2. Repeat until no more productions are added to P 1 : For each production A  x 1 x 2... x n (n  2, x i  T  V) add A  C 1 C 2... C n to P 1 where C i = x i if x i  V or C i = B a if x i = a

32 32 Theorem Proof: Construct G^ = (V^, T, S, P^) from G 1 = (V 1, T, S, P 1 ). V^ = V 1. Reduce the length of the right sides of the productions: 1. Put all productions A  a and A  BC into P^. 2. Repeat until no more productions are added to P^: For each production A  C 1 C 2... C n (n  2) add A  C 1 D 1, D 1  C 2 D 2,..., D n-2  C n-1 D n to P^.

33 33 Example S  ABa A  aaB B  aC

34 34 Greibach Normal Form A context-free grammar G = (V, T, S, P) is in Greibach normal form iff all productions are of the form: A  ax where a  T and x  V*.

35 35 Theorem Any context-free grammar G = (V, T, S, P) such that  L(G) has an equivalent grammar G^ = (V^, T, S, P^) in Greibach normal form.

36 36 Theorem Proof: Rewrite the grammar in Chomsky normal form. Relabel variables A 1, A 2,..., A n. Rewrite the grammar so that all productions have one of the following forms: A i  A j x j (j > i) Z i  A j x j (j  n, Z i introduced to eliminate left recursion) A i  ax i (a  T and x i  V*) Start from A n  ax n to derive Greibach productions.

37 37 Example A 2  A 1 A 2 | b A 1  A 2 A 2 | a

38 38 Homework Exercises: 3, 4, 5, 6, 7, 8, 17, 22 of Section 6.1 - Linz’s book. Exercises: 2, 3, 4, 6, 9, 10, 11 of Section 6.2 - Linz’s book. Presentations: Section 6.3 and Section 7.4.


Download ppt "1 Simplification of Context-Free Grammars Some useful substitution rules. Removing useless productions. Removing -productions. Removing unit-productions."

Similar presentations


Ads by Google