13-Normal Forms For CFG - CNF and GNF-07-03-2024

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

GREIBACH NORMAL FORM

GNF stands for Greibach normal form. A CFG(context free grammar) is in GNF(Greibach
normal form) if all the production rules satisfy one of the following conditions:

• A start symbol generating ε. For example, S → ε.


• A non-terminal generating a terminal. For example, A → a.
• A non-terminal generating a terminal which is followed by any number of non-
terminals. For example, S → aASB.

Algorithm to Convert into Greibach Normal Form,


Step 1. If the start symbol S occurs on some right side, create a new start symbol S1
and a new production S1→ S.
Step 2. Remove useless symbols.
Removing useless symbols from the grammar does not affect the language
generated by the grammar.
Eliminating useless symbol begins with two approaches,

1. Say X is generating if 𝑋 ⇒ 𝑤 for some terminal w. Every terminal is
generating, since w can be that terminal itself, which is derived by zero
steps.

2. Say x is reachable if there is a derivation 𝑆 ⇒ 𝛼𝑋𝛽 for some 𝛼 𝑎𝑛𝑑 𝛽.
Step 3. Remove Null productions or ε - productions.
Basis:
If A → ∈ is a production of G, then A is nullable.
Induction:
If there is a production B → C1C2..Ck , where each Ci is nullable then B
is nullable. Ci must be variable to be nullable, so we only have to
consider productions with all-variable bodies.
Step 4. Remove unit productions.
It is a production of the form A → B , where A and B are the variables. Unit
productions can complicate certain proofs and they introduce extra steps into
derivations. That are, technically need not to be there in CFG.
Step 5. Replace each production A → B1…Bn where n > 2 with A → B1C where C →
B2 …Bn. Repeat this step for all productions having two or more symbols in
the right side.

Dr.C.Sathiya Kumar, Associate Professor, VIT Univerrsity Page 1


Step 6. If the right side of any production is in the form A → aB where a is a terminal
and A, B are non-terminal, then the production is replaced by A → XB and
X → a. Repeat this step for every production which is in the form A → aB.
Step 7. Change the Name of Non-terminal Symbols in terms of 𝐴𝑖 in ascending order
of i
Step 8. Alter the rules of G, so that the non terminals are in ascending order such that
if the production of the form.
If 𝑨𝒊 → 𝑨𝒋 𝑿 is a rule then, 𝒊 < 𝒋 and should never be 𝒊 ≥ 𝒋
Step 9. Remove for left recursion if any
𝑖) 𝐼𝑓 𝑨 → 𝑨𝛼 | 𝛽, 𝑡ℎ𝑒𝑛 𝑡ℎ𝑖𝑠 𝑖𝑠 𝑐ℎ𝑎𝑛𝑔𝑒𝑑 𝑖𝑛𝑡𝑜
𝐴 → 𝛽𝑍, 𝑍 → 𝛼𝑍/𝜀
Or
𝐴 → 𝛽𝑍/𝛽, 𝑍 → 𝛼𝑍/𝛼
𝑖𝑖) 𝐼𝑓 𝐴 → 𝐴𝛼1 | 𝐴𝛼2 | … 𝐴𝛼𝑚 | 𝛽1 /𝛽2 / …/𝛽𝑛 , 𝑡ℎ𝑒𝑛 𝑡ℎ𝑖𝑠 𝑖𝑠 𝑐ℎ𝑎𝑛𝑔𝑒𝑑 𝑖𝑛𝑡𝑜
𝐴 → 𝛽1 𝑍/𝛽2 𝑍/ …/𝛽𝑛 𝑍, 𝑍 → 𝛼1 𝑍/𝛼2 𝑍/ …/𝛼𝑚 𝑍/𝜀
Or
𝐴 → 𝛽1 𝑍/𝛽2 𝑍/ …/𝛽𝑛 𝑍/𝛽1 /𝛽2 / …/𝛽𝑛 , 𝑍
→ 𝛼1 𝑍/𝛼2 𝑍/ …/𝛼𝑚 𝑍/𝛼1 /𝛼2 / …/𝛼𝑚

