Theory of Computation (Fall 2014): Elimination of Useless Symbols from CFGs

11
Theory of Computation Elimination of Useless Symbols from Context-Free Grammars Vladimir Kulyukin

description

 

Transcript of Theory of Computation (Fall 2014): Elimination of Useless Symbols from CFGs

Page 1: Theory of Computation (Fall 2014): Elimination of Useless Symbols from CFGs

Theory of Computation

Elimination of Useless Symbols from Context-Free Grammars

Vladimir Kulyukin

Page 2: Theory of Computation (Fall 2014): Elimination of Useless Symbols from CFGs

Outline

Useful & Useless Symbols An Algorithm to Eliminate Useless Symbols from

CFGs

www.vkedco.blogsot.com

Page 3: Theory of Computation (Fall 2014): Elimination of Useless Symbols from CFGs

Useful & Useless Symbols

Let G = (V, T, P, S) be a CFG grammar A symbol X is useful if there is a derivation S * αXβ * w for some α, β in (V U T)* and w is in T*

A symbol X is useless if there is no such derivation

Page 4: Theory of Computation (Fall 2014): Elimination of Useless Symbols from CFGs

Example: Useful & Useless Symbols

Suppose CFG G has the following productions:

S AB | aA a

A, B are useless symbols (Why is A useless?)

S is a useful symbol

Page 5: Theory of Computation (Fall 2014): Elimination of Useless Symbols from CFGs

Elimination of Useless Symbols

.which

for some is there'in each for such that

,',,''CFG equivalentan exists There

. and ,,, CFG aLet :Lemma1

*

*

wA

TwVA

SPTVG

GLSPTVG

Page 6: Theory of Computation (Fall 2014): Elimination of Useless Symbols from CFGs

Computation of V’ OLDVars = { };

// put into NEWVars those variables that derive terminals in one step.

NEWVars = {A | A w for some w in T*}

// keep looping until no more variables can be added to NEWVars.

while OLDVars != NEWVars {

OLDVars = NEWVars;

// Put into NEWVars only those variables in the left-hand side of

// grammar rules if the corresponding right hand side consists of terminals

// or variables in OLDVars.

NEWVars = OLDVars U { A | A α, α in (T U OLDVars)* };

}

V’ = NEWVars; // V’ contains only those variables that generate terminal strings

P’ is the set of productions in P whose symbols are in V’ U T

Page 7: Theory of Computation (Fall 2014): Elimination of Useless Symbols from CFGs

Elimination of Useless Symbols

.

in are symbols whosesproduction ofset theis

. and toadded becan symbols

more no until untilit doing Keep . into ,...,in

in terminalsall and into ,...,in variables

all add then Pin production a is |...|

and in is If . into Place :Insight Basic

.such that in

and are therein each for such that

,',',''CFG equivalentan exists There

.,,, CFG aLet :2 Lemma

*

1

1

1

**

T'V'

P'

T'V'

T'

V'

A

V'AV'S

XS T'V'

T'V'X

SPTVG

SPTVG

n

n

n

Page 8: Theory of Computation (Fall 2014): Elimination of Useless Symbols from CFGs

Elimination of Useless Symbols

.12 :Proof

symbols. useless no has and

'Gsuch that ,',',''CFG

equivalentan exists There.Gsuch that

.,,, CFG aLet :Theorem

' GLemmaLemmaG

GLLSPTVG

L

SPTVG

Page 9: Theory of Computation (Fall 2014): Elimination of Useless Symbols from CFGs

Elimination of Useless Symbols

Every non-empty context-free language that does not contain ɛ can be generated by a grammar with no useless symbols or ɛ-productions.

Page 10: Theory of Computation (Fall 2014): Elimination of Useless Symbols from CFGs

Example

Let G be a CFG with the following productions:1) S AB | a→2) A a →

Lemma 1 gives us S a, A a→ → Lemma 2 eliminates A a and keeps S a→ → The result grammar has only one production: S a→

Page 11: Theory of Computation (Fall 2014): Elimination of Useless Symbols from CFGs

References & Reading Suggestions

Hopcroft and Ullman. Introduction to Automata Theory, Languages, and Computation, Narosa Publishing House

Moll, Arbib, and Kfoury. An Introduction to Formal Language Theory

www.youtube.com/vkedco