Complexity and Computability Theory I Lecture #9 Instructor: Rina Zviel-Girshin Lea Epstein.

Slides:



Advertisements
Similar presentations
Erik Jonsson School of Engineering and Computer Science FEARLESS Engineering CS 4384 – 001 Automata Theory Thursday: Context-Free.
Advertisements

Grammars, constituency and order A grammar describes the legal strings of a language in terms of constituency and order. For example, a grammar for a fragment.
Chapter Chapter Summary Languages and Grammars Finite-State Machines with Output Finite-State Machines with No Output Language Recognition Turing.
Context-Free Grammars Sipser 2.1 (pages 99 – 109).
1 Introduction to Computability Theory Lecture5: Context Free Languages Prof. Amos Israeli.
Context-Free Grammars Sipser 2.1 (pages 99 – 109).
Lecture 15UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 15.
CS5371 Theory of Computation
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.1: Context-Free Grammars) David Martin With some.
CS Master – Introduction to the Theory of Computation Jan Maluszynski - HT Lecture 4 Context-free grammars Jan Maluszynski, IDA, 2007
Context-Free Grammars Lecture 7
January 14, 2015CS21 Lecture 51 CS21 Decidability and Tractability Lecture 5 January 14, 2015.
1 Foundations of Software Design Lecture 23: Finite Automata and Context-Free Grammars Marti Hearst Fall 2002.
Introduction to the Theory of Computation John Paxton Montana State University Summer 2003.
Normal forms for Context-Free Grammars
Specifying Languages CS 480/680 – Comparative Languages.
Lecture 9UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.
COP4020 Programming Languages
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.
FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY
EECS 6083 Intro to Parsing Context Free Grammars
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
Lecture 21: Languages and Grammars. Natural Language vs. Formal Language.
Chapter 4 Context-Free Languages Copyright © 2011 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1.
Complexity and Computability Theory I Lecture #13 Instructor: Rina Zviel-Girshin Lea Epstein Yael Moses.
Formal Grammars Denning, Sections 3.3 to 3.6. Formal Grammar, Defined A formal grammar G is a four-tuple G = (N,T,P,  ), where N is a finite nonempty.
Lecture 16 Oct 18 Context-Free Languages (CFL) - basic definitions Examples.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
Context-free Grammars Example : S   Shortened notation : S  aSaS   | aSa | bSb S  bSb Which strings can be generated from S ? [Section 6.1]
Languages, Grammars, and Regular Expressions Chuck Cusack Based partly on Chapter 11 of “Discrete Mathematics and its Applications,” 5 th edition, by Kenneth.
Context-free Grammars [Section 2.1] - more powerful than regular languages - originally developed by linguists - important for compilation of programming.
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.
CSCI 2670 Introduction to Theory of Computing September 15, 2005.
Chapter 5 Context-Free Grammars
Grammars CPSC 5135.
PART I: overview material
Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left.
Lecture # 9 Chap 4: Ambiguous Grammar. 2 Chomsky Hierarchy: Language Classification A grammar G is said to be – Regular if it is right linear where each.
CS 3240: Languages and Computation Context-Free Languages.
Part VII. Models for Context-Free Languages 1/50.
CMSC 330: Organization of Programming Languages Context-Free Grammars.
Complexity and Computability Theory I Lecture #2 Rina Zviel-Girshin Leah Epstein Winter
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.
Complexity and Computability Theory I Lecture #8 Instructor: Rina Zviel-Girshin Lea Epstein.
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.
Grammars Hopcroft, Motawi, Ullman, Chap 5. Grammars Describes underlying rules (syntax) of programming languages Compilers (parsers) are based on such.
Grammars CS 130: Theory of Computation HMU textbook, Chap 5.
Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b.
Syntax Analyzer (Parser)
CSCI 2670 Introduction to Theory of Computing September 14, 2005.
CSCI 4325 / 6339 Theory of Computation Zhixiang Chen Department of Computer Science University of Texas-Pan American.
FORMAL LANGUAGES, AUTOMATA, AND COMPUTABILITY
CSE 311 Foundations of Computing I Lecture 19 Recursive Definitions: Context-Free Grammars and Languages Spring
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
CSE 311 Foundations of Computing I Lecture 18 Recursive Definitions: Context-Free Grammars and Languages Autumn 2011 CSE 3111.
Complexity and Computability Theory I Lecture #12 Instructor: Rina Zviel-Girshin Lea Epstein.
Syntax Analysis By Noor Dhia Syntax analysis:- Syntax analysis or parsing is the most important phase of a compiler. The syntax analyzer considers.
CSCI 2670 Introduction to Theory of Computing September 16, 2004.
Lecture 17: Theory of Automata:2014 Context Free Grammars.
Context-Free Grammars: an overview
Formal Language & Automata Theory
CS510 Compiler Lecture 4.
Introduction to Parsing (adapted from CS 164 at Berkeley)
Complexity and Computability Theory I
Context-free Languages
CHAPTER 2 Context-Free Languages
Context-Free Languages
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Context-Free Grammars
Presentation transcript:

Complexity and Computability Theory I Lecture #9 Instructor: Rina Zviel-Girshin Lea Epstein

Rina Overview Grammars Example Context-free grammars Examples Ambiguity

Rina Grammar Another computational model. A member in the family of rewriting systems. The computation is by rewriting a string. –We start with an empty string and rewrite the string according to the grammar until we have an output. All possible outputs of a grammar is the language of the grammar.

Rina The origin The origin of the name grammar for this computational model is in natural languages, where grammar is a collection of rules. This collection defines what is legal in the language and what is not.

Rina The grammar computational model In the same manner the grammar computational model is primarily a collection of: –rules of rewriting, –rules how to build strings that are in the language, –structural rules for the language.

Rina Some facts The grammar consists of a collection of rules over an alphabet  and a set of variables (usually denoted by capital letters of the Latin alphabet). Every grammar has a start symbol also called a start variable (usually denoted by S). Every grammar has at least one rule.

Rina  notation We will use the notation  in grammar rules.  What does it mean: (  )? It means : –  can be replaced by  –  constructs  –  produces  –  rewrites to  –  reduces to 

Rina Example of a grammar  ={a,b,c} The following grammar generates all strings over . S  aS(add a) S  bS(add b) S  cS(add c) S  (delete S)

Rina w =aacb production How can the word w=aacb be produced? S  aS We used the S  aS production because w starts with a and the only rule that starts with a is S  aS. From S that remains we need to produce w'=acb. S  aS  aaS We used the S  aS production because w' also starts with a and the only rule that starts with a is S  aS.

Rina w =aacb production (cont.) From S we need to produce w''=cb. S  aS  aaS  aacS We used the S  cS production because w'' starts with c and the only rule that starts with c is S  cS. From S we need to produce b. S  aS  aaS  aacS  aacbS We used the S  bS production to produce b.

Rina w =aacb production (cont.) But S is still remaining in final production. We want to delete it. We will use the rule S  to delete S. S  aS  aaS  aacS  aacbS  aacb So we managed to produce w using the rules of the grammar.

Rina Parsing What we did is called parsing a word w accordingly to a given grammar. To parse a word or sentence means to break it into parts that confirm to a given grammar. We can represent the same production sequence by a parse tree or derivation tree. Each node in the tree is either letter or variable. Only a variable node can have children.

Rina Parsing w=aacb

Rina Parsing w=aacb Or a step by step derivation:

Rina Parsing w=aacb (cont.)

Rina Context-free grammar A context-free grammar (CFG) G is a 4-tuple (V, , S, R), where 1. V is a finite set called the variables 2.  is a finite set, disjoint from V, called the terminals 3. S is a start symbol 4. R is a finite set of production rules, with each rule being a variable and a string of variables and terminals: a  b, a  V and b  (VU  )*

Rina uAv yields uwv If u, v and w are strings of variables and terminals and A  w is a rule of the grammar, we say that uAv yields uwv, written uAv  uwv. We write u  *w if there exists a sequence u1, u2,..uk, k  0 and u  u1  u2 ...  w.

Rina  notation We also use the following notations:  means derives in one step  + means derives in one or more steps  * means derives in zero or more steps

Rina The language of the grammar The language of the grammar is L(G) = {w  * | w  * and S  * w} The language generated by CFG is called a context-free language (CFL).

Rina Is the following definition correct? The language of the grammar is L(G) = {w  * | w  * and S  + w} Yes. Because a derivation in zero steps derivation produces only S. S is not a string over  *, so can't belong to L.

Rina Examples over  ={0,1} Construct a grammar for the following language L = {0,00,1} G = (V={S},  ={0,1},S, R) where R: S  0 S  00 S  1 or S  0 | 00 | 1

Rina Examples over  ={0,1} Construct a grammar for the following language L = {0 n 1 n |n  0} G = (V={S},  ={0,1},S, R) where R: S  0S1 S  or S  0S1 | 

Rina Examples over  ={0,1} Construct a grammar for the following language L = {0 n 1 n |n  1} G = (V={S},  ={0,1},S, R) where R: S  0S1 | 01

Rina Examples over  ={0,1} Construct a grammar for the following language L = {0 * 1 + } G = (V={S,B},  ={0,1},S, R) where R: S  0S | 1B B  1B | 

Rina Examples over  ={0,1} Construct a grammar for the following language L = {0 2i+1 | i  0} G = (V={S},  ={0,1},S, R) where R: S  0 | 00S

Rina Examples over  ={0,1} Construct a grammar for the following language L = {0 i+1 1 i | i  0} G = (V={S},  ={0,1},S, R) where R: S  0 | 0S1

Rina Examples over  ={0,1} Construct a grammar for the following language L = {w| w  * and |w|mod 2=1} G = (V={S},  ={0,1},S, R) where R: S  0 | 1| 1S1| 0S0 |1S0 | 0S1

Rina Examples over  ={0,1} Construct a grammar for the following language L = {0 n 1 n |n  1}  {1 n 0 n | n  0} G = (V={S,A,B},  ={0,1},S, R) where R: S  A | B A  0A1 | 01 B  1B0 | 

Rina From a grammar to a CFL Give a description of L(G) for the following grammar: S  0S0 | 1 L(G) = {0 n 10 n |n  0}

Rina From a grammar to a CFL Give a description of L(G) for the following grammar: S  0S0 | 1S1 | # L(G) = {The subset of all palindromes over  ={0,1} with # in the middle} or L(G) = {w#w R | w  *}

Rina From a grammar to a CFL Give a description of L(G) for the following grammar: S  0A | 0B A  1S B1B1 L(G) = {(01) n |n  1 }

Rina From a grammar to a CFL Give a description of L(G) for the following grammar: S  0S11 | 0 L(G) = {0 n+1 1 2n |n  1 }

Rina From a grammar to a CFL Give a description of L(G) for the following grammar: S  E | NE N  D | DN D  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 E  0 | 2 | 4 | 6 L(G) = {w | w represents an even octal number }

Rina From a grammar to a CFL Give a description of L(G) for the following grammar: S  N.N | -N.N N  D | DN D  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 L(G) = {w | w represents a rational number (that has a finite representation) }

Rina Question Can any finite language be constructed by a grammar? Yes. Proof: Let L={w i | i  n and w i  *} be a finite language over . We construct the following grammar: Sw1Sw1 Sw2Sw2.. SwnSwn

Rina Question (cont.) The grammar derives all n words of L. The grammar is finite (n production rules). The grammar syntax is correct.

Rina Ambiguity The ability of grammar to generate the same string in several ways is called ambiguity. That means that the string have different parse trees and may have different meanings. A grammar is ambiguous if there exists a string w that has at least two different parse trees.

Rina Example E  E+E | E*E | T T  0|1|2|..|9 The string 3+4*5 can be produced in several ways:

Rina Example (cont.) So if we use this grammar to produce a programming language then we will have several computations of 3+4*5. There is no precedence of * over the +. This language will be impossible to use because the user won't know which computation compiler uses. Two possible results: 35 or 23.

Rina The conclusion The conclusion: programming languages should have a unique interpretation or the grammar of the programming language would be unambiguous.

Rina Any Questions?