Problem
Convert the following CFG into CNF
𝑆 → 𝑋𝐴 | 𝐵𝐵, 𝐵 → 𝑏 | 𝑆𝐵, 𝑋 → 𝑏, 𝐴 → 𝑎
Solution
(1) If the given grammar is not in CNF, apply step 1 to step 6 to convert the
grammar in CNF.
(2) Change the Name of Non-terminal Symbols in terms of 𝐴𝑖 in ascending order of i
𝑆 → 𝑋𝐴 | 𝐵𝐵, 𝑠𝑡𝑎𝑟𝑡𝑖𝑛𝑔 𝑓𝑟𝑜𝑚 𝑠𝑡𝑎𝑟𝑡 𝑠𝑦𝑚𝑏𝑜𝑙,
𝑡ℎ𝑒𝑛 𝑛𝑎𝑚𝑒 𝑎𝑙𝑙 𝑡ℎ𝑒 𝑛𝑜𝑛𝑡𝑒𝑟𝑚𝑖𝑛𝑎𝑙𝑠 𝑓𝑟𝑜𝑚 𝑙𝑒𝑓𝑡 𝑡𝑜 𝑟𝑖𝑔ℎ𝑡, 𝑙𝑖𝑘𝑒 𝑓𝑖𝑟𝑠𝑡 𝑆, 𝑡ℎ𝑒𝑛 𝑋, 𝐴, 𝐵
𝐵 → 𝑏 | 𝑆𝐵, 𝐵, 𝑆 𝑎𝑙𝑟𝑒𝑎𝑑𝑦 𝑛𝑎𝑚𝑒𝑑 𝑠𝑜 𝑛𝑜 𝑛𝑒𝑒𝑑 𝑡𝑜 𝑛𝑎𝑚𝑒 𝑖𝑡 𝑎𝑔𝑎𝑖𝑛
𝑋 → 𝑏 , 𝑋 𝑎𝑙𝑟𝑒𝑎𝑑𝑦 𝑛𝑎𝑚𝑒𝑑 𝑠𝑜 𝑛𝑜 𝑛𝑒𝑒𝑑 𝑡𝑜 𝑛𝑎𝑚𝑒 𝑖𝑡 𝑎𝑔𝑎𝑖𝑛
𝐴 → 𝑎 , 𝐴 𝑎𝑙𝑟𝑒𝑎𝑑𝑦 𝑛𝑎𝑚𝑒𝑑 𝑠𝑜 𝑛𝑜 𝑛𝑒𝑒𝑑 𝑡𝑜 𝑛𝑎𝑚𝑒 𝑖𝑡 𝑎𝑔𝑎𝑖𝑛

S with 𝐴1

Dr.C.Sathiya Kumar, Associate Professor, VIT Univerrsity Page 2


X with 𝐴2
A with 𝐴3
B with 𝐴4
Then the given grammar becomes
𝐴1 → 𝐴2 𝐴3 /𝐴4 𝐴4
𝐴4 → 𝑏 / 𝐴1 𝐴4
𝐴2 → 𝑏
𝐴3 → 𝑎
(3) Alter the rules of G, so that the non terminals are in ascending order such that if the
production of the form.
If 𝑨𝒊 → 𝑨𝒋 𝑿 is a rule then, 𝒊 < 𝒋 and should never be 𝒊 ≥ 𝒋
𝑨𝟏 → 𝑨𝟐 𝐴3 /𝑨𝟒 𝐴4
1 < 2, 1 < 4 𝑔𝑟𝑎𝑚𝑚𝑎𝑟 𝑠𝑎𝑡𝑖𝑠𝑓𝑖𝑒𝑠 𝑡ℎ𝑒 𝑟𝑢𝑙𝑒
𝑨𝟒 → 𝑏 / 𝑨𝟏 𝐴4
4! < 1, 𝑢𝑠𝑒 𝑠𝑢𝑏𝑠𝑡𝑖𝑡𝑢𝑡𝑖𝑜𝑛 𝑟𝑢𝑙𝑒 𝑡𝑜 𝑟𝑒𝑝𝑙𝑎𝑐𝑒 𝑡ℎ𝑒 𝐴1 𝑝𝑟𝑜𝑑𝑢𝑐𝑡𝑖𝑜𝑛 𝑏𝑦 𝑖𝑡𝑠
𝑟𝑒𝑠𝑝𝑒𝑐𝑡𝑖𝑣𝑒 𝑝𝑟𝑜𝑑𝑢𝑐𝑡𝑖𝑜𝑛 𝑟𝑢𝑙𝑒𝑠, 𝑡ℎ𝑒𝑛 𝑨𝟐 𝑐ℎ𝑎𝑛𝑔𝑒𝑠 𝑖𝑛𝑡𝑜
𝐴4 → 𝑏 / 𝑨𝟐 𝑨𝟑 𝐴4 /𝑨𝟒 𝑨𝟒 𝐴4
Here 𝑨𝟒 → 𝑨𝟐 𝐴3 𝐴4 , 𝑖 > 𝑗, which is not allowed as per rule, using substitution rule
replace A2 by its production then the production is
𝐴4 → 𝑏 / 𝒃𝐴3 𝐴4 /𝐴4 𝐴4 𝐴4
𝑁𝑜𝑤 𝑖𝑛 𝑨𝟒 → 𝑏 / 𝑏𝐴3 𝐴4 /𝑨𝟒 𝐴4 𝐴4
Here 𝐴4 → 𝐴4 𝐴4 𝐴4
𝐴4 → 𝐴4 , 𝑖 = 𝑗, 𝑤ℎ𝑖𝑐ℎ 𝑖𝑠 𝑛𝑜𝑡 𝑎𝑙𝑙𝑜𝑤𝑒𝑑 𝑎𝑠 𝑝𝑒𝑟 𝑎𝑙𝑡𝑒𝑟𝑎𝑡𝑖𝑜𝑛 𝑟𝑢𝑙𝑒.
𝑇ℎ𝑖𝑠 𝑖𝑠 𝑐𝑎𝑙𝑙𝑒𝑑 𝑎𝑠 𝑙𝑒𝑓𝑡 𝑟𝑒𝑐𝑢𝑟𝑠𝑖𝑜𝑛.
Eliminate left recursion as mentioned in Step 9. Then the grammar changed into
𝐴4 → 𝑏 / 𝑏𝐴3 𝐴4 /𝐴4 𝐴4 𝐴4

