LR Parsing - Conflicts

Similar documents
Recursive Descent Parsers

LR Parsing - The Items

CUP. Lecture 18 CUP User s Manual (online) Robb T. Koether. Hampden-Sydney College. Fri, Feb 27, 2015

The Critical-Path Algorithm

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

Minimal Spanning Trees

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

The Traveling Salesman Problem Brute Force Method

Scope and Parameter Passing

Scheduling and Digraphs

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

Solving Recursive Sequences by Iteration

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

The Traveling Salesman Problem Nearest-Neighbor Algorithm

The Class Construct Part 1

Magnification and Minification

Friends and Unary Operators

Scope and Parameter Passing

The CYK Parsing Algorithm

The Decreasing-Time Algorithm

The Pairwise-Comparison Method

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Fri, Feb 16, 2018

Programming Languages

The Plurality-with-Elimination Method

Recursive Linked Lists

Sampling Distribution Examples Sections 15.4, 15.5

Regular Expressions. Lecture 10 Sections Robb T. Koether. Hampden-Sydney College. Wed, Sep 14, 2016

Rotations and Translations

Building the Abstract Syntax Trees

Downloaded from Page 1. LR Parsing

The Graphics Pipeline

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

Implementing Linked Lists

Stacks and their Applications

XPath Lecture 34. Robb T. Koether. Hampden-Sydney College. Wed, Apr 11, 2012

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

Introduction to Compiler Design

Recognition of Tokens

Boxplots. Lecture 17 Section Robb T. Koether. Hampden-Sydney College. Wed, Feb 10, 2010

Linked Lists. Lecture 16 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 22, 2017

XQuery FLOWR Expressions Lecture 35

Density Curves Sections

Programming Languages

Basic PHP Lecture 17

Total Orders. Lecture 41 Section 8.5. Robb T. Koether. Hampden-Sydney College. Mon, Apr 8, 2013

List Iterator Implementation

Abstract Syntax Trees Synthetic and Inherited Attributes

Conflicts in LR Parsing and More LR Parsing Types

Displaying Distributions - Quantitative Variables

XPath. Lecture 36. Robb T. Koether. Wed, Apr 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, / 28

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Mon, Feb 22, 2016

Function Definition Syntax Tree

Lecture 8: Deterministic Bottom-Up Parsing

The Graphics Pipeline

Recursion. Lecture 26 Sections , Robb T. Koether. Hampden-Sydney College. Mon, Apr 6, 2015

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

Recursion. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 17, 2018

Bottom-Up Parsing. Lecture 11-12

The string Class. Lecture 21 Sections 2.9, 3.9, Robb T. Koether. Wed, Oct 17, Hampden-Sydney College

Webpage Navigation. Lecture 27. Robb T. Koether. Hampden-Sydney College. Mon, Apr 2, 2018

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

Relational Databases Lecture 2

Formal Languages and Compilers Lecture VII Part 4: Syntactic A

Let us construct the LR(1) items for the grammar given below to construct the LALR parsing table.

PHP Queries and HTML Forms Lecture 23

The Constructors. Lecture 7 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 1, 2017

The x86 Instruction Set

Street-Routing Problems

Relational Databases

XML Attributes. Lecture 33. Robb T. Koether. Hampden-Sydney College. Wed, Apr 25, 2018

Functional Dependencies and Normal Forms

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

Functional Dependencies and Normal Forms

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

In One Slide. Outline. LR Parsing. Table Construction

Dynamic Allocation of Memory

The Coefficient of Determination

MODULE 14 SLR PARSER LR(0) ITEMS

Basic PHP. Lecture 19. Robb T. Koether. Hampden-Sydney College. Mon, Feb 26, 2108

Integer Overflow. Lecture 8 Section 2.5. Robb T. Koether. Hampden-Sydney College. Mon, Jan 27, 2014

Introduction to Databases

Triggers. Lecture 14. Robb T. Koether. Hampden-Sydney College. Wed, Feb 14, 2018

The Constructors. Lecture 6 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 26, 2018

Parsing Wrapup. Roadmap (Where are we?) Last lecture Shift-reduce parser LR(1) parsing. This lecture LR(1) parsing

Nondeterministic Programming in C++

Ambient and Diffuse Light

Aggregation. Lecture 7 Section Robb T. Koether. Hampden-Sydney College. Wed, Jan 29, 2014

Bottom-Up Parsing. Lecture 11-12

Binary Tree Applications

JFlex Regular Expressions

AJAX. Lecture 26. Robb T. Koether. Fri, Mar 21, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) AJAX Fri, Mar 21, / 16

MIT Parse Table Construction. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

LALR Parsing. What Yacc and most compilers employ.

LR Parsing LALR Parser Generators

shift-reduce parsing

Function Usage. Lecture 15 Sections 6.3, 6.4. Robb T. Koether. Hampden-Sydney College. Mon, Oct 1, 2018

The Normal Distribution

Abstract Data Types. Lecture 23 Section 7.1. Robb T. Koether. Hampden-Sydney College. Wed, Oct 24, 2012

Transcription:

LR Parsing - Conflicts Lecture 15 Sections 4.5, 4.6 Robb T. Koether Hampden-Sydney College Fri, Feb 20, 2015 Robb T. Koether (Hampden-Sydney College) LR Parsing - Conflicts Fri, Feb 20, 2015 1 / 15

