Conflicts in LR Parsing and More LR Parsing Types

Similar documents
In One Slide. Outline. LR Parsing. Table Construction

Bottom-Up Parsing. Lecture 11-12

LR Parsing LALR Parser Generators

LR Parsing. Table Construction

LR Parsing LALR Parser Generators

More Bottom-Up Parsing

Bottom-Up Parsing. Lecture 11-12

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

Lecture 7: Deterministic Bottom-Up Parsing

Lecture 8: Deterministic Bottom-Up Parsing

Compilers. Bottom-up Parsing. (original slides by Sam

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

Simple LR (SLR) LR(0) Drawbacks LR(1) SLR Parse. LR(1) Start State and Reduce. LR(1) Items 10/3/2012

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

Formal Languages and Compilers Lecture VII Part 4: Syntactic A

LALR stands for look ahead left right. It is a technique for deciding when reductions have to be made in shift/reduce parsing. Often, it can make the

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

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).

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

Lexical and Syntax Analysis. Bottom-Up Parsing

Lecture Bottom-Up Parsing

Table-driven using an explicit stack (no recursion!). Stack can be viewed as containing both terminals and non-terminals.

Example CFG. Lectures 16 & 17 Bottom-Up Parsing. LL(1) Predictor Table Review. Stacks in LR Parsing 1. Sʹ " S. 2. S " AyB. 3. A " ab. 4.

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

S Y N T A X A N A L Y S I S LR

LR(0) Parsing Summary. LR(0) Parsing Table. LR(0) Limitations. A Non-LR(0) Grammar. LR(0) Parsing Table CS412/CS413

Parsers. Xiaokang Qiu Purdue University. August 31, 2018 ECE 468

shift-reduce parsing

Context-free grammars

Wednesday, August 31, Parsers

Wednesday, September 9, 15. Parsers

Parsers. What is a parser. Languages. Agenda. Terminology. Languages. A parser has two jobs:

Compiler Construction: Parsing

Monday, September 13, Parsers

LALR Parsing. What Yacc and most compilers employ.

G53CMP: Lecture 4. Syntactic Analysis: Parser Generators. Henrik Nilsson. University of Nottingham, UK. G53CMP: Lecture 4 p.1/32

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

Downloaded from Page 1. LR Parsing

UNIT III & IV. Bottom up parsing

Principles of Programming Languages

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

MODULE 14 SLR PARSER LR(0) ITEMS

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

CS415 Compilers. LR Parsing & Error Recovery

LR Parsing. Leftmost and Rightmost Derivations. Compiler Design CSE 504. Derivations for id + id: T id = id+id. 1 Shift-Reduce Parsing.

Review: Shift-Reduce Parsing. Bottom-up parsing uses two actions: Bottom-Up Parsing II. Shift ABC xyz ABCx yz. Lecture 8. Reduce Cbxy ijk CbA ijk

Lecture Notes on Bottom-Up LR Parsing

Configuration Sets for CSX- Lite. Parser Action Table

A left-sentential form is a sentential form that occurs in the leftmost derivation of some sentence.

3. Syntax Analysis. Andrea Polini. Formal Languages and Compilers Master in Computer Science University of Camerino

LR Parsing - The Items

Bottom-up parsing. Bottom-Up Parsing. Recall. Goal: For a grammar G, withstartsymbols, any string α such that S α is called a sentential form

Parser Generation. Bottom-Up Parsing. Constructing LR Parser. LR Parsing. Construct parse tree bottom-up --- from leaves to the root

LR Parsing - Conflicts

Concepts Introduced in Chapter 4

Parsing. Handle, viable prefix, items, closures, goto s LR(k): SLR(1), LR(1), LALR(1)

Bottom up parsing. The sentential forms happen to be a right most derivation in the reverse order. S a A B e a A d e. a A d e a A B e S.

Bottom-Up Parsing II. Lecture 8

LL Parsing: A piece of cake after LR

Compiler Construction

How do LL(1) Parsers Build Syntax Trees?

CS143 Handout 20 Summer 2011 July 15 th, 2011 CS143 Practice Midterm and Solution

Compiler Construction

Chapter 3 Parsing. time, arrow, banana, flies, like, a, an, the, fruit

Lecture Notes on Bottom-Up LR Parsing

Fall Compiler Principles Lecture 5: Parsing part 4. Roman Manevich Ben-Gurion University

SYED AMMAL ENGINEERING COLLEGE (An ISO 9001:2008 Certified Institution) Dr. E.M. Abdullah Campus, Ramanathapuram

Midterm I - Solution CS164, Spring 2014


Compiler Construction

Bottom Up Parsing. Shift and Reduce. Sentential Form. Handle. Parse Tree. Bottom Up Parsing 9/26/2012. Also known as Shift-Reduce parsing

Fall Compiler Principles Lecture 4: Parsing part 3. Roman Manevich Ben-Gurion University of the Negev

Introduction to Parsing. Lecture 5

CS 4120 Introduction to Compilers

CSE302: Compiler Design

Action Table for CSX-Lite. LALR Parser Driver. Example of LALR(1) Parsing. GoTo Table for CSX-Lite

LR Parsing Techniques

CSE P 501 Compilers. LR Parsing Hal Perkins Spring UW CSE P 501 Spring 2018 D-1

Section A. A grammar that produces more than one parse tree for some sentences is said to be ambiguous.

Parser. Larissa von Witte. 11. Januar Institut für Softwaretechnik und Programmiersprachen. L. v. Witte 11. Januar /23

CS 314 Principles of Programming Languages

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

Introduction to Parsing. Lecture 5

Compilation 2012 Context-Free Languages Parsers and Scanners. Jan Midtgaard Michael I. Schwartzbach Aarhus University

Lecture Notes on Shift-Reduce Parsing

A Bison Manual. You build a text file of the production (format in the next section); traditionally this file ends in.y, although bison doesn t care.

CS143 Handout 14 Summer 2011 July 6 th, LALR Parsing

PART 3 - SYNTAX ANALYSIS. F. Wotawa TU Graz) Compiler Construction Summer term / 309

Syntax-Directed Translation

Syntax and Parsing COMS W4115. Prof. Stephen A. Edwards Fall 2003 Columbia University Department of Computer Science

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

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

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

UNIVERSITY OF CALIFORNIA

Using an LALR(1) Parser Generator

EXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully

Bottom-Up Parsing. Parser Generation. LR Parsing. Constructing LR Parser

Compiler Design 1. Bottom-UP Parsing. Goutam Biswas. Lect 6

Compiler Construction 2016/2017 Syntax Analysis

Midterm I (Solutions) CS164, Spring 2002

Transcription:

Conflicts in LR Parsing and More LR Parsing Types Lecture 10 Dr. Sean Peisert ECS 142 Spring 2009 1

Status Project 2 Due Friday, Apr. 24, 11:55pm The usual lecture time is being replaced by a discussion section on Friday, too. SemCheck 2

LR Parsing Tables Parsing tables (i.e., the DFA) can be constructed automatically for a CFG. But we still need to understand the construction to work with parser generators E.g., they report errors in terms of sets of items What kind of errors can we expect? 3

Shift/Reduce Conflicts If a DFA state contains both: [X α aß, b] and [Y γ, a] Then on input a we could either: Shift into state [X αa ß, b], or Reduce with [Y γ] This is called a shift-reduce conflict. 4

Shift/Reduce Conflicts Typically due to ambiguities in the grammar Classic example: the dangling else: S if E then S if E then S else S OTHER Will have a DFA state containing [S if E then S, else] [S if E then S else S, x ] If else follows then we can shift or reduce Default (bison, CUP, etc..) is to shift. Default behavior is as needed in this case. 5

Shift/Reduce Conflicts Consider the ambiguous grammar: E E + E E * E int Will have the DFA states containing [E E * E, +] [E E * E, +] [E E + E, +] E [E E + E, +] Again we have a shift/reduce input on + We need to reduce (* binds more tightly than +) Recall solution: declare precedence of + and * 6

Associativity Example: 1-2 - 5 Stack contains 1-2 and the look-ahead is - Left associative: (1-2) - 5 Right associative: 1 - (2-5) 7

Shift/Reduce Conflicts In bison declare precedence and associativity: %left + (left associative) %left * (left associative, higher precedence) Precedence of a rule = that of its last terminal See bison manual ( Conflict-Dependent Precedence ) for ways to override this default Bison resolves shift/reduce conflicts with a shift if: no precedence declared for either rule or terminal input terminal has higher precedence than the rule the precedences are the same and right associative 8

