Chomsky Normal Form (CNF) &
Greibach Normal Form (GNF)
Dosen Pembina Danang Junaedi
IF-UTAMA 2
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
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
0
Type
1
Type
2
Type
3
Type
⊂
⊂
⊂
You don’t have to know thisIF-UTAMA 5
The Chomsky Hierarchy
Context Free Languages Computable Languages Partially Computable Languages
{anbn, n ≥ 0} Regular Languages {anbncn, n ≥ 0} {ambn, m,n ≥ 0} {M, H(<M>)} IF-UTAMA 6
Chomsky Normal Form (CNF)
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.
Examples of CNF
Example 1:
S
→
AB
A
→
BC | CC | a
B
→
CB | b
C
→
c
Example 2:
S
→
AB |
BC | AC |
ε
A
→
BC | a
B
→
AC | b
C
→
AB | c
Examples of CNF
b
A
SA
A
a
S
AS
S
→
→
→
→
Not Chomsky
Normal Form
aa
A
SA
A
AAS
S
AS
S
→
→
→
→
Chomsky
Normal Form
IF-UTAMA 9
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?
CNF
IF-UTAMA 10
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.
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.
Step 1: Convert every production into either:
A
→
B
1B
2…
B
nor
A
→
a
e.g. A
→
bCDeF becomes:
A
→
BCDEF
B
→
b
E
→
e
Step 2: Convert production of the form A
→
B
1B
2…
B
ninto A
→
C
1C
2:
e.g. A
→
BCDEF becomes:
A
→
BX
X
→
CY
Y
→
DZ
Z
→
EF
Conversion into CNF
IF-UTAMA 13
Example: Convert to CNF
Original:
S -> ABa
A -> aab
B -> Ac
Step 1:
S -> ABB
aA -> B
aB
aB
bB -> AB
cB
a-> a
B
b-> b
B
c-> c
Step 2:
S -> AD
1D
1->BB
aA -> B
aD
2D
2->B
aB
bB -> AB
cB
a-> a
B
b-> b
B
c-> c
IF-UTAMA 14Convert the following CFG into Chomsky
Normal Form:
S
→ ε
S
→
ABBA
B
→
bCb
A
→
a
C
→
c
Class Discussion
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 17
Conversion Algorithm
•
Step (1)
Find equivalent grammar
G´
in CNF
•
Step (2)
Order nonterminals of
G´
from
X
1to
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.
IF-UTAMA 18
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.
GNF
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|
…
|
α
nX
→
X
β
1| X
β
2|
…
| X
β
nby:
X
→ α
1|
α
2|
…
|
α
nX
→ α
1Z |
α
2Z |
…
|
α
nZ
Z
→ β
1|
β
2|
…
|
β
nZ
→ β
1Z |
β
2Z |
…
|
β
nZ
where X
∈
V
α
i,
β
i∈
(V
∪
T)*
such that
α
idoes
not start with X
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 → α1Z | α2Z | …| αnZ Z → β1| β2| …| βn Z → β1Z | β2Z | …| βnZ
dimana simbol Z adalah variabel non terminal baru yang kita buat sesuai banyaknya variablel pada aturan produksi yang rekursif kiri
IF-UTAMA 21 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
E →TZ Z →+T Z →+TZ
Contoh Penghilangan Rekursif Kiri
Aturan produksi baru yang dihasilkan dari pengilangan aturan produksi yang rekursif kiri
IF-UTAMA 22
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
Pembentukan GNF (1)
[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 kiri di posisi setelah simbolαh
Pembentukan GNF dengan Subtitusi(2)
[5]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α1begitu juga untuk variabelβ
Contoh:
Diketahui Grammar G=(Vn,Vt,S,P), dimana Vn: {S,A,B,C,D}
Vt: {a,b,d} S: {S}
P: { SCA Aa|d Bb CDD DAB }
IF-UTAMA 25
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
Aturan produksi menjadi
α1α4α2 α2a | d α3b
α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) 5 4 3 2 1 } { : α α α α α ↓ ↓ ↓ ↓ ↓ D C B A S Vn IF-UTAMA 26
Pembentukan GNF dengan Subtitusi(4)
[5]α5α2α3menjadiα5aα3| dα3
sehingga menghasilkan aturan produksi dengan urutan yang benar yaitu: α1α4α2 α2a | d α3b α4α5α5 α5aα3| dα3 4. Subtitusi mundur α5aα3| dα3 α4aα3α5 | dα3α5 α1aα3α5α2 | dα3α5 α2
Ubah variabelα1,…, αnke variabel semula
Menjadi SaBDA | dBDA Aa | d B b C aBD | dBD D aB | dB D C B A S ↓ ↓ ↓ ↓ ↓ 5 4 3 2 1 α α α α α
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
Studi Kasus
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://www-cs.ccny.cuny.edu/~vmitsou/extra/Chomskyhier.ppt, Tanggal Akses 16 Mei 2009
5. Firrar Utdirartatmo, “Teori Bahasa dan Otomata”, Graha Ilmu, Yogyakarta, 2005