UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
CFG to CNF.pptx
1. • Step 1
Make sure the start symbol (S) doesn't appear on right hand
side. If so, add new Start symbol.
Step 2
Remove ε-productions.
Step 3
Remove unit productions.
Step 4: Break right sides longer than two into a chain of
productions with right sides of two variables.
Step 5: Make sure that the productions are in either these two
forms:
1. A -> BC (body is two variables).
2. A -> a (body is a single terminal).
2. So with the continuation of the previous
example,
• S0 → ASA | aB | a | SA |AS
• S → ASA | aB | a | SA | AS
• A → b | ASA | aB | a | SA | AS
• B → b
Introduce new variables.
So for S0 → ASA & S → ASA , we replace SAwith a new
variable, A1 therefore:
• S0 → ASA | aB | a | SA |AS
• S → ASA | aB | a | SA | AS
• A → b | ASA | aB | a | SA | AS
• B → b
S0 → A A1 | aB | a | SA | AS
S → A A1 | aB | a | SA |AS
A → b | A A1 | aB | a | SA |AS
A1 → SA
B → b
Recal
l:
1. A -> BC (body is
two
variables).
3. 1. A -> BC (body is two variables).
2. A -> a (body is a single terminal).
• S0 → A A1 | aB | a | SA |AS
• S → A A1 | aB | a | SA |AS
• A → b | A A1 | aB | a | SA |AS
• A1 → SA
• B → b
So we replace right hand sides with the wrong form by
introducing a new variable just like the previous step. In this
example, A2
• S0 → A A1 | aB | a | SA |AS
• S → AA1 | aB | a | SA | AS
• A → b | A A1 | aB | a | SA |AS
• A1 → SA
• B → b
S0 → A A1 | A2 B| a | SA |AS
S → A A1 | A2 B | a | SA |AS
A → b | A A1 | A2 B | a | SA | AS
A1 → SA
A2 → a
B → b
4. Example
#2:
• S → aXbX
• X → aY | bY |
ε
• Y → X | c
Remove ε-productions.
Then since the right hand
of S contains X,
X → ε
S → aXbX
Consider all
cases,
Case 1: S → a ε bX
S → abX
Case 2: S → aXb ε
S → aXb
Case 3: S → a ε b ε
S → ab
The variable X is nullable
and therefore, also Y so,
X →
ε Y
→ X
Y →
ε
X → aY | bY
X → a | b add this
new production to X
5. Example
#2
• From previous step we
obtain, S → aXbX | abX |
aXb | ab
X → aY | bY | a |
b Y → X | c
Now we eliminate unit productions such
as
Y → X so,
S → aXbX | abX | aXb |
ab X → aY | bY | a | b
Y → aY | bY | a | b | c
Recall that:
If A =>* B by a series of
unit productions, and B -
> is a non-unit-
production, then add
production A -> .
6. Example
#2 S → aXbX | abX | aXb |
ab
X → aY | bY | a | b
Y → aY | bY | a | b | c
Now we replace longer productions with shorter
ones
by adding new
variables.
D →
aX E
→ bX
New grammar:
S → DE | aE |Db|
ab X → aY | bY |
a | b
Y → aY | bY | a | b |
c D → aX
E → bX
7. Example
#2 S → DE | aE |Db| ab
X → aY | bY | a | b
Y → aY | bY | a | b |
c D → aX
E → bX
Then we correct the productions with wrong forms
and introduce new variables F & G:
F →
a G
→ b
8. Example #2
Final CNG in Chomsky Normal
Form: S → DE | FE |DF| FG
X → FY | GY | a | b
Y → FY | GY | a | b |
c D → FX
E → GX
F →
a G
→ b