About Grammars CS 130 Theory of Computation HMU Textbook: Sec 7.1, 6.3, 5.4.

Slides:



Advertisements
Similar presentations
Closure Properties of CFL's
Advertisements

Context-Free Grammars
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.
Fall 2004COMP 3351 Simplifications of Context-Free Grammars.
Prof. Busch - LSU1 Simplifications of Context-Free Grammars.
Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory VII.
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.
CFGs and PDAs Sipser 2 (pages ). Last time…
CS5371 Theory of Computation
Applied Computer Science II Chapter 2 : Context-free languages Prof. Dr. Luc De Raedt Institut für Informatik Albert-Ludwigs Universität Freiburg Germany.
CS 310 – Fall 2006 Pacific University CS310 Pushdown Automata Sections: 2.2 page 109 October 11, 2006.
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.
1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar.
Normal forms for Context-Free Grammars
January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015.
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.
INHERENT LIMITATIONS OF COMPUTER PROGRAMS CSci 4011.
Lecture 21: Languages and Grammars. Natural Language vs. Formal Language.
Context-Free Grammars
CONVERTING TO CHOMSKY NORMAL FORM
Formal Languages Context free languages provide a convenient notation for recursive description of languages. The original goal of CFL was to formalize.
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.
CSCI 2670 Introduction to Theory of Computing September 20, 2005.
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.
Classification of grammars Definition: A grammar G is said to be 1)Right-linear if each production in P is of the form A  xB or A  x where A and B are.
Context Free Grammars CIS 361. Introduction Finite Automata accept all regular languages and only regular languages Many simple languages are non regular:
CSCI 2670 Introduction to Theory of Computing September 15, 2005.
Grammars CPSC 5135.
Context Free Grammar. Introduction Why do we want to learn about Context Free Grammars?  Used in many parsers in compilers  Yet another compiler-compiler,
CS 3240: Languages and Computation Context-Free Languages.
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.
CS 208: Computing Theory Assoc. Prof. Dr. Brahim Hnich Faculty of Computer Sciences Izmir University of Economics.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
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.
Grammars CS 130: Theory of Computation HMU textbook, Chap 5.
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.
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
CSCI 2670 Introduction to Theory of Computing September 14, 2005.
Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1 Chapter 2 Context-Free Languages Some slides are in courtesy.
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.
Dept. of Computer Science & IT, FUUAST Theory of Computation 2 Regular Expressions Regular Expression.
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.
CSCI 2670 Introduction to Theory of Computing September 16, 2004.
Closed book, closed notes
Normal Forms for CFG’s Eliminating Useless Variables Removing Epsilon
Context-Free Grammars: an overview
Complexity and Computability Theory I
7. Properties of Context-Free Languages
Lecture 22 Pumping Lemma for Context Free Languages
Simplifications of Context-Free Grammars
Regular Grammar.
7. Properties of Context-Free Languages
Chapter 6 Simplification of Context-free Grammars and Normal Forms
CHAPTER 2 Context-Free Languages
Chapter 2 Context-Free Language - 01
Context-Free Languages
Presentation transcript:

About Grammars CS 130 Theory of Computation HMU Textbook: Sec 7.1, 6.3, 5.4

About grammars Simplifying grammars Normal forms for grammars Grammar Ambiguity

Grammar Productions Formal definition of a grammar provides much leeway Productions can be simplified or restricted to make proofs about CFGs simpler

Simplifications Removing useless symbols Those that cannot be derived from S and those that cannot reduce to a terminal string Removing є-productions A  є Removing unit productions A  B Normal forms e.g., Chomsky Normal Form

Useless symbols We want to ensure all productions in the grammar have no useless symbols, i.e., all symbols are generating and reachable Generating symbols All variables that could eventually derive a string of terminals; i.e., all A in V, such that there exists a string w of terminals where A  * w Reachable symbols All variables that can be reached from the start symbol; i.e., all A in V, such that S  * uAw, for some u and w

Removing useless productions Remove productions with non-generating symbols Requires identifying generating symbols recursively: right hand side of production contains only terminals and generating symbols Remove productions with non-reachable symbols Requires identifying reachable symbols recursively: S is reachable, and so are symbols that exist on the right hand side of productions with reachable symbols on the left hand side

Epsilon Productions є-productions: productions of the form A  є Nullable symbols: symbols A where A  є or A  B 1 B 2 …B n such that each B i is nullable For each production that has a nullable symbol on the right hand side, add a production without that symbol; apply rule iteratively on resulting productions After this step, all є-productions can be removed Note, if the language L generated by the original grammar includes є, then the language generated by the resulting grammar will be L – {є}

Unit Productions Unit productions: all productions of the form A  B Removing unit productions Identify unit pairs: pairs of variables (A, B) such that A  * B, and the derivation involves only unit productions For each unit pair (A, B), add the production A  w, whenever B  w and w is not a variable Unit productions may now be removed

Chomsky Normal Form CNF: all productions are of the form A  BC(B, C are variables) A  a(a is a terminal) How do we convert a grammar to an equivalent CNF grammar?

Greibach Normal Form GNF: all productions are of the form A  aB 1 B 2 …B n Note that A  a is allowed Note that if the grammar is GNF, each step in a derivation of a string adds a terminal How do we convert a grammar to an equivalent GNF grammar?

Recall CFG to PDA conversion Transition function  is based on the variables, productions and terminals of the grammar:  (q 0, є, A) includes (q 0, w) whenever A  w  (q 0, a, a) = (q 0, є ) for each a in T Easier and more intuitive if the grammar is of GNF  (q 0, a, A) = (q 0, B 1 B 2 …B n ) for each production A  aB 1 B 2 …B n

Ambiguous grammar A grammar G is ambiguous if there exists a string for which two different parse trees exist (two different leftmost derivations) Example: S  i = E E  n E  i E  E + E E  E * E Parse tree for i = n + n * n ?

Two leftmost derivations S  i = E  i = E + E  i = n + E  i = n + E * E  i = n + n * E  i = n + n * n S  i = E  i = E * E  i = E + E * E  i = n + E * E  i = n + n * E  i = n + n * n

Grammar and precedence S  i = E E  E + T E  T T  T * F T  F F  n F  i Parse tree for i = n + n * n ? S  i = E  i = E + T  i = T + T  i = F + T  i = n + T  i = n + T * F  i = n + F * F  i = n + n * F  i = n + n * n

Chomsky hierarchy Relaxing or adding restrictions to productions in a grammar leads towards a hierarchy of languages Note: Context-free grammar definition imposes that a production should take the form A  w, where A  T and w is a string over T  V

Chomsky hierarchy Regular languages (type 3) A  sB, A  s (A, B  V, s  T) Context-free languages (type 2) A  w (w is a string over T  V) Context-sensitive languages (type 1) uAw  uvw (u,v,w are strings over T  V) Recursively enumerable languages (type 0) v  w (productions are unrestricted)

Chomsky hierarchy regular recursive recursively enumerable context-free context-sensitive type 0 type 1 type 2 type 3