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.

Slides:



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

Closure Properties of CFL's
Context-Free Grammars
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.
SIMPLIFYING GRAMMARS Definition: A useless symbol of a context-free grammar is one which does not occur in the derivation of any sentence of that grammar.
About Grammars CS 130 Theory of Computation HMU Textbook: Sec 7.1, 6.3, 5.4.
1 Chomsky Normal Form of CFG’s Definition Purpose Method of Constuction.
Fall 2004COMP 3351 Simplifications of Context-Free Grammars.
Prof. Busch - LSU1 Simplifications of Context-Free Grammars.
SIMPLIFYING GRAMMARS Definition: A useless symbol of a context-free
Context Free Grammars.
Lecture # 11 Grammar Problems.
Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory VII.
FORMAL LANGUAGES, AUTOMATA, AND COMPUTABILITY
Western Michigan University CS6800 Advanced Theory of Computation Spring 2014 By Abduljaleel Alhasnawi & Rihab Almalki.
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 310 – Fall 2006 Pacific University CS310 Pushdown Automata Sections: 2.2 page 109 October 11, 2006.
Lecture Note of 12/22 jinnjy. Outline Chomsky Normal Form and CYK Algorithm Pumping Lemma for Context-Free Languages Closure Properties of CFL.
Costas Buch - RPI1 Simplifications of Context-Free Grammars.
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.
1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar.
Linear Grammars and Normal Forms (Lecture 21)
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
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.
INHERENT LIMITATIONS OF COMPUTER PROGRAMS CSci 4011.
Chapter 12: Context-Free Languages and Pushdown Automata
1 Properties of Context-free Languages Reading: Chapter 7.
Chapter 4 Context-Free Languages Copyright © 2011 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1.
Context-Free Grammars
Formal Grammars Denning, Sections 3.3 to 3.6. Formal Grammar, Defined A formal grammar G is a four-tuple G = (N,T,P,  ), where N is a finite nonempty.
Lecture 16 Oct 18 Context-Free Languages (CFL) - basic definitions Examples.
Formal Languages Context free languages provide a convenient notation for recursive description of languages. The original goal of CFL was to formalize.
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 21, 2004.
Context Free Grammars CIS 361. Introduction Finite Automata accept all regular languages and only regular languages Many simple languages are non regular:
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 Grammar. Introduction Why do we want to learn about Context Free Grammars?  Used in many parsers in compilers  Yet another compiler-compiler,
ELIMINATING LEFT RECURSIVENESS. Abbreviation. “cfg” stands for “context free grammar” Definition. A cfg is left recursive if it contains a production.
Section 12.4 Context-Free Language Topics
Chapter 6 Simplification of Context-free Grammars and Normal Forms These class notes are based on material from our textbook, An Introduction to Formal.
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.
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.
Exercises on Chomsky Normal Form and CYK parsing
Chomsky Normal Form.
Lecture # 10 Grammar Problems. Problems with grammar Ambiguity Left Recursion Left Factoring Removal of Useless Symbols These can create problems for.
About Grammars Hopcroft, Motawi, Ullman, Chap 7.1, 6.3, 5.4.
Compiler Chapter 5. Context-free Grammar Dept. of Computer Engineering, Hansung University, Sung-Dong Kim.
Normal Forms for CFG’s Eliminating Useless Variables Removing Epsilon
Complexity and Computability Theory I
7. Properties of Context-Free Languages
Lecture 22 Pumping Lemma for Context Free Languages
Simplifications of Context-Free Grammars
Simplifications of Context-Free Grammars
Jaya Krishna, M.Tech, Assistant Professor
NORMAL FORMS FDP ON THEORY OF COMPUTING
7. Properties of Context-Free Languages
Chapter 6 Simplification of Context-free Grammars and Normal Forms
Context-Free Languages
Presentation transcript:

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 S   wXy  * wxy where w, x, y are in T *. Definition: A symbol X in V  T is inaccessible in a CFG G = (V, T, P, S) if X does not appear in any sentential form. (inaccessible implies not reachable) Definition: A symbol X is generating if X   w for some w in T *.

Algorithm (NF1) (Is L(G) empty?) Input: A CFG G = (V, T, P, S). Output: “YES” if L(G) = , “NO” otherwise. Method: Construct sets N 0, N 1, …recursively as follows: 1.N 0 =  ; i = 1; 2.N i = {A | A  in P and  in (N i-1  T) * }  N i-1 ; 3. If N i  N i-1 then begin i = i +1; go to 2; end; 4.N e = N i ; 5.If S is in N e then output “NO” else output “YES”.

Example: S  AA | AB A  SA | AB B  BB | b

Algorithm (NF2): (Removal of inaccessible symbols) Input: A CFG G = (V, T, P, S). Output: CFG G = (V, T, P, S) such that (1) L(G) = L(G), (2) No X in V  T is unreachable. Method: Construct sets N 0, N 1, …recursively as follows: 1.N 0 = {S}; i = 1; 2.N i = {X | some A   X  is in P and A in N i-1 }  N i-1 ; 3.If N i  N i-1 then begin i=i+1; go to 2; end; 4.V = N i  V; T = N i  T; P = those productions of P with symbols from N i ; S = S;

Algorithm (NF3): (Useless Symbol Removal) Input: A CFG G = (V, T, P, S). Output: CFG G = (V, T, P, S) such that (1) L(G) = L(G), (2) No X in V  T is useless. Method: 1.Apply algorithm NF1 to G to get N e. 2.Let G 1 = (V  N e, T, P 1, S) where P 1 contains those productions of P involving only symbols from N e  T. 3.Apply algorithm NF2 to G 1 to obtain G = (V, T, P, S)

Example: S  a | A A  AB B  b

Definitions: 1.A   is called  -production 2.A   is called A-production 3.A  B where A and B are in V is called unit production (or single production). 4.A variable A is nullable if A   . 5.A CFG G = (V, T, P, S) is  -free if either (i) P has no  -productions, or (ii) there is exactly one  -production S   and S does not appear on the right side of any production in P.

Algorithm (NF4): Conversion to  -free grammar Input: A CFG G = (V, T, P, S). Output: Equivalent  -free CFG G = (V, T, P, S) Method: 1)Construct N e = {A | A in V and A  +  } 2)If A   0 B 1  1 B 2  2 … B k  k is in P, k  0 and for 1  i  k each B i in N e but no symbol in any  j is in N e, 0  j  k, add to P all productions of the form A   0 X 1  1 X 2  2 … X k  k where X i is either B i or , without adding A   to P 3)If S is in N e add to P the productions S   | S where S is a new symbol and set V = V  {S}. Otherwise set V = V and S =S. 4)G =(V, T, P, S)

Example: S  aSbS | bSaS | 

Algorithm (NF5): Removal of unit productions Input: An  -free CFG G = (V, T, P, S). Output: Equivalent  -free CFG G = (V, T, P, S) Method 1.Construct for each A in V the sets N A = {B | A  * B} as follows: (a) N 0 = {A}; i = 1; (b) N i = {C | B  C is in P and B in N i-1 }  N i-1 (c)If N i  N i-1 then i=i+1; go to (b); else N A = N i 2. Construct P as follows: If B   is in P and not a unit production, place A   in P for A such that B is in N A. 3. G = (V, T, P, S)

Example: E  E + T | T T  T * F | F E  ( E ) | a

Definition: A CFG G = (V, T, P, S) is said to be cycle-free if there is no derivation of the form A  * A for any A in V. G is said to be proper it it is cycle-free,  -free, and has no useless symbols.  -free and no unit productions imply cycle-free.

Chomsky Normal Form A CFG G = (V, T, P, S) is said to be in Chomsky Normal Form (CNF) if each production in P is one of the forms (1)A  BC with A, B, C in V, or (2)A  a with a in T, or (3)If  is in L(G), then S   is a production and S does not appear on the right side of any production

Algorithm (NF6) Conversion to CNF Input: A proper CFG G=(V, T, P, S) with no single production Output: A CFG G 1 =(V 1, T, P 1, S) such that L(G) = L(G 1 ) Method: From G, construct G 1 as follows: 1)Add each production of the form A → a in P to P 1 2)Add each production of the form A → BC in P to P 1 (A, B, C are non terminals)

3) If S →  is in P, add S →  to P 1 4) For each production of the form A → X 1 …X k in P, k > 2, add to P 1 the following productions: A → Y 1 → Y 2 … → Y k-1 Y k where Y j stands for X i if X i is a nonterminal otherwise it is a new nonterminal added to V 1

5) For each production of the form A → X 1 X 2 where either X 1 or X 2 or both are terminals add to P 1 the production A → Y 1 Y 2 6) For each terminal a replaced by a Y, add the production Y → a

Example: S  bA | aB A  bAA | aS | a B  aBB | bB | b

Greibach Normal Form Recursive, left-recursive, right-recursive: A nonterminal A in a CFG G = (V,T,P,S) is said to be recursive if A  +  A  for some  and . If  = , then A is left-recursive, if  = , then A is right-recursive. A grammar with at least one left-(right-) recursive nonterminal is said to be left- (right-) recursive

Lemma: Let G = (V,T,P,S) be a CFG in which A → A  1 | …|A  m |  1 | … |  n are all the A-productions in P and no  begins with A. Let G 1 = (V  {B}, T, P 1, S) where P 1 is P with the above productions are replaced by A →  1 | …|  n |  1 B| … |  n B B →  1 | … |  m |  1 B | …|  m B Then L(G) = L(G 1 )

Algorithm – elimination of left recursion. Input: A proper CFG G=(V,T,P,S) Output: A CFG G 1 with no left-recursion Method: Let V = {A 1,..., A n }. Transform G so that if A i →  is a production, then  begins with either a terminal of a symbol A j such that j > i. this can be accomplished by substitution for lower numbered productions and eliminating immediate left-recursion.

Example: A → BC | a B → CA | Ab C → AB | CC | a

Definition: A CFG G = (V,T,P,S) is said to be in Greibauch normal form (GNF) if G is  - free and each non  -production is of the form A → a  where a is a terminal and  is a string in V*.

Lemma: Let G=(V,T,P,S) be a non-left recursive grammar. Then there is linear order < on V such that A → B  is in P, the A < B. Proof: Let R be a relation A R B if and only if A  * B  for some . R is a partial order Extend R to a linear order

Algorithm: Conversion to GNF Input: G=(V,T,P,S), G non-left-recursive, proper CFG Output: A CFG G 1 in GNF Method: 1) Construct a linear order on V 2) Set i= n-1 3) If i=0 go to step 5. Otherwise replace each production of the form A i → A j , j>i, by A i →  1  | …|  m  where A j →  1 | …|  m are all the A j -productions (  i ’s begin with terminal)

4) Set i=i-; go to step 2 5) For each production A → aX 1 …X k, if X j is a terminal replace it by a nonterminal Y j and add a production Y j → X j E → T | TE 1 E 1 → +T | +TE 1 T → F | FT 1 T 1 → *F | *FT 1 F → (E) | a