1 of 51

Simplifications �of �Context-Free Grammars

1

Fall 2003

2 of 51

A Substitution Rule

2

Fall 2003

Substitute

​

Equivalent

grammar

3 of 51

A Substitution Rule

3

Fall 2003

Equivalent

grammar

Substitute

​

4 of 51

4

Fall 2003

In general:

Substitute

equivalent

grammar

5 of 51

Nullable Variables

5

Fall 2003

Nullable Variable:

6 of 51

Removing Nullable Variables

6

Fall 2003

Example Grammar:

Nullable variable

7 of 51

7

Fall 2003

Substitute

​

Final Grammar

8 of 51

Unit-Productions

8

Fall 2003

Unit Production:

(a single variable in both sides)

9 of 51

Removing Unit Productions

9

Fall 2003

Observation:

Is removed immediately

10 of 51

10

Fall 2003

Example Grammar:

11 of 51

11

Fall 2003

Substitute

​

12 of 51

12

Fall 2003

Remove

​

13 of 51

13

Fall 2003

Substitute

​

14 of 51

14

Fall 2003

Remove repeated productions

Final grammar

15 of 51

Useless Productions

15

Fall 2003

Some derivations never terminate...

Useless Production

16 of 51

16

Fall 2003

Another grammar:

Not reachable from S

Useless Production

17 of 51

17

Fall 2003

In general:

if

then variable is useful

otherwise, variable is useless

contains only

terminals

18 of 51

18

Fall 2003

A production is useless

if any of its variables is useless

Productions

useless

useless

useless

useless

Variables

useless

useless

useless

19 of 51

Removing Useless Productions

19

Fall 2003

Example Grammar:

20 of 51

20

Fall 2003

First:

find all variables that can produce

strings with only terminals

Round 1:

Round 2:

21 of 51

21

Fall 2003

Keep only the variables

that produce terminal symbols:

(the rest variables are useless)

Remove useless productions

22 of 51

22

Fall 2003

Second:

Find all variables

reachable from

Use a Dependency Graph

not

reachable

23 of 51

23

Fall 2003

Keep only the variables

reachable from S

Final Grammar

(the rest variables are useless)

Remove useless productions

24 of 51

Removing All

​

​

Step 1: Remove Nullable Variables

​

Step 2: Remove Unit-Productions

​

Step 3: Remove Useless Variables

24

Fall 2003

25 of 51

Normal Forms�for�Context-free Grammars

25

Fall 2003

26 of 51

Chomsky Normal Form

26

Fall 2003

Each productions has form:

variable

variable

or

terminal

27 of 51

27

Fall 2003

Examples:

Not Chomsky

Normal Form

Chomsky

Normal Form

28 of 51

Convertion to Chomsky Normal Form

​

Example:

28

Fall 2003

Not Chomsky

Normal Form

29 of 51

29

Fall 2003

Introduce variables for terminals:

30 of 51

30

Fall 2003

Introduce intermediate variable:

31 of 51

31

Fall 2003

Introduce intermediate variable:

32 of 51

32

Fall 2003

Final grammar in Chomsky Normal Form:

Initial grammar

33 of 51

33

Fall 2003

From any context-free grammar

(which doesn’t produce )

not in Chomsky Normal Form

we can obtain:

An equivalent grammar

in Chomsky Normal Form

In general:

34 of 51

34

Fall 2003

The Procedure

First remove:

​

Nullable variables

​

Unit productions

35 of 51

35

Fall 2003

Then, for every symbol :

In productions: replace with

Add production

New variable:

36 of 51

36

Fall 2003

Replace any production

with

New intermediate variables:

37 of 51

37

Fall 2003

Theorem:

For any context-free grammar

(which doesn’t produce )

there is an equivalent grammar

in Chomsky Normal Form

38 of 51

38

Fall 2003

Observations

  • Chomsky normal forms are good

for parsing and proving theorems

  • It is very easy to find the Chomsky normal

form for any context-free grammar

39 of 51

Greinbach Normal Form

39

Fall 2003

All productions have form:

symbol

variables

40 of 51

40

Fall 2003

Examples:

Greinbach

Normal Form

Not Greinbach

Normal Form

41 of 51

41

Fall 2003

Conversion to Greinbach Normal Form:

Greinbach

Normal Form

42 of 51

42

Fall 2003

Theorem:

For any context-free grammar

(which doesn’t produce )

there is an equivalent grammar

in Greinbach Normal Form

43 of 51

43

Fall 2003

Observations

  • Greinbach normal forms are very good

for parsing

  • It is hard to find the Greinbach normal

form of any context-free grammar

44 of 51

The CYK Parser

44

Fall 2003

45 of 51

45

Fall 2003

The CYK Membership Algorithm

Input:

  • Grammar in Chomsky Normal Form
  • String

Output:

find if

46 of 51

46

Fall 2003

The Algorithm

  • Grammar :
  • String :

Input example:

47 of 51

47

Fall 2003

48 of 51

48

Fall 2003

49 of 51

49

Fall 2003

50 of 51

50

Fall 2003

51 of 51

51

Fall 2003

Therefore:

Time Complexity:

The CYK algorithm can be

easily converted to a parser

(bottom up parser)

Observation: