chapter 6 simplification of cfgs and normal forms

32
1 Chapter 6 Chapter 6 Simplification of CFGs and Simplification of CFGs and Normal Forms Normal Forms

Upload: barbie

Post on 25-Feb-2016

58 views

Category:

Documents


2 download

DESCRIPTION

Chapter 6 Simplification of CFGs and Normal Forms. 6.1: Methods for Transforming Grammars (1) A Useful Substitution Rule. Theorem 6.1 : This intuitive theorem allows us to simplify grammars. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 6 Simplification of CFGs and Normal Forms

1

Chapter 6Chapter 6

Simplification of CFGs and Simplification of CFGs and Normal FormsNormal Forms

Page 2: Chapter 6 Simplification of CFGs and Normal Forms

2

6.1: Methods for Transforming Grammars (1)6.1: Methods for Transforming Grammars (1) A Useful Substitution Rule A Useful Substitution Rule

Theorem 6.1Theorem 6.1:: This intuitive theorem allows us to simplify grammars.This intuitive theorem allows us to simplify grammars. Let Let G = G = ((NTNT, , TT, , SS, , PP) be a context-free grammar. Suppose ) be a context-free grammar. Suppose

that that P P contains a production rule of the form contains a production rule of the form A → xBz Assume that A and B are different NT and that B → y1 | y2 | ... | yn is the set of all productions in P

which have B as the left side.

Let Let GG’ =’ = ((NTNT, , TT, , SS, , P’P’) be the grammar in which ) be the grammar in which PP’ is ’ is constructed from constructed from P P by replacing rule by replacing rule A → xBz with A → x y1z | xy2z | ... | xynz

Then Then LL((GG’) = ’) = LL((GG) )

Page 3: Chapter 6 Simplification of CFGs and Normal Forms

3

6.1: Methods for Transforming Grammars (2)6.1: Methods for Transforming Grammars (2) A Useful Substitution Rule A Useful Substitution Rule

Let Let G G be be S → a | aaS | abBc B → abbS | b

Applying theorem 6.1 results inApplying theorem 6.1 results in S → a | aaS | ababbSc | abbc B → abbS | b

The rules The rules B B → → abbS abbS | | bb, which are still part of the , which are still part of the grammar, no longer serve any purpose.grammar, no longer serve any purpose. Both of these useless rules may be deleted

without effectively changing the grammar.

Page 4: Chapter 6 Simplification of CFGs and Normal Forms

4

6.1: Methods for Transforming Grammars (3)6.1: Methods for Transforming Grammars (3) Removing Useless Productions Removing Useless Productions

