Chomsky Normal Form.

Slides:



Advertisements
Similar presentations
Context free languages 1. Equivalence of context free grammars 2. Normal forms.
Advertisements

Closure Properties of CFL's
101 The Cocke-Kasami-Younger Algorithm An example of bottom-up parsing, for CFG in Chomsky normal form G :S  AB | BB A  CC | AB | a B  BB | CA | b C.
Simplifying CFGs There are several ways in which context-free grammars can be simplified. One natural way is to eliminate useless symbols those that cannot.
About Grammars CS 130 Theory of Computation HMU Textbook: Sec 7.1, 6.3, 5.4.
Context Free Grammars.
FORMAL LANGUAGES, AUTOMATA, AND COMPUTABILITY
Conversion of a Chomsky normal form grammar to Greibach normal form
Theory Of Automata By Dr. MM Alam
Pushdown Automata Part II: PDAs and CFG Chapter 12.
CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.
Chapter 4 Normal Forms for CFGs Chomsky Normal Form n Defn A CFG G = (V, , P, S) is in chomsky normal form if each rule in G has one of.
CS5371 Theory of Computation
CS Master – Introduction to the Theory of Computation Jan Maluszynski - HT Lecture 4 Context-free grammars Jan Maluszynski, IDA, 2007
104 Closure Properties of Regular Languages Regular languages are closed under many set operations. Let L 1 and L 2 be regular languages. (1) L 1  L 2.
Introduction to the Theory of Computation John Paxton Montana State University Summer 2003.
Normal forms for Context-Free Grammars
1 Module 32 Chomsky Normal Form (CNF) –4 step process.
How to Convert a Context-Free Grammar to Greibach Normal Form
January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015.
1 Background Information for the Pumping Lemma for Context-Free Languages Definition: Let G = (V, T, P, S) be a CFL. If every production in P is of the.
Cs466(Prasad)L8Norm1 Normal Forms Chomsky Normal Form Griebach Normal Form.
Context-Free Grammars Chapter 3. 2 Context-Free Grammars and Languages n Defn A context-free grammar is a quadruple (V, , P, S), where  V is.
CS 3813 Introduction to Formal Languages and Automata Chapter 6 Simplification of Context-free Grammars and Normal Forms These class notes are based on.
Chapter 12: Context-Free Languages and Pushdown Automata
Problem of the DAY Create a regular context-free grammar that generates L= {w  {a,b}* : the number of a’s in w is not divisible by 3} Hint: start by designing.
Lecture 21: Languages and Grammars. Natural Language vs. Formal Language.
Chapter 4 Context-Free Languages Copyright © 2011 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1.
Lecture 16 Oct 18 Context-Free Languages (CFL) - basic definitions Examples.
CONVERTING TO CHOMSKY NORMAL FORM
Context-free Grammars Example : S   Shortened notation : S  aSaS   | aSa | bSb S  bSb Which strings can be generated from S ? [Section 6.1]
Context-Free Grammars Normal Forms Chapter 11. Normal Forms A normal form F for a set C of data objects is a form, i.e., a set of syntactically valid.
CSCI 2670 Introduction to Theory of Computing September 20, 2005.
Normal Forms for Context-Free Grammars Definition: A symbol X in V  T is useless in a CFG G=(V, T, P, S) if there does not exist a derivation of the form.
Context-Free Grammars – Chomsky Normal Form Lecture 16 Section 2.1 Wed, Sep 26, 2007.
CSCI 2670 Introduction to Theory of Computing September 21, 2004.
1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n  0} is not regular. L 2 = {(), (()), ((())),...} is not regular.  some properties.
Classification of grammars Definition: A grammar G is said to be 1)Right-linear if each production in P is of the form A  xB or A  x where A and B are.
Lecture # 19. Example Consider the following CFG ∑ = {a, b} Consider the following CFG ∑ = {a, b} 1. S  aSa | bSb | a | b | Λ The above CFG generates.
Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left.
Lecture # 9 Chap 4: Ambiguous Grammar. 2 Chomsky Hierarchy: Language Classification A grammar G is said to be – Regular if it is right linear where each.
Context Free Grammars.
Section 12.4 Context-Free Language Topics
Lecture 11 Theory of AUTOMATA
Chapter 6 Simplification of Context-free Grammars and Normal Forms These class notes are based on material from our textbook, An Introduction to Formal.
CSCI 3130: Formal languages and automata theory Tutorial 4 Chin.
1 Simplification of Context-Free Grammars Some useful substitution rules. Removing useless productions. Removing -productions. Removing unit-productions.
Closure Properties Lemma: Let A 1 and A 2 be two CF languages, then the union A 1  A 2 is context free as well. Proof: Assume that the two grammars are.
Lecture # 26 Theory Of Automata By Dr. MM Alam 1.
CS 208: Computing Theory Assoc. Prof. Dr. Brahim Hnich Faculty of Computer Sciences Izmir University of Economics.
Re-enter Chomsky More about grammars. 2 Parse trees S  A B A  aA | a B  bB | b Consider L = { a m b n | m, n > 0 } (one/more a ’s followed by one/more.
Grammars CS 130: Theory of Computation HMU textbook, Chap 5.
1 Chapter 6 Simplification of CFGs and Normal Forms.
Grammars A grammar is a 4-tuple G = (V, T, P, S) where 1)V is a set of nonterminal symbols (also called variables or syntactic categories) 2)T is a finite.
Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b.
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
Structure and Ambiguity Removing Ambiguity Chomsky Normal Form Pushdown Automata Intro (who is he foolin', thinking that there will be time to get to this?)
Lecture # 31 Theory Of Automata By Dr. MM Alam 1.
Exercises on Chomsky Normal Form and CYK parsing
Dept. of Computer Science & IT, FUUAST Theory of Computation 2 Regular Expressions Regular Expression.
Compiler Chapter 5. Context-free Grammar Dept. of Computer Engineering, Hansung University, Sung-Dong Kim.
Lecture 17: Theory of Automata:2014 Context Free Grammars.
Recap lecture 33 Example of trees, Polish Notation, examples, Ambiguous CFG, example,
Complexity and Computability Theory I
CSC312 Automata Theory Grammatical Format Chapter # 13 by Cohen
Theorem 29 Given any PDA, there is another PDA that accepts exactly the same language with the additional property that whenever a path leads to ACCEPT,
Even-Even Devise a grammar that generates strings with even number of a’s and even number of b’s.
Theory Of Automata By Dr. MM Alam
ReCap Chomsky Normal Form, Theorem regarding CNF, examples of converting CFG to be in CNF, Example of an FA corresponding to Regular CFG, Left most and.
Theory of Computation Lecture #
Presentation transcript:

Chomsky Normal Form

Chomsky Normal Form (CNF) Definition If a CFG has only productions of the form Non-Terminal  string of exactly two non-terminals OR Non-Terminal  one terminal It is said to be in Chomsky Normal Form.

Theorem 25 If L is a language generated by some CFG, then there exist another CFG that generate all the non-null words of L, all of whose productions are of one of the two basic forms: Non-Terminal  string of only Non-terminals Or Non-Terminal  one Terminal

Proof The proof is a constructive algorithm by converting a given CFG in a CFG of the production of designed format. Let us suppose in given CFG the non-terminal are S, X1, X2, X3, … . Let us also assume {a,b} are two terminals. STEP-1 We now add two new non-terminals A and B and the productions A  a B  b

Proof Theorem 25 contd… STEP-2 now for every previous production involving terminals, we replace each ‘a’ with the non-terminal A and each ‘b’ with B. e.g. X3  X4aX1SbbX7a Becomes X3  X4AX1SBBX7A Which is a string of solid non-terminals X6  abaab X6  ABAAB

Example S  X1 | X2aX2 | aSb | b X1  X2X2 | b X2  aX2 | aaX2

Theorem If CFG G = (V, ∑, P, S) is in CNF form if each rule has one of the following form A  BC A  a S  Λ Where B, C ε V – {S} Pre-Conditions The start symbol is non-recursive G does not have lambda rules other than S  Λ G does not contain unit/chain rules G does not contain useless symbols

Proof of theorem The rules of type ii, and iii already fulfill CNF condition so that become part of CNF directly Right sides of other production may contain terminals and non-terminals e.g. N  aM | NaMb We need to have string of solid non-terminal on right side we introduce now products as X a Y  b And apply them to all rules getting N  XM | NXMY

Proof contd. Introduce new non-terminals to restrict the length to 2 N  XM X  a N  NR1 Y  b R1  XR2 R2  MY

Theorem 26 Theorem 26: For any context-free language L, the non- words of L can be generated by a grammar in which all productions are in CNF.

Proof of Theorem 26 The proof will be by constructive algorithm. From Theorems 23 and 24, we know that there is a CFG for L (or for all L except Λ) that has no Λ-productions and no unit productions. Suppose further that we have made the CFG fit the form specified in Theorem 25. For productions of the form Nonterminal  one terminal we leave them alone. For each production of the form Nonterminal  string of Nonterminals we use the following expansion that involves new nonterminals R1, R2, ...

Proof of Theorem 26 contd. For instance, the production S  X1X2X3X8 should be replaced by S  X1R1 where R1  X2R2 and where R2  X3X8. Thus, we convert a production with long string of non-terminals into a sequence of productions with exactly two non-terminals. If we need to convert another production, we introduce new R’s with different subscripts. The new grammar generates the same language as the old grammar with the possible exception of the word Λ.

Remarks Theorem 26 has enabled a form of algebra of productions by introducing non-terminals R1, R2, R3 … The derivation tree associated with a derivation in Chomsky Normal Form grammar is a binary tree.

Example S  aSa | bSb | a | b | aa | bb S  ASA S  AR2 S  BSB S  AA S  BB S  AR2 R2  SA S  BR1 R1  SB S  AA S  BB S  a S  b A  a B  b

Leftmost Derivation Definition: The leftmost non-terminal in a working string is the first non-terminal that we encounter when we scan the string from left to right. Example: In the string abNbaXYa, the leftmost non terminal is N. If a word w is generated by a CFG such that at each step in the derivation, a production is applied to the leftmost non-terminal in the working string, then this derivation is called a leftmost derivation.

Example: Left derivation S  aSX | b X  Xb | a aababa S  aSX  aaSXX  aabXX  aabXbX  aababX  aababa

Parsing Definition: the process of finding the derivation of word generated by particular grammar is called parsing. There are different parsing techniques, containing the following three Top down parsing. Bottom up parsing. Parsing technique for particular grammar of arithmetic expression.

Top down parsing Following is an example showing top down parsing technique As can be observed from the name of top down parsing, the parsing starts from the nonterminal S and the structure similar to that of total language tree is developed. The branches of the tree are extended till the required word is found as a branch.

Left Recursion Removal Consider the grammar A  Aa | b Halting condition of top-down parsing depend upon the generation of terminal prefixes to discover dead ends. Repeated application of above rule fail to generate a prefix that can terminate the parse.

Removing left recursion To remove left recursion from A, the A rules are divided into two groups. Left recursive and others A  AU1 | AU2 | AU3 | ….| AUj A  V1 | V2 | V3 | … | Vk Solution: A  V1 | V2 | V3 | … | Vk| V1Z | V2Z | … | VkZ Z  u1Z | u2Z | u3Z | … | ujZ | u1 | u2 | u3 | … | uj

Example A  Aa | b Solution: A  bZ | b Z  aZ | a A  Aa | Ab | b | c A  bZ | cZ | b | c Z  aZ | bZ | a | b Consider another example A  AB | BA | a B  b | c A  BAZ | aZ | BA Z  BZ | B