Professional Documents
Culture Documents
Simplification of
Simplification of CFGs
1. Substitution Rule
2. Removing λ-productions
3. Removing Unit Productions/Chain rules
4. Removing Useless symbols
5. Augmented a Grammar
6. Removing Left Recursion
7. Left Factoring
A Substitution Rule
Equivalent
grammar
S aB
S aB | ab
A aaA
A aaA
A abBc Substitute
B b A abBc | abbc
B aA
B aA
Bb
National University of Prof. Busch
Computer and -Emerging
LSU Sciences, FAST, Islamabad 3
Lecture 20: Theory of Automata:2015
S aB | ab
A aaA
A abBc | abbc
B aA
Substitute
B aA
S aB | ab | aaA
Equivalent
A aaA grammar
In general: A xBz
B y1
Substitute
B y1
equivalent
A xBz | xy1z
grammar
Killing λ-Productions
Killing λ-Productions
λ-Productions are undesirable.
In a given CFG, we call a non-terminal N nullable
• if there is a production N λ, or
• there is a derivation that starts at N and lead to a λ.
N ……… λ
We can replace λ-production with appropriate non-λ
productions.
Theorem 23
If L is CFL generated by a CFG having λ-productions, then
there is a different CFG that has no λ-production and still
generates either the whole language L (if L does not include
λ) or else generate the language of all the words in L other
than λ.
Proof by Construction (Replacement Rule):
1. Workout the Nullable Non-terminal
2. Delete all λ-Productions.
3. Add the following productions:
For every production of the X old string Add new
production of the form X .., where right side will
account for every modification of the old string that
can be formed by deleting all possible subsets of
nullable non-Terminals, except that we do not
allow X λ, to be formed if all the character in
old string are nullable
National University of Computer and Emerging Sciences, FAST, Islamabad
8
Lecture 20: Theory of Automata:2015
Nullable Variables
production : X
Nullable Variable: Y
Example:
S aMb
M aMb
M
Nullable variable
National University of Prof. Busch
Computer
production
and -Emerging
LSU Sciences, FAST, Islamabad 10
Lecture 20: Theory of Automata:2015
Removing production s
S aMb S aMb | ab
M aMb
Substitute
M M aMb | ab
M
Example
Consider the CFG X and Y are nullable
S a | Xb | aYa X as X λ
XY|λ Y as Y X λ
Yb|X
S a | Xb | aa | aYa |b
XY
Yb|X
Example
Consider the CFG
S Xa
X aX | bX | λ
S a | Xa
X aX | bX | a | b
Example
S XY
X Zb
Y bW • Null-able Non-terminals are?
Z AB • A, B, Z and W
WZ
A aA | bA | λ
B Ba | Bb | λ
Killing unit-productions
• Definition: A production of the form
Nonterminal one Nonterminal
is called a unit production.
The following theorem allows us to get rid of unit
productions:
Theorem 24:
If there is a CFG for the language L that has no
λ-productions, then there is also a CFG for L with
no λ-productions and no unit productions.
Proof of Theorem 24
This is another proof by constructive algorithm
• Algorithm: For every pair of nonterminals A and B, if the
CFG has a unit production A B, or if there is a chain
A X1 X2 … B
where X1, X2, ... are nonterminals, create new productions
as follows:
• If the non-unit productions from B are
B s1 | s2| …
where s1, s2, ... are strings, we create the productions
A s1| s2| …
Example
• Consider the CFG
S A| bb
AB|b
BS|a
• The non-unit productions are
S bb A b Ba
• And unit productions are
SA
AB
BS
National University of Computer and Emerging Sciences, FAST, Islamabad 19
Lecture 20: Theory of Automata:2015
Example contd.
• Let’s list all unit productions and their sequences and create new
productions:
SA gives Sb
SAB gives Sa
AB gives Aa
ABS gives A bb
BS gives B bb
BSA gives Bb
• Eliminating all unit productions, the new CFG is
S bb | b | a
A b | a | bb
B a | bb | b
• This CFG generates a finite language since there are no
nonterminals in any strings produced from S.
Unit-Productions
Unit Production:
X Y
(a single variable in both sides)
Example:
S aA
Aa
A B Unit Productions
BA
BProf.Busch
bb- LSU
National University of Computer and Emerging Sciences, FAST, Islamabad 21
Lecture 20: Theory of Automata:2015
S aA
S aA | aB
Aa
Aa
A B
Substitute
A B B A| B
BA
B bb
B bb
National University ofProf.
Computer and- Emerging
Busch LSU Sciences, FAST, Islamabad 22
Lecture 20: Theory of Automata:2015
Unit productions of form
X X
can be removed immediately
S aA | aB S aA | aB
Aa Remove
Aa
B A| B BB BA
B bb B bb
S aA | aB
S aA | aB | aA
Aa
Substitute
BA Aa
BA
B bb
B bb
Final grammar
S aA | aB | aA S aA | aB
Aa Aa
B bb B bb
Useless Symbols
Useless Productions
S aSb
S
SA
A aA Useless Production
S A aA aaA aaaA
National University of Computer and Emerging Sciences, FAST, Islamabad 30
Lecture 20: Theory of Automata:2015
Another grammar:
SA
A aA
A
B bA Useless Production
Not reachable from S
w L(G )
A production A x is useless
if any of its variables is useless
S aSb
S Productions
Variables S A useless
useless A aA useless
useless B C useless
useless C D useless
National University of Computer and Emerging Sciences, FAST, Islamabad 33
Lecture 20: Theory of Automata:2015
S aS | A | C
Aa
B aa
C aCb
S aS | A | C Round 1: { A, B}
Aa SA
B aa
C aCb Round 2: { A, B, S}
National University of Computer and Emerging Sciences, FAST, Islamabad 35
Lecture 20: Theory of Automata:2015
S aS | A | C
Aa S aS | A
B aa Aa
C aCb B aa
Remove useless productions
National University of Computer and Emerging Sciences, FAST, Islamabad 36
Lecture 20: Theory of Automata:2015
S aS | A
Aa S A B
B aa not
reachable
National University of Computer and Emerging Sciences, FAST, Islamabad 37
Lecture 20: Theory of Automata:2015
Example
• Consider following CFG
G: S AC | BS | B
A aA | aF
B CF | b
C cC | D
D aD | BD | C
E aA | BSA
F bB | b
A Two stage procedure
Satge-1 :
Construction of the set of Variables that derive terminal string
New Grammar from
S AC | BS | B
TERM will be
A aA | aF
GT:
B CF | b
C cC | D S BS | B
D aD | BD | C A aA | aF
E aA | BSA Bb
F bB | b E aA | BSA
F bB | b
Iteration TERM PREV
0 {B, F} {}
1 {B, F, A, S} {B, F}
2 {B, F, A, S, E} {B, F, A, S}
3 {B, F, A, S, E} {B, F, A, S, E}
National University of Computer and Emerging Sciences, FAST, Islamabad 41
Lecture 20: Theory of Automata:2015
Removing All