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.

Slides:



Advertisements
Similar presentations
REMOVING LEFT RECURSION AND INDIRECT LEFT RECURSION
Advertisements

Closure Properties of CFL's
CYK Parser Von Carla und Cornelia Kempa. Overview Top-downBottom-up Non-directional methods Unger ParserCYK Parser.
1 Parsing The scanner recognizes words The parser recognizes syntactic units Parser operations: Check and verify syntax based on specified syntax rules.
Exercise 1: Balanced Parentheses Show that the following balanced parentheses grammar is ambiguous (by finding two parse trees for some input sequence)
Context-Free Grammars
CFGs and PDAs Sipser 2 (pages ). Long long ago…
LR-Grammars LR(0), LR(1), and LR(K).
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.
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
FORMAL LANGUAGES, AUTOMATA, AND COMPUTABILITY
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.
CFGs and PDAs Sipser 2 (pages ). Last time…
CS5371 Theory of Computation
Lecture Note of 12/22 jinnjy. Outline Chomsky Normal Form and CYK Algorithm Pumping Lemma for Context-Free Languages Closure Properties of CFL.
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.1: Context-Free Grammars) David Martin With some.
Normal Forms for CFG’s Eliminating Useless Variables Removing Epsilon
Costas Buch - RPI1 Simplifications of Context-Free Grammars.
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.
Introduction to the Theory of Computation John Paxton Montana State University Summer 2003.
Normal forms for Context-Free Grammars
CS5371 Theory of Computation Lecture 8: Automata Theory VI (PDA, PDA = CFG)
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.
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.
Context-free Grammars
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.
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.
CSE 3813 Introduction to Formal Languages and Automata Chapter 8 Properties of Context-free Languages These class notes are based on material from our.
Formal Languages Context free languages provide a convenient notation for recursive description of languages. The original goal of CFL was to formalize.
1 Homework #7 (Models of Computation, Spring, 2001) Due: Section 1; April 16 (Monday) Section 2; April 17 (Tuesday) 2. Covert the following context-free.
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.
The Pumping Lemma for Context Free Grammars. Chomsky Normal Form Chomsky Normal Form (CNF) is a simple and useful form of a CFG Every rule of a CNF grammar.
CSCI 2670 Introduction to Theory of Computing September 21, 2004.
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.
ELIMINATING LEFT RECURSIVENESS. Abbreviation. “cfg” stands for “context free grammar” Definition. A cfg is left recursive if it contains a production.
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.
CS 208: Computing Theory Assoc. Prof. Dr. Brahim Hnich Faculty of Computer Sciences Izmir University of Economics.
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.
Syntax Analyzer (Parser)
Exercises on Chomsky Normal Form and CYK parsing
Chomsky Normal Form.
About Grammars Hopcroft, Motawi, Ullman, Chap 7.1, 6.3, 5.4.
Syntax Analysis By Noor Dhia Syntax analysis:- Syntax analysis or parsing is the most important phase of a compiler. The syntax analyzer considers.
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
Fall Compiler Principles Context-free Grammars Refresher
Complexity and Computability Theory I
Even-Even Devise a grammar that generates strings with even number of a’s and even number of b’s.
Simplifications of Context-Free Grammars
Context-Free Languages
CSE322 LEFT & RIGHT LINEAR REGULAR GRAMMAR
Chapter 6 Simplification of Context-free Grammars and Normal Forms
Fall Compiler Principles Context-free Grammars Refresher
Answer Questions about Exam2 problems
Context-Free Languages
Presentation transcript:

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. For example:G→ RT R→ Ra T→b HereR is useless.

Clearly a symbol is useless if and only if either: a)we cannot derive any string containing it from the goal symbol and/or b)we cannot derive a terminal string from that symbol Notation: a) is expressed by saying that the symbol is not reachable from the goal symbol. b) is expressed by saying that the symbol does not derive a terminal string

Algorithm: To find all those symbols that are not reachable from the goal symbol. 1) Make a list of all the grammar symbols, all initially unflagged. 2) Flag the goal symbol. 3) Go through the grammar from the 1 st production to the last. If A→ x 1 x 2 …x n is one of these productions and A is flagged, then flag x 1,x 2,…,x n (those ones not already flagged). 4) Where any new symbols flagged during the iteration of step 3? If so, repeat step 3 again, otherwise stop. Any symbol that has not been flagged at this stage is not reachable from the goal symbol.

EXAMPLE Grammar 1 z → b e a → a e | e b → c e | a f c → c f d → f D is not reachable f √ f c √ c d is not reachable

Grammar 2 Z → E + T E → E | S + F | T F → F | F P | P P → G G → G | G G | F T→ T * i | i Q → E | E + F | T | S S → i Q is not reachable Q is not reachable

Grammar 3 G → A Q → P R P→Q Q, P, R are not reachable

Algorithm: To determine which symbols do not derive a terminal string. 1) Make a fresh list of all the symbols, initially unflagged. 2) Flag all the terminals. 3) Go through the grammar from the first production to the last. If A→ x 1 x 2 …x n is any such production, then if x 1, x 2, …,x n are all flagged, flag A. 4) Were any new symbols flagged in step 3? If so, go back to step 3. If not, all symbols not flagged at this stage do not derive a terminal string.

TRY THE ABOVE ALGORITHM ON GRAMMARS 1- 3 ABOVE

Definitions: 1) A  α means you can derive α from A or α=A 2) A symbol A is said to vanish if A  ε 3) A production of the form χ→ε is called an ε-production

Algorithm: To determine which symbols of a grammar vanish. 1) Make a list of symbols, initially unflagged. 2) Flag all the left hand sides of ε-productions. 3) Go through the grammar from 1 st production to last. If A→ x 1 x 2 …x n is any such production, then if x 1, x 2, …,x n are all flagged, then flag A. 4)Were any new symbols flagged in step 3? If so, go back to step 3, else stop. The flagged symbols are those which vanish.

Example: Try the algorithm on the following Grammar. Grammar G4 A → b Y D | A Y c Y → E F | ε D → g h i F → N O | Y N N → ε O → Y N E → Y O N Y

Defns: An  - production is one of the form A -> . If A, in this case, is the goal symbol, the production is referred to as a null goal production Theorem: For every cfg G, there exists a cfg G’, such that L(G’) = L(G), and G’ has no  -productions with exception that if   L(G), then G’ contains a null goal production.

Proof. G’ can be formed from G as follows: 1. Discard all the  -productions. 2. For each production of G, add to the grammar all possible productions that can be formed from it by omitting from its rhs some subset of those symbols (if any) that vanish.. 3. Remove all productions with useless symbols. 4. If the goal symbol of G vanishes, add a null goal production.

Example G -> AVw A -> aA | a V -> rUcW |  U ->  W ->  First of all, determine which symbols vanish: U, V, W.

1) Remove  -productions, gives: G -> AVw A -> aA | a V -> rUcW 2) Considering G -> AVw in step 2 of the algorithm, we add to the grammar G -> Aw Considering V -> rUcW, we add V -> rc V -> rUc V -> rcW 3) W, U are now useless symbols, so leaving out all productions with W, U, we get: G -> AVw | Aw A -> aA | a V -> rc

Exercise. Apply the above theorem to eliminate  - productions from: V -> Bc B -> RT | NM N ->  M -> KK K -> 