𝐴4 → 𝑏𝑍 / 𝑏𝐴3 𝐴4 𝑍
𝑍 → 𝐴4 𝐴4 𝑍/𝜀
Or
𝑨𝟒→ 𝑏𝑍 / 𝒃𝑨𝟑𝑨𝟒𝒁/𝑏 / 𝒃𝑨𝟑𝑨𝟒𝐴4 → 𝑏𝑍 / 𝑏𝐴3 𝐴4 𝑍/𝑏/ 𝑏𝐴3 𝐴4
𝑍 → 𝐴4 𝐴4 𝑍/𝐴4 𝐴4

Dr.C.Sathiya Kumar, Associate Professor, VIT Univerrsity Page 3


𝐴2 → 𝑏
𝐴3 → 𝑎
(4) After altering the order of the non-terminal the grammar is
𝐴1 → 𝐴2 𝐴3 /𝐴4 𝐴4
𝐴4 → 𝑏𝑍 / 𝑏𝐴3 𝐴4 𝑍/𝑏/ 𝑏𝐴3 𝐴4
𝑍 → 𝐴4 𝐴4 𝑍/𝐴4 𝐴4
𝐴2 → 𝑏
𝐴3 → 𝑎
(5) Now in 𝑨𝟏 and Z the productions are not in GNF, since it starts with Non-terminal ,
substitute those non terminals by its corresponding productions then the grammar is
𝐴1 → 𝒃𝐴3 / 𝒃𝒁𝐴4 / 𝒃𝑨𝟑 𝑨𝟒 𝒁𝐴4 /𝒃𝐴4 / 𝒃𝑨𝟑 𝑨𝟒 𝐴4
𝐴4 → 𝑏𝑍 / 𝑏𝐴3 𝐴4 𝑍/𝑏/ 𝑏𝐴3 𝐴4
𝑍 → 𝒃𝒁𝐴4 𝑍 / 𝒃𝑨𝟑 𝑨𝟒 𝒁𝐴4 𝑍/𝒃𝐴4 𝑍/ 𝒃𝑨𝟑 𝑨𝟒 𝐴4 𝑍/𝒃𝒁𝐴4 / 𝒃𝑨𝟑 𝑨𝟒 𝒁𝐴4 /𝒃𝐴4 / 𝒃𝑨𝟑 𝑨𝟒 𝐴4
𝐴2 → 𝑏
𝐴3 → 𝑎

Dr.C.Sathiya Kumar, Associate Professor, VIT Univerrsity Page 4

You might also like