Download presentation
Presentation is loading. Please wait.
Published byBennett Melton Modified over 9 years ago
1
1 Chapter 6 Simplification of CFGs and Normal Forms
2
2 6.1: Methods for Transforming Grammars (1) A Useful Substitution Rule 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 A → xBz Assume that A and B are different NT and that B → y 1 | y 2 |... | y n is the set of all productions in P which have B as the left side. Let G’ = (NT, T, S, P’) be the grammar in which P’ is constructed from P by replacing rule A → xBz with A → x y 1 z | xy 2 z |... | xy n z Then L(G’) = L(G)
3
3 6.1: Methods for Transforming Grammars (2) A Useful Substitution Rule Let G be S → a | aaS | abBc B → abbS | b Applying theorem 6.1 results in S → a | aaS | ababbSc | abbc B → abbS | b The rules B → abbS | b, which are still part of the grammar, no longer serve any purpose. Both of these useless rules may be deleted without effectively changing the grammar.
4
4 6.1: Methods for Transforming Grammars (3) Removing Useless Productions A non-terminal A is useful (it occurs in at least one derivation.) if: it is reachable: occurs in a sentential form S * A it is live: generates a terminal string A * w T * A non-terminal is useless if: A non-terminal A is useless if: A does not occur in any sentential form It cannot be reached from start symbol OR A does not generate any string of terminals. It cannot derive a terminal string A terminal is useful if it occurs in a sentence w L(G) Any production involving a useless symbol is a useless production.
5
5 6.1: Methods for Transforming Grammars (4) Removing Useless Productions To eliminate useless symbols: First: Find the set TERM that contains all non- terminals that derive a terminal string A * w, where w T * Non-terminals NOT in TERM are useless, they cannot contribute to generate strings in L(G) Second: Find the set REACH that contains all non-terminals A TERM that are reachable from S S * A
6
6 6.1: Methods for Transforming Grammars (5) Removing Useless Productions Example 1 G:S → AC | BS | B A → aA | aF B → CF | b C → cC | D D → aD | BD | C E → aA | BSA F → bB | b L(G) is b + B, F TERM, since both generate terminals S TERM, since S → B and hence S * b A TERM, since A → aF and hence A * ab E TERM, since E → aA and hence E * aab
7
7 6.1: Methods for Transforming Grammars (6) Removing Useless Productions C and D do not belong to TERM, so all rules containing C and D are removed The new grammar is G T :S → BS | B A → aA | aF B → b E → aA | BSA F → bB | b All non-terminals in G T 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 non- terminals derivable from S A set REACH is built that contains all non- terminals TERM derivable from S
8
8 6.1: Methods for Transforming Grammars (7) Removing Useless Productions G T :S → BS | B A → aA | aF B → b E → aA | BSA F → bB | b S REACH, since it is the start symbol B REACH, since S → SB, and hence B is derivable from S A, E, and F can not be derived from S or B, so all rules containing A, E and F are removed
9
9 6.1: Methods for Transforming Grammars (8) Removing Useless Productions The new grammar is G U :S → BS | B B → b L(G U ) = b + The set of terminals of G U is {b}, a is removed since it does not occur in any string in the language of G U The order is important: Applying Second step (REACH) before First Step (TERM) may not remove all useless symbols.
10
10 6.1: Methods for Transforming Grammars (9) Removing Useless Productions Home exercise: Remove all useless productions. S → AB | CD | ADF | CF | EA A → abA | ab B → bB | aD | BF | aF C → cB | EC | Ab D → bB | FFB E → bC | AB F → abbF | baF | bD | BB G → EbE | CE | ba Let G = ({S, A, B, C}, {a, b}, S, {S → aS | A | C, A → a, B → aa, C → aCb}) be a CFG. Remove all useless productions Final grammar is G’ = ({S}, {a}, S, {S → aS | a})
11
11 6.1: Methods for Transforming Grammars (10) Removing - Productions Let G be Let G beS → SaB | aBB → bB | A non-terminal symbol that can derive the null string ( is called nullable. For example, in G above, B is nullable since B For example, in G above, B is nullable since B → A grammar without nullable non-terminals is called non-contracting G, above, is not non-contracting, since it has one nullable non-terminal, which is B.
12
12 6.1: Methods for Transforming Grammars (11) Removing - Productions How to find nullable non-terminals? Mark all non-terminals A for which there exists a production of the form A → Repeat Mark non-terminal X for which there exists X → and all symbols in have been marked as nullable Until no new non-terminal is marked Read Theorem 6.3
13
13 6.1: Methods for Transforming Grammars (12) Removing - Productions The set of nullable non-terminals of the grammar S → ACA A → aAa | B | C B → bB | b C → cC | is {S, A, C} C is nullable since C → and hence C * A is nullable since A → C, and C is nullable S is nullable since S → ACA, and A and C are nullable
14
14 6.1: Methods for Transforming Grammars (13) Removing - Productions Find nullable non-terminals. S → aS | SS | bA A → BB B → CC | ab | aAbC C →
15
15 6.1: Methods for Transforming Grammars (14) Removing - Productions If L(G), we can eliminate all productions A If L(G), we can eliminate all productions A → For every B referring to A: For example, if B and A BABa For example, if B → and A → BABa Then after eliminating the rule B , new rules for A will be added Then after eliminating the rule B → , new rules for A will be added A → BABa A → ABa A → BAa A → Aa B → A | … A → | … B → | A | … A → …
16
16 6.1: Methods for Transforming Grammars (15) Removing - Productions Let G be S → SaB | aB B → bB | After removing -productions, the new grammar will be S → SaB | Sa | aB | a B → bB | b The removal of -productions increases the number of rules but reduces the length of derivations.
17
17 6.1: Methods for Transforming Grammars (16) Removing - Productions Let G Let G S → ACA A → aAa | B | C B → bB | b C → cC | The equivalent essentially non-contracting grammar G L is G L :S → ACA | CA | AA | AC | A | C | A → aAa | aa | B | C B → bB | b C → cC | c Since S * in G, the rule S is allowed in G L, but all other -productions are replaced Since S * in G, the rule S → is allowed in G L, but all other -productions are replaced A grammar satisfying these conditions is called essentially non-contracting (only start symbol is nullable)
18
18 6.1: Methods for Transforming Grammars (17) Removing - Productions Let G be S → aS | SS | bA A → BB B → ab | aAbC | aAb | CC C → We eliminate C by replacing: We eliminate C → by replacing: B → CC into B → CC, B → C, and B → B → aAbC into B → aAbC and B → aAb Since C is only C production Since C → is only C production only B and B aAb retained. only B → and B → aAb retained. The new grammar: S → aS | SS | bA A → BB B → | ab | aAb
19
19 6.1: Methods for Transforming Grammars (18) Removing - Productions The new grammar: S → aS | SS | bA A → BB B → | ab | aAb We eliminate B by replacing We eliminate B → by replacing A → BB into A → BB, A → B, and A → Since there are other B productions, these are all retained The new grammar: S → aS | SS | bA A → BB | B | B → ab | aAb
20
20 6.1: Methods for Transforming Grammars (19) Removing - Productions The new grammar: S → aS | SS | bA A → BB | B | B → ab | aAb Finally we eliminate A by replacing Finally we eliminate A → by replacing B → aAb into B → aAb, B → ab S → bA into S → bA | b The final CFG is: S → aS | SS | bA | b A → BB | B B → ab | aAb
21
21 6.1: Methods for Transforming Grammars (20) Removing of Unit Rules Rules having this form A B are called unit rules Rules having this form A → B are called unit rules Consider the rules A → aA | a | B B → bB | b | C The unit rule A B indicates that any string derivable from B is also derivable from A The unit rule A → B indicates that any string derivable from B is also derivable from A The removal of unit rules increases the number of rules but reduces the length of derivations.
22
22 6.1: Methods for Transforming Grammars (21) Removing of Unit Rules To eliminate the unit rule, add A rules that directly generate the same strings as B Add a rule A → u for each B → u and deleting A → B from the grammar Read Theorem 6.4 A → B B → |... A → |... B → |...
23
23 6.1: Methods for Transforming Grammars (22) Removing of Unit Rules Consider the rules A → aA | a | B B → bB | b | C The new rules after eliminating the unit rule A → B A → aA | a | bB | b | C B → bB | b | C We add new rules to A by replacing B in A with all its RHS rules
24
24 6.1: Methods for Transforming Grammars (23) Removing of Unit Rules G L :S → ACA | CA | AA | AC | A | C | A → aAa | aa | B | C B → bB | b C → cC | c The new equivalent grammar (without unit rules) G C :S → ACA | CA | AA | AC | aAa | aa | bB | b | cC | c | A → aAa | aa | bB | b | cC | c B → bB | b C → cC | c
25
25 6.1: Methods for Transforming Grammars (24) Removing of Unit Rules Remove unit rules: S → T | S + T T → F | F * T F → a | (S) 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)
26
26 6.2:Chomsky Normal Form (1) The Chomsky normal form places restrictions on the length and the composition of the right-hand side of a rule Definition 6.4: A CFG is in Chomsky normal form if each production rule has one of the following forms: A → a A → BC S → where B, C NT Read Theorem 6.6
27
27 6.2:Chomsky Normal Form (2) Algorithm Step 1 Make sure that the following are satisfied: No -productions (other than S → ) No chain rules No useless symbols
28
28 6.2:Chomsky Normal Form (3) Algorithm Step 2 Eliminate terminals from RHS of productions For each production A → X 1 X 2 …X m where X i NT If m 1, replace each terminal a RHS of A Add (if needed) C a → a for each a , where each C a is new non-terminal. In production A, replace terminal a with corresponding C a
29
29 6.2:Chomsky Normal Form (4) Algorithm Step 3 Eliminate productions with long RHS: For each production: A → B 1 B 2 …B m, m 2, where B i NT replace with productions A → B 1 D 1 D 1 → B 2 D 2 … D m-2 → B m-1 B m where D 1 …D m-2 are new non-terminals.
30
30 6.2:Chomsky Normal Form (5) 1. Original grammar (no chain rules, useless symbols, or -productions): S X a Y | Y b X Y X a Y | a Y S S | a X | b 2. Grammar after eliminating terminals from RHSs: S X A Y | Y BA a X Y X A Y | aB b Y S S | A X | b 3. Grammar after eliminating long RHSs: S X T | Y BT A YA a X Y F | a F X G B b Y S S | A X | b G A Y Note: Could simplify by combining redundant variables T and G
31
31 6.2:Chomsky Normal Form (6) 1. Original grammar (no chain rules, useless symbols, or -productions): S aXYZ | aX aX | a Y bcY | bcZ cZ | c 2. Grammar after eliminating terminals from RHSs: S AXYZ | aA a X AX | aB b Y BCY | BC C c Z CZ | c 3. Grammar after eliminating long RHSs: S AF | aA aF XG X AX | a B b G YZ Y BH | BC C cH CY Z CZ | c See Example 6.8
32
32 6.2: Greibach Normal Form (7) A context-free grammar is in Greibach Normal Form if every production is of the form A → aX where A NT, X NT *, and a Σ Examples: G 1 = ({S, A}, {a, b}, S, {S → aSA | a, A → aA | b}) GNF G 2 = ({S, A}, {a, b}, S, {S → AS | AAS, A → SA | aa}) not GNF This grammar S ABA aA | bB | b B b is not in GNF This grammar S aAB | bBB | bB A aA | bB | bB b is in GNF
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.