0% found this document useful (0 votes)
526 views64 pages

Week 10 (Part B) Context Free Grammar (Removing NULL, Unit, and Useless Productions NFA DFA Conversion To CFG)

The document discusses simplifying context-free grammars (CFGs) by removing unnecessary productions. It covers killing λ-productions by replacing productions with the empty string λ with equivalent non-empty productions. It also discusses killing unit productions by replacing productions with a single non-terminal on the right-hand side with productions generated from the right-hand non-terminal's other productions. Finally, it discusses removing useless variables by identifying variables that cannot generate terminal strings or are not reachable from the start symbol S. The overall aim is to simplify CFGs by removing unnecessary complexity.

Uploaded by

AbdulJaleelMemon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
526 views64 pages

Week 10 (Part B) Context Free Grammar (Removing NULL, Unit, and Useless Productions NFA DFA Conversion To CFG)

The document discusses simplifying context-free grammars (CFGs) by removing unnecessary productions. It covers killing λ-productions by replacing productions with the empty string λ with equivalent non-empty productions. It also discusses killing unit productions by replacing productions with a single non-terminal on the right-hand side with productions generated from the right-hand non-terminal's other productions. Finally, it discusses removing useless variables by identifying variables that cannot generate terminal strings or are not reachable from the start symbol S. The overall aim is to simplify CFGs by removing unnecessary complexity.

Uploaded by

AbdulJaleelMemon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 64

Theory of Automata

Context Free Grammars


Dr Waseem Shehzad, Mr Hafiz Tayyeb Javed &
Mehreen Alam
Week 10
Contents
• Simplification of CFGs
– Killing Λ-Productions
– Killing unit-productions
– Removing Useless Variables
• Symbols & Productions
– Augmented Grammar
– Removal of Left Recursion

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
XY|Λ
Y is nullable
Yb|X
Old nullable New So the new CFG is
Production Production
XY nothing S  a | Xb | aa | aYa |b
XΛ nothing XY
YX nothing Yb|X
S  Xb Sb
S  aYa S  aa

Theory of Automata 5
Example
Consider the CFG
S  Xa
X  aX | bX | Λ
X is nullable

Old nullable New So the new CFG is


Production Production
S  Xa Sa S  a | Xa
X  aX | bX | a | b
X  aX Xa

X  bX Xb

Theory of Automata 6
Example

S  XY
X  Zb
Y  bW • Null-able Non-terminals are?

Z  AB • A, B, Z and W
WZ
A  aA | bA | Λ
B  Ba | Bb | Λ

Theory of Automata 7
S  XY
Example Contd. X  Zb
Y  bW
Z  AB
WZ
A  aA | bA | Λ
B  Ba | Bb | Λ

Old nullable New So the new CFG is


Production Production
X  Zb Xb
S  XY
Y  bW Yb X  Zb | b
Z  AB Z  A and Z  B Y  bW | b
WZ Nothing new
Z  AB | A | B
A  aA Aa
A  bA Ab WZ
B  Ba B a A  aA | bA | a | b
B  Bb Bb
B  Ba | Ba | a | b
Theory of Automata 8
Remove Nulls

Theory of Automata 9
Theory of Automata 10
Killing unit-productions

• Definition: A production of the form


– non-terminal  one non-terminal
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.

Theory of Automata 11
Proof of Theorem 24

• This is another proof by constructive algorithm.


• Algorithm: For every pair of non-terminals 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 non-terminals, 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| …

Theory of Automata 12
Example

• Consider the CFG


S  A| bb
AB|b
BS|a
• The non-unit productions are
S  bb Ab Ba
• And unit productions are
SA
AB
BS

Theory of Automata 13
Example contd.
• Let’s list all unit productions and their sequences and create new productions:
SA gives Sb Consider the CFG
SAB gives Sa S  A| bb
AB|b
AB gives Aa BS|a
ABS gives A  bb
BS gives B  bb
BSA gives Bb
• 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

For a variable to be useful two conditions must be satisfied.

1. The variable must occur in a sentential form of the grammar


2. There must be a derivation of a terminal string from the variable.
• A variable that occurs in a sentential form is said to be reachable from S.
• A two part procedure is presented to eliminate useless symbols.

Theory of Automata 15
Useless Productions
S  aSb
S 
SA
A  aA Useless Production

Some derivations never terminate...

Theory of Automata 18
Another grammar:
SA
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 )

then variable A is useful

otherwise, variable A is useless

Theory of Automata 20
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


Theory of Automata 21
Removing Useless Productions
Example Grammar:

S  aS | A | C
Aa
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}
Aa SA
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
Aa S  aS | A
B  aa Aa
C  aCb B  aa
Remove useless productions
Theory of Automata 24
Second: Find all variables
reachable from S

Use a Dependency Graph

S  aS | A
Aa 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
Aa
Aa
B  aa

Remove useless productions

Theory of Automata 26
Set of variables that Derive terminal symbols

• Input = CFG (V, ∑, P , S)

• TERM = { A | there is a rule Aw ε P with w ε ∑*


• repeat
– PREV = TERM
– For each variable in A ε V do
• If there is a rule A  w and w ε (PREV U ∑)* then
TERM = TERM U {A}
• Until PREV = TERM

Theory of Automata 27
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

Theory of Automata 28
Construction of set of reachable Variables

• Input = CFG (V, ∑, P , S)


• REACH = {S}
1. PREV = null
2. repeat
i. NEW = REACH – PREV
ii. PREV = REACH
iii. For each variable A in NEW do
i. For each rule A  w do add all variables in w to
REACH
3. Until REACH = PREV

Theory of Automata 29
GT:
S BS | B G’T:
A  aA | aF S BS | B
Bb Bb
E  aA | BSA
F  bB | b

Iteration REACH PREV NEW


0 {S} {} {}
1 {S, B} {S} {S}
2 {S, B} {S, B} {B}
3 {S, B} {S, B} {}

Theory of Automata 30
Removing All

• Step 1: Remove Nullable Variables

• Step 2: Remove Unit-Productions

• Step 3: Remove Useless Variables

Theory of Automata 31
Another Linear Grammar
G SA
Grammar : A  aB | 
B  Ab

L(G)  {a b : n  0}
n n

Prof. Busch - LSU 32


Right-Linear Grammars
A  xB
All productions have form: or
A x

S  abS string of
terminals
Example: S a

Prof. Busch - LSU 33


Left-Linear Grammars
A  Bx
All productions have form: or
A x

S  Aab string of
Example: A  Aab | B terminals

Ba
Prof. Busch - LSU 34
Regular Grammars

Prof. Busch - LSU 35


Regular Grammars
Generate
Regular Languages

Prof. Busch - LSU 36


Theorem
Languages
Generated by
Regular Grammars  Regular
Languages

Prof. Busch - LSU 37


Theorem - Part 1

Languages
Generated by
Regular Grammars
 Regular
Languages

Any regular grammar generates


a regular language

Prof. Busch - LSU 38


Theorem - Part 2

Languages
Generated by
Regular Grammars
 Regular
Languages

Any regular language is generated


by a regular grammar

Prof. Busch - LSU 39


Proof – Part 1
Languages
Generated by
Regular Grammars
 Regular
Languages

The language L(G ) generated by


any regular grammar G is regular

Prof. Busch - LSU 40


The case of Right-Linear Grammars
G
Let be a right-linear grammar
L(G )
We will prove: is regular

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
Bb B|a

Prof. Busch - LSU 42


Construct NFA
Msuch that
every state is a grammar variable:
A
special
S VF final state
B
S  aA | B
A  aa B
Bb B|a Prof. Busch - LSU 43
Add edges for each production:
a A
S VF
B

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

S  aA  aaaB  aaabB  aaaba


Prof. Busch - LSU 49
NFA M Grammar
A G
a
S  aA | B
a
A  aa B
S a
B  bB | a
 VF
a
B
L( M )  L(G ) 
b
aaab * a  b * a
Prof. Busch - LSU 50
M
We construct the NFA such that:
Vi
each variable corresponds to a node:
V1 V3
V0
VF
V2 V4 special
final state
Prof. Busch - LSU 51
Vi  a1a2 amV j
For each production:

we add transitions and intermediate nodes

Vi a1 a2 ………
am V
j

Prof. Busch - LSU 52


G
Left
Construct right-linear
A  Ba1a2 ak
grammar
G
linear
A → Bv

Right
G A  ak a2a1B
linear
Av B R
Prof. Busch - LSU 53
L(G)  L(G) R

It is easy to see that:


G
Since is right-linear, we have:

L(G) L(G) R L(G )


Regular Regular Regular
Language Language Language

Prof. Busch - LSU 54


Proof - Part 2
Languages
Generated by
Regular Grammars
 Regular
Languages

Any regular language L is generated


by some regular grammar G

Prof. Busch - LSU 55


Any regular language L is generated
by some regular grammar G

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

Prof. Busch - LSU 58


b

M a
a
q0 q1 q2
q0  aq1
 b
q1  bq1 q3
q1  aq2

Prof. Busch - LSU 59


b

M a
a
q0 q1 q2
q0  aq1
q1  bq1  b

q1  aq2 q3

q2  bq3

Prof. Busch - LSU 60


L(G )  L( M )  L
G b

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

variable terminal variable


Prof. Busch - LSU 62
For any final state: qf

Add production: qf 

Prof. Busch - LSU 63


What if the terminals are on right
side of non-terminals in the
Grammar
A regular grammar is any
right-linear or left-linear grammar

Examples:G1 G2
S  abS S  Aab
S a A  Aab | B
Ba
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 Ba

L(G1)  (ab) * a L(G2 )  aab(ab) *


Prof. Busch - LSU 65
We will discuss Left Recursion and
Discussed CFG’s above in next
week

Theory of Automata 66

You might also like