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

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)

( ) 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 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

Context-Free Grammars

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

Ambiguity and Errors Syntax-Directed Translation

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

Introduction to Parsing. Lecture 8

Syntax Analysis Check syntax and construct abstract syntax tree

Compiler Design Concepts. Syntax Analysis

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

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

LR Parsing LALR Parser Generators

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

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

EECS 6083 Intro to Parsing Context Free Grammars

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

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

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

CSE 3302 Programming Languages Lecture 2: Syntax

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)

Bottom-Up Parsing. Lecture 11-12

CS 406/534 Compiler Construction Parsing Part I

HW 3: Bottom-Up Parsing Techniques

Lecture Notes on Bottom-Up LR Parsing

CSCI312 Principles of Programming Languages!

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

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

LR Parsing LALR Parser Generators

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 - Θεωρία και Πρακτική Μεταγλωττιστών

Syntax-Directed Translation

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

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

Formal Languages and Compilers Lecture VII Part 4: Syntactic A

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

Derivations of a CFG. MACM 300 Formal Languages and Automata. Context-free Grammars. Derivations and parse trees

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

Summary: Semantic Analysis

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

Question Points Score

3. Context-free grammars & parsing

Syntax Analysis. Tokens ---> Parse Tree. Main Problems. Grammars. Convert the list of tokens into a parse tree ( hierarchical analysis)

CPS 506 Comparative Programming Languages. Syntax Specification

CMSC 330: Organization of Programming Languages

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

Transcription:

Overview Grammars and ambiguity CS164 3:30-5:00 TT 10 vans derivations and parse trees dferent derivations produce may produce same parse tree ambiguous grammars what they are and how to fix them 1 2 Recall: derivations and parse trees A derivation is a sequence of productions S d d Derivation xample Grammar () A derivation can be drawn as a parse tree Start symbol is the tree s root For a production X d Y 1 Y n add children Y 1,, Y n to node X String You need parse trees to build ASTs 3 4 Derivation xample (Cont.) Derivation in Detail (1) 5 6 1

Derivation in Detail (2) Derivation in Detail (3) 7 8 Derivation in Detail (4) Derivation in Detail (5) 9 10 Derivation in Detail (6) 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 11 12 2

Left-most and Right-most Derivations Right-most Derivation in Detail (1) 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 13 14 Right-most Derivation in Detail (2) Right-most Derivation in Detail (3) 15 16 Right-most Derivation in Detail (4) Right-most Derivation in Detail (5) 17 18 3

Right-most Derivation in Detail (6) Derivations and Parse Trees Note that right-most and left-most derivations have the same parse tree The dference is only in the order in which branches are added 19 20 Ambiguity Grammar d ( ) ambiguity Strings 22 Ambiguity. xample Ambiguity. xample This string has two parse trees This string has two parse trees is left-associative has higher precedence than 23 24 4

Ambiguity (Cont.) A grammar is ambiguous 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 Dealing with Ambiguity There are several ways to handle ambiguity Most direct method is to rewrite the grammar unambiguously d T T T d T ( ) nforces precedence of over nforces left-associativity of and 25 26 Ambiguity. xample Ambiguity: The Dangling lse The has ony one parse tree now T T T Conser the grammar S then S then S else S OTHR This grammar is also ambiguous 27 28 The Dangling lse: xample The Dangling lse: A Fix The expression 1 then 2 then S 3 else S 4 has two parse trees 1 S 4 2 S 3 Typically we want the second form 1 2 S 3 S 4 29 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 then MIF else MIF OTHR UIF then S then MIF else UIF Describes the same set of strings 30 5

The Dangling lse: xample Revisited The expression 1 then 2 then S 3 else S 4 1 1 S 4 Ambiguity No general techniques for handling ambiguity Impossible to convert automatically an ambiguous grammar to an unambiguous one 2 S 3 S 4 A val parse tree (for a UIF) 2 S 3 Not val because the then expression is not a MIF Used with care, ambiguity can simply the grammar Sometimes allows more natural definitions We need disambiguation mechanisms 31 32 Precedence and Associativity Declarations Associativity Declarations Instead of rewriting the grammar Use the more natural (ambiguous) grammar Along with disambiguating declarations Conser the grammar Ambiguous: two parse trees of LR (bottom-up) parsers allow precedence and associativity declarations to disambiguate grammars xamples 33 Left-associativity declaration: %left 34 Precedence Declarations Conser the grammar And the string Precedence declarations: %left %left 35 6