Fall 2004COMP 3351 Simplifications of Context-Free Grammars.

Slides:



Advertisements
Similar presentations
Simplifications of Context-Free Grammars
Advertisements

Fall 2006Costas Buch - RPI1 Simplifications of Context-Free Grammars.
Context free languages 1. Equivalence of context free grammars 2. Normal forms.
CFGs and PDAs Sipser 2 (pages ). Long long ago…
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.
Prof. Busch - LSU1 Simplifications of Context-Free Grammars.
FORMAL LANGUAGES, AUTOMATA, AND COMPUTABILITY
CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.
CS5371 Theory of Computation
Courtesy Costas Busch - RPI1 Positive Properties of Context-Free languages.
1 … NPDAs continued. 2 Pushing Strings Input symbol Pop symbol Push string.
Courtesy Costas Buch - RPI1 Simplifications of Context-Free Grammars.
Costas Busch - RPI1 NPDAs Accept Context-Free Languages.
Lecture Note of 12/22 jinnjy. Outline Chomsky Normal Form and CYK Algorithm Pumping Lemma for Context-Free Languages Closure Properties of CFL.
Courtesy Costas Busch - RPI1 NPDAs Accept Context-Free Languages.
Costas Buch - RPI1 Simplifications of Context-Free Grammars.
1 Converting NPDAs to Context-Free Grammars. 2 For any NPDA we will construct a context-free grammar with.
1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal.
Fall 2004COMP 3351 NPDA’s Accept Context-Free Languages.
Fall 2003Costas Busch - RPI1 Decidable Problems of Regular Languages.
1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar.
1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal.
1 Simplifications of Context-Free Grammars. 2 A Substitution Rule Substitute Equivalent grammar.
1 Compilers. 2 Compiler Program v = 5; if (v>5) x = 12 + v; while (x !=3) { x = x - 3; v = 10; } Add v,v,0 cmp v,5 jmplt ELSE THEN: add x, 12,v.
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
Fall 2004COMP 3351 Another NFA Example. Fall 2004COMP 3352 Language accepted (redundant state)
Cs466(Prasad)L8Norm1 Normal Forms Chomsky Normal Form Griebach Normal Form.
Fall 2006Costas Busch - RPI1 PDAs Accept Context-Free Languages.
Prof. Busch - RPI1 Decidable Problems of Regular Languages.
1 Applications of Regular Closure. 2 The intersection of a context-free language and a regular language is a context-free language context free regular.
1 PDAs Accept Context-Free Languages. 2 Context-Free Languages (Grammars) Languages Accepted by PDAs Theorem:
CS 3813 Introduction to Formal Languages and Automata Chapter 6 Simplification of Context-free Grammars and Normal Forms These class notes are based on.
CONVERTING TO CHOMSKY NORMAL FORM
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.
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.
CSCI 2670 Introduction to Theory of Computing September 21, 2004.
Context Free Grammar. Introduction Why do we want to learn about Context Free Grammars?  Used in many parsers in compilers  Yet another compiler-compiler,
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.
CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Normal forms.
Costas Busch - LSU1 Parsing. Costas Busch - LSU2 Compiler Program File v = 5; if (v>5) x = 12 + v; while (x !=3) { x = x - 3; v = 10; } Add v,v,5.
Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1 Chapter 2 Context-Free Languages Some slides are in courtesy.
Grammar Set of variables Set of terminal symbols Start variable Set of Production rules.
Exercises on Chomsky Normal Form and CYK parsing
CS 154 Formal Languages and Computability March 8 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron Mak.
About Grammars Hopcroft, Motawi, Ullman, Chap 7.1, 6.3, 5.4.
Costas Busch - LSU1 PDAs Accept Context-Free Languages.
Normal Forms for CFG’s Eliminating Useless Variables Removing Epsilon
Chomsky Normal Form CYK Algorithm
PDAs Accept Context-Free Languages
Ambiguity Parsing algorithms
7. Properties of Context-Free Languages
Simplifications of Context-Free Grammars
Simplifications of Context-Free Grammars
PDAs Accept Context-Free Languages
NPDAs Accept Context-Free Languages
7. Properties of Context-Free Languages
Chapter 6 Simplification of Context-free Grammars and Normal Forms
Parsing Costas Busch - LSU.
Closure Properties of Context-Free languages
Decidable Problems of Regular Languages
Applications of Regular Closure
Normal forms and parsing
Normal Forms for Context-free Grammars
Context-Free Languages
Presentation transcript:

Fall 2004COMP 3351 Simplifications of Context-Free Grammars

Fall 2004COMP 3352 A Substitution Rule Substitute Equivalent grammar

Fall 2004COMP 3353 A Substitution Rule Equivalent grammar Substitute

Fall 2004COMP 3354 In general: Substitute equivalent grammar

Fall 2004COMP 3355 Nullable Variables Nullable Variable:

Fall 2004COMP 3356 Removing Nullable Variables Example Grammar: Nullable variable

Fall 2004COMP 3357 Substitute Final Grammar

Fall 2004COMP 3358 Unit-Productions Unit Production: (a single variable in both sides)

Fall 2004COMP 3359 Removing Unit Productions Observation: Is removed immediately

Fall 2004COMP Example Grammar:

Fall 2004COMP Substitute

Fall 2004COMP Remove

Fall 2004COMP Substitute

Fall 2004COMP Remove repeated productions Final grammar

Fall 2004COMP Useless Productions Some derivations never terminate... Useless Production

Fall 2004COMP Another grammar: Not reachable from S Useless Production

Fall 2004COMP In general: if then variable is useful otherwise, variable is useless contains only terminals

Fall 2004COMP A production is useless if any of its variables is useless Productions useless Variables useless

Fall 2004COMP Removing Useless Productions Example Grammar:

Fall 2004COMP First: find all variables that can produce strings with only terminals Round 1: Round 2:

Fall 2004COMP Keep only the variables that produce terminal symbols: (the rest variables are useless) Remove useless productions

Fall 2004COMP Second: Find all variables reachable from Use a Dependency Graph not reachable

Fall 2004COMP Keep only the variables reachable from S Final Grammar (the rest variables are useless) Remove useless productions

Fall 2004COMP Removing All Step 1: Remove -productions Step 2: Remove Unit-productions Step 3: Remove Useless productions

Fall 2004COMP Normal Forms for Context-free Grammars

Fall 2004COMP Chomsky Normal Form Each productions has form: variable or terminal

Fall 2004COMP Examples: Not Chomsky Normal Form Chomsky Normal Form

Fall 2004COMP Conversion to Chomsky Normal Form Example: Not Chomsky Normal Form

Fall 2004COMP Introduce variables for terminals:

Fall 2004COMP Introduce intermediate variable:

Fall 2004COMP Introduce intermediate variable:

Fall 2004COMP Final grammar in Chomsky Normal Form: Initial grammar

Fall 2004COMP From any context-free grammar (which doesn’t produce ) not in Chomsky Normal Form we can obtain: An equivalent grammar in Chomsky Normal Form In general:

Fall 2004COMP The Procedure First remove: Nullable variables Unit productions

Fall 2004COMP Then, for every symbol : In productions: replace with Add production New variable:

Fall 2004COMP Replace any production with New intermediate variables:

Fall 2004COMP Theorem: For any context-free grammar (which doesn’t produce ) there is an equivalent grammar in Chomsky Normal Form

Fall 2004COMP Observations Chomsky normal forms are good for parsing and proving theorems It is very easy to find the Chomsky normal form for any context-free grammar

Fall 2004COMP Greibach Normal Form All productions have form: symbolvariables

Fall 2004COMP Examples: Greibach Normal Form Not Greibach Normal Form

Fall 2004COMP Conversion to Greibach Normal Form: Greibach Normal Form

Fall 2004COMP Theorem: For any context-free grammar (which doesn’t produce ) there is an equivalent grammar in Greibach Normal Form

Fall 2004COMP Observations Greibach normal forms are very good for parsing It is hard to find the Greibach normal form of any context-free grammar

Fall 2004COMP The CYK Parser

Fall 2004COMP The CYK Membership Algorithm Input: Grammar in Chomsky Normal Form String Output: find if

Fall 2004COMP The Algorithm Grammar : String : Input example:

Fall 2004COMP 33547

Fall 2004COMP 33548

Fall 2004COMP 33549

Fall 2004COMP 33550

Fall 2004COMP Therefore: Time Complexity: The CYK algorithm can be easily converted to a parser (bottom up parser) Observation: