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.
C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language) 1.
Closure Properties of CFL's
Exercise 1: Balanced Parentheses Show that the following balanced parentheses grammar is ambiguous (by finding two parse trees for some input sequence)
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.
CS5371 Theory of Computation
Courtesy Costas Buch - RPI1 Simplifications of Context-Free Grammars.
Costas Busch - RPI1 NPDAs Accept Context-Free Languages.
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.
Costas Buch - RPI1 Simplifications of Context-Free Grammars.
Courtesy Costas Busch - RPI
1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal.
1 The Pumping Lemma for Context-Free Languages. 2 Take an infinite context-free language Example: Generates an infinite number of different strings.
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
Cs466(Prasad)L8Norm1 Normal Forms Chomsky Normal Form Griebach Normal Form.
Fall 2006Costas Busch - RPI1 PDAs Accept Context-Free Languages.
Prof. Busch - LSU1 Linear Grammars Grammars with at most one variable at the right side of a production Examples:
Prof. Busch - LSU1 Pumping Lemma for Context-free 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.
Fall 2003Costas Busch - RPI1 Linear Grammars Grammars with at most one variable at the right side of a production Examples:
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.
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.
Context Free Grammar. Introduction Why do we want to learn about Context Free Grammars?  Used in many parsers in compilers  Yet another compiler-compiler,
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.
Costas Busch - LSU1 Pumping Lemma for Context-free Languages.
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:
Costas Busch - LSU1 The Post Correspondence Problem.
About Grammars Hopcroft, Motawi, Ullman, Chap 7.1, 6.3, 5.4.
Costas Busch - LSU1 PDAs Accept Context-Free Languages.
Closed book, closed notes
Normal Forms for CFG’s Eliminating Useless Variables Removing Epsilon
Context-Free Grammars: an overview
PDAs Accept Context-Free Languages
7. Properties of Context-Free Languages
NPDAs Accept Context-Free Languages
Simplifications of Context-Free Grammars
NPDAs Accept Context-Free Languages
Lecture 14 Grammars – Parse Trees– Normal Forms
7. Properties of Context-Free Languages
Chapter 6 Simplification of Context-free Grammars and Normal Forms
CHAPTER 2 Context-Free Languages
The Post Correspondence Problem
Parsing Costas Busch - LSU.
Pumping Lemma for Context-free Languages
Decidable Problems of Regular Languages
Chapter 2 Context-Free Language - 01
Applications of Regular Closure
Normal forms and parsing
Answer Questions about Exam2 problems
The Chomsky Hierarchy Costas Busch - LSU.
Normal Forms for Context-free Grammars
Context-Free Languages
Presentation transcript:

Simplifications of Context-Free Grammars Costas Busch - LSU

A Substitution Rule Equivalent grammar Substitute Costas Busch - LSU

Substitute Equivalent grammar Costas Busch - LSU

In general: Substitute equivalent grammar Costas Busch - LSU

Nullable Variables Nullable Variable: Example: Nullable variable Costas Busch - LSU

all the nullable variables disappear (except for the start variable) Removing Substitute After we remove all the all the nullable variables disappear (except for the start variable) Costas Busch - LSU

Unit-Productions Unit Production: (a single variable in both sides) Example: Unit Productions Costas Busch - LSU

Removal of unit productions: Substitute Costas Busch - LSU

Unit productions of form can be removed immediately Remove Costas Busch - LSU

Substitute Costas Busch - LSU

Remove repeated productions Final grammar Costas Busch - LSU

Useless Productions Useless Production Some derivations never terminate... Costas Busch - LSU

Another grammar: Useless Production Not reachable from S Costas Busch - LSU

If there is a derivation In general: If there is a derivation consists of terminals Then variable is useful Otherwise, variable is useless Costas Busch - LSU

A production is useless if any of its variables is useless Productions useless Variables useless Costas Busch - LSU

Removing Useless Variables and Productions Example Grammar: Costas Busch - LSU

First: find all variables that can produce strings with only terminals or (possible useful variables) Round 1: (the right hand side of production that has only terminals) Round 2: (the right hand side of a production has terminals and variables of previous round) This process can be generalized Costas Busch - LSU

Then, remove productions that use variables other than Costas Busch - LSU

Second: Find all variables reachable from Use a Dependency Graph where nodes are variables unreachable Costas Busch - LSU

Keep only the variables reachable from S Final Grammar Contains only useful variables Costas Busch - LSU

Step 1: Remove Nullable Variables Step 2: Remove Unit-Productions Removing All Step 1: Remove Nullable Variables Step 2: Remove Unit-Productions Step 3: Remove Useless Variables This sequence guarantees that unwanted variables and productions are removed Costas Busch - LSU

Normal Forms for Context-free Grammars Costas Busch - LSU

Chomsky Normal Form Each production has form: or variable variable terminal Costas Busch - LSU

Examples: Chomsky Not Chomsky Normal Form Normal Form Costas Busch - LSU

Conversion to Chomsky Normal Form Example: Not Chomsky Normal Form We will convert it to Chomsky Normal Form Costas Busch - LSU

Introduce new variables for the terminals: Costas Busch - LSU

Introduce new intermediate variable to break first production: Costas Busch - LSU

Introduce intermediate variable: Costas Busch - LSU

Final grammar in Chomsky Normal Form: Initial grammar Costas Busch - LSU

From any context-free grammar (which doesn’t produce ) In general: From any context-free grammar (which doesn’t produce ) not in Chomsky Normal Form we can obtain: an equivalent grammar in Chomsky Normal Form Costas Busch - LSU

First remove: The Procedure Nullable variables Unit productions (Useless variables optional) Costas Busch - LSU

In productions with length at least 2 replace with Then, for every symbol : New variable: Add production In productions with length at least 2 replace with Productions of form do not need to change! Costas Busch - LSU

Replace any production with New intermediate variables: Costas Busch - LSU

Observations Chomsky normal forms are good for parsing and proving theorems It is easy to find the Chomsky normal form for any context-free grammar (which doesn’t generate ) Costas Busch - LSU

Greinbach Normal Form All productions have form: symbol variables Costas Busch - LSU

Examples: Greinbach Normal Form Not Greinbach Normal Form Costas Busch - LSU

Conversion to Greinbach Normal Form: Costas Busch - LSU

Observations Greinbach normal forms are very good for parsing strings (better than Chomsky Normal Forms) However, it is difficult to find the Greinbach normal of a grammar Costas Busch - LSU