Theory of Computation (Fall 2014): Elimination of Useless Symbols from CFGs
-
Upload
vladimir-kulyukin -
Category
Science
-
view
169 -
download
1
description
Transcript of Theory of Computation (Fall 2014): Elimination of Useless Symbols from CFGs
Theory of Computation
Elimination of Useless Symbols from Context-Free Grammars
Vladimir Kulyukin
Outline
Useful & Useless Symbols An Algorithm to Eliminate Useless Symbols from
CFGs
www.vkedco.blogsot.com
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
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
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
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
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
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
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.
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→
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