CS 536 Spring 20011 Ambiguity Lecture 8. CS 536 Spring 20012 Announcement Reading Assignment –“Context-Free Grammars” (Sections 4.1, 4.2) Programming.

Slides:



Advertisements
Similar presentations
Exercise 1: Balanced Parentheses Show that the following balanced parentheses grammar is ambiguous (by finding two parse trees for some input sequence)
Advertisements

Mooly Sagiv and Roman Manevich School of Computer Science
LESSON 18.
Cse321, Programming Languages and Compilers 1 6/12/2015 Lecture #10, Feb. 14, 2007 Modified sets of item construction Rules for building LR parse tables.
6/12/2015Prof. Hilfinger CS164 Lecture 111 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik)
Context-Free Grammars Lecture 7
Prof. Bodik CS 164 Lecture 81 Grammars and ambiguity CS164 3:30-5:00 TT 10 Evans.
Prof. Bodik CS 164 Lecture 61 Building a Parser II CS164 3:30-5:00 TT 10 Evans.
1 Foundations of Software Design Lecture 23: Finite Automata and Context-Free Grammars Marti Hearst Fall 2002.
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
COP4020 Programming Languages
Parsing II : Top-down Parsing Lecture 7 CS 4318/5331 Apan Qasem Texas State University Spring 2015 *some slides adopted from Cooper and Torczon.
1 Chapter 3 Context-Free Grammars and Parsing. 2 Parsing: Syntax Analysis decides which part of the incoming token stream should be grouped together.
CPSC Compiler Tutorial 3 Parser. Parsing The syntax of most programming languages can be specified by a Context-free Grammar (CGF) Parsing: Given.
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Context-Free Grammars and Parsing 1.
EECS 6083 Intro to Parsing Context Free Grammars
1 Lecture 10 Syntax-Directed Translation grammar disambiguation, Earley parser, syntax-directed translation Ras Bodik Shaon Barman Thibaud Hottelier Hack.
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
CMSC 330: Organization of Programming Languages
Top-Down Parsing - recursive descent - predictive parsing
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
10/13/2015IT 3271 Tow kinds of predictive parsers: Bottom-Up: The syntax tree is built up from the leaves Example: LR(1) parser Top-Down The syntax tree.
Introduction to Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Introduction to Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Context-Free Grammars
CS Describing Syntax CS 3360 Spring 2012 Sec Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison Wesley)
Context-Free Grammars and Parsing
PART I: overview material
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
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.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Parsing Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 3.
Left Recursion Lecture 7 Fri, Feb 4, 2005.
Homework #3 J. H. Wang Nov. 1, Homework #3 Chap. 4 –4.1 (c) –4.7 (c) –4.8 (a)(b)(c) –4.11.
Syntax Context-Free Grammars, Syntax Trees. CFG for Arithmetic Expressions E::= E op E| (E) | num op ::= + | * num ::= 0 | 1 | 2 | … Backus-Naur Form.
CFG1 CSC 4181Compiler Construction Context-Free Grammars Using grammars in parsers.
CMSC 330: Organization of Programming Languages
Introduction to Parsing
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 3: Introduction to Syntactic Analysis.
Parsing — Part II (Top-down parsing, left-recursion removal) Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
1 Context free grammars  Terminals  Nonterminals  Start symbol  productions E --> E + T E --> E – T E --> T T --> T * F T --> T / F T --> F F --> (F)
1 Nonrecursive Predictive Parsing  It is possible to build a nonrecursive predictive parser  This is done by maintaining an explicit stack.
Grammars CS 130: Theory of Computation HMU textbook, Chap 5.
Chapter 3 Context-Free Grammars Dr. Frank Lee. 3.1 CFG Definition The next phase of compilation after lexical analysis is syntax analysis. This phase.
Top-Down Parsing.
Syntax Analyzer (Parser)
1 Pertemuan 7 & 8 Syntax Analysis (Parsing) Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
CSE 5317/4305 L3: Parsing #11 Parsing #1 Leonidas Fegaras.
1 Introduction to Parsing. 2 Outline l Regular languages revisited l Parser overview Context-free grammars (CFG ’ s) l Derivations.
Spring 16 CSCI 4430, A Milanova 1 Announcements HW1 will be out this evening Due Monday, 2/8 Submit in HW Server AND at start of class on 2/8 A review.
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
Syntax Analysis Or Parsing. A.K.A. Syntax Analysis –Recognize sentences in a language. –Discover the structure of a document/program. –Construct (implicitly.
Syntax(1). 2 Syntax  The syntax of a programming language is a precise description of all its grammatically correct programs.  Levels of syntax Lexical.
CS510 Compiler Lecture 4.
Introduction to Parsing (adapted from CS 164 at Berkeley)
Syntax (1).
Syntax Specification and Analysis
Compiler Construction (CS-636)
CSE 3302 Programming Languages
Lecture 3: Introduction to Syntax (Cont’)
BOTTOM UP PARSING Lecture 16.
Lecture 7: Introduction to Parsing (Syntax Analysis)
Ambiguity, Precedence, Associativity & Top-Down Parsing
CSC 4181Compiler Construction Context-Free Grammars
CSC 4181 Compiler Construction Context-Free Grammars
BNF 9-Apr-19.
Presentation transcript:

CS 536 Spring Ambiguity Lecture 8

CS 536 Spring Announcement Reading Assignment –“Context-Free Grammars” (Sections 4.1, 4.2) Programming Assignment 2 –due Friday! Homework 1 –due in a week (Wed Feb 21) –not Feb 25!

CS 536 Spring Ambiguity = program structure not defined String id * id + id has two parse trees: E E EE E * id+ E E EE E+ *

CS 536 Spring Ambiguity A grammar is ambiguous if, for any string –it has more than one parse tree, or –there is more than one right-most derivation, or –there is more than one left-most derivation. (the three conditions are equivalent) Ambiguity is BAD –Leaves meaning of some programs ill-defined

CS 536 Spring Dealing with Ambiguity There are several ways to handle ambiguity We’ll discuss two of them: –rewriting the grammar –parser-generator declarations

CS 536 Spring Outline Rewriting: –Expression Grammars precedence associativity –IF-THEN-ELSE the Dangling-ELSE problem Declarations –Expression Grammars precedence associativity

CS 536 Spring Expression Grammars (precedence) Rewrite the grammar –use a different nonterminal for each precedence level –start with the lowest precedence (MINUS) E  E - E | E / E | ( E ) | id rewrite to E  E - E | T T  T / T | F F  id | ( E )

CS 536 Spring Example parse tree for id – id / id E  E - E | T T  T / T | F F  id | ( E ) E E FF T - id / T F TT E

CS 536 Spring TEST YOURSELF #1 Attempt to construct a parse tree for id-id/id that shows the wrong precedence. Question: –Why do you fail to construct this parse tree?

CS 536 Spring Associativity The grammar captures operator precedence, but it is still ambiguous! –fails to express that both subtraction and division are left associative; e.g., is equivalent to: ((5-3)-2) and not to: (5-(3-2)). TEST YOURSELF #3 –Draw two parse trees for the expression using the grammar given above; one that correctly groups 5-3, and one that incorrectly groups 3-2.

CS 536 Spring Recursion A grammar is recursive in nonterminal X if: –X  + … X … recall that  + means “in one or more steps, X derives a sequence of symbols that includes an X” A grammar is left recursive in X if: –X  + X … in one or more steps, X derives a sequence of symbols that starts with an X A grammar is right recursive in X if: –X  + … X in one or more steps, X derives a sequence of symbols that ends with an X

CS 536 Spring How to fix associativity The grammar given above is both left and right recursive in nonterminals exp and term –try at home: write the derivation steps that show this. To correctly expresses operator associativity: –For left associativity, use left recursion. –For right associativity, use right recursion. Here's the correct grammar: E  E – T | T T  T / F | F F  id | ( E )

CS 536 Spring Ambiguity: The Dangling Else Consider the grammar E  if E then E | if E then E else E | print This grammar is also ambiguous

CS 536 Spring The Dangling Else: Example The expression if E 1 then if E 2 then E 3 else E 4 has two parse trees if E1E1 E2E2 E3E3 E4E4 E1E1 E2E2 E3E3 E4E4 Typically we want the second form

CS 536 Spring The Dangling Else: A Fix else matches the closest unmatched then We can describe this in the grammar E  MIF /* all then are matched */ | UIF /* some then are unmatched */ MIF  if E then MIF else MIF | print UIF  if E then E | if E then MIF else UIF Describes the same set of strings

CS 536 Spring The Dangling Else: Example Revisited The expression if E 1 then if E 2 then E 3 else E 4 if E1E1 E2E2 E3E3 E4E4 E1E1 E2E2 E3E3 E4E4 Not valid because the then expression is not a MIF A valid parse tree (for a UIF)

CS 536 Spring Precedence and Associativity Declarations Instead of rewriting the grammar –Use the more natural (ambiguous) grammar –Along with disambiguating declarations Most parser generators allow precedence and associativity declarations to disambiguate grammars Examples …

CS 536 Spring Associativity Declarations Consider the grammar E  E - E | int Ambiguous: two parse trees of int - int - int E E EE E - int- E E EE E- - Left associativity declaration: %left +

CS 536 Spring Precedence Declarations Consider the grammar E  E + E | E * E | int –And the string int + int * int E E EE E + int* E E EE E* + Precedence declarations: %left + %left *