Week 10 (Part B) Context Free Grammar (Removing NULL, Unit, and Useless Productions NFA DFA Conversion To CFG)
Week 10 (Part B) Context Free Grammar (Removing NULL, Unit, and Useless Productions NFA DFA Conversion To CFG)
Theory of Automata 2
Killing Λ-Productions
Λ-Productions:
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 ……… Λ
• Λ-Productions are undesirable.
• We can replace Λ-production with appropriate non-Λ
productions.
Theory of Automata 3
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 Λ.
Replacement Rule.
1.Delete all Λ-Productions.
2.Add the following productions:
For every production of the X old string
Add new production(s) 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 null-able Non-Terminals, except that we do
not allow X Λ, to be formed if all the character in old
string are null-able
Theory of Automata 4
Example
Consider the CFG
S a | Xb | aYa
X is nullable
XY|Λ
Y is nullable
Yb|X
Old nullable New So the new CFG is
Production Production
XY nothing S a | Xb | aa | aYa |b
XΛ nothing XY
YX nothing Yb|X
S Xb Sb
S aYa S aa
Theory of Automata 5
Example
Consider the CFG
S Xa
X aX | bX | Λ
X is nullable
X bX Xb
Theory of Automata 6
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 | Λ
Theory of Automata 7
S XY
Example Contd. X Zb
Y bW
Z AB
WZ
A aA | bA | Λ
B Ba | Bb | Λ
Theory of Automata 9
Theory of Automata 10
Killing unit-productions
Theory of Automata 11
Proof of Theorem 24
Theory of Automata 12
Example
Theory of Automata 13
Example contd.
• Let’s list all unit productions and their sequences and create new productions:
SA gives Sb Consider the CFG
SAB gives Sa S A| bb
AB|b
AB gives Aa BS|a
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 non-terminals in any
strings produced from S.
Theory of Automata 14
Useless Symbols
• A symbol that is not useful is useless
• Let a CFG G. A symbol x ε (V U ∑) is useful if there is a derivation
* *
S UxV w
G G
Where U and V ε (V U ∑) and w ε ∑*.
• A terminal is useful if it occurs in a string of the language of G.
• A variable is useful if it occurs in a derivation that begins from S and generates a
terminal string
Theory of Automata 15
Useless Productions
S aSb
S
SA
A aA Useless Production
Theory of Automata 18
Another grammar:
SA
A aA
A
B bA Useless Production
Not reachable from S
Theory of Automata 19
In general:
if S xAy w
and w contains only terminals
w L(G )
Theory of Automata 20
A production A x is useless
if any of its variables is useless
S aSb
S Productions
Variables SA useless
useless A aA useless
useless BC useless
S aS | A | C
Aa
B aa
C aCb
Theory of Automata 22
First: find all variables that can produce
strings with only terminals
S aS | A | C Round 1: { A, B}
Aa SA
B aa
C aCb Round 2: { A, B, S}
Theory of Automata 23
Keep only the variables
that produce terminal symbols: { A, B, S}
(the rest variables are useless)
S aS | A | C
Aa S aS | A
B aa Aa
C aCb B aa
Remove useless productions
Theory of Automata 24
Second: Find all variables
reachable from S
S aS | A
Aa S A B
B aa not
reachable
Theory of Automata 25
Keep only the variables
reachable from S
(the rest variables are useless)
Final Grammar
S aS | A
S aS | A
Aa
Aa
B aa
Theory of Automata 26
Set of variables that Derive terminal symbols
Theory of Automata 27
Example
Theory of Automata 28
Construction of set of reachable Variables
Theory of Automata 29
GT:
S BS | B G’T:
A aA | aF S BS | B
Bb Bb
E aA | BSA
F bB | b
Theory of Automata 30
Removing All
Theory of Automata 31
Another Linear Grammar
G SA
Grammar : A aB |
B Ab
L(G) {a b : n 0}
n n
S abS string of
terminals
Example: S a
S Aab string of
Example: A Aab | B terminals
Ba
Prof. Busch - LSU 34
Regular Grammars
Languages
Generated by
Regular Grammars
Regular
Languages
Languages
Generated by
Regular Grammars
Regular
Languages
M
L( M ) NFA
Proof idea: We will construct L(G )
with Prof. Busch - LSU 41
G
Grammar is right-linear
Example:
S aA | B
A aa B
Bb B|a
S aA
Prof. Busch - LSU 44
a A
S VF
B
S aA | B
Prof. Busch - LSU 45
A
a a
S a VF
B
S aA | B
A aa B
Prof. Busch - LSU 46
A
a a
S a VF
B
S aA | B
b
A aa B
B bB Prof. Busch - LSU 47
A
a a
S a VF
a
B
S aA | B
b
A aa B
B bB | a Prof. Busch - LSU 48
A
a a
S a VF
a
B
b
Vi a1 a2 ………
am V
j
Right
G A ak a2a1B
linear
Av B R
Prof. Busch - LSU 53
L(G) L(G) R
Proof idea:
Let M be the NFA with L L(M ).
Construct from M a regular grammar G
such that
L( M ) L(G )
Prof. Busch - LSU 56
L
Since is regularM L L(M )
there is an NFA such that b
Example:
M a
a
q0 q1 q2
b
L ab * ab(b * ab) * q3
L L(M ) Prof. Busch - LSU 57
M
b
Convert to a right-linear grammar
M a
a
q0 q1 q2
q0 aq1 b
q3
M a
a
q0 q1 q2
q0 aq1
b
q1 bq1 q3
q1 aq2
M a
a
q0 q1 q2
q0 aq1
q1 bq1 b
q1 aq2 q3
q2 bq3
q0 aq1 M
a a
q0 q1 q2
q1 bq1
q1 aq2 b
q2 bq3 q3
q3 q1
q3
Prof. Busch - LSU 61
In General
a
For any transition: q p
Add production: q ap
Add production: qf
Examples:G1 G2
S abS S Aab
S a A Aab | B
Ba
Prof. Busch - LSU 64
What if the terminals are on right
side of non-terminals in the
Grammar
Regular grammars generate regular languages
G2
Examples:
G1 S Aab
S abS A Aab | B
S a Ba
Theory of Automata 66