Using Precedence to Solve S/R Conflicts Example: [E E * E, +] [E E * E, +] [E E + E, +] E [E E + E, +] Will choose reduce because precedence of rule E E * E is higher than of terminal + (Last terminal in E E * E is * ) 9

Using Precedence to Solve S/R Conflicts Grammar: E E + E E * E int States: [E E + E, +] [E E + E, +] [E E + E, +] E [E E + E, +] Now we also have a shift/reduce on input + Reduce, because E E + E and + have the same precedence and + is left-associative. 10

Using Precedence to Solve S/R Conflicts Dangling else [S if E then S, else] [S if E then S else S, x] Can eliminate conflict by declaring else with higher precedence than then Or just rely on the default shift action But this starts to look like hacking the parser Best to avoid overuse of precedence declarations or you ll end up with unexpected parse trees. 11

Rewriting Grammar to Solve S/R Conflicts Ambiguous grammar (w/o precedence): E E + E E * E (E) int Input: int + int * int Rewritten Grammar: E E + T T T T * F F F (E) int gives + a lower precedence gives (, ) a higher precedence and only ONE parse tree 12

Reduce/Reduce Conflicts If a DFA state contains both: [X α, a] and [Y ß, a] Then on input a we don t know which production to reduce This is called a reduce-reduce conflict. 13

Reduce/Reduce Conflicts Usually due to gross ambiguity in the grammar Example: a sequence of identifiers S ε id id S There are two parse trees for the string id S id S id S id How does this confuse the parser? 14

Reduce/Reduce Conflicts Consider states: [S id, $] [S S, $] [S id S, $] [S, $] E [S, $] [S id, $] [S id, $] [S id S, $] [S id S, $] Reduce/reduce conflict on input $ S S id S S id S id Better rewrite the grammar S ε id S 15

Using Parser Generators Parser generators construct the parsing DFA given a CFG Use precedence declarations and default conventions to resolve conflicts The parser algorithm is the same for all grammars (and is provided as a library function) But most parser generators do not construct the DFA as described before Because the LR(1) parsing DFA has 1000s of states even for a simple language 16

LR(1) Parsing Tables are Big But many states are similar, e.g.: (1) E int, $/+ red. E int on $, + and (5) E int, )/+ red. E int on ), + Idea: merge the DFA states whose items differ only in the lookahead tokens. Such states have the same core. We obtain: (1 ) E int, $/+/) red. E int on $, +, ) 17

Core of a Set of LR Items Definition: the core of a set of LR items is the set of first components without the lookahead terminals Example: the core of is [X α ß, b] and [Y γ δ, d] [X α ß] and [Y γ δ] 18

LALR States Consider for example the LR(1) states: {[X α, a], [Y ß, c]} {[X α, b], [Y ß, d]} Same core, different lookahead. They can be merged. Merged state contains: {[X α, a/b], [Y ß, c/d]} These are called LALR(1) states Stands for LookAhead LR Typically 10 times fewer LALR(1) states than LR(1) 19

LALR(1) DFA Repeat until all states have distinct core: Choose two distinct states with the same core. Merge the states by creating a new one with the union of all the items Point edges from predecessors to new state New state points to all the previous successors. 20

21

LALR Parser Can Have Conflicts Consider for example, the LR(1) states {[X α, a], [Y ß, b]} {[X α, b], [Y ß, a]} And the merged LALR(1) state: {[X α, a/b], [Y ß, a/b]} Has a new reduce-reduce conflict In practice such cases are rare. However, no new shift/reduce conflicts. Why? 22

LALR vs. Canonical LR Parsing LALR languages are not natural They are an efficiency hack on LR languages. Any reasonable programming language has an LALR(1) grammar. LALR(1) has become a standard for programming languages and for parser generators. 23

SLR Parsers LALR uses lookahead sets computed by using closure operation. SLR only uses follow sets. Follow sets do not have context information, and so create more conflicts. LALR can be more selective because it has contexts, and so can avoid conflicts. 24

Grammar Hierarchy 25

End Notes Parsing: A solid foundation: CFGs A simple parser: LL(1) A more powerful parser: LR(1) An efficiency hack on LR(0): LALR(1) LALR(1) Parser Generators: yacc, bison, CUP Next Class:Top-Down Parsing LL(1) 26