1 Shift/Reduce Conflicts 2 Precedence and Associativity 3 Assignment Robb T. Koether (Hampden-Sydney College) LR Parsing - Conflicts Fri, Feb 20, 2015 2 / 15

Example Example (A Simplified Grammar) We may simplify our grammar to E E + E E E * E E ( E ) E id In this form, the precedence rules for + and * are not implicit. They must be incorporated into the tables. Robb T. Koether (Hampden-Sydney College) LR Parsing - Conflicts Fri, Feb 20, 2015 3 / 15

Example Example (FIRST and FOLLOW) Nullable FIRST FOLLOW S No {(, id} {$} E No {(, id} {$, +, *, )} Robb T. Koether (Hampden-Sydney College) LR Parsing - Conflicts Fri, Feb 20, 2015 4 / 15

Outline 1 Shift/Reduce Conflicts 2 Precedence and Associativity 3 Assignment Robb T. Koether (Hampden-Sydney College) LR Parsing - Conflicts Fri, Feb 20, 2015 5 / 15

Shift/Reduce Conflicts It is possible that a cell will contain both a shift operation and a reduce operation. This is called a shift/reduce conflict. To choose between shift and reduce, each case must be considered on its own merit. Consider the case of E E + E E E and the inputs a + b * c and a * b + c. Robb T. Koether (Hampden-Sydney College) LR Parsing - Conflicts Fri, Feb 20, 2015 6 / 15

Reduce/Reduce Conflicts It is possible that a cell will contain two different reduce operations. This is called a reduce/reduce conflict. This occurs when a sequence of tokens matches the right-hand sides of two different productions at the same time. For each such conflict in the table, we must choose which reduction to apply. Robb T. Koether (Hampden-Sydney College) LR Parsing - Conflicts Fri, Feb 20, 2015 7 / 15

Reduce/Reduce Conflicts It is possible that a cell will contain two different reduce operations. This is called a reduce/reduce conflict. This occurs when a sequence of tokens matches the right-hand sides of two different productions at the same time. For each such conflict in the table, we must choose which reduction to apply. The presence of a reduce/reduce conflict indicates that you should find another grammar. Robb T. Koether (Hampden-Sydney College) LR Parsing - Conflicts Fri, Feb 20, 2015 7 / 15

Example Example (The Action and Goto Tables) State Action Goto + * ( ) id $ E 0 s2 s3 1 1 s4 s5 acc 2 s2 s3 6 3 r4 r4 r4 r4 4 s2 s3 7 5 s2 s3 8 6 s4 s5 s9 7 s4/r1 s5/r1 r1 r1 8 s4/r2 s5/r2 r2 r2 9 r3 r3 r3 r3 Robb T. Koether (Hampden-Sydney College) LR Parsing - Conflicts Fri, Feb 20, 2015 8 / 15

Outline 1 Shift/Reduce Conflicts 2 Precedence and Associativity 3 Assignment Robb T. Koether (Hampden-Sydney College) LR Parsing - Conflicts Fri, Feb 20, 2015 9 / 15

Example Example (Shift/Reduce Conflicts and Associativity) The shift/reduce conflict in cell (7, +) is between shifting a + and reducing by E E + E. If we choose shift, then we will make addition right associative. If we choose reduce, then we will make addition left associative. The case is similar in cell (8, *) regarding multiplication. Robb T. Koether (Hampden-Sydney College) LR Parsing - Conflicts Fri, Feb 20, 2015 10 / 15

Example Example (Shift/Reduce Conflicts and Precedence) The shift/reduce conflict in cell (8, +) is between shifting a + and reducing by E E * E. If we choose shift, then we will give multiplication a higher precedence than addition. If we choose reduce, then we will give addition a higher precedence than multiplication. The case is similar in cell (7, *). Robb T. Koether (Hampden-Sydney College) LR Parsing - Conflicts Fri, Feb 20, 2015 11 / 15

Example Example (The Action and Goto Tables) State Action Goto + * ( ) id $ E 0 s2 s3 1 1 s4 s6 acc 2 s2 s3 6 3 r4 r4 r4 r4 4 s2 s3 7 5 s2 s3 8 6 s4 s5 s9 7 r1 s5 r1 r1 8 r2 r2 r2 r2 9 r3 r3 r3 r3 Robb T. Koether (Hampden-Sydney College) LR Parsing - Conflicts Fri, Feb 20, 2015 12 / 15

Outline 1 Shift/Reduce Conflicts 2 Precedence and Associativity 3 Assignment Robb T. Koether (Hampden-Sydney College) LR Parsing - Conflicts Fri, Feb 20, 2015 13 / 15

Assignment Assignment Let the grammar be S ( L ) id L L, S S Parse the expression (id,id). (continued... ) Robb T. Koether (Hampden-Sydney College) LR Parsing - Conflicts Fri, Feb 20, 2015 14 / 15

Assignment Assignment The grammar for if and if-else statements is S if ( E ) S if ( E ) S else S id = num E id == num where S is a statement and E is an expression (boolean). Write the action and goto tables for this grammar. Find the shift/reduce conflict(s) and decide how to handle them. Robb T. Koether (Hampden-Sydney College) LR Parsing - Conflicts Fri, Feb 20, 2015 15 / 15