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

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

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

Introduction to Parsing. Lecture 5. 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

Introduction to Parsing Ambiguity and Syntax Errors

Ambiguity. Lecture 8. CS 536 Spring

Introduction to Parsing. Lecture 5

Introduction to Parsing Ambiguity and Syntax Errors

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

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

Intro To Parsing. Step By Step

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

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

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

Compiler Design Concepts. Syntax Analysis

Compilers Course Lecture 4: Context Free Grammars

CS 314 Principles of Programming Languages

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

Syntax Analysis Check syntax and construct abstract syntax tree

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

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

LR Parsing LALR Parser Generators

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

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

Bottom-Up Parsing. Lecture 11-12

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

Context-Free Grammar (CFG)

Bottom-Up Parsing. Lecture 11-12

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

Principles of Programming Languages COMP251: Syntax and Grammars

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

Introduction to Parsing

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

Syntax Analysis. Prof. James L. Frankel Harvard University. Version of 6:43 PM 6-Feb-2018 Copyright 2018, 2015 James L. Frankel. All rights reserved.

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

EECS 6083 Intro to Parsing Context Free Grammars

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

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

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

LR Parsing LALR Parser Generators

In One Slide. Outline. LR Parsing. Table Construction

Context Free Languages

CSE 3302 Programming Languages Lecture 2: Syntax

Conflicts in LR Parsing and More LR Parsing Types

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

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

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

CS153: Compilers Lecture 4: Recursive Parsing

Abstract Syntax Trees & Top-Down Parsing

More Bottom-Up Parsing

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

Natural Language Processing

Lecture Notes on Bottom-Up LR Parsing

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

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

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

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

Context Free Grammars. CS154 Chris Pollett Mar 1, 2006.

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

Lecture Notes on Bottom-Up LR Parsing

Introduction to Bottom-Up Parsing

Error Handling Syntax-Directed Translation Recursive Descent Parsing

COP 3402 Systems Software Top Down Parsing (Recursive Descent)

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.

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

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

Dr. D.M. Akbar Hussain

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

3. Parsing. Oscar Nierstrasz

CS1622. Today. A Recursive Descent Parser. Preliminaries. Lecture 9 Parsing (4)

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

A Simple Syntax-Directed Translator

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

Where We Are. CMSC 330: Organization of Programming Languages. This Lecture. Programming Languages. Motivation for Grammars

CMSC 330: Organization of Programming Languages. Context Free Grammars

Parsing. source code. while (k<=n) {sum = sum+k; k=k+1;}

LR Parsing. Table Construction

MIT Specifying Languages with Regular Expressions and Context-Free Grammars. Martin Rinard Massachusetts Institute of Technology

Bottom-Up Parsing II (Different types of Shift-Reduce Conflicts) Lecture 10. Prof. Aiken (Modified by Professor Vijay Ganesh.

CS 406/534 Compiler Construction Parsing Part I

3. Context-free grammars & parsing

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

Syntax Analysis Part I

ECE251 Midterm practice questions, Fall 2010

Parsing II Top-down parsing. Comp 412

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. Context Free Grammars

Part 5 Program Analysis Principles and Techniques

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

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

Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5

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

Eng. Maha Talaat Page 1

Transcription:

Parsing: Derivations, Ambiguity, Precedence, Associativity Lecture 8 (Modified by Professor Vijay Ganesh) 1

Topics covered so far Regular languages and Finite automaton Parser overview Context-free grammars (CFG s) Derivations First, follow, predict sets 2

Outline of Today s Lecture Ambiguity in context-free grammars Left and right derivations Associativity Precedence Bottom-up Parsing 3

CFGs (Cont.) A CFG consists of A set of terminals T A set of non-terminals N A start symbol S (a non-terminal) A set of productions X YY L Y 1 2 n where { } X N and Y T N ε i 4

xamples of CFGs (cont.) Simple arithmetic expressions: + ( ) 5

Arithmetic xample Simple arithmetic expressions: + () Some elements of the language: + () () () 6

Derivation xample Grammar + () String + 7

Derivation xample (Cont.) + + + + * + + 8

Derivation in Detail (1) 9

Derivation in Detail (2) + + 10

Derivation in Detail (3) + + + * 11

Derivation in Detail (4) + + + * + 12

Derivation in Detail (5) + + + + * + 13

Derivation in Detail (6) + + + + * + + 14

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 15

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 + + + + + 16

Ambiguity Grammar + () String + 17

Ambiguity (Cont.) This string has two parse trees + * * + 18

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 19

Dealing with Ambiguity There are several ways to handle ambiguity Most direct method is to rewrite grammar unambiguously + ' ' " () " () nforces precedence of * over + ' 20

Ambiguity in Arithmetic xpressions Recall the grammar + * ( ) The string * + has two parse trees: + * * + 21

Ambiguity: The Dangling lse Conser the grammar if then if then else OTHR This grammar is also ambiguous 22

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 23

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 is unmatched */ MIF if then MIF else MIF OTHR UIF if then if then MIF else UIF Describes the same set of strings (Associativity) 24

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

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 26

Precedence and Associativity Declarations Instead of rewriting the grammar Use the more natural (ambiguous) grammar Along with disambiguating declarations Most grammar generators allow precedence and associativity declarations to disambiguate grammars xamples 27

Associativity Declarations Conser the grammar + Ambiguous: two parse trees of + + + + + + Left associativity declaration: %left + 28

Precedence Declarations Conser the grammar + * And the string + * * + + * Precedence declarations: %left + %left * 29