Upload presentasi
Presentasi sedang didownload. Silahkan tunggu
Diterbitkan olehDale OβNealβ Telah diubah "6 tahun yang lalu
1
Chomsky Normal Form (CNF) & Greibach Normal Form
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
3
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
4
The Chomsky Hierarchy Partially Computable Languanges
{M, H(<π>)} Computable Languanges { π π π π π π , n β₯0 Context Free Languanges { π π π π , n β₯0 Regular Languanges { π π π π , m,n β₯0
5
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
6
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
7
Examples of CNF S βπ΄π S βπ A βππ΄ A βπ Not Chomsky Normal Form: S βπ΄π΄π
8
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?
9
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
10
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
11
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
12
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
13
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
14
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
15
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 πΌ β
16
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 π½
Presentasi serupa
© 2024 SlidePlayer.info Inc.
All rights reserved.