CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
Context Free Grammars.
Lecture # 11 Grammar Problems.
Lecture 17 Naveen Z Quazilbash Simplification of Grammars.
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.
Theory Of Automata By Dr. MM Alam
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.
1 CSC 3130: Automata theory and formal languages Tutorial 4 KN Hung Office: SHB 1026 Department of Computer Science & Engineering.
Costas Buch - RPI1 Simplifications of Context-Free Grammars.
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.
CS 3240 – Chapter 8.  Is a n b n c n context-free? CS Properties of Context-Free Languages2.
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.
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
نظریه زبان ها و ماشین ها فصل دوم Context-Free Languages دانشگاه صنعتی شریف بهار 88.
CONVERTING TO CHOMSKY NORMAL FORM
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.
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.
Context-Free Grammars – Chomsky Normal Form Lecture 16 Section 2.1 Wed, Sep 26, 2007.
CSCI 2670 Introduction to Theory of Computing September 21, 2004.
1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n  0} is not regular. L 2 = {(), (()), ((())),...} is not regular.  some properties.
Lecture # 19. Example Consider the following CFG ∑ = {a, b} Consider the following CFG ∑ = {a, b} 1. S  aSa | bSb | a | b | Λ The above CFG generates.
Context Free Grammar. Introduction Why do we want to learn about Context Free Grammars?  Used in many parsers in compilers  Yet another compiler-compiler,
Section 12.4 Context-Free Language Topics
Lecture 11 Theory of AUTOMATA
Chapter 6 Simplification of Context-free Grammars and Normal Forms These class notes are based on material from our textbook, An Introduction to Formal.
CSCI 3130: Formal languages and automata theory Tutorial 4 Chin.
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.
Lecture # 26 Theory Of Automata By Dr. MM Alam 1.
CS 208: Computing Theory Assoc. Prof. Dr. Brahim Hnich Faculty of Computer Sciences Izmir University of Economics.
Re-enter Chomsky More about grammars. 2 Parse trees S  A B A  aA | a B  bB | b Consider L = { a m b n | m, n > 0 } (one/more a ’s followed by one/more.
1 Chapter 6 Simplification of CFGs and Normal Forms.
Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b.
Mathematical Foundations of Computer Science Chapter 3: Regular Languages and Regular Grammars.
Transparency No. 1 Formal Language and Automata Theory Homework 5.
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.
1 Context Free Grammars Xiaoyin Wang CS 5363 Spring 2016.
About Grammars Hopcroft, Motawi, Ullman, Chap 7.1, 6.3, 5.4.
Theory of Computation Automata Theory Dr. Ayman Srour.
Compiler Chapter 5. Context-free Grammar Dept. of Computer Engineering, Hansung University, Sung-Dong Kim.
Context Free Grammars Chapter 12.
Normal Forms for CFG’s Eliminating Useless Variables Removing Epsilon
7. Properties of Context-Free Languages
CSC312 Automata Theory Grammatical Format Chapter # 13 by Cohen
Recap Lecture 34 Example of Ambiguous Grammar, Example of Unambiguous Grammer (PALINDROME), Total Language tree with examples (Finite and infinite trees),
Simplifications of Context-Free Grammars
NORMAL FORMS FDP ON THEORY OF COMPUTING
Chapter 6 Simplification of Context-free Grammars and Normal Forms
Subject Name: Finite Automata & Formal Languages Subject Code 10CS56.
Context-Free Languages
Presentation transcript:

CS 3240 – Chapter 6

 6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms  Chomsky Normal (CNF)  6.3: A Membership Algorithm  CYK Algorithm  An example of bottom-up parsing CS Normal Forms for Context-Free Languages2

 Variables in CFGs can often be eliminated  If they are not recursive, you can substitute their rules throughout  See next slide CS Normal Forms for Context-Free Languages3

4 A ➞ a | aaA | abBc B ➞ abbA | b Just substitute directly for B: A ➞ a | aaA | ababbAc | abbc

 A variable is useless if:  It can’t be reached from the start state, or  It never leads to a terminal string ▪ Due to endless recursion  Both problems can be detected by a dependency graph  See next slide CS Normal Forms for Context-Free Languages5

6 S ➞ aSb | A | λ A ➞ aA A is useless (non-terminating): S ➞ aSb | λ

CS Normal Forms for Context-Free Languages7 S ➞ A A ➞ aA | λ B ➞ bA B is useless (non-reachable): S ➞ A A ➞ aA | λ

CS Normal Forms for Context-Free Languages8 Simplify the following: S ➞ aS | A | C A ➞ a B ➞ aa C ➞ aCb

CS Normal Forms for Context-Free Languages9

10 Simplify the following grammar: S ➞ AB | AC A ➞ aAb | bAa | a B ➞ bbA | aaB | AB C ➞ abCa | aDb D ➞ bD | aC

 Any variable that can eventually terminate in the empty string is said to be nullable  Note: a variable may be indirectly nullable  In general: if A ➞ V 1 V 2 …V n, and all the V i are nullable, then A is also nullable ▪ See Theorem 6.3 CS Normal Forms for Context-Free Languages11

CS Normal Forms for Context-Free Languages12 Consider the following grammar: S ➞ a | Xb | aYa X ➞ Y | λ Y ➞ b | X Which variables are nullable? How can we substitute the effect of λ before removing it?

 First find all nullable variables  Then substitute (A + λ) for every nullable variable A, and expand  Then remove λ everywhere from the grammar  What’s left is equivalent to the original grammar  except the empty string may be lost  we won’t worry about that CS Normal Forms for Context-Free Languages13

CS Normal Forms for Context-Free Languages14 Consider the following grammar (again): S ➞ a | Xb | aYa X ➞ Y | λ Y ➞ b | X How can we substitute the effect of λ before removing it?

S → aSbS | bSaS | λS → aSa | bSb | X X → aYb | bYa Y → aY | bY | λ CS Normal Forms for Context-Free Languages15

 Unit Productions often occur in chains  A ➞ B ➞ C  Must maintain the effect of B and C when substituting for A throughout  Procedure:  Find all unit chains  Rebuild grammar by: ▪ Keeping all non-unit productions ▪ Keeping only the effect of all unit productions/chains CS Normal Forms for Context-Free Languages16

CS Normal Forms for Context-Free Languages17 S ➞ A | bb A ➞ B | b B ➞ S | a Note that S ⇒ * {A,B}, A ⇒ * {B,S}, B ⇒ * {S,A} Giving: S ➞ bb | b | a// Added non-unit part of A and B A ➞ b | a | bb// Added non-unit part of B and S B ➞ a | bb | b// Added non-unit part of S and A

 1) Determine all variables reachable by unit rules for each variable  2) Keep all non-unit rules  3) Substitute non-unit rules in place of each variable reachable by unit productions CS Normal Forms for Context-Free Languages18

