Chomsky Normal Form (CNF) & Greibach Normal Form
Normal Forms Normal forms are special types of context-free languages Having more restricted (but still powerful) grammar forms make important algorithms efficient Two widely-known forms: Chomsky Normal and Greibach Normal
The Chomsky Hierarchy Type Language Grammar Autoamtion Partially Computabel Unrestricted DTM, NTM 1 Context Sensitive Linearly Bounded Automation 2 Context Free NPDA 3 Regular Right regular, left regular DFA, NFA Type 3 ⊂𝑇𝑦𝑝𝑒 2⊂𝑇𝑦𝑝𝑒 1⊂𝑇𝑦𝑝𝑒 0
The Chomsky Hierarchy Partially Computable Languanges {M, H(<𝑀>)} Computable Languanges { 𝑎 𝑛 𝑏 𝑛 𝑐 𝑛 , n ≥0 Context Free Languanges { 𝑎 𝑛 𝑏 𝑛 , n ≥0 Regular Languanges { 𝑎 𝑚 𝑏 𝑚 , m,n ≥0
Chomsky Normal Form A CFG is in Chomsky Normal Form if all its productions are of the form: A → BC or A → a Where A, B, C ∈𝑉 and a ∈ T. also, S → ε 𝑚𝑎𝑦 𝑏𝑒 𝑜𝑛𝑒 𝑜𝑓 𝑡ℎ𝑒 𝑝𝑟𝑜𝑑𝑢𝑐𝑡𝑖𝑜𝑛𝑠 A →𝐵𝐶 𝑜𝑟 𝐴 → a
Example of CNF Example 1: S → AB A → BC ׀ 𝐶𝐶 ׀ a B → CB ׀b C → c S → AB BC ׀ 𝐴𝐶 ׀ ε A → BC ׀ a B → AC ׀b C → AB ׀c
Examples of CNF S →𝐴𝑆 S →𝑎 A →𝑆𝐴 A →𝑏 Not Chomsky Normal Form: S →𝐴𝐴𝑆
CNF Is all that all Context Free Grammars can be expressed in Chomsky Norma Form? Consider the following simple grammar: A →𝑐𝐴 ׀a B →𝐴𝐵𝐶 ׀b C →𝑐 How to convert this grammar to CNF?
Algorithm for converting to CNF Step (1) Eliminate 𝜀−𝑝𝑟𝑜𝑑𝑢𝑐𝑡𝑖𝑜𝑛𝑠 and unit productions Step (2) for remaining production 𝛼→ 𝛽 not form A → BC or not form A → a, replace occurences od terminals a, b, c, …. in 𝛽 with new nonterminal representatives Ca, Cb, Cc … and then add new productions Ca → a Cb → b Cc → c ….. Step (3) if right-hand side of any production contains three or more nonterinal, then decompose this production into a series of productions the right-hand sides of which consists of exactly two nonterminals
Examples: Convert to CNF Step 1 S → ABBa A → BaBaBb B → Abc Ba → a Bb → b Bc → c Step 2: S → AD1 D1 → Bba A → BaD2 D2 → BaBb B → ABc
Greibach Normal Form (GNF) A CFG is in Greibach Normal Form if all its productions are pf the form”: A → a𝛼 where A ∈𝑉, 𝑎 ∈ T and 𝛼 ∈ 𝑣 ∗ . Also, S → 𝜀 𝑚𝑎𝑦 𝑏𝑒 𝑜𝑛𝑒 𝑜𝑓 𝑡ℎ𝑒 𝑝𝑟𝑜𝑑𝑢𝑐𝑡𝑖𝑜𝑛𝑠 Advantages: - Every derivation of a string s contains 𝑠 rule applications - Greibach normal form grammars can easily be converted to pushdown automata with no- 𝜀 transitions. This is useful because such PDAs are guaranteed to halt
Conversio Algorithm Step (1) Find equivalent grammar G in CNF Step (2) order nonterminals of G’ from X1 to Xn Step (3) work upward through nonterminals of G’ making replacements so as to ensure that all ultimately ascending Step (4) work downward through nonterminals, making replacements so as to ensure that all productions ultimately in GNF, i.e,, of form X →𝑎𝛼, 𝑤ℎ𝑒𝑟𝑒 𝑋 𝑛𝑜𝑛𝑡𝑒𝑟𝑚𝑖𝑛𝑎𝑙, 𝑎 𝑖𝑠 𝑎 𝑡𝑒𝑟𝑚𝑖𝑛𝑎𝑙 𝑎𝑛𝑑 𝛼 (possibly empty) string of non-terminals
GNF Is that all context free grammars can be expressed in Greibach Normal Form? Transformation can be done by a combination of substitution and removal of left recursion
Removal of Left Recursion We want to remove all the productions in the form of: A →𝐴𝛼 This is called a left recursion. Replace: X → 𝛼 1 ׀ 𝛼 2 ׀……. ׀ 𝛼 𝑛 where x 𝜖 𝑉, 𝛼 1 , 𝛽 1 𝜖 ( 𝑉 ∪𝑇) ∗ , such that 𝛼 1 does not start with X X → 𝑋𝛽 1 ׀ 𝑋𝛽 2 ׀……. ׀ 𝑋𝛽 𝑛 by: X → 𝛼 1 ׀ 𝛼 2 ׀……. ׀ 𝛼 𝑛 X → 𝛼 1 Z ׀ 𝛼 2 Z ׀……. ׀ 𝛼 𝑛 Z Z → 𝛽 1 ׀ 𝛽 2 ׀……. ׀ 𝛽 𝑛 Z → 𝛽 1 Z ׀ 𝛽 2 Z ׀……. ׀ 𝛽 𝑛 Z
Pembentukan GNF Aturan produksi telah dalam bentuk CNF dan tidak menghasilkan 𝜀 Tentukan urutan simbol-simbol variabel yang ada dalam tata bahasa, misalkan terdapat “n” variabel non terminal dengan urutan 𝛼 1 , 𝛼 2 , ……, 𝛼 𝑛 Berdasarkan urutan simbol tersebut selurug aturan produksi dapat ditulis dalam bentuk 𝛼 ℎ → 𝛼 𝑖 𝛾 ( dimana h ≤𝑖) a. Jika h <𝐼, 𝑎𝑡𝑢𝑟𝑎𝑛 𝑝𝑟𝑜𝑑𝑢𝑘𝑠𝑖 𝑠𝑢𝑑𝑎ℎ 𝑏𝑒𝑛𝑎𝑟 b. Jika h >𝐼, 𝑙𝑎𝑘𝑢𝑘𝑎𝑛 𝑠𝑢𝑏𝑠𝑡𝑖𝑡𝑢𝑠𝑖 𝑏𝑒𝑟𝑢𝑙𝑎𝑛𝑔 ( 𝑔𝑎𝑛𝑡𝑖 𝛼 1 dengan variabel yang ada di ruas kanannya) sampai diperoleh bentuk 𝛼 ℎ → 𝛼 𝑝 𝛾 (dimana h ≤𝑝) Jika h < p, aturan produksi sudah benar Jika h=p, lakukan penghilangan rekursif kiri → sisiplan simbol baru hasil penghilangan rekusif kiri di posisi setelah simbol 𝛼 ℎ
Jika step 2 dan 3 sudah dikerjakan akan menghasilkan aturan produksi dengan urutan yang benar yaitu 𝛼 ℎ → 𝛼𝛾 (a simbol terminal) dan 𝛽𝑥 → 𝛾 (simbol baru dari hasil penghilangan rekursif kiri) 4. Bentuk normal greibach diperoleh dengan cara melakukan substitusi mundur untuk simbol 𝛼 mulai dari 𝛼 𝑛 sampai 𝛼 1 begitu juga untuk variabel 𝛽