Ambiguity. Lecture 8. CS 536 Spring

Similar documents
Grammars and ambiguity. CS164 3:30-5:00 TT 10 Evans. Prof. Bodik CS 164 Lecture 8 1

Parsing: Derivations, Ambiguity, Precedence, Associativity. Lecture 8. Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

E E+E E E (E) id. id + id E E+E. id E + E id id + E id id + id. Overview. derivations and parse trees. Grammars and ambiguity. ambiguous grammars

( ) i 0. Outline. Regular languages revisited. Introduction to Parsing. Parser overview. Context-free grammars (CFG s) Lecture 5.

Outline. Regular languages revisited. Introduction to Parsing. Parser overview. Context-free grammars (CFG s) Lecture 5. Derivations.

Introduction to Parsing. Lecture 5. Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

Introduction to Parsing. Lecture 5

Introduction to Parsing. Lecture 5

Introduction to Parsing Ambiguity and Syntax Errors

Introduction to Parsing Ambiguity and Syntax Errors

Ambiguity, Precedence, Associativity & Top-Down Parsing. Lecture 9-10

Context-Free Grammars

Context-Free Grammars

Ambiguity. Grammar E E + E E * E ( E ) int. The string int * int + int has two parse trees. * int

Intro To Parsing. Step By Step

Ambiguity and Errors Syntax-Directed Translation

Outline. Parser overview Context-free grammars (CFG s) Derivations Syntax-Directed Translation

Formal Languages and Compilers Lecture V: Parse Trees and Ambiguous Gr

Parsing Part II. (Ambiguity, Top-down parsing, Left-recursion Removal)

CS 314 Principles of Programming Languages

Compilers Course Lecture 4: Context Free Grammars

Introduction to Parsing. Lecture 8

Defining syntax using CFGs

Outline. Limitations of regular languages. Introduction to Parsing. Parser overview. Context-free grammars (CFG s)

3. Context-free grammars & parsing

COMPILER (CSE 4120) (Lecture 6: Parsing 4 Bottom-up Parsing )

Principles of Programming Languages COMP251: Syntax and Grammars

([1-9] 1[0-2]):[0-5][0-9](AM PM)? What does the above match? Matches clock time, may or may not be told if it is AM or PM.

MIDTERM EXAMINATION - CS130 - Spring 2003

Lecture 8: Deterministic Bottom-Up Parsing

Conflicts in LR Parsing and More LR Parsing Types

Parsing II Top-down parsing. Comp 412

Defining syntax using CFGs

Eng. Maha Talaat Page 1

Bottom-Up Parsing. Lecture 11-12

CS153: Compilers Lecture 4: Recursive Parsing

Syntax Analysis Check syntax and construct abstract syntax tree

EDAN65: Compilers, Lecture 04 Grammar transformations: Eliminating ambiguities, adapting to LL parsing. Görel Hedin Revised:

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

Lecture 7: Deterministic Bottom-Up Parsing

Recursive Descent Parsers

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS

Bottom-Up Parsing. Lecture 11-12

Introduction to Bottom-Up Parsing

Lecture 14: Parser Conflicts, Using Ambiguity, Error Recovery. Last modified: Mon Feb 23 10:05: CS164: Lecture #14 1

CSE431 Translation of Computer Languages

Midterm I (Solutions) CS164, Spring 2002

EECS 6083 Intro to Parsing Context Free Grammars

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS

Programming Languages & Translators PARSING. Baishakhi Ray. Fall These slides are motivated from Prof. Alex Aiken: Compilers (Stanford)

CPS 506 Comparative Programming Languages. Syntax Specification

Building a Parser II. CS164 3:30-5:00 TT 10 Evans. Prof. Bodik CS 164 Lecture 6 1

afewadminnotes CSC324 Formal Language Theory Dealing with Ambiguity: Precedence Example Office Hours: (in BA 4237) Monday 3 4pm Wednesdays 1 2pm

CSE 3302 Programming Languages Lecture 2: Syntax

Administrativia. WA1 due on Thu PA2 in a week. Building a Parser III. Slides on the web site. CS164 3:30-5:00 TT 10 Evans.

Announcements. Written Assignment 1 out, due Friday, July 6th at 5PM.

Compiler Design Concepts. Syntax Analysis

Administrativia. PA2 assigned today. WA1 assigned today. Building a Parser II. CS164 3:30-5:00 TT 10 Evans. First midterm. Grammars.

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part2 3.3 Parse Trees and Abstract Syntax Trees

CS 536 Midterm Exam Spring 2013

Outline. Limitations of regular languages Parser overview Context-free grammars (CFG s) Derivations Syntax-Directed Translation

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS

CS415 Compilers. Syntax Analysis. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Syntax/semantics. Program <> program execution Compiler/interpreter Syntax Grammars Syntax diagrams Automata/State Machines Scanning/Parsing

Compilation 2013 Parser Generators, Conflict Management, and ML-Yacc

University of Technology Department of Computer Sciences. Final Examination st Term. Subject:Compilers Design

Building a Parser III. CS164 3:30-5:00 TT 10 Evans. Prof. Bodik CS 164 Lecture 6 1

Introduction to Parsing

In One Slide. Outline. LR Parsing. Table Construction

syntax tree - * * * - * * * * * 2 1 * * 2 * (2 * 1) - (1 + 0)

Parsing #1. Leonidas Fegaras. CSE 5317/4305 L3: Parsing #1 1

LR Parsing - Conflicts

Question Points Score

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS. Regrades 10/6/15. Prelim 1. Prelim 1. Expression trees. Pointers to material

CSE 401 Midterm Exam Sample Solution 2/11/15

Derivations vs Parses. Example. Parse Tree. Ambiguity. Different Parse Trees. Context Free Grammars 9/18/2012

If you are going to form a group for A2, please do it before tomorrow (Friday) noon GRAMMARS & PARSING. Lecture 8 CS2110 Spring 2014

LR Parsing LALR Parser Generators

Sometimes an ambiguous grammar can be rewritten to eliminate the ambiguity.

1. Explain the input buffer scheme for scanning the source program. How the use of sentinels can improve its performance? Describe in detail.

Grammars & Parsing. Lecture 12 CS 2112 Fall 2018

LR Parsing - The Items

LECTURE 3. Compiler Phases

CS 44 Exam #2 February 14, 2001

LL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012

Compilers - Chapter 2: An introduction to syntax analysis (and a complete toy compiler)

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Part 5 Program Analysis Principles and Techniques

Outline. The strategy: shift-reduce parsing. Introduction to Bottom-Up Parsing. A key concept: handles

Review main idea syntax-directed evaluation and translation. Recall syntax-directed interpretation in recursive descent parsers

Principles of Programming Languages COMP251: Syntax and Grammars

CS 406/534 Compiler Construction Parsing Part I

CMSC 330: Organization of Programming Languages. Context-Free Grammars Ambiguity

Chapter 4: Syntax Analyzer

CSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Spring UW CSE P 501 Spring 2018 C-1

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised:

Parsing. Roadmap. > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing

Dr. D.M. Akbar Hussain

Parsing. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.

3. Parsing. Oscar Nierstrasz

Transcription:

Ambiguity Lecture 8 CS 536 Spring 2001 1

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 2001 2

Ambiguity = program structure not defined + () id String id * id + id has two parse trees: + * * id id + id id id id CS 536 Spring 2001 3

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 2001 4

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 2001 5

Outline Rewriting: xpression Grammars precedence associativity IF-THN-LS the Dangling-LS problem Declarations xpression Grammars precedence associativity CS 536 Spring 2001 6

xpression Grammars (precedence) Rewrite the grammar use a different nonterminal for each precedence level start with the lowest precedence (MINUS) - / ( ) id rewrite to - T T T / T F F id ( ) CS 536 Spring 2001 7

xample parse tree for id id / id - T T T / T F F id ( ) T - T F T F / T F id id id CS 536 Spring 2001 8

TST YOURSLF #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 2001 9

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

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 2001 11

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: T T T T / F F F id ( ) CS 536 Spring 2001 12

Ambiguity: The Dangling lse Consider the grammar if then if then else pr This grammar is also ambiguous CS 536 Spring 2001 13

The Dangling lse: xample The expression if 1 then if 2 then 3 else 4 has two parse trees if if 1 if 4 1 if 2 3 2 3 4 Typically we want the second form CS 536 Spring 2001 14

The Dangling lse: A Fix else matches the closest unmatched then We can describe this in the grammar MIF /* all then are matched */ UIF /* some then are unmatched */ MIF if then MIF else MIF pr UIF if then if then MIF else UIF Describes the same set of strings CS 536 Spring 2001 15

The Dangling lse: xample Revisited The expression if 1 then if 2 then 3 else 4 if if 1 if 1 if 4 2 3 4 2 3 A valid parse tree (for a UIF) Not valid because the then expression is not a MIF CS 536 Spring 2001 16

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 xamples CS 536 Spring 2001 17

Associativity Declarations Consider the grammar - Ambiguous: two parse trees of - - - - - - Left associativity declaration: %left + CS 536 Spring 2001 18

Precedence Declarations Consider the grammar + * And the string + * * + + * Precedence declarations: %left + %left * CS 536 Spring 2001 19