IF-UTAMA 1 Chomsky Normal Form (CNF) & Greibach Normal Form (GNF) Dosen Pembina Danang Junaedi Language Is Cool Language: A protocol for the transmission of concepts and intentions between humans Documentation is not available Documentation does not really work Learned through exposure and use Significant amount of internal structure, redundancy, and consistency Who makes language? Kids. Adults coin words here and there, but when they re forced to invent a common language to get things done, it s called a Pidgin, and it s terrible The kids hear it, and invent a Creole a merged language of significantly greater accuracy and depth Children make languages Adults make working languages Programmers make barely working languages IF-UTAMA 2 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 Automaton 0 Partially Computable Unrestricted DTM - NTM 1 Context Sensitive Context Sensitive Linearly Bounded Automaton 2 Context Free Context Free NPDA 3 Regular right regular, left regular DFA, NFA Type 3 Type 2 Type 1 Type 0 You don t have to know this IF-UTAMA 3 IF-UTAMA 4 IF - UTAMA 1
The Chomsky Hierarchy Chomsky Normal Form (CNF) Partially Computable Languages Computable Languages Context Free Languages Regular Languages {M, H(<M>)} {a n b n c n, n 0} {a n b n, n 0} {a m b n, m,n 0} A CFG is in Chomsky Normal Form if all its productions are of the form: A BC or A a where A, B, C V and a T. Also, S ε may be one of the productions. IF-UTAMA 5 IF-UTAMA 6 Examples of CNF Example 1: Example 2: S AB A BC CC a B CB b C c S AB BC AC ε A BC a B AC b C AB c Examples of CNF S AS S a A SA A b Chomsky Normal Form S AS S AAS A SA A aa Not Chomsky Normal Form IF-UTAMA 7 IF-UTAMA 8 IF - UTAMA 2
CNF Is that all Context Free Grammars can be expressed in Chomsky Normal Form? Consider the following simple grammar: A ca a B ABC b C c How to convert this grammar to CNF? Algorithm for Converting to CNF Step (1) Eliminate ε-productions and unit productions Step (2) For remaining production β not form A BC nor of form A a, replace occurrences of terminals a, b, c, in β with new nonterminal representatives C a, C b, C c, and then add new productions C a a C b b C c c Step (3) If right-hand side of any production contains three or more nonterminals, then decompose this production into a series of productions the right-hand sides of which consist of exactly two nonterminals. IF-UTAMA 9 IF-UTAMA 10 Power of Chomsky Normal Every CFG can be rewritten in Chomsky Normal Form Step 1: For any production rule with more than one terminal on the right, substitute all with variables. Step 2: Substitute more variables in order to make the variable strings shorter. IF-UTAMA 11 Conversion into CNF Step 1: Convert every production into either: A B 1 B 2 B n or A a e.g. A bcdef becomes: A BCDEF B b E e Step 2: Convert production of the form A B 1 B 2 B n into A C 1 C 2 : e.g. A BCDEF becomes: A BX X CY Y DZ Z EF IF-UTAMA 12 IF - UTAMA 3
Example: Convert to CNF Class Discussion Original: S -> ABa A -> aab B -> Ac Step 1: S -> ABB a A -> B a B a B b B -> AB c B a -> a B b -> b B c -> c Step 2: S -> AD 1 D ->BB 1 a A -> B a D 2 D 2 ->B a B b B -> AB c B a -> a B b -> b B c -> c Convert the following CFG into Chomsky Normal Form: S ε S ABBA B bcb A a C c IF-UTAMA 13 IF-UTAMA 14 Greibach Normal Form (GNF) A CFG is in Greibach Normal Form if all its productions are of the form: A a where A V, a T and V*. Also, S ε may be one of the productions. Advantages: Every derivation of a string s contains s 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. Examples of GNF Example 1:S aabc A aa a B bb b C cc c Example 2:S bab ε A abaa aaab a B babb bbba b IF-UTAMA 15 IF-UTAMA 16 IF - UTAMA 4
Conversion Algorithm Step (1) Find equivalent grammar G in CNF Step (2) Order nonterminals of G from X 1 to X n. 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 a, where X nonterminal, a is a terminal, and (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. IF-UTAMA 17 IF-UTAMA 18 Removal of Left Recursion We want to remove all the productions in the form of: A A This is called a left recursion. Replace: X 1 2 n X Xβ 1 Xβ 2 Xβ n by: X 1 2 n X 1 Z 2 Z n Z Z β 1 β 2 β n Z β 1 Z β 2 Z β n Z where X V i, β i (V T)* such that i does not start with X IF-UTAMA 19 Tahapan Penghilangan Rekursif Kiri [5] 1. Pisahkan aturan produksi yang rekursif kiri dan tidak rekursif kiri Aturan produksi yang rekursif kiri X Xβ 1 Xβ 2 Xβ n Kita dapatkan simbol β 1, β 2,,β n Aturan produksi yang tidak rekursif kiri X 1 2 n Kita dapatkan simbol 1, 2,, n 2. Lakukan penggantian aturan produksi yang rekursif kiri menjadi X 1 2 n X 1 Z 2 Z n Z Z β 1 β 2 β n Z β 1 Z β 2 Z β n Z dimana simbol Z adalah variabel non terminal baru yang kita buat sesuai banyaknya variablel pada aturan produksi yang rekursif kiri IF-UTAMA 20 IF - UTAMA 5
Contoh Penghilangan Rekursif Kiri Hilangkan rekursif kiri untuk aturan produksi berikut: E E+T E T Solusi: 1. Pisahkan aturan produksi yang rekursif kiri dan tidak rekursif kiri Aturan produksi yang rekursif kiri E E+T Kita dapatkan simbol 1 +T Aturan produksi yang tidak rekursif kiri E T Kita dapatkan simbol β 1 T 2. Lakukan penggantian aturan produksi yang rekursif kiri menjadi E T Aturan produksi baru yang E TZ dihasilkan dari pengilangan Z +T aturan produksi yang rekursif kiri Z +TZ Studi Kasus [5] Hilangkan rekursif kiri dari aturan-aturan produksi berikut ini: 1. S Sab asc dd ff Sbd 2. S Sab Sb ca A Aa a bd 3. S Sa aac c ε A Ab ba IF-UTAMA 21 IF-UTAMA 22 Pembentukan GNF (1) [5] Pembentukan GNF dengan Subtitusi(2) [5] 1. Aturan Produksi telah dalam bentuk CNF dan tidak menghasilkanε 2. Tentukan urutan simbol-simbol variabel yang ada dalam tata bahasa. Misalkan terdapat n variabel non terminal dengan urutan 1, 2,, n 3. Berdasarkan urutan simbol tersebut seluruh aturan produksi dapat ditulis dalam bentuk h i λ (dimana h i) a. Jika h < I, aturan produksi sudah benar b. Jika h>i, lakukan subtitusi berulang (ganti I dengan variabel yang ada di ruas kanannya) sampai diperoleh bentuk h p λ (dimana h p) Jika h<p, aturan produksi sudah benar Jika h=p, lakukan penghilangan rekursif kiri sisipkan simbol baru hasil penghilangan rekrusif Jika step 2 dan 3 sudah dikerjakan akan menghasilkan aturan produksi dengan urutan yang benar yaitu h i λ (h<i) serta aturan produksi yang lain h aλ (a simbol terminal) dan β x λ (simbol baru dari hasil penghilangan rekursif kiri) 4. Bentuk normal greibach diperoleh dengan cara melakukan subtitusi mundur untuk simbol mulai dari n sampai 1 begitu juga untuk variabel β Contoh: Diketahui Grammar G=(V n,v t,s,p), dimana V n : {S,A,B,C,D} V t : {a,b,d} S: {S} P: { S CA A a d B b C DD D AB } Ubah ke dalam bentuk normal greibach kiri di posisi setelah simbol h IF-UTAMA 23 IF-UTAMA 24 IF - UTAMA 6
Pembentukan GNF dengan Subtitusi(3) [5] Solusi: 2. Tentukan urutan simbol-simbol variabel yang ada dalam tata bahasa. Misalkan terdapat n variabel non terminal dengan urutan 1, 2,, n :{ S A B C D } V n 1 2 3 4 5 Aturan produksi menjadi 1 4 2 2 a d 3 b 4 5 5 5 2 3 3. Cek apakah berdasarkan urutan simbol tersebut seluruh aturan produksi dalam bentuk h i λ dimana (h i) 1 4 2 (sudah benar karena 1<4) 4 5 5 (sudah benar karena 4<5) 5 2 3 (salah karena 5>2) =>subtitusi berulang (ganti 2 dengan variabel yang ada di ruas kanannya) sampai diperoleh bentuk h p λ (dimana h p) IF-UTAMA 25 Pembentukan GNF dengan Subtitusi(4) [5] 5 2 3 menjadi 5 a 3 d 3 sehingga menghasilkan aturan produksi dengan urutan yang benar yaitu: 1 4 2 2 a d 3 b 4 5 5 5 a 3 d 3 4. Subtitusi mundur 5 a 3 d 3 4 a 3 5 d 3 5 1 a 3 5 2 d 3 5 2 Ubah variabel 1,, n ke variabel semula S A B Menjadi S abda dbda A a d B b C abd dbd D ab db 1 2 3 4 5 IF-UTAMA 26 C D Studi Kasus Ubah aturan produksi berikut ini ke dalam bentuk normal greibach: 1. S c Sa SbA A c 2. S AA d A SS b 3. A ABC B CA b C AB a 4. S asb ab SS References 1. -, Simplification of CFG, http://www.geocities. com/ parouyr_sevak/03l11.ppt,tanggal Akses 16 Mei 2009 2. Dr. William Thacker s, Context-Free Grammars: Chapter 11 Normal Formal,http://cba.winthrop. edu/thackerw/csci371/powerpoint%20slides/ch11bnormalfor ms.ppt, Tanggal Akses 16 Mei 2009 3. -, Context-Free Languages and Pushdown-Stack Automata : An Intermediate-Strength Model of Computation, http://home.manhattan.edu/~gregory.taylor/thcomp/slides/chapt er10.ppt, Tanggal Akses 16 Mei 2009 4. -, Chomsky Hierarchy Language Operations and Properties, http://wwwcs.ccny.cuny.edu/~vmitsou/extra/chomskyhier.ppt, Tanggal Akses 16 Mei 2009 5. Firrar Utdirartatmo, Teori Bahasa dan Otomata, Graha Ilmu, Yogyakarta, 2005 IF-UTAMA 27 IF-UTAMA 28 IF - UTAMA 7