Context-Free Grammars – Derivations Lecture 15 Section 2.1 Mon, Sep 24, 2007.

Slides:



Advertisements
Similar presentations
4.5 Inherently Ambiguous Context-free Language For some context-free languages, such as arithmetic expressions, may have many different CFG’s to generate.
Advertisements

Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
LR-Grammars LR(0), LR(1), and LR(K).
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.
1 Introduction to Computability Theory Lecture5: Context Free Languages Prof. Amos Israeli.
CS5371 Theory of Computation
1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Context-Free Grammars Lecture 7
1 Context Free Languages October 2, Announcement HW 3 due now.
Balanced Parentheses G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (S) | SS | /\}
Courtesy Costas Busch - RPI1 Context-Free Languages.
Chapter 3: Formal Translation Models
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Lecture 9UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.
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.
Fall 2004COMP 3351 Context-Free Languages. Fall 2004COMP 3352 Regular Languages.
Prof. Busch - LSU1 Context-Free Languages. Prof. Busch - LSU2 Regular Languages Context-Free Languages.
FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Fall 2005Costas Busch - RPI1 Context-Free Languages.
Context-Free Languages Hinrich Schütze CIS, LMU, Slides based on RPI CSCI 2400 Thanks to Costas Busch.
Chapter 4 Context-Free Languages Copyright © 2011 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1.
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.
CMSC 330: Organization of Programming Languages
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 – Chomsky Normal Form Lecture 16 Section 2.1 Wed, Sep 26, 2007.
CSCI 2670 Introduction to Theory of Computing September 21, 2004.
Syntax Analysis The recognition problem: given a grammar G and a string w, is w  L(G)? The parsing problem: if G is a grammar and w  L(G), how can w.
Context-free Grammars [Section 2.1] - more powerful than regular languages - originally developed by linguists - important for compilation of programming.
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.
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.
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.
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.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Lecture 15 Naveen Z Quazilbash Ambiguity. Overview S-Grammars Ambiguity in Grammars Ambiguous grammars and Unambiguous Grammars.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
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.
Context-Free Grammars Chapter 11. Languages and Machines.
CS 3240: Languages and Computation Context-Free Languages.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 11 Midterm Exam 2 -Context-Free Languages Mälardalen University 2005.
CS 3240 – Chapter 5. LanguageMachineGrammar RegularFinite AutomatonRegular Expression, Regular Grammar Context-FreePushdown AutomatonContext-Free Grammar.
1 Parse Trees Definitions Relationship to Left- and Rightmost Derivations Ambiguity in Grammars.
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 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.
Unit-3 Parsing Theory (Syntax Analyzer) PREPARED BY: PROF. HARISH I RATHOD COMPUTER ENGINEERING DEPARTMENT GUJARAT POWER ENGINEERING & RESEARCH INSTITUTE.
Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b.
Chapter 5 Context-free Languages
Context-Free Languages
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
LECTURE 4 Syntax. SPECIFYING SYNTAX Programming languages must be very well defined – there’s no room for ambiguity. Language designers must use formal.
CSCI 4325 / 6339 Theory of Computation Zhixiang Chen Department of Computer Science University of Texas-Pan American.
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
CS 461 – Sept. 23 Context-free grammars Derivations Ambiguity Proving correctness.
Theory of Languages and Automata By: Mojtaba Khezrian.
Theory Of Automata By Dr. MM Alam
Syntax Analysis By Noor Dhia Syntax analysis:- Syntax analysis or parsing is the most important phase of a compiler. The syntax analyzer considers.
CONTEXT-FREE LANGUAGES
Relationship to Left- and Rightmost Derivations
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Relationship to Left- and Rightmost Derivations
Theory of Computation Lecture #
Presentation transcript:

Context-Free Grammars – Derivations Lecture 15 Section 2.1 Mon, Sep 24, 2007

Context-Free Grammars A context-free grammar is a grammar whose rules have the following properties: The left-hand is a single variable. The right-hand side is any string of variables and terminals.

Example Let V and  be V = {E},  = {a, b, c, +, *, (, )}. Let R be E  E + E | E * E | (E) | a | b | c

Examples Find CFGs for the following languages. {a n b n | n  0} {a n b m | n  m  0} {a n b m | m  n  0}

Equal No. of a’s and b’s Find CFGs for the language {w | w contains an equal number of a’s and b’s}

Equal No. of a’s and b’s Let w be a string with an equal number of a’s and b’s. There are four cases: w = aza w = azb w = bza w = bzb.

Equal No. of a’s and b’s In each case, what do we know about z?

Equal No. of a’s and b’s In the cases azb and bza, z is again a string with an equal number of a’s and b’s.

Equal No. of a’s and b’s In the cases aza and bzb, the string w must necessarily split into a concatenation of strings w 1 w 2, where w 1 and w 2 each have an equal number of a’s and b’s. Why?

Derivations A string u yields a string v if we can apply a grammar rule to u and get v. We write u  v.

Derivations We say that u derives v if there is a sequence u 1, u 2, …, u k (k  0), where u = u 1, v = u k, and u 1  u 2  …  u k. We write u  * v. The sequence is called a derivation.

Leftmost and Rightmost Derivations A leftmost derivation is a derivation in which, at each step, the leftmost variable is replaced with a string. Similarly for a rightmost derivation.

Example Using the grammar S  SS | aSb | bSa |  find leftmost and rightmost derivations of abab.

Ambiguity Some grammars provide more than one way to derive a string. For example, abab can be derived in two different ways using the grammar rules S  SS | aSb | bSa |  (Typically the leftmost and rightmost derivations are different.)

Ambiguity If a language contains a string that has more than one leftmost derivation, then the grammar is ambiguous. If every grammar for a language is ambiguous, then the language is inherently ambiguous.

Ambiguity Consider again the grammar rules S  SS | aSb | bSa |  Suppose that, for every string, we need to associate with each a a unique b. Does the grammar show us how to do this?

Ambiguity Consider the grammar E  E + E | E * E | (E) | a | b | c Derive the string a + b * c in two different ways. Is this grammar ambiguous? Is this language inherently ambiguous?

Ambiguity See Example 2.4 on p. 103 an unambiguous grammar for this language.