A A non-terminalnon-terminal AA is useful (it occurs in at least one is useful (it occurs in at least one derivation.) if: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 A non-terminal A is is uselessuseless if: if: A does not occur in any sentential form

It cannot be reached from start symbolOR A does not generate any string of terminals.

It cannot derive a terminal string

A A terminalterminal is useful if it occurs in a is useful if it occurs in a sentencesentence ww LL((GG))

Any production involving a useless symbol is a Any production involving a useless symbol is a useless useless productionproduction..

Page 5: Chapter 6 Simplification of CFGs and Normal Forms

5

6.1: Methods for Transforming Grammars (4)6.1: Methods for Transforming Grammars (4) Removing Useless Productions Removing Useless Productions

To eliminate useless symbols: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

Page 6: Chapter 6 Simplification of CFGs and Normal Forms

6

6.1: Methods for Transforming Grammars (5)6.1: Methods for Transforming Grammars (5) Removing Useless Productions Removing Useless Productions

Example 1Example 1 G: S → AC | BS | B

A → aA | aFB → CF | b C → cC | DD → aD | BD | CE → aA | BSAF → 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

Page 7: Chapter 6 Simplification of CFGs and Normal Forms

7

6.1: Methods for Transforming Grammars (6)6.1: Methods for Transforming Grammars (6) Removing Useless Productions Removing Useless Productions

CC and and DD do not belong to do not belong to TERMTERM, so all rules , so all rules containing containing CC and and DD are are removedremoved

The new grammar isThe new grammar is GT: S → BS | B

A → aA | aFB → bE → aA | BSAF → bB | b

All non-terminals in All non-terminals in GGTT derive terminal strings derive terminal strings Now, we must remove the non-terminals that do Now, we must remove the non-terminals that do

not occur in sentential forms of the grammarnot occur in sentential forms of the grammar A set A set REACHREACH is built that contains all non- is built that contains all non-

terminals terminals TERM derivable from derivable from SS

Page 8: Chapter 6 Simplification of CFGs and Normal Forms

8

6.1: Methods for Transforming Grammars (7)6.1: Methods for Transforming Grammars (7) Removing Useless Productions Removing Useless Productions

GT: S → BS | BA → aA | aFB → bE → aA | BSAF → bB | b

SS REACHREACH, since it is the start symbol, 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

Page 9: Chapter 6 Simplification of CFGs and Normal Forms

9

6.1: Methods for Transforming Grammars (8)6.1: Methods for Transforming Grammars (8) Removing Useless Productions Removing Useless Productions

The new grammar isThe new grammar is GU: S → BS | B

B → b L(GU) = b+

The set of terminals of The set of terminals of GGUU is { is {bb}, }, aa is removed is removed since it does not occur in any string in the language since it does not occur in any string in the language of of GGUU

The order is important:The order is important: Applying Second step (REACH) before First

Step (TERM) may not remove all useless symbols.

Page 10: Chapter 6 Simplification of CFGs and Normal Forms

10

6.1: Methods for Transforming Grammars (9)6.1: Methods for Transforming Grammars (9) Removing Useless Productions Removing Useless Productions

Home exercise: Remove all 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 Let G = G = ({({S, A, B, CS, A, B, C}, {}, {aa, , bb}, }, SS, {, {S S → → aS aS | | A A | | C,C, A A → → aa, , B B → → aaaa, , C C → → aCbaCb}) be a CFG. }) be a CFG.

Remove all useless productionsRemove all useless productions Final grammar is

G’ = ({S}, {a}, S, {S → aS | a})

Page 11: Chapter 6 Simplification of CFGs and Normal Forms

11

6.1: Methods for Transforming Grammars (10)6.1: Methods for Transforming Grammars (10) Removing Removing --ProductionsProductions

Let Let GG be be S → SaB | aB B → bB |

A non-terminal symbol that can A non-terminal symbol that can derivederive the the null null stringstring ( (is called is called nullablenullable. .

For example, in For example, in GG above, above, BB is nullable since B is nullable since B →

A grammar A grammar withoutwithout nullablenullable non-terminals is non-terminals is called called non-contractingnon-contracting

GG, above, is not non-contracting, since it has one , above, is not non-contracting, since it has one nullable non-terminal, which is nullable non-terminal, which is BB..

Page 12: Chapter 6 Simplification of CFGs and Normal Forms

12

6.1: Methods for Transforming Grammars (11)6.1: Methods for Transforming Grammars (11) Removing Removing --ProductionsProductions

How to find nullable non-terminals?How to find nullable non-terminals?

Mark all non-terminals A for which there exists a production of the form A →

RepeatMark 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.3Read Theorem 6.3

Page 13: Chapter 6 Simplification of CFGs and Normal Forms

13

6.1: Methods for Transforming Grammars (12)6.1: Methods for Transforming Grammars (12) Removing Removing --ProductionsProductions

The set of nullable non-terminals of the grammarThe set of nullable non-terminals of the grammar S → ACA

A → aAa | B | CB → bB | bC → 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

Page 14: Chapter 6 Simplification of CFGs and Normal Forms

14

6.1: Methods for Transforming Grammars (13)6.1: Methods for Transforming Grammars (13) Removing Removing --ProductionsProductions

Find nullable Find nullable non-terminalsnon-terminals.. S → aS | SS | bA

A → BBB → CC | ab | aAbCC →

Page 15: Chapter 6 Simplification of CFGs and Normal Forms

15

6.1: Methods for Transforming Grammars (14)6.1: Methods for Transforming Grammars (14) Removing Removing --ProductionsProductions

If If LL((GG), we can eliminate all productions ), we can eliminate all productions A A →

For every For every BB referring to referring to AA::

For example, if For example, if BB → and and AA → BABaBABa Then after eliminating the rule Then after eliminating the rule BB → , new rules for , new rules for AA will will

be addedbe added A → BABa A → ABa A → BAa A → Aa

B → A | …A → | …

B → | A | …A → …

Page 16: Chapter 6 Simplification of CFGs and Normal Forms

16

6.1: Methods for Transforming Grammars (15)6.1: Methods for Transforming Grammars (15) Removing Removing --ProductionsProductions

Let Let GG be be S → SaB | aB

B → bB | After removing After removing -productions, the new grammar -productions, the new grammar

will bewill be S → SaB | Sa | aB | a

B → bB | b

The removal of The removal of -productions-productions increases the increases the number of rulesnumber of rules but but reduces the length of reduces the length of derivationsderivations..

Page 17: Chapter 6 Simplification of CFGs and Normal Forms

17

6.1: Methods for Transforming Grammars (16)6.1: Methods for Transforming Grammars (16) Removing Removing --ProductionsProductions

Let Let GG S → ACAA → aAa | B | CB → bB | bC → cC |

The equivalent essentially The equivalent essentially non-contractingnon-contracting grammar grammar GGLL is is GL: S → ACA | CA | AA | AC | A | C |

A → aAa | aa | B | CB → bB | bC → cC | c

Since Since SS ** in G, the rule in G, the rule SS → is allowed in is allowed in GGLL, but all , but all other other -productions are replaced-productions are replaced

A grammar satisfying these conditions is called A grammar satisfying these conditions is called essentially essentially non-contractingnon-contracting (only start symbol is nullable) (only start symbol is nullable)

Page 18: Chapter 6 Simplification of CFGs and Normal Forms

18

6.1: Methods for Transforming Grammars (17)6.1: Methods for Transforming Grammars (17) Removing Removing --ProductionsProductions

Let Let GG be be S → aS | SS | bA A → BB B → ab | aAbC | aAb | CC C →

We eliminate We eliminate CC → by replacing: by replacing: B → CC into B → CC, B → C, and B → B → aAbC into B → aAbC and B → aAb

Since Since C C → is only is only CC production production only only B B → and and B B → aAbaAb retained. retained.

The new grammar:The new grammar: S → aS | SS | bA A → BB B → | ab | aAb

Page 19: Chapter 6 Simplification of CFGs and Normal Forms

19

6.1: Methods for Transforming Grammars (18)6.1: Methods for Transforming Grammars (18) Removing Removing --ProductionsProductions

The new grammar:The new grammar: S → aS | SS | bA A → BB B → | ab | aAb

We eliminate We eliminate BB → by replacing by replacing A → BB into A → BB, A → B, and A →

Since there are other Since there are other BB productions, these are all productions, these are all retainedretained

The new grammar:The new grammar: S → aS | SS | bA A → BB | B | B → ab | aAb

Page 20: Chapter 6 Simplification of CFGs and Normal Forms

20

6.1: Methods for Transforming Grammars (19)6.1: Methods for Transforming Grammars (19) Removing Removing --ProductionsProductions

The new grammar:The new grammar: S → aS | SS | bA A → BB | B | B → ab | aAb

Finally we eliminate Finally we eliminate A A → by replacing by replacing B → aAb into B → aAb, B → ab S → bA into S → bA | b

The final CFG is:The final CFG is: S → aS | SS | bA | b A → BB | B B → ab | aAb

Page 21: Chapter 6 Simplification of CFGs and Normal Forms

21

6.1: Methods for Transforming Grammars (20)6.1: Methods for Transforming Grammars (20) Removing of Unit RulesRemoving of Unit Rules

Rules having this form Rules having this form AA → BB are called are called unit rulesunit rules

Consider the rules Consider the rules A → aA | a | B B → bB | b | C

The unit rule The unit rule AA → BB indicates that any string indicates that any string derivable from derivable from BB is also derivable from is also derivable from AA

The The removal of unitremoval of unit rules rules increases the number of increases the number of rulesrules but but reduces the length of derivationsreduces the length of derivations..

Page 22: Chapter 6 Simplification of CFGs and Normal Forms

22

6.1: Methods for Transforming Grammars (21)6.1: Methods for Transforming Grammars (21) Removing of Unit RulesRemoving of Unit Rules

To eliminate the unit rule, add To eliminate the unit rule, add AA rules that directly rules that directly generate the same strings as generate the same strings as BB Add a rule A → u for each B → u and deleting

A → B from the grammar

Read Read Theorem 6.4Theorem 6.4

A → B B → | ...

A → | ...B → | ...

Page 23: Chapter 6 Simplification of CFGs and Normal Forms

23

6.1: Methods for Transforming Grammars (22)6.1: Methods for Transforming Grammars (22) Removing of Unit RulesRemoving 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

Page 24: Chapter 6 Simplification of CFGs and Normal Forms

24

6.1: Methods for Transforming Grammars (23)6.1: Methods for Transforming Grammars (23) Removing of Unit RulesRemoving of Unit Rules

GL: S → ACA | CA | AA | AC | A | C | A → aAa | aa | B | CB → bB | bC → cC | c

The new equivalent grammar (without unit rules)The new equivalent grammar (without unit rules) GC: S → ACA | CA | AA | AC |

aAa | aa | bB | b | cC | c | A → aAa | aa | bB | b | cC | c

B → bB | bC → cC | c

Page 25: Chapter 6 Simplification of CFGs and Normal Forms

25

6.1: Methods for Transforming Grammars (24)6.1: Methods for Transforming Grammars (24) Removing of Unit RulesRemoving of Unit Rules

Remove 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)

Page 26: Chapter 6 Simplification of CFGs and Normal Forms

26

6.2:6.2: Chomsky Normal Form (1)Chomsky Normal Form (1)

The Chomsky normal form places restrictions on The Chomsky normal form places restrictions on the the lengthlength and the and the compositioncomposition of the of the right-hand right-hand sideside of a rule of a rule

Definition 6.4:Definition 6.4: A CFG is in A CFG is in Chomsky normal formChomsky normal form if each if each

production rule has one of the following forms:production rule has one of the following forms: A → a A → BC S → where B, C NT

Read Read Theorem 6.6Theorem 6.6

Page 27: Chapter 6 Simplification of CFGs and Normal Forms

27

6.2:6.2: Chomsky Normal Form (2) Chomsky Normal Form (2)

Algorithm Step 1Algorithm Step 1

Make sure that the following are satisfied:Make sure that the following are satisfied: No -productions (other than S → ) No chain rules No useless symbols

Page 28: Chapter 6 Simplification of CFGs and Normal Forms

28

6.2:6.2: Chomsky Normal Form (3) Chomsky Normal Form (3)

Algorithm Step 2Algorithm Step 2

Eliminate Eliminate terminalsterminals from RHS of productions from RHS of productions For each production A → X1X2…Xm

where Xi NT

If m 1, replace each terminal a RHS of A Add (if needed) Ca → a for each a , where each Ca is new

non-terminal. In production A, replace terminal a with corresponding Ca

Page 29: Chapter 6 Simplification of CFGs and Normal Forms

29

6.2:6.2: Chomsky Normal Form (4) Chomsky Normal Form (4)

Algorithm Step 3Algorithm Step 3

Eliminate productions with long RHS:Eliminate productions with long RHS: For each production:

A → B1B2…Bm, m 2, where Bi NT replace with productions

A → B1D1

D1 → B2D2

… Dm-2 → Bm-1Bm

where D1…Dm-2 are new non-terminals.

Page 30: Chapter 6 Simplification of CFGs and Normal Forms

30

6.2:6.2: Chomsky Normal Form (5) Chomsky Normal Form (5)

1.1. Original grammar (no chain rules, useless symbols, or Original grammar (no chain rules, useless symbols, or -productions):-productions):S X a Y | Y bX Y X a Y | aY S S | a X | b

2.2. Grammar after eliminating terminals from RHSs:Grammar after eliminating terminals from RHSs:S X A Y | Y B A aX Y X A Y | a B bY S S | A X | b

3.3. Grammar after eliminating long RHSs:Grammar after eliminating long RHSs:S X T | Y B T A Y A aX Y F | a F X G B bY S S | A X | b G A Y

Note: Could simplify by combining redundant variables Note: Could simplify by combining redundant variables TT and and GG

Page 31: Chapter 6 Simplification of CFGs and Normal Forms

31

6.2:6.2: Chomsky Normal Form (6) Chomsky Normal Form (6)

1.1. Original grammar (no chain rules, useless symbols, or Original grammar (no chain rules, useless symbols, or -productions):-productions):S aXYZ | a X aX | aY bcY | bc Z cZ | c

2.2. Grammar after eliminating terminals from RHSs:Grammar after eliminating terminals from RHSs:S AXYZ | a A aX AX | a B bY BCY | BC C cZ CZ | c

3.3. Grammar after eliminating long RHSs:Grammar after eliminating long RHSs:S AF | a A a F XGX AX | a B b G YZY BH | BC C c H CYZ CZ | c

See Example 6.8See Example 6.8

Page 32: Chapter 6 Simplification of CFGs and Normal Forms

32

6.2:6.2: Greibach Normal Form (7) Greibach Normal Form (7)

A context-free grammar is in A context-free grammar is in Greibach Normal FormGreibach Normal Form if if every production is of the form every production is of the form A A → → aXaX where A NT, X NT*, and a Σ

Examples:Examples: G1 = ({S, A}, {a, b}, S, {S → aSA | a, A → aA | b})

GNF G2 = ({S, A}, {a, b}, S, {S → AS | AAS, A → SA | aa})

not GNF This grammar This grammar S S AB AB A A aA aA || bB bB | | b B b B b b

is not in GNFis not in GNF This grammar This grammar S S aAB | bBB | bB aAB | bBB | bB

A A aA aA || bB bB | | bb B B b bis in GNFis in GNF