Prof. Bodik CS 164 Lecture 81 Grammars and ambiguity CS164 3:30-5:00 TT 10 Evans.

Slides:



Advertisements
Similar presentations
1 Parsing The scanner recognizes words The parser recognizes syntactic units Parser operations: Check and verify syntax based on specified syntax rules.
Advertisements

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 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)
Prof. Fateman CS 164 Lecture 91 Bottom-Up Parsing Lecture 9.
CS 536 Spring Ambiguity Lecture 8. CS 536 Spring Announcement Reading Assignment –“Context-Free Grammars” (Sections 4.1, 4.2) Programming.
Prof. Bodik CS 164 Lecture 91 Bottom-up parsing CS164 3:30-5:00 TT 10 Evans.
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.
CPSC Compiler Tutorial 3 Parser. Parsing The syntax of most programming languages can be specified by a Context-free Grammar (CGF) Parsing: Given.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Context-Free Grammars and Parsing 1.
EECS 6083 Intro to Parsing Context Free Grammars
8/19/2015© Hal Perkins & UW CSEC-1 CSE P 501 – Compilers Parsing & Context-Free Grammars Hal Perkins Winter 2008.
1 Lecture 10 Syntax-Directed Translation grammar disambiguation, Earley parser, syntax-directed translation Ras Bodik Shaon Barman Thibaud Hottelier Hack.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
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.
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
PART I: overview material
CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Syntax (cont.) Lecture 4 – Syntax (Cont.), Spring CSE3302 Programming Languages,
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.
Compiler Parsing 邱锡鹏 复旦大学计算机科学技术学院
Parsing Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 3.
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.
Introduction to Parsing
Prof. Necula CS 164 Lecture 8-91 Bottom-Up Parsing LR Parsing. Parser Generators. Lecture 6.
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.
Syntax Analysis - Parsing Compiler Design Lecture (01/28/98) Computer Science Rensselaer Polytechnic.
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.
Top-down Parsing. 2 Parsing Techniques Top-down parsers (LL(1), recursive descent) Start at the root of the parse tree and grow toward leaves Pick a production.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Top-Down Parsing.
Syntax Analyzer (Parser)
Programming Languages and Design Lecture 2 Syntax Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
1 Introduction to Parsing. 2 Outline l Regular languages revisited l Parser overview Context-free grammars (CFG ’ s) l Derivations.
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
Introduction to Parsing
WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Dr. Mohammad Nassef Department of Computer Science, Faculty of Computers and Information, Cairo University.
CSE 3302 Programming Languages
Introduction to Parsing
Parsing & Context-Free Grammars
Programming Languages Translator
Bottom-up parsing Goal of parser : build a derivation
CS510 Compiler Lecture 4.
Fall Compiler Principles Context-free Grammars Refresher
Introduction to Parsing (adapted from CS 164 at Berkeley)
Compiler Construction (CS-636)
CSE 3302 Programming Languages
Parsing & Context-Free Grammars Hal Perkins Autumn 2011
(Slides copied liberally from Ruth Anderson, Hal Perkins and others)
Syntax-Directed Translation
Lecture 7: Introduction to Parsing (Syntax Analysis)
Ambiguity, Precedence, Associativity & Top-Down Parsing
CSC 4181Compiler Construction Context-Free Grammars
Introduction to Parsing
Introduction to Parsing
CSC 4181 Compiler Construction Context-Free Grammars
Theory of Computation Lecture #
Fall Compiler Principles Context-free Grammars Refresher
Parsing & Context-Free Grammars Hal Perkins Summer 2004
Parsing & Context-Free Grammars Hal Perkins Autumn 2005
Presentation transcript:

Prof. Bodik CS 164 Lecture 81 Grammars and ambiguity CS164 3:30-5:00 TT 10 Evans

Prof. Bodik CS 164 Lecture 8 2 Overview derivations and parse trees –different derivations produce may produce same parse tree ambiguous grammars –what they are –and how to fix them

Prof. Bodik CS 164 Lecture 8 3 Recall: derivations and parse trees A derivation is a sequence of productions S  …  … A derivation can be drawn as a parse tree –Start symbol is the tree’s root –For a production X  Y 1 … Y n add children Y 1, …, Y n to node X You need parse trees to build ASTs

Prof. Bodik CS 164 Lecture 8 4 Derivation Example Grammar String

Prof. Bodik CS 164 Lecture 8 5 Derivation Example (Cont.) E E EE E+ id*

Prof. Bodik CS 164 Lecture 8 6 Derivation in Detail (1) E

Prof. Bodik CS 164 Lecture 8 7 Derivation in Detail (2) E EE+

Prof. Bodik CS 164 Lecture 8 8 Derivation in Detail (3) E E EE E+ *