CS Normal Forms for Context-Free Languages19 S ➞ aA A ➞ BB B ➞ aBb | λ Now remove nulls and see what happens…. (Also see the solution for #15 in 6.1)

CS Normal Forms for Context-Free Languages20 S ➞ AB A ➞ B B ➞ aB | BB | λ

 Do things in the following recommended order:  Remove nulls  Remove unit productions  Remove useless variables  Simplify by substitution as desired CS Normal Forms for Context-Free Languages21

 Very important for our purposes  All CNF rules are of one of the following two forms:  A ➞ c(a single terminal)  A ➞ XY(exactly two variables)  Must begin the transformation after simplifying the grammar (removing λ, all unit productions, useless variables, etc.) CS Normal Forms for Context-Free Languages22

CS Normal Forms for Context-Free Languages23 Convert to CNF: S ➞ bA | aB A ➞ bAA | aS | a B ➞ aBB | bS | b (NOTE: already has no nulls or units)

CS Normal Forms for Context-Free Languages24 Convert the following grammar to CNF: S ➞ abAB A ➞ bAB | λ B ➞ BAa | A | λ

CS Normal Forms for Context-Free Languages25 Convert the following grammar to CNF: S ➞ aS | bS | B B ➞ bb | C | λ C ➞ cC | λ

 Single terminal character followed by zero or more variables (cV *, c ∈ Σ )  V → a  V → aBCD…  λ allowed only in S → λ  Sometimes need to make up new variable names CS Normal Forms for Context-Free Languages26

CS Normal Forms for Context-Free Languages27 S → AB A → aA | bB | b B → b Substitute for A in first rule (i.e., add B to each rule for A): S → aAB | bBB | bB The other rules are okay

CS Normal Forms for Context-Free Languages28 S → abSb |aa Add rules to generate a and b: S → aBSB |aA A → a B → b

 The “parsing” problem  How do we know if a string is generated by a given grammar?  Bottom-up parsing (CYK Algorithm)  An Example of Dynamic Programming  Requires Chomsky Normal Form (CNF)  Start by considering A ➞ c rules  Build up the parse tree from there CS Normal Forms for Context-Free Languages29

CS Normal Forms for Context-Free Languages30 S ➞ XY X ➞ XA | a | b Y ➞ AY | a A ➞ a Does this grammar generate “baaa”?

CS Normal Forms for Context-Free Languages31 CNF yields binary trees. (Can you find a third parse tree?)

CS Normal Forms for Context-Free Languages32 S ➞ XY X ➞ XA | a | b Y ➞ AY | a A ➞ a Stage 1: b ⇐ X a ⇐ X, Y, A Stage 2: ba ⇐ X(X,Y,A) = XX, XY, XA ⇐ S, X aa ⇐ (X,Y,A)(X,Y,A) = XX, XY, XA, YX, YY, YA, AX, AY, AA ⇐ S, X, Y Stage 3: baa ⇐ ba a ⇐ (S,X)(X,Y,A) = SX, SY, SA, XX, XY, XA = S, X baa ⇐ b aa ⇐ X(S,X,Y) = XS, XX, XY = S aaa ⇐ aa a ⇐ (S,X,Y)(X,Y,A) = _________ aaa ⇐ a aa ⇐ (X,Y,A)(S,X,Y) = _________ Stage 4: (you finish…) baaa: b aaa ⇐ __________

CS Normal Forms for Context-Free Languages33

CS Normal Forms for Context-Free Languages34

CS Normal Forms for Context-Free Languages35

CS Normal Forms for Context-Free Languages36

CS Normal Forms for Context-Free Languages37 Does the following grammar generate “abbaab”? S ➞ SAB | λ A ➞ aA | λ B ➞ bB | λ