Chapter 6 Simplification of CFGs and Normal Forms

  • Slides: 32
Download presentation
 Chapter 6 Simplification of CFGs and Normal Forms 1

Chapter 6 Simplification of CFGs and Normal Forms 1

 6. 1: Methods for Transforming Grammars (1) A Useful Substitution Rule m m

6. 1: Methods for Transforming Grammars (1) A Useful Substitution Rule m m m Theorem 6. 1: This intuitive theorem allows us to simplify grammars. Let G = (NT, T, S, P) be a context-free grammar. Suppose that P contains a production rule of the form q A → x. Bz q Assume that A and B are different NT and that q B → y 1 | y 2 |. . . | yn is the set of all productions in P which have B as the left side. m Let G’ = (NT, T, S, P’) be the grammar in which P’ is constructed from P by replacing rule q A → x. Bz with A → x y 1 z | xy 2 z |. . . | xynz m Then L(G’) = L(G) 2

 6. 1: Methods for Transforming Grammars (2) A Useful Substitution Rule m m

6. 1: Methods for Transforming Grammars (2) A Useful Substitution Rule m m m Let G be q S → a | aa. S | ab. Bc q B → abb. S | b Applying theorem 6. 1 results in q S → a | aa. S | ababb. Sc | abbc q B → abb. S | b The rules B → abb. S | b, which are still part of the grammar, no longer serve any purpose. q Both of these useless rules may be deleted without effectively changing the grammar. 3

 6. 1: Methods for Transforming Grammars (3) Removing Useless Productions m A non-terminal

6. 1: Methods for Transforming Grammars (3) Removing Useless Productions m A non-terminal A is useful (it occurs in at least one derivation. ) if: q it is reachable: occurs in a sentential form S * a. A q it is live: generates a terminal string A * w T* m A non-terminal A is useless if: q A does not occur in any sentential form Ø It cannot be reached from start symbol OR q A does not generate any string of terminals. Ø It cannot derive a terminal string m A terminal is useful if it occurs in a sentence w L(G) m Any production involving a useless symbol is a useless production. 4

 6. 1: Methods for Transforming Grammars (4) Removing Useless Productions m To eliminate

6. 1: Methods for Transforming Grammars (4) Removing Useless Productions m To eliminate useless symbols: q First: Find the set TERM that contains all nonterminals that derive a terminal string Ø A * w, where w T* q Non-terminals NOT in TERM are useless, they cannot contribute to generate strings in L(G) q Second: Find the set REACH that contains all non-terminals A TERM that are reachable from S Ø S * a. A 5

 6. 1: Methods for Transforming Grammars (5) Removing Useless Productions m Example 1

6. 1: Methods for Transforming Grammars (5) Removing Useless Productions m Example 1 q G: S → AC | BS | B A → a. A | a. F B → CF | b C → c. C | D D → a. D | BD | C E → a. A | BSA F → b. B | b q L(G) is b+ q B, F TERM, since both generate terminals q S TERM, since S → B and hence S * b q A TERM, since A → a. F and hence A * ab q E TERM, since E → a. A and hence E * aab 6

 6. 1: Methods for Transforming Grammars (6) Removing Useless Productions m m m

6. 1: Methods for Transforming Grammars (6) Removing Useless Productions m m m C and D do not belong to TERM, so all rules containing C and D are removed The new grammar is q GT: S → BS | B A → a. A | a. F B→b E → a. A | BSA F → b. B | b All non-terminals in GT derive terminal strings Now, we must remove the non-terminals that do not occur in sentential forms of the grammar A set REACH is built that contains all nonterminals TERM derivable from S 7

 6. 1: Methods for Transforming Grammars (7) Removing Useless Productions q m GT:

6. 1: Methods for Transforming Grammars (7) Removing Useless Productions q m GT: S → BS | B A → a. A | a. F B→b E → a. A | BSA F → b. B | b S REACH, since it is the start symbol q B REACH, since S → SB, and hence B is derivable from S q A, E, and F can not be derived from S or B, so all rules containing A, E and F are removed 8

 6. 1: Methods for Transforming Grammars (8) Removing Useless Productions m m m

6. 1: Methods for Transforming Grammars (8) Removing Useless Productions m m m The new grammar is q GU: S → BS | B B→b q L(GU) = b+ The set of terminals of GU is {b}, a is removed since it does not occur in any string in the language of GU The order is important: q Applying Second step (REACH) before First Step (TERM) may not remove all useless symbols. 9

 6. 1: Methods for Transforming Grammars (9) Removing Useless Productions m m m

6. 1: Methods for Transforming Grammars (9) Removing Useless Productions m m m Home exercise: Remove all useless productions. q S → AB | CD | ADF | CF | EA q A → ab. A | ab q B → b. B | a. D | BF | a. F q C → c. B | EC | Ab q D → b. B | FFB q E → b. C | AB q F → abb. F | ba. F | b. D | BB q G → Eb. E | CE | ba Let G = ({S, A, B, C}, {a, b}, S, {S → a. S | A | C, A → a, B → aa, C → a. Cb}) be a CFG. Remove all useless productions q Final grammar is Ø G’ = ({S}, {a}, S, {S → a. S | a}) 10

 6. 1: Methods for Transforming Grammars (10) Removing e-Productions S → Sa. B

6. 1: Methods for Transforming Grammars (10) Removing e-Productions S → Sa. B | a. B B → b. B | m Let G be m A non-terminal symbol that can derive the null string ( ) is called nullable. m For example, in G above, B is nullable since B → m A grammar without nullable non-terminals is called non-contracting m G, above, is not non-contracting, since it has one nullable non-terminal, which is B. 11

 6. 1: Methods for Transforming Grammars (11) Removing e-Productions m How to find

6. 1: Methods for Transforming Grammars (11) Removing e-Productions m How to find nullable non-terminals? q Mark all non-terminals A for which there exists a production of the form A → q Repeat ØMark non-terminal X for which there exists X → and all symbols in have been marked as nullable q Until m no new non-terminal is marked Read Theorem 6. 3 12

 6. 1: Methods for Transforming Grammars (12) Removing e-Productions m The set of

6. 1: Methods for Transforming Grammars (12) Removing e-Productions m The set of nullable non-terminals of the grammar q S → ACA A → a. Aa | B | C B → b. B | b C → c. C | q is {S, A, C} q C is nullable Øsince C → e and hence C * q. A is nullable Øsince A → C, and C is nullable q. S is nullable Øsince S → ACA, and A and C are nullable 13

 6. 1: Methods for Transforming Grammars (13) Removing e-Productions m Find nullable non-terminals.

6. 1: Methods for Transforming Grammars (13) Removing e-Productions m Find nullable non-terminals. S → a. S | SS | b. A A → BB B → CC | ab | a. Ab. C C→ 14

6. 1: Methods for Transforming Grammars (14) Removing e-Productions m If L(G), we can

6. 1: Methods for Transforming Grammars (14) Removing e-Productions m If L(G), we can eliminate all productions A → m For every B referring to A: B→ a. A |… A→ |… m m B→ a |a. A |… A→ … For example, if B → and A → BABa Then after eliminating the rule B → , new rules for A will be added q A → BABa q A → BAa q A → Aa 15

6. 1: Methods for Transforming Grammars (15) Removing e-Productions m Let G be S

6. 1: Methods for Transforming Grammars (15) Removing e-Productions m Let G be S → Sa. B | a. B B → b. B | After removing -productions, the new grammar will be q S → Sa. B | Sa | a. B | a B → b. B | b q m m The removal of -productions increases the number of rules but reduces the length of derivations. 16

6. 1: Methods for Transforming Grammars (16) Removing e-Productions m Let G S →

6. 1: Methods for Transforming Grammars (16) Removing e-Productions m Let G S → ACA A → a. Aa | B | C B → b. B | b C → c. C | e m The equivalent essentially non-contracting grammar GL is q G L: S → ACA | AA | AC | A | C | A → a. Aa | aa | B | C B → b. B | b C → c. C | c m Since S * in G, the rule S → is allowed in GL, but all other -productions are replaced m A grammar satisfying these conditions is called essentially non-contracting (only start symbol is nullable) 17

6. 1: Methods for Transforming Grammars (17) Removing e-Productions m Let G be q

6. 1: Methods for Transforming Grammars (17) Removing e-Productions m Let G be q S → a. S | SS | b. A q A → BB q B → ab | a. Ab. C | a. Ab | CC q C→ m We eliminate C → by replacing: q B → CC into B → CC, B → C, and B → q B → a. Ab. C into B → a. Ab. C and B → a. Ab m Since C → is only C production q only B → and B → a. Ab retained. m The new grammar: q S → a. S | SS | b. A q A → BB q B → | ab | a. Ab 18

6. 1: Methods for Transforming Grammars (18) Removing e-Productions m m The new grammar:

6. 1: Methods for Transforming Grammars (18) Removing e-Productions m m The new grammar: q S → a. S | SS | b. A q A → BB q B → | ab | a. Ab We eliminate B → by replacing q A → BB into A → BB, A → B, and A → Since there are other B productions, these are all retained The new grammar: q S → a. S | SS | b. A q A → BB | q B → ab | a. Ab 19

6. 1: Methods for Transforming Grammars (19) Removing e-Productions m The new grammar: q

6. 1: Methods for Transforming Grammars (19) Removing e-Productions m The new grammar: q S → a. S | SS | b. A q A → BB | q B → ab | a. Ab m Finally we eliminate A → by replacing q B → a. Ab into B → a. Ab, B → ab q S → b. A into S → b. A | b m The final CFG is: q S → a. S | SS | b. A | b q A → BB | B q B → ab | a. Ab 20

6. 1: Methods for Transforming Grammars (20) Removing of Unit Rules m Rules having

6. 1: Methods for Transforming Grammars (20) Removing of Unit Rules m Rules having this form A → B are called unit rules m Consider the rules q A → a. A | a | B q B → b. B | b | C m The unit rule A → B indicates that any string derivable from B is also derivable from A m The removal of unit rules increases the number of rules but reduces the length of derivations. 21

6. 1: Methods for Transforming Grammars (21) Removing of Unit Rules m To eliminate

6. 1: Methods for Transforming Grammars (21) Removing of Unit Rules m To eliminate the unit rule, add A rules that directly generate the same strings as B q Add a rule A → u for each B → u and deleting A → B from the grammar A → B B → a |. . . m A → a |. . . B → a |. . . Read Theorem 6. 4 22

6. 1: Methods for Transforming Grammars (22) Removing of Unit Rules m Consider the

6. 1: Methods for Transforming Grammars (22) Removing of Unit Rules m Consider the rules q A → a. A | a | B q B → b. B | b | C m The new rules after eliminating the unit rule A → B q A → a. A | a | b. B | b | C q B → b. B | b | C m We add new rules to A by replacing B in A with all its RHS rules 23

6. 1: Methods for Transforming Grammars (23) Removing of Unit Rules S → ACA

6. 1: Methods for Transforming Grammars (23) Removing of Unit Rules S → ACA | AA | AC | A | C | A → a. Aa | aa | B | C B → b. B | b C → c. C | c m G L: m The new equivalent grammar (without unit rules) GC : S → ACA | AA | AC | a. Aa | aa | b. B | b | c. C | c | A → a. Aa | aa | b. B | b | c. C | c B → b. B | b C → c. C | c m 24

6. 1: Methods for Transforming Grammars (24) Removing of Unit Rules m Remove unit

6. 1: Methods for Transforming Grammars (24) Removing of Unit Rules m Remove unit rules: q S→T|S+T q T→F|F*T q F → a | (S) q q q S→T|S+T T → a | (S) | F * T F → a | (S) S → a | (S) | F * T | S + T T → a | (S) | F * T F → a | (S) 25

 6. 2: Chomsky Normal Form (1) m The Chomsky normal form places restrictions

6. 2: Chomsky Normal Form (1) m The Chomsky normal form places restrictions on the length and the composition of the right-hand side of a rule m m Definition 6. 4: A CFG is in Chomsky normal form if each production rule has one of the following forms: q A→a q A → BC q S→ q where B, C NT m Read Theorem 6. 6 26

 6. 2: Chomsky Normal Form (2) m Algorithm Step 1 m Make sure

6. 2: Chomsky Normal Form (2) m Algorithm Step 1 m Make sure that the following are satisfied: q No -productions (other than S → ) q No chain rules q No useless symbols 27

 6. 2: Chomsky Normal Form (3) m Algorithm Step 2 m Eliminate terminals

6. 2: Chomsky Normal Form (3) m Algorithm Step 2 m Eliminate terminals from RHS of productions q For each production A → X 1 X 2…Xm Ø where Xi NT T q If m 1, replace each terminal a RHS of A Ø Add (if needed) Ca → a for each a T, where each Ca is new non-terminal. Ø In production A, replace terminal a with corresponding Ca 28

 6. 2: Chomsky Normal Form (4) m Algorithm Step 3 m Eliminate productions

6. 2: Chomsky Normal Form (4) m Algorithm Step 3 m Eliminate productions with long RHS: q For each production: Ø A → B 1 B 2…Bm, q replace with productions m 2, where Bi NT Ø A → B 1 D 1 Ø D 1 → B 2 D 2 Ø… Ø Dm-2 → Bm-1 Bm q where D 1…Dm-2 are new non-terminals. 29

 6. 2: Chomsky Normal Form (5) 1. Original grammar (no chain rules, useless

6. 2: Chomsky Normal Form (5) 1. Original grammar (no chain rules, useless symbols, or -productions): S Xa. Y|Yb X YXa. Y|a Y SS|a. X|b 2. Grammar after eliminating terminals from RHSs: S XAY|YB A a X YXAY|a B b Y SS|AX|b 3. Grammar after eliminating long RHSs: S XT|YB T AY X YF|a F XG Y SS|AX|b G AY A a B b Note: Could simplify by combining redundant variables T and G 30

 6. 2: Chomsky Normal Form (6) 1. Original grammar (no chain rules, useless

6. 2: Chomsky Normal Form (6) 1. Original grammar (no chain rules, useless symbols, or -productions): S a. XYZ | a X a. X | a Y bc. Y | bc Z c. Z | c 2. Grammar after eliminating terminals from RHSs: S AXYZ | a A a X AX | a B b Y BCY | BC C c Z CZ | c 3. Grammar after eliminating long RHSs: S AF | a A a X AX | a B b Y BH | BC C c Z CZ | c m See Example 6. 8 31 F XG G YZ H CY

 6. 2: Greibach Normal Form (7) m A context-free grammar is in Greibach

6. 2: Greibach Normal Form (7) m A context-free grammar is in Greibach Normal Form if every production is of the form A → a. X q where A NT, X NT*, and a Σ m Examples: q G 1 = ({S, A}, {a, b}, S, {S → a. SA | a, A → a. A | b}) Ø GNF q G 2 = ({S, A}, {a, b}, S, {S → AS | AAS, A → SA | aa}) Ø not GNF m m This grammar S AB A a. A | b. B | b is not in GNF This grammar S a. AB | b. B A a. A | b. B | b B b is in GNF 32 B b