28

Chomsky & Greibach Normal Forms

Embed Size (px)

Citation preview

Page 1: Chomsky & Greibach Normal Forms
Page 2: Chomsky & Greibach Normal Forms

Presentation Outline

2

• Introduction• Chomsky normal form

• Preliminary simplifications• Final steps

• Greibach Normal Form• Algorithm (Example)

• Summary

Page 3: Chomsky & Greibach Normal Forms

Introduction

3

Grammar: G = (V, T, P, S)

T = { a, b }Terminals

V = A, B, CVariables

SStart Symbol

P = S → AProduction

Page 4: Chomsky & Greibach Normal Forms

Introduction

4

Grammar example

S → aBScS → abcBa → aBBb → bb

L = { anbncn | n ≥ 1 }

S aBSc aBabcc aaBbcc aabbcc

Page 5: Chomsky & Greibach Normal Forms

Introduction

5

Context free grammar

The head of any production contains only one non-terminal symbol

S → PP → aPbP → ε

L = { anbn | n ≥ 0 }

Page 6: Chomsky & Greibach Normal Forms

Presentation Outline

6May 27, 2009

• Introduction• Chomsky normal form

• Preliminary simplifications• Final simplification

• Greibach Normal Form• Algorithm (Example)

• Summary

Page 7: Chomsky & Greibach Normal Forms

Chomsky Normal Form

7

A → BCA → α

A context free grammar is said to be in Chomsky Normal Form if all productions are in the following form:

• A, B and C are non terminal symbols• α is a terminal symbol

Page 8: Chomsky & Greibach Normal Forms

Presentation Outline

8

• Introduction• Chomsky normal form

• Preliminary simplifications• Final steps

• Greibach Normal Form• Algorithm (Example)

• Summary

Page 9: Chomsky & Greibach Normal Forms

Preliminary Simplifications

9

Eliminate Useless Symbols1

Eliminate ε productions 2

Eliminate unit productions3

There are three preliminary simplifications

Page 10: Chomsky & Greibach Normal Forms

Preliminary Simplifications

10

Eliminate Useless Symbols

We need to determine if the symbol is useful by identifying if a symbol is generating and is reachable

• X is generating if X ω for some terminal string ω.• X is reachable if there is a derivation X αXβ for some α and β

**

Page 11: Chomsky & Greibach Normal Forms

Preliminary Simplifications

11May 27, 2009

Example: Removing non-generating symbols

S → AB | aA → b Initial CFL grammar

S → AB | aA → b Identify generating symbols

S → a A → b Remove non-generating

Page 12: Chomsky & Greibach Normal Forms

Preliminary Simplifications

12

Example: Removing non-reachable symbols

S → a Eliminate non-reachable

S → a A → b Identify reachable symbols

Page 13: Chomsky & Greibach Normal Forms

Preliminary Simplifications

13

The order is important.

S → AB | aA → b

Looking first for non-reachable symbols and then for non-generating symbols can still leave some useless symbols.

S → a A → b

Page 14: Chomsky & Greibach Normal Forms

Preliminary Simplifications

14

Finding generating symbols

If there is a production A → α, and every symbol of α is already known to be generating. Then A is generating

S → AB | aA → b

We cannot use S → AB because B has not been established to be generating

Page 15: Chomsky & Greibach Normal Forms

Preliminary Simplifications

15

Eliminate Useless Symbols1

Eliminate ε productions 2

Eliminate unit productions3

There are three preliminary simplifications

Page 16: Chomsky & Greibach Normal Forms

Preliminary Simplifications

16

Eliminate ε Productions

• In a grammar ε productions are convenient but not essential

• If L has a CFG, then L – {ε} has a CFG

Nullable variable

A ε*

Page 17: Chomsky & Greibach Normal Forms

Preliminary Simplifications

17

If A is a nullable variable

• Whenever A appears on the body of a production A might or might not derive ε

S → ASA | aBA → B | SB → b | ε

Nullable: {A, B}

Page 18: Chomsky & Greibach Normal Forms

Preliminary Simplifications

18

• Create two version of the production, one with the nullable variable and one without it

• Eliminate productions with ε bodies

S → ASA | aBA → B | SB → b | ε

S → ASA | aB | AS | SA | S | aA → B | SB → b

Eliminate ε Productions

Page 19: Chomsky & Greibach Normal Forms

Preliminary Simplifications

19

• Create two version of the production, one with the nullable variable and one without it

• Eliminate productions with ε bodies

S → ASA | aBA → B | SB → b | ε

S → ASA | aB | AS | SA | S | aA → B | SB → b

Eliminate ε Productions

Page 20: Chomsky & Greibach Normal Forms

Preliminary Simplifications

20

• Create two version of the production, one with the nullable variable and one without it

• Eliminate productions with ε bodies

S → ASA | aBA → B | SB → b | ε

S → ASA | aB | AS | SA | S | aA → B | SB → b

Eliminate ε Productions

Page 21: Chomsky & Greibach Normal Forms

Preliminary Simplifications

21

Eliminate Useless Symbols1

Eliminate ε productions 2

Eliminate unit productions3

There are three preliminary simplifications

Page 22: Chomsky & Greibach Normal Forms

Preliminary Simplifications

22

Eliminate unit productions

A unit production is one of the form A → B where both A and B are variables

A B*

A → B, B → ω, then A → ω

Identify unit pairs

Page 23: Chomsky & Greibach Normal Forms

Eliminate unit productions

• Consider the CFG S->0A | 1B|C A-> 0S|00 B->1|A C->01 remove unit productions.

S-> CB-> A unit productions

S->0A|1B|01A->0S|00B->1|0S|00

May 27, 2009 23

Page 24: Chomsky & Greibach Normal Forms

Brain Activity

Can you see both the frog and the horse on this visual illusion?

Page 25: Chomsky & Greibach Normal Forms

Brain ActivityIs that a polar bear or a seal on the cool optical illusion picture below?

Page 26: Chomsky & Greibach Normal Forms

Brain Activity

Victory or defeat? Both

Page 27: Chomsky & Greibach Normal Forms

Brain Activity

Page 28: Chomsky & Greibach Normal Forms

Brain Activity