Prof. Bodik CS 164 Lecture 8 9 Derivation in Detail (4) E E EE E+ * id

Prof. Bodik CS 164 Lecture 8 10 Derivation in Detail (5) E E EE E+ * id

Prof. Bodik CS 164 Lecture 8 11 Derivation in Detail (6) E E EE E+ id*

Prof. Bodik CS 164 Lecture 8 12 Notes on Derivations A parse tree has –Terminals at the leaves –Non-terminals at the interior nodes An in-order traversal of the leaves is the original input The parse tree shows the association of operations, the input string does not

Prof. Bodik CS 164 Lecture 8 13 Left-most and Right-most Derivations The example is a left- most derivation –At each step, replace the left-most non-terminal There is an equivalent notion of a right-most derivation

Prof. Bodik CS 164 Lecture 8 14 Right-most Derivation in Detail (1) E

Prof. Bodik CS 164 Lecture 8 15 Right-most Derivation in Detail (2) E EE+

Prof. Bodik CS 164 Lecture 8 16 Right-most Derivation in Detail (3) E EE+ id

Prof. Bodik CS 164 Lecture 8 17 Right-most Derivation in Detail (4) E E EE E+ id*

Prof. Bodik CS 164 Lecture 8 18 Right-most Derivation in Detail (5) E E EE E+ id*

Prof. Bodik CS 164 Lecture 8 19 Right-most Derivation in Detail (6) E E EE E+ id*

Prof. Bodik CS 164 Lecture 8 20 Derivations and Parse Trees Note that right-most and left-most derivations have the same parse tree The difference is only in the order in which branches are added

ambiguity

Prof. Bodik CS 164 Lecture 8 22 Ambiguity Grammar E  E + E | E * E | ( E ) | int Strings int + int + int int * int + int

Prof. Bodik CS 164 Lecture 8 23 Ambiguity. Example This string has two parse trees E E EE E + int+ E E EE E+ + + is left-associative

Prof. Bodik CS 164 Lecture 8 24 Ambiguity. Example This string has two parse trees E E EE E * int+ E E EE E+ * * has higher precedence than +

Prof. Bodik CS 164 Lecture 8 25 Ambiguity (Cont.) A grammar is ambiguous if it has more than one parse tree for some string –Equivalently, there is more than one right-most or left-most derivation for some string Ambiguity is bad –Leaves meaning of some programs ill-defined Ambiguity is common in programming languages –Arithmetic expressions –IF-THEN-ELSE

Prof. Bodik CS 164 Lecture 8 26 Dealing with Ambiguity There are several ways to handle ambiguity Most direct method is to rewrite the grammar unambiguously E  E + T | T T  T * int | int | ( E ) Enforces precedence of * over + Enforces left-associativity of + and *

Prof. Bodik CS 164 Lecture 8 27 Ambiguity. Example The int * int + int has ony one parse tree now E E EE E * int+ E T T T+ * E

Prof. Bodik CS 164 Lecture 8 28 Ambiguity: The Dangling Else Consider the grammar S  if E then S | if E then S else S | OTHER This grammar is also ambiguous

Prof. Bodik CS 164 Lecture 8 29 The Dangling Else: Example The expression if E 1 then if E 2 then S 3 else S 4 has two parse trees if E1E1 E2E2 S3S3 S4S4 E1E1 E2E2 S3S3 S4S4 Typically we want the second form

Prof. Bodik CS 164 Lecture 8 30 The Dangling Else: A Fix else matches the closest unmatched then We can describe this in the grammar (distinguish between matched and unmatched “then”) S  MIF /* all then are matched */ | UIF /* some then are unmatched */ MIF  if E then MIF else MIF | OTHER UIF  if E then S | if E then MIF else UIF Describes the same set of strings

Prof. Bodik CS 164 Lecture 8 31 The Dangling Else: Example Revisited The expression if E 1 then if E 2 then S 3 else S 4 if E1E1 E2E2 S3S3 S4S4 E1E1 E2E2 S3S3 S4S4 Not valid because the then expression is not a MIF A valid parse tree (for a UIF)

Prof. Bodik CS 164 Lecture 8 32 Ambiguity No general techniques for handling ambiguity Impossible to convert automatically an ambiguous grammar to an unambiguous one Used with care, ambiguity can simplify the grammar –Sometimes allows more natural definitions –We need disambiguation mechanisms

Prof. Bodik CS 164 Lecture 8 33 Precedence and Associativity Declarations Instead of rewriting the grammar –Use the more natural (ambiguous) grammar –Along with disambiguating declarations LR (bottom-up) parsers allow precedence and associativity declarations to disambiguate grammars Examples …

Prof. Bodik CS 164 Lecture 8 34 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 +

Prof. Bodik CS 164 Lecture 8 35 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 *