Docsity

Eliminating Useless Variables & Reaching Chomsky Normal Form in Context-Free Grammars, Slides of Theory of Automata

Partial preview of the text

Download Eliminating Useless Variables & Reaching Chomsky Normal Form in Context-Free Grammars and more Slides Theory of Automata in PDF only on Docsity!

1

Normal Forms for CFG’s

Eliminating Useless Variables

Removing Epsilon

Removing Unit Productions

Chomsky Normal Form

2

Variables That Derive Nothing

Consider: S -> AB, A -> aA | a, B -> AB

Although A derives all strings of a’s, B derives no terminal strings (can youprove this fact?).

Thus, S derives nothing, and the language is empty.

4

Testing – (2)

Eventually, we can find no more variables.

An easy induction on the order in which variables are discovered shows thateach one truly derives a terminal string.

Conversely, any variable that derives a terminal string will be discovered by thisalgorithm.

5

Proof of Converse

The proof is an induction on the height of the least-height parse tree by whicha variable A derives a terminal string.

Basis: Height = 1.

Tree looks like:

Then the basis of the algorithm tells us that A will be discovered.

A

a

an

...

Docsity.com

7

Algorithm to Eliminate

Variables That Derive Nothing

Discover all variables that deriveterminal strings.

For all other variables, remove allproductions in which they appeareither on the left or the right.

8

Example: Eliminate Variables

S -> AB | C, A -> aA | a, B -> bB, C -> c 

Basis: A and C are identified becauseof A -> a and C -> c.

Induction: S is identified because ofS -> C.

Nothing else can be identified.

Result: S -> C, A -> aA | a, C -> c

10

Unreachable Symbols – (2)

Easy inductions in both directions show that when we can discover no moresymbols, then we have all and only thesymbols that appear in derivations from S.

Algorithm: Remove from the grammar all symbols not discovered reachable from Sand all productions that involve thesesymbols.

11

Eliminating Useless Symbols

A symbol is

useful

if it appears in

some derivation of some terminalstring from the start symbol.

Otherwise, it is

useless.

Eliminate all useless symbols by:

Eliminate symbols that derive no terminalstring.

Eliminate unreachable symbols.

13

Why It Works

After step (1), every symbol remaining derives some terminal string.

After step (2) the only symbols remaining are all derivable from S.

In addition, they still derive a terminal string, because such a derivation canonly involve symbols reachable from S.

14

Epsilon Productions

We can almost avoid using productions of the form A ->

ε

(called

-productions ).

The problem is that

ε

cannot be in the

language of any grammar that has no

ε

productions.

Theorem: If L is a CFL, then L-{

ε

} has a

CFG with no

ε

-productions.

16

Example: Nullable Symbols

S -> AB, A -> aA |

ε

, B -> bB | A

Basis: A is nullable because of A ->

ε

Induction: B is nullable because of B -> A.

Then, S is nullable because of S -> AB.

17

Proof of Nullable-Symbols

Algorithm

The proof that this algorithm finds all and only the nullable variables is verymuch like the proof that the algorithmfor symbols that derive terminal stringsworks.

Do you see the two directions of the proof?

On what is each induction?

19

Example: Eliminating

Productions

S -> ABC, A -> aA |

ε

, B -> bB |

ε

, C ->

ε

A, B, C, and S are all nullable.

New grammar: S -> ABC | AB | AC | BC | A | B | CA -> aA | aB -> bB | b

Note: C is now useless.Eliminate its productions.

20

Why it Works

Prove that for all variables A:

If w

ε

and A =>*

old

w, then A =>*

new

w.

If A =>*

new

w then w

ε

and A =>*

old

w.

Then, letting A be the start symbolproves that L(new) = L(old) – {

ε

(1) is an induction on the number ofsteps by which A derives w in the oldgrammar.