Costas Buch - RPI1 Simplifications of Context-Free Grammars.

Slides:



Advertisements
Similar presentations
Simplifications of Context-Free Grammars
Advertisements

Fall 2006Costas Buch - RPI1 Simplifications of Context-Free Grammars.
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.
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.
Courtesy Costas Busch - RPI1 Positive Properties of Context-Free languages.
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 Busch - RPI1 Grammars. Costas Busch - RPI2 Grammars Grammars express languages Example: the English language.
Courtesy Costas Busch - RPI1 The Pumping Lemma for Context-Free Languages.
1 Converting NPDAs to Context-Free Grammars. 2 For any NPDA we will construct a context-free grammar with.
Fall 2006Costas Busch - RPI1 The Post Correspondence Problem.
Courtesy Costas Busch - RPI
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.
Costas Busch - RPI1 Context-Free Languages. Costas Busch - RPI2 Regular Languages.
1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar.
Costas Busch - RPI1 The Pumping Lemma for Context-Free Languages.
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.
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.
Fall 2003Costas Busch - RPI1 Linear Grammars Grammars with at most one variable at the right side of a production Examples:
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.
Formal Languages, Automata and Models of Computation
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
Costas Busch - LSU1 Linear Grammars Grammars with at most one variable at the right side of a production Examples:
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
PDAs Accept Context-Free Languages
7. Properties of Context-Free Languages
NPDAs Accept 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 for Context-free Grammars
Context-Free Languages
Presentation transcript:

Costas Buch - RPI1 Simplifications of Context-Free Grammars

Costas Buch - RPI2 A Substitution Rule Substitute Equivalent grammar

Costas Buch - RPI3 A Substitution Rule Equivalent grammar Substitute

Costas Buch - RPI4 In general: Substitute equivalent grammar

Costas Buch - RPI5 Nullable Variables Nullable Variable:

Costas Buch - RPI6 Removing Nullable Variables Example Grammar: Nullable variable

Costas Buch - RPI7 Substitute Final Grammar

Costas Buch - RPI8 Unit-Productions Unit Production: (a single variable in both sides)

Costas Buch - RPI9 Removing Unit Productions Observation: Is removed immediately

Costas Buch - RPI10 Example Grammar:

Costas Buch - RPI11 Substitute

Costas Buch - RPI12 Remove

Costas Buch - RPI13 Substitute

Costas Buch - RPI14 Remove repeated productions Final grammar

Costas Buch - RPI15 Useless Productions Some derivations never terminate... Useless Production

Costas Buch - RPI16 Another grammar: Not reachable from S Useless Production

Costas Buch - RPI17 In general: if then variable is useful otherwise, variable is useless contains only terminals

Costas Buch - RPI18 A production is useless if any of its variables is useless Productions useless Variables useless

Costas Buch - RPI19 Removing Useless Productions Example Grammar:

Costas Buch - RPI20 First: find all variables that can produce strings with only terminals Round 1: Round 2:

Costas Buch - RPI21 Keep only the variables that produce terminal symbols: (the rest variables are useless) Remove useless productions

Costas Buch - RPI22 Second: Find all variables reachable from Use a Dependency Graph not reachable

Costas Buch - RPI23 Keep only the variables reachable from S Final Grammar (the rest variables are useless) Remove useless productions

Costas Buch - RPI24 Removing All Step 1: Remove Nullable Variables Step 2: Remove Unit-Productions Step 3: Remove Useless Variables

Costas Buch - RPI25 Normal Forms for Context-free Grammars

Costas Buch - RPI26 Chomsky Normal Form Each productions has form: variable or terminal

Costas Buch - RPI27 Examples: Not Chomsky Normal Form Chomsky Normal Form

Costas Buch - RPI28 Convertion to Chomsky Normal Form Example: Not Chomsky Normal Form

Costas Buch - RPI29 Introduce variables for terminals:

Costas Buch - RPI30 Introduce intermediate variable:

Costas Buch - RPI31 Introduce intermediate variable:

Costas Buch - RPI32 Final grammar in Chomsky Normal Form: Initial grammar

Costas Buch - RPI33 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:

Costas Buch - RPI34 The Procedure First remove: Nullable variables Unit productions

Costas Buch - RPI35 Then, for every symbol : In productions: replace with Add production New variable:

Costas Buch - RPI36 Replace any production with New intermediate variables:

Costas Buch - RPI37 Theorem: For any context-free grammar (which doesn’t produce ) there is an equivalent grammar in Chomsky Normal Form

Costas Buch - RPI38 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

Costas Buch - RPI39 Greinbach Normal Form All productions have form: symbolvariables

Costas Buch - RPI40 Examples: Greinbach Normal Form Not Greinbach Normal Form

Costas Buch - RPI41 Conversion to Greinbach Normal Form: Greinbach Normal Form

Costas Buch - RPI42 Theorem: For any context-free grammar (which doesn’t produce ) there is an equivalent grammar in Greinbach Normal Form

Costas Buch - RPI43 Observations Greinbach normal forms are very good for parsing It is hard to find the Greinbach normal form of any context-free grammar

Costas Buch - RPI44 The CYK Parser

Costas Buch - RPI45 The CYK Membership Algorithm Input: Grammar in Chomsky Normal Form String Output: find if

Costas Buch - RPI46 The Algorithm Grammar : String : Input example:

Costas Buch - RPI47

Costas Buch - RPI48

Costas Buch - RPI49

Costas Buch - RPI50

Costas Buch - RPI51 Therefore: Time Complexity: The CYK algorithm can be easily converted to a parser (bottom up parser) Observation: