2. Topics to be covered
Simplification of Grammar
Normal Forms
Session wind up
Day 7
3. The Simplification of Grammars
● Method 1: Remove Useless Productions.
● Method 2: Removing λ-Productions.
● Method 3: Removing Unit Productions.
We additionally have the substituting method that we will discuss first
There exists a probability to consider unnecessary variables
when deriving a grammar.
4. The Simplification of Grammars
Useful Substitution Method.
● Many rules govern generating equivalent grammars by means of
substitutions. Here we give one that is very useful for
simplifying grammars in various ways. What is meant by it is the
removal of certain types of undesirable productions.
● The process does not necessarily result in an actual reduction
of the number of rules.
5. The Simplification of Grammars
Useful Substitution Method.
● Let G = (V, T, S, P) be a context-free grammar. Suppose that
P contains a production of the form A → x1
Bx2
● Assume that A and B are different variables and that
B → y1
|y2
|...|yn
is the set of all productions in P;
that have B as the left side.
6. The Simplification of Grammars
Useful Substitution Method.
● Let G’= (V, T, S,P’ ) be the grammar in which is constructed by
deleting A → x1
Bx2
from P.
● And adding to it A → x1
y1
x2
| x1
y1
x2
|...| x1
yn
x2
● Hence L(G’) = L(G)
7. The Simplification of Grammars
Useful Substitution Method.
Consider G = ({A, B}, {a,b,c}, A, P) with productions
Using the suggested substitution for the variable B, we get the
grammar G’ with productions
The new grammar is equivalent to G.
8. The Simplification of Grammars
Method 1: Remove Useless Productions.
● One invariably wants to remove productions from a grammar that can
never take part in any derivation. For example, in the grammar whose
entire production set is
● The production S → A clearly plays no role, as A cannot be transformed
into a terminal string. While A can occur in a string derived from S, this
can never lead to a sentence. Removing this production leaves the
language unaffected and is a simplification by any definition.
9. The Simplification of Grammars
Method 1: Remove Useless Productions.
● Let G = (V, T, S, P) be a context-free grammar. A variable A ∈ V is said to
be useful if and only if there is at least one ω ∈ L (G) such that
with x, y in (VUT)* . In other words, a variable is useful if and only if it
occurs in at least one derivation. A variable that is not useful is called
useless. A production is useless if it involves any useless variable.
10. The Simplification of Grammars
Method 1: Remove Useless Productions.
● A variable may be useless because there is no way of getting a terminal string from
it. The case just mentioned is of this kind. Another reason a variable may be useless
is shown in the next grammar. In a grammar with start symbol S and productions
the variable B is useless and so is the production B → bA. Although B can derive a
terminal string, there is no way we can achieve
This example illustrates the two
reasons why a variable is useless:
11. The Simplification of Grammars
Method 1: Remove Useless Productions.
Q. Eliminate useless symbols and productions from G = (V,T,S,P), where V =
{S, A, B, C} and T = {a,b}, with P consisting of
12. The Simplification of Grammars
Method 1: Remove Useless Productions.
First, we identify the set of variables that can lead to a terminal string. Because A → a and B →aa,
the variables A and B belong to this set. So does S, because S ⇒ A ⇒ a. However, this argument
cannot be made for C, thus identifying it as useless. Removing C and its corresponding
productions, we are led to the grammar G1 with variables V1 = {S, A, B}, terminals T = {a}, and
productions
13. The Simplification of Grammars
Method 1: Remove Useless Productions.
Next we want to eliminate the variables that cannot be reached from the start variable. For
this, we can draw a dependency graph for the variables. Dependency graphs are a way of
visualizing complex relationships and are found in many applications. For context-free
grammars, a dependency graph has its vertices labeled with variables, with an edge
between vertices C and D if and only if there is a production of the form
14. The Simplification of Grammars
Method 1: Remove Useless Productions.
A variable is useful only if there is a path from the vertex labeled S to the vertex labeled with
that variable. In our case, B is useless. Removing it and the affected productions and
terminals, we are led to the final answer
And with the production
15. The Simplification of Grammars
Method 2: Removing λ-Productions
One kind of production that is sometimes undesirable is one in which the right side is the
empty string.
Any production of a context-free grammar of the form A → λ
is called a λ-production. Any variable A for which the derivation
is possible is called nullable.
16. The Simplification of Grammars
Method 2: Removing λ-Productions
One kind of production that is sometimes undesirable is one in which the right side is the
empty string.
Any production of a context-free grammar of the form A → λ
is called a λ-production. Any variable A for which the derivation
is possible is called nullable.
A grammar may generate a language not containing λ, yet have some λ-productions or
nullable variables. In such cases, the λ-productions can be removed.
18. The Simplification of Grammars
Method 3: Removing Unit Productions
As we have seen productions in which both sides are a single variable are at times
undesirable.
Any production of a context-free grammar of the form A → B,
where A, B ∈ V, is called a unit-production.
24. Normal Forms
● Chomsky Normal Form (CNF)
● Greibach Normal Form (GNF)
There are many kinds of normal forms we can establish for context-free
grammars. Some of these, because of their wide usefulness, have been
studied extensively. We consider two of them briefly.
25. Chomsky Normal Form
A context-free grammar is in Chomsky normal form if all productions
are of the form
A → BC
Or
A → a,
where A, B, C are in V, and a is in T.
27. Chomsky Normal Form
The following steps are followed to standardize the grammar using CNF:
● Rule-01: Reduce the grammar completely by:
1.Eliminating ∈ productions
2.Eliminating unit productions
3.Eliminating useless productions
● Rule-02: Replace each production of the form A → B1
B2
B3
….Bn
where n > 2 with A →
B1
C where C → B2
B3
….Bn
. Repeat this step for all the productions having more than
two variables on RHS.
● Rule-03: Replace each production of the form A → aB with A → XB and X → a.
Repeat this step for all the productions having the form A → aB.
29. Chomsky Normal Form
Step 1: Eliminate all null and unit productions
No null or unit productions
Step 2: Check for productions already in CNF
There are no productions that are already in CNF.
Step 3: Put Ba, Bb, Bc for a, b, c
30. Chomsky Normal Form
Step 4: we introduce additional
variables to get the first two
productions into normal form
32. Chomsky Normal Form
Step-01:
The given grammar is already completely reduced.
Step-02:
The productions already in chomsky normal form are-
B → b
D → d
These productions will remain as they are.
33. Chomsky Normal Form
The productions not in chomsky normal form are-
S → aAD
A → aB / bAB
We will convert these productions in chomsky normal
form.
34. Chomsky Normal Form
Step-03:
Replace the terminal symbols a and b by new variables Ca
and Cb
.
This is done by introducing the following two new productions in the
grammar:
Ca
→ a
Cb
→ b
Now, the productions above modify to:
S → Ca
AD
A → Ca
B / Cb
AB
35. Chomsky Normal Form
Replace AD and AB by new variables CAD
and CAB
respectively.
This is done by introducing the following two new productions in the
grammar-
CAD
→ AD
CAB
→ AB
Now, the production modifies to-
S → Ca
CAD
A → Ca
B / Cb
CAB
36. Chomsky Normal Form
Step-04: The resultant grammar is-
S → Ca
CAD
A → Ca
B / Cb
CAB
B → b
D → d
Ca
→ a
Cb
→ b
CAD
→ AD
CAB
→ AB
This grammar is in
chomsky normal form.
37. Greibach Normal Form
A context-free grammar is said to be in Greibach normal form if all
productions have the form
A → aX,
where a ∈ T and X ∈ V*
39. Greibach Normal Form
The following steps are followed to standardize the grammar using GNF:
Step 1. Convert the grammar into CNF.
If the given grammar is not in CNF, convert it to CNF.
Step 2. Eliminate left recursion from grammar if it exists.
If CFG contains left recursion, eliminate them.
Step 3. Convert the production rules into GNF form.
If any production rule is not in the GNF form, convert them.
40. Greibach Normal Form
Q1. Convert the following grammar to GNF
S → XY | Xn | p
X → mX | m
Y → Xn | o
41. Greibach Normal Form
Here, S does not appear on the right side of any production and there are no unit
or null productions in the production rule set. So, we can skip Step 1 to Step 3.
Step 4:
Now after replacing X in S → XY | Xo | p
With mX | m we obtain
S → mXY | mY | mXo | mo | p.
And after replacing X in Y → Xn | o
with the right side of X → mX | m
we obtain
Y → mXn | mn | o.
Two new productions O → o and P → p
are added to the production set and then
we came to the final GNF as:
S → mXY | mY | mXC | mC | p
X → mX | m
Y → mXD | mD | o
O → o
P → p
42. Greibach Normal Form
Q2. Consider the given grammar G1:
S → XA|BB
B → b|SB
X → b
A → a
As G1 is already in CNF and there is no left recursion, we can skip step 1 and 2
and directly move to step 3.
43. Greibach Normal Form
The production rule B->SB is not in GNF, therefore, we substitute S -> XA|BB in
production rule B->SB as:
S → XA|BB
B → b|XAB|BBB
X → b
A → a
44. Greibach Normal Form
The production rules S->XA and B->XAB is not in GNF, therefore, we substitute
X->b in production rules S->XA and B->XAB as:
S → bA|BB
B → b|bAB|BBB
X → b
A → a
45. Greibach Normal Form
Removing left recursion (B->BBB),
we get:
S → bA|BB
B → bC|bABC
C → BBC| ε
X → b
A → a
Removing null production (C-> ε),
we get:
S → bA|BB
B → bC|bABC|b|bAB
C → BBC|BB
X → b
A → a
46. Greibach Normal Form
The production rules S->BB is
not in GNF, therefore, we
substitute B → bC|bABC|b|bAB
in production rules S->BB as:
S → bA|
bCB|bABCB|bB|bABB
B → bC|bABC|b|bAB
C → BBC|BB
X → b
A → a
The production rules C->BB is
not in GNF, therefore, we
substitute B → bC|bABC|b|bAB
in production rules C->BB as:
S → bA|
bCB|bABCB|bB|bABB
B → bC|bABC|b|bAB
C → BBC
C →
bCB|bABCB|bB|bABB
X → b
A → a
The production rules C->BBC
is not in GNF, therefore, we
substitute B → bC|bABC|b|bAB
in production rules C->BBC as:
S → bA|
bCB|bABCB|bB|bABB
B → bC|bABC|b|bAB
C →
bCBC|bABCBC|bBC|bABBC
C → bCB|bABCB|bB|bABB
X → b
A → a
This is in GNF
47. End of Day 7
www.linkedin.com/in/wadkar-rushabh
@RushabhWadkar
Thank you...