Lecture 17 Naveen Z Quazilbash Simplification of Grammars.

Slides:



Advertisements
Similar presentations
Grammar types There are 4 types of grammars according to the types of rules: – General grammars – Context Sensitive grammars – Context Free grammars –
Advertisements

Closure Properties of CFL's
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.
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.
SIMPLIFYING GRAMMARS Definition: A useless symbol of a context-free
Context Free Grammars.
Lecture # 11 Grammar Problems.
Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory VII.
Western Michigan University CS6800 Advanced Theory of Computation Spring 2014 By Abduljaleel Alhasnawi & Rihab Almalki.
CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.
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
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.
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.
Cs466(Prasad)L8Norm1 Normal Forms Chomsky Normal Form Griebach Normal Form.
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 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.
Lecture 19 Naveen Z Quazilbash. Overview CNFs-Assignment Greibach Normal Forms.
Context Free Grammars CIS 361. Introduction Finite Automata accept all regular languages and only regular languages Many simple languages are non regular:
Chapter 5 Context-Free Grammars
Context Free Grammar. Introduction Why do we want to learn about Context Free Grammars?  Used in many parsers in compilers  Yet another compiler-compiler,
Transparency No. P2C2-1 Formal Language and Automata Theory PART II: Chapter 2 Linear Grammars and Normal Forms (Lecture 21)
Section 12.4 Context-Free Language Topics
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.
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.
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
Formal Languages, Automata and Models of Computation
Mathematical Foundations of Computer Science Chapter 3: Regular Languages and Regular Grammars.
1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 8 Mälardalen University 2011.
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.
1 Section 12.2 Pushdown Automata A pushdown automaton (PDA) is a finite automaton with a stack that has stack operations pop, push, and nop. PDAs always.
About Grammars Hopcroft, Motawi, Ullman, Chap 7.1, 6.3, 5.4.
Theory of Computation Automata Theory Dr. Ayman Srour.
20 G M aaba acba aaba.. What is it about? Models of Language Generation Models of Language Recognition.
Normal Forms for CFG’s Eliminating Useless Variables Removing Epsilon
Chomsky Normal Form CYK Algorithm
Syntax Specification and Analysis
Complexity and Computability Theory I
7. Properties of Context-Free Languages
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
Simplifications of Context-Free Grammars
Jaya Krishna, M.Tech, Assistant Professor
Context-Free Languages
REGULAR LANGUAGES AND REGULAR GRAMMARS
Context Free Grammar: Simplification
Regular Grammars.
NORMAL FORMS FDP ON THEORY OF COMPUTING
7. Properties of Context-Free Languages
Chapter 6 Simplification of Context-free Grammars and Normal Forms
Subject Name: Finite Automata & Formal Languages Subject Code 10CS56.
LECTURE # 07.
Context-Free Languages
Presentation transcript:

Lecture 17 Naveen Z Quazilbash Simplification of Grammars

Overview Attendance Motivation Simplification of Grammars Eliminating useless variables Eliminating null productions Eliminating unit productions Quiz result

Motivation for grammar simplification Parsing Problem Given a CFG G and string w, determine if w ϵ L(G). Fundamental problem in compiler design and natural language processing If G is in general form then the procedure maybe very inefficient. So the grammar is “transformed” into a simpler form to make the parsing problem easier.

Simplification of Grammars It involves the removal of: 1. Useless variables 2. ε -productions 3. Unit productions

Useless variables: There are two types of useless variables: 1. Variables that cannot be reached 2. Variables that do not derive any strings

3. ε -productions E.g.: A  ε Note that if we remove these productions, the language no longer includes the empty string.

4. Unit productions: They are of the form A  B Or A  A

1) Unreachable Variables E.g.: S  BS|B|E A  DA|D|S B  CB|C C  aC|a D  bD|b E  cE|c

To find unreachable variables, draw a dependency graph Dependency Graph: Vertices of the graph are variables The graph doesn’t include alphabet symbols, such as “a” or “b” If there is a production A  …..B…, i.e., the left side is A and the right side includes B, then there is an edge A  B

