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

Similar documents
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

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

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

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

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

Introduction to Parsing. Lecture 5

Introduction to Parsing Ambiguity and Syntax Errors

Ambiguity. Lecture 8. CS 536 Spring

Introduction to Parsing Ambiguity and Syntax Errors

Introduction to Parsing. Lecture 5

Intro To Parsing. Step By Step

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

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

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

Context-Free Grammars

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

Context-Free Grammars

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

Ambiguity and Errors Syntax-Directed Translation

Introduction to Parsing. Lecture 8

Syntax Analysis Check syntax and construct abstract syntax tree

([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.

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

Compiler Design Concepts. Syntax Analysis

Compilers Course Lecture 4: Context Free Grammars

CS 314 Principles of Programming Languages

CSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Winter /15/ Hal Perkins & UW CSE C-1

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

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

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

Principles of Programming Languages COMP251: Syntax and Grammars

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

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

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

Introduction to Parsing

EECS 6083 Intro to Parsing Context Free Grammars

Fall Compiler Principles Context-free Grammars Refresher. Roman Manevich Ben-Gurion University of the Negev

Compilers and computer architecture From strings to ASTs (2): context free grammars

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

Syntax Analysis Part I

Principle of Compilers Lecture IV Part 4: Syntactic Analysis. Alessandro Artale

LR Parsing LALR Parser Generators

MIDTERM EXAMINATION - CS130 - Spring 2003

Part 5 Program Analysis Principles and Techniques

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

Bottom-Up Parsing. Lecture 11-12

A Simple Syntax-Directed Translator

Context-Free Grammar (CFG)

CIT Lecture 5 Context-Free Grammars and Parsing 4/2/2003 1

Bottom-Up Parsing. Lecture 11-12

CSE 3302 Programming Languages Lecture 2: Syntax

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

CS 406/534 Compiler Construction Parsing Part I

Lecture Notes on Bottom-Up LR Parsing

CSCI312 Principles of Programming Languages!

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

CSE 401 Midterm Exam Sample Solution 2/11/15

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

CMSC 330: Organization of Programming Languages. Architecture of Compilers, Interpreters

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

Lecture Notes on Bottom-Up LR Parsing

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

3. Parsing. Oscar Nierstrasz

Conflicts in LR Parsing and More LR Parsing Types

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.

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

Midterm I (Solutions) CS164, Spring 2002

Formal Languages and Grammars. Chapter 2: Sections 2.1 and 2.2

CMSC 330: Organization of Programming Languages. Context Free Grammars

Formal Languages and Compilers Lecture VII Part 4: Syntactic A

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

Parsing II Top-down parsing. Comp 412

CSE 401 Midterm Exam Sample Solution 11/4/11

MA513: Formal Languages and Automata Theory Topic: Context-free Grammars (CFG) Lecture Number 18 Date: September 12, 2011

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. Context Free Grammars

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

UPTR - a simple parse tree representation format

CSE 12 Abstract Syntax Trees

ECE251 Midterm practice questions, Fall 2010

programming languages need to be precise a regular expression is one of the following: tokens are the building blocks of programs

3. Context-free grammars & parsing

CPS 506 Comparative Programming Languages. Syntax Specification

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

LR Parsing LALR Parser Generators

Compilerconstructie. najaar Rudy van Vliet kamer 140 Snellius, tel rvvliet(at)liacs(dot)nl. college 3, vrijdag 22 september 2017

CSE431 Translation of Computer Languages

Syntax Analysis. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

In One Slide. Outline. LR Parsing. Table Construction

Syntax Analysis. The Big Picture. The Big Picture. COMP 524: Programming Languages Srinivas Krishnan January 25, 2011

Architecture of Compilers, Interpreters. CMSC 330: Organization of Programming Languages. Front End Scanner and Parser. Implementing the Front End

Eng. Maha Talaat Page 1

Syntax-Directed Translation

Lecture 8: Deterministic Bottom-Up Parsing

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

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

Chapter 3. Parsing #1

Transcription:

Grammars and ambiguity CS164 3:30-5:00 TT 10 vans 1

Overview derivations and parse trees different derivations produce may produce same parse tree ambiguous grammars what they are and how to fix them 2

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 3

Derivation xample Grammar () id String id id id 4

Derivation xample (Cont.) id * id id id id id id id id 5

Derivation in Detail (1) 6

Derivation in Detail (2) 7

Derivation in Detail (3) * 8

Derivation in Detail (4) * id id 9

Derivation in Detail (5) id * id id id id 10

Derivation in Detail (6) id * id id id id id id id id 11

Notes on Derivations A parse tree has Terminals at the leaves Non-terminals at the erior 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 12

Left-most and Right-most Derivations The example is a leftmost derivation At each step, replace the left-most non-terminal There is an equivalent notion of a right-most derivation id id id id id id id 13

Right-most Derivation in Detail (1) 14

Right-most Derivation in Detail (2) 15

Right-most Derivation in Detail (3) id id 16

Right-most Derivation in Detail (4) id * id id 17

Right-most Derivation in Detail (5) id id * id id id id 18

Right-most Derivation in Detail (6) id id * id id id id id id id id 19

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 20

ambiguity

Ambiguity Grammar * ( ) Strings * 22

Ambiguity. xample This string has two parse trees is left-associative 23

Ambiguity. xample This string has two parse trees * * * has higher precedence than 24

Ambiguity (Cont.) A grammar is ambiguous if it has more than one parse tree for some string quivalently, 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-THN-LS 25

Dealing with Ambiguity There are several ways to handle ambiguity Most direct method is to rewrite the grammar unambiguously T T T T * ( ) nforces precedence of * over nforces left-associativity of and * 26

Ambiguity. xample The * has ony one parse tree now T * T T * 27

Ambiguity: The Dangling lse Consider the grammar S if then S if then S else S OTHR This grammar is also ambiguous 28

The Dangling lse: xample The expression if 1 then if 2 then S 3 else S 4 has two parse trees if if 1 if S 4 1 if 2 S 3 2 S 3 S 4 Typically we want the second form 29

The Dangling lse: 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 then MIF else MIF OTHR UIF if then S if then MIF else UIF Describes the same set of strings 30

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

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 32

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 xamples 33

Associativity Declarations Consider the grammar Ambiguous: two parse trees of Left-associativity declaration: %left 34

Precedence Declarations Consider the grammar * And the string * * * Precedence declarations: %left %left * 35