0% found this document useful (0 votes)
16 views

Unit 3 (Part II)

Uploaded by

ioterottishruti
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Unit 3 (Part II)

Uploaded by

ioterottishruti
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

SIMPLIFICATION OF CFG

➢ Most of the time, some of the productions of CFGs are not useful and are redundant. This happens because the
definition of CFGs does not restrict us from making these redundant productions.
➢ By simplifying CFGs we remove all these redundant productions from a grammar , while keeping the
transformed grammar equivalent to the original grammar.
➢ Two grammars are called equivalent if they produce the same language. Simplifying CFGs is necessary to later
convert them into Normal forms.

A CFG can be simplified by eliminating:


1. Useless Symbols
2. Є - Productions
3. Unit Productions

TCS : UNIT 3 1
SIMPLIFICATION OF CFG : ELIMINATION OF USELESS SYMBOLS

CFG:
S➔aaB/abA/S CFG:

Useless productions – The A➔aA S➔AB/a


productions that can never A➔b
B➔ab/b
take part in derivation of any
string , are called useless C➔ad
productions. Similarly , a
variable that can never take
part in derivation of any string
is called a useless variable. For
eg. Simplified CFG: Simplified CFG:
S➔aaB/S S➔ a
B➔ab/b

TCS : UNIT 3 2
SIMPLIFICATION OF CFG : ELIMINATION OF NULL(Є) PRODUCTIONS

CFG: CFG: CFG:


S➔aMb S➔XYX S ➔ a S/ A
M➔aMb X➔0X/Є A➔Є
M➔Є Y➔0Y/Є

Simplified CFG: Simplified CFG:


S➔XYX/YX/XY/XX/X/Y Simplified CFG:
S➔aMb/ab
S ➔ aS/a/ Є
M➔aMb/ab X➔0X/0
Y➔0Y/0

TCS : UNIT 3 3
λ productions or Null Productions – The productions of type ‘A -> λ’ are called λ productions ( also called lambda
productions and null productions) . These productions can only be removed from those grammars that do not
generate λ (an empty string). It is possible for a grammar to contain null productions and yet not produce an empty
string.
To remove null productions , we first have to find all the nullable variables. A variable ‘A’ is called nullable if λ can be
derived from ‘A’. For all the productions of type ‘A -> λ’ , ‘A’ is a nullable variable. For all the productions of type ‘B ->
A1A2…An ‘ , where all ’Ai’s are nullable variables , ‘B’ is also a nullable variable.
After finding all the nullable variables, we can now start to construct the null production free grammar. For all the
productions in the original grammar , we add the original production as well as all the combinations of the production
that can be formed by replacing the nullable variables in the production by λ. If all the variables on the RHS of the
production are nullable , then we do not add ‘A -> λ’ to the new grammar. An example will make the point clear.
Consider the grammar –

S -> ABCd (1) A -> BC (2) B -> bB | λ (3) C -> cC | λ (4)


Lets first find all the nullable variables. Variables ‘B’ and ‘C’ are clearly nullable because they contain ‘λ’ on the RHS of
their production. Variable ‘A’ is also nullable because in (2) , both variables on the RHS are also nullable. So variables
‘A’ , ‘B’ and ‘C’ are nullable variables.
Lets create the new grammar. We start with the first production. Add the first production as it is. Then we create all
the possible combinations that can be formed by replacing the nullable variables with λ. Therefore line (1) now
becomes ‘S -> ABCd | ABd | ACd | BCd | Ad | Bd |Cd | d ’.We apply the same rule to line (2) but we do not add ‘A -> λ’
even though it is a possible combination. We remove all the productions of type ‘V -> λ’. The new grammar now
becomes –

S -> ABCd | ABd | ACd | BCd | Ad | Bd |Cd | d A -> BC | BTCS


| C: BUNIT
-> 3bB | b C -> cC | c 49
SIMPLIFICATION OF CFG : ELIMINATION OF UNIT PRODUCTIONS
Simplified CFG:
CFG: CFG:
S➔AC
S ➔ 0 A/ 1 B / C S➔AC
A➔a
A➔0S/00 A➔a
C➔b/d
B➔1/A C➔B/d
B➔b
C➔01 B➔D
D➔b
D➔E
Removal by E➔b
E➔b
Substitution

Simplified CFG: Simplified CFG:


S ➔ 0 A/ 1 B / 0 1 S ➔ 0 A/ 1 B / 0 1 Simplified CFG:
A➔0S/00 A➔0S/00 S➔AC
B➔1/0S/00 B➔1/0S/00 A➔a
C➔01 C➔b/d
TCS : UNIT 3 5
CHOMSKY NORMAL FORMS (CNF)

A context free grammar (CFG) is in Chomsky Normal Form (CNF) if all production
rules satisfy one of the following conditions:
• A non-terminal generating a terminal (e.g.; X->x)
• A non-terminal generating two non-terminals (e.g.; X->YZ)
• Start symbol generating ε. (e.g.; S-> ε)

Consider the following grammars,


G1 = { S->a, S->AZ, A->a, Z->z }
G2 = { S->a, S->aZ, Z->a}

The grammar G1 is in CNF as production rules satisfy the rules specified for CNF.
However, the grammar G2 is not in CNF as the production rule S->aZ contains terminal followed
by non-terminal which does not satisfy the rules specified for CNF.
TCS : UNIT 3 6
➢ For a given grammar, there can be more than one CNF.

➢ CNF produces the same language as generated by CFG.

➢ CNF is used as a preprocessing step for many algorithms for CFG like CYK(membership

algo), bottom-up parsers etc.

➢ For generating string w of length ‘n’ requires ‘2n-1’ production or steps in CNF.

➢ Any Context free Grammar that do not have ε in it’s language has an equivalent CNF.

TCS : UNIT 3 7
CONVERSION OF CFG TO CNF
Step 1. Eliminate start symbol from RHS.
If start symbol S is at the RHS of any production in the grammar, create a
new production as: S0->S where S0 is the new start symbol.
Step 2. Eliminate null, unit and useless productions.
If CFG contains null, unit or useless production rules, eliminate them.
Step 3. Eliminate terminals from RHS if they exist with other terminals or non-terminals.
e.g,; production rule X->xY can be decomposed as:
X->ZY
Z->x
Step 4. Eliminate RHS with more than two non-terminals.
e.g,; production rule X->XYZ can be decomposed as:
X->PZ
P->XY
Example 1 – Consider the given grammar G1: Step 2. Remove null production A-> ε
S → ASB S0->S

A → aAS|a|ε S → ASB|SB

B → SbS|A|bb A → aAS|a|aS
B → SbS|A|bb|Є
Remove null production B→ ε
Convert CFG to CNF
S0->S
Soln:
S → ASB| SB| AS | S
Step 1. As start symbol S appears on the RHS, we A → aAS|aS|a
will create a new production rule S0->S. B → SbS| A|bb
Therefore, the grammar will become: Remove unit production B->A
S0->S S0->S

S → ASB S → ASB| SB| AS | S

A → aAS|a|ε A → aAS|aS|a
B → SbS|bb| aAS|aS|a
B → SbS|A|bb
TCS : UNIT 3 9
Step 2. Remove unit production S0->S Step 3. Continued….
Step 4. In production rule S0->ASB,
S0->ASB| SB| AS | S S0-> ASB| SB| AS
RHS has more than two symbols
S → ASB| SB| AS | S S → ASB| SB| AS
S0-> PB| SB| AS
A → aAS|aS|a A → XAS|XS|a
S → PB| SB| AS
B → SbS|bb| aAS|aS|a B → SYS|bb| XAS|XS|a
A → XP|XS|a
Remove unit production S->S & S0->S X→a
B → SYS|YY| XP|XS|a
S0-> ASB| SB| AS Y→b
X→a
S → ASB| SB| AS Remove B -> bb, it can’t be part of CNF
Y→b
A → aAS|aS|a S0-> ASB| SB| AS
B → SbS|bb| aAS|aS|a S → ASB| SB| AS P → AS
Step 3. In production rule A- A → XAS|XS|a
>aAS |aS and B-> B → SYS|YY| XAS|XS|a
SbS|bb|aAS|aS|a, terminals a X→a
and b exist on RHS with non- Y→b
terminals. Removing them from
TCS : UNIT 3 10
RHS:
Similarly, B-> SYS has more than S0-> PB| SB| AS
two symbols, removing it S → QB| SB| AS
S0-> PB| SB| AS A → RS|XS|a
S → PB| SB| AS B → TS|YY|RS|XS|a
A → XP|XS|a X→a
B → TS|YY| XP|XS|a Y→b
X→a P → AS
Y→b R → XA
P → AS T → SY
T → SY
A context free grammar (CFG) is in Chomsky Normal Form (CNF) if all production rules satisfy one
of the following conditions:
• A non-terminal generating a terminal (e.g.; X->x)
• A non-terminal generating two non-terminals (e.g.; X->YZ)
• Start symbol generating ε. (e.g.; S-> ε)
EX2: Convert the given grammar into CNF Step 2 − Eliminate unit productions { S->A}
S->AAA|B S ->AAA | AA | aA | a | B | ε
A->aA|B A -> aA | a | B
B-> ε There is no production for B, hence the unit productions of
Soln: Step 1 − Eliminate ε productions type V -> B cannot be eliminated.

Replacing B with ε in all other productions − Step 3 − Useless symbols

S-> AAA | ε | B B is a useless symbol, it can be removed.

A->aA | ε | B S->AAA | AA | aA | a | ε

Replacing A with ε in all other productions − A->aA | a

S ->AAA | AA | A | B | ε Step 4- In production rule S-> aA & A->aA, terminal a exist


on RHS with non-terminal A. Removing them from RHS:
A->aA | a | B
S->AAA | AA | ZA | a | ε
{ Replacing S with ε is not required
A->ZA | a
S -> AAA | AA | A | B
A -> aA | a | B } Z-> a
TCS : UNIT 3 12
Step 4. In production rule S->AAA, RHS has more than two symbols

S -> TA | AA | ZA | a | ε
A -> ZA | a
Z -> a
T ->AA

In the above grammar, all productions are of the form A - > BC or A - >b.
Therefore, it is in Chomsky normal form (CNF).

TCS : UNIT 3 13
GREIBACH NORMAL FORMS (GNF)

A context free grammar (CFG) is in Greibach Normal Form (GNF) if all production
rules satisfy one of the following conditions:
• A non-terminal generating a terminal (e.g.; X->x)
• A non-terminal generating a terminal followed by any number of non-
terminals (e.g.; X->xX1X2…Xn)
• Start symbol generating ε. (e.g.; S-> ε)
Consider the following grammars,
G1 = {S -> aA | bB, B -> bB | b, A -> aA | a}
G2 = {S -> aA | bB, B -> bB | ε, A -> aA |ε}
The grammar G1 is in GNF as production rules satisfy the rules specified for GNF. However, the grammar G2
is not in GNF as the production rules B-> ε and A-> ε do not satisfy the rules specified for GNF (only start
symbol can generate ε).
TCS : UNIT 3 14
CONVERSION OF CFG TO GNF

Step 1. Convert the Grammar to CNF

Step 2. Eliminate left recursion from grammar if it exists.


A -> A a / b
A -> A a / b ----Left Recursion
A -> b A’ A->bC
C->aC/ ε
A’ -> a A’ / ε
Step 3. Convert the production rules to GNF form.

TCS : UNIT 3 15
Example – Consider the given grammar G1: Step 3. Convert the production rules to GNF form
S → XA|BB The production rule B->SB is not in GNF, therefore, we
B → b|SB
substitute S -> XA|BB in production rule B->SB as:
X → b
A → a S → XA|BB
Convert CFG to CNF B → b|XAB|BBB
X → b
A → a

The production rules S->XA and B->XAB is not in GNF,


Soln:
therefore, we substitute X->b in production rules S->XA
As G1 is already in CNF and there is not left
and B->XAB as:
recursion, we can skip step 1and 2 and directly
S → bA|BB
move to step 3. B → b|bAB|BBB
X → b
A → a

TCS : UNIT 3 16
S → bA|BB A -> A a / b The production rules S->BB is not in GNF, therefore, we
B → b|bAB|BBB
X → b A->bC substitute B → bC|bABC|b|bAB in production rules S->BB as:
A → a C->aC/ ε S → bA| bCB|bABCB|bB|bABB
B → bC|bABC|b|bAB
Removing Left Recursion B -> BBB in B → b|bAB|BBB
C → BBC|BB
S → bA|BB
X → b
B → bC|bABC
A → a
C → BBC| ε
The production rules C->BB is not in GNF, therefore, we
X → b
substitute B → bC|bABC|b|bAB in production rules C->BB as:
A → a
S → bA| bCB|bABCB|bB|bABB
Removing null production (C-> ε), we get:
B → bC|bABC|b|bAB
S → bA|BB
C → BBC
B → bC|bABC|b|bAB
C → bCB|bABCB|bB|bABB
C → BBC|BB
X → b
X → b
A → a
A → a TCS : UNIT 3 17
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

Greibach Normal Form

TCS : UNIT 3 18

You might also like