Presentasi sedang didownload. Silahkan tunggu

Presentasi sedang didownload. Silahkan tunggu

Chomsky Normal Form (CNF) & Greibach Normal Form

Presentasi serupa


Presentasi berjudul: "Chomsky Normal Form (CNF) & Greibach Normal Form"β€” Transcript presentasi:

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 𝛽


Download ppt "Chomsky Normal Form (CNF) & Greibach Normal Form"

Presentasi serupa


Iklan oleh Google