A variable is reachable if there is a path from S to this variable S itself is always reachable After identifying unreachable variables, remove all productions with unreachable left side.

S  BS|B|E A  DA|D|S B  CB|C C  aC|a D  bD|b E  cE|c Drawing its dependency graph: Reachable: S, B, C, E S DAE CB

Grammar without unreachable variables: S  BS|B|E B  CB|C C  aC|a E  cE|c Ex: Determine its language!!

2) Variables that don’t terminate A variable A terminates if either: There is a production A  …. with no variables on the right, e.g. A  aabc, OR There is a production A  … where all variables on the right terminate; e.g. A  aBbaC, where B and C terminate. Note: to find all variables that terminate, keep looking for such productions until you cannot find any new ones.

TASK Example: S  A|BC|DE A  aA|bA B  bB|b C  EF D  dD|BD|BA E  aE|a F  cFc|c Remove all productions that include a variable that doesn’t terminate. Note: We remove a production if it has such a variable on either side.

Solution xS  A|BC|DE XA  aA|bA xB  bB|b xC  EF XD  dD|BD|BA xE  aE|a xF  cFc|c

S  BC B  bB|b C  EF E  aE|a F  cFc|c Ex: Determine its language.

3) Eliminating ε -Productions Nullable variables: A variable is nullable if either: There is a production A  ε, or There is a production A  B 1 B 2 …B n (only variables, no symbols), where all variables on the right side are nullable. Note: to find all nullable variables, keep looking for such productions, until you cannot find any new ones.

TASK S  SAB|SBC|BC A  aA|a B  bB|bC|C C  cC| ε First we find variables that can lead to the empty string: C=> ε B=>C=> ε S=>BC=>B=>C=> ε

xS  SAB|SBC|BC A  aA|a xB  bB|bC|C xC  cC| ε Thus, S, B, and C can lead to ε ; they are called nullable variables

For each production that has nullable variables, consider all possible ways to skip some of these variables and add the corresponding productions. E.g. W  aWXaYZb, suppose that X, Y and Z are nullable; then there are 8 ways to skip some of them. W  aWab|aWXab|aWaYb|aWaZb|aWXaYb|aWXaZb| aWaYZb|aWXaYZb

Back to our grammar where S,B and C are nullable: S  A|AB|SA|SAB|S|B|C|SB|BC|SBC A  aA|a B  b|bB|bC|C C  c|cC| ε Now, we can remove the ε- productions without changing the language. The only possible change is losing the empty string, if it is in the original language.

So our grammar without null productions becomes: S  A|AB|SA|SAB|S|B|C|SB|BC|SBC A  aA|a B  b|bB|bC|C C  c|cC

4) Eliminating Unit Productions S  Aa|B A  a|bc|B B  A|bb|C|cC C  a|C First, for every variable, we find all single variables that can be reached from it: For S: S=>B=>A, S=>B=>C For A: A=>B=>C For B: B=>A, B=>C For C: NONE (C itself doesn’t count)

For finding reachable single variables, what should we do?

Use Dependency Graph! Drawing Dependency Graph: Vertices of the graph are variables. If there is a unit production A  B, then there is an edge A  B. A single variable is reachable from A if there is a pth from A to B.

Dependency Graph: S A B C

To construct an equivalent grammar without unit productions: Remove all unit productions For each pair A=>*B, where B is a single variable reachable from A, consider all productions B  p 1 |p 2 |…|p n ; and add the corresponding productions A  p 1 |p 2 |…|p n. for example, since A=>*B and B  bb|cC, add the productions A  bb|cC

S  Aa|B A  a|bc|B B  A|bb|C|cC C  a|C S  Aa B  bb|cC A  a|bc CaCa Note that the variable B has become useless and we need to remove it! S  bb|cC|a|bc|a B  a|bc|a A  bb|cC|a C  a Old non-unit productions new productions

Summary Main steps of simplifying a grammar: Remove useless variables, which cannot be reached or do not terminate. Remove ε- productions. Remove unit productions. Remove useless variables again!