Chomsky Normal Form
Continuation
By: Jasmine Peniel Combo
BSCS 2-3
Recall that:
• A CFG is said to be in Chomsky Normal Form
if every production is of one of these two
forms:
1. A -> BC (body is two variables).
2. A -> a (body is a single terminal).
• Theorem: If L is a CFL, then L – {ε} has a CFG
in CNF.
Recall the first steps:
• 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 rules
Step 4: Break right sides longer than two into
a chain of productions with right sides of two
variables
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 SA with 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
Recall:
1. A -> BC (body is 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).
• 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 → A A1 | 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
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
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 -> .
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
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
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

Chomsky Normal Form

  • 1.
    Chomsky Normal Form Continuation By:Jasmine Peniel Combo BSCS 2-3
  • 2.
    Recall that: • ACFG is said to be in Chomsky Normal Form if every production is of one of these two forms: 1. A -> BC (body is two variables). 2. A -> a (body is a single terminal). • Theorem: If L is a CFL, then L – {ε} has a CFG in CNF.
  • 3.
    Recall the firststeps: • 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 rules
  • 4.
    Step 4: Breakright sides longer than two into a chain of productions with right sides of two variables 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 SA with 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 Recall: 1. A -> BC (body is two variables).
  • 5.
    Step 5: Makesure that the productions are in either these two forms: 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 → A A1 | 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
  • 6.
    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
  • 7.
    Example #2 • Fromprevious 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 -> .
  • 8.
    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
  • 9.
    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
  • 10.
    Example #2 Final CNGin 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