Professional Documents
Culture Documents
Chapter Three
Chapter Three
Chapter Three
Normal Forms
a) Chomsky Normal Form (CNF)
CNF is used as a preprocessing step for many algorithms for CFG like CYK,
bottom-up parsers etc.
Cont…
Example – Let us take an example to convert CFG to CNF.
Step1. As start symbol S appears on the RHS, we will create a new production
rule S0->S. Therefore, the grammar will become:
Step 3. In production rule A->aAS |aS and B-> SbS|aAS|aS, terminals a and b
exist on RHS with non-terminates. Removing them from RHS:
S → ASA | aB, A → B | S| ε, B → b | ε
Solution:
(1) Since S appears in R.H.S, we add a new state S0 and S0→S is
added to the production set and it becomes:
S0→S, S→ ASA | aB, A → B | S, B → b | ∈
(2) Now we will remove the null productions: B → ∈ and A → ∈
→ B | S, B → b
Conti…
(3) Now we will remove the unit productions. After removing S
→ S, the production set becomes: S0→S, S→ ASA | aB | a | AS |
SA, A → B | S, B → b
A → B | S, B → b
A→S|b
B→b
Conti…
After removing A→ S, the production set becomes: S0 → ASA |
aB | a | AS | SA, S→ ASA | aB | a | AS | SA
A → b |ASA | aB | a | AS | SA, B → b
(4) Now we will find out more than two variables in the R.H.S
Here, S0→ ASA, S → ASA, A→ ASA violates two Non-
terminals in R.H.S. Hence we will apply step 4 and step 5 to get
the following final production set which is in CNF:
S0→ AX | aB | a | AS | SA
S→ AX | aB | a | AS | SA
A → b |AX | aB | a | AS | SA
B→b
X → SA
Conti…
(5) We have to change the productions S0→ aB, S→ aB, A→ aB
S0→ AX | YB | a | AS | SA
S→ AX | YB | a | AS | SA
A → b A → b |AX | YB | a | AS | SA
B→b
X → SA
Y→a
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 ε).
Note: For a given grammar, there can be more than one GNF. GNF produces the same
language as generated by CFG.
Algorithm to Convert a CFG into Greibach Normal Form
A → a | SA
B→b
X→a
The grammar,
SAB,
A aA/bB/b,
S aAB/bBB/Bb,
A aA/bB/b,
B b
Example 2: S → XA|BB B → b|SB X → b A → aAS G1 is already in CNF
and there is not left recursion, we can skip step 1 and 2 and directly move to
step 3.
The production rule B->SB is not in GNF, therefore, we substitute S -> XA|BB
in production rule B->SB as:
S → XY | Xo | p
X → mX | m
Y → Xn | o
S → mXY | mY | mXC | mC | p
X → mX | m
Y → mXn | mn | o
Pumping Lemma
Pumping lemma for CFG is used to prove that a language is NOT
Context Free.
iii. |vxy| ≤ P
Pumping Lemma
There are two types of pumping Lemmas, which are defined for:
i. |xy| <= n
ii.|y| >=1
Case 1 − vwx has no 2s. Then vx has only 0’s and 1’s. Then uwy,
which would have to be in L, has n 2’s, but fewer than n 0’s or
1’s.