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

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

LR Parsing, Part 2. Constructing Parse Tables. An NFA Recognizing Viable Prefixes. Computing the Closure. GOTO Function and DFA States

Bottom-Up Parsing. Lecture 11-12

Bottom-Up Parsing. Lecture 11-12

Lexical and Syntax Analysis. Bottom-Up Parsing

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

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

Principles of Programming Languages

Conflicts in LR Parsing and More LR Parsing Types

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

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

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

In One Slide. Outline. LR Parsing. Table Construction

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

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

Lecture 8: Deterministic Bottom-Up Parsing

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

SLR parsers. LR(0) items

Monday, September 13, Parsers

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

Lecture 7: Deterministic Bottom-Up Parsing

Outline CS412/413. Administrivia. Review. Grammars. Left vs. Right Recursion. More tips forll(1) grammars Bottom-up parsing LR(0) parser construction

Wednesday, August 31, Parsers

CS 4120 Introduction to Compilers

LR Parsing LALR Parser Generators

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

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

Formal Languages and Compilers Lecture VII Part 4: Syntactic A

MODULE 14 SLR PARSER LR(0) ITEMS

Context-free grammars

Wednesday, September 9, 15. Parsers

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

LR Parsing LALR Parser Generators

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.

Compiler Construction: Parsing

Lecture Bottom-Up Parsing

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

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

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

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

LR Parsing. Table Construction

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

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

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

UNIT III & IV. Bottom up parsing

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

shift-reduce parsing

Bottom-Up Parsing LR Parsing

CS415 Compilers. LR Parsing & Error Recovery

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

More Bottom-Up Parsing

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

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

How do LL(1) Parsers Build Syntax Trees?

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

Bottom-Up Parsing II. Lecture 8

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

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

CSE302: Compiler Design

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

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.

CS453 : JavaCUP and error recovery. CS453 Shift-reduce Parsing 1

LR Parsing - The Items

Concepts Introduced in Chapter 4

LALR Parsing. What Yacc and most compilers employ.

UNIT-III BOTTOM-UP PARSING

Compiler Construction 2016/2017 Syntax Analysis

Downloaded from Page 1. LR Parsing

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

Syntax Analysis: Context-free Grammars, Pushdown Automata and Parsing Part - 4. Y.N. Srikant

LR Parsing Techniques

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


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

General Overview of Compiler

Review of CFGs and Parsing II Bottom-up Parsers. Lecture 5. Review slides 1

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

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

CSC 4181 Compiler Construction. Parsing. Outline. Introduction

Syntax Analyzer --- Parser

Syntax Analysis. Amitabha Sanyal. ( as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

CSE 401 Compilers. LR Parsing Hal Perkins Autumn /10/ Hal Perkins & UW CSE D-1

Acknowledgements. The slides for this lecture are a modified versions of the offering by Prof. Sanjeev K Aggarwal

Chapter 3: Lexing and Parsing

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

Compiler Construction

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

LR Parsers. Aditi Raste, CCOEW

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

Bottom Up Parsing Handout. 1 Introduction. 2 Example illustrating bottom-up parsing


Talen en Compilers. Johan Jeuring , period 2. January 17, Department of Information and Computing Sciences Utrecht University

Parsing Techniques. AST Review. AST Data Structures. Implicit AST Construction. AST Construction CS412/CS413. Introduction to Compilers Tim Teitelbaum

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

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

3. Parsing. Oscar Nierstrasz

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

1 Recursive Descent (LL(1) grammars)

CS502: Compilers & Programming Systems

CS606- compiler instruction Solved MCQS From Midterm Papers

Transcription:

LR(0) Parsing ummary C412/C41 Introduction to Compilers Tim Teitelbaum Lecture 10: LR Parsing February 12, 2007 LR(0) item = a production with a dot in RH LR(0) state = set of LR(0) items valid for viable prefixes Compute LR(0) states and build DFA: tart state: V(ε) = { [. ] } * Other states: V(αX) = V(α) x * Build the LR(0) parsing table from the DFA Use the LR(0) parsing table to determine whether to reduce or to shift C 412/41 pring 2007 Introduction to Compilers 1 C 412/41 pring 2007 Introduction to Compilers 2 LR(0) Limitations An LR(0) machine only works if each state with a reduce action has only one possible reduce action and no shift action With some grammars, construction gives states with shift/reduce or reduce/reduce conflicts Need to use look-ahead to choose ok [ L L,. ] shift /reduce [ L L,. ] [.,L ] reduce / reduce [L,L. ] [L. ] LR(0) Parsing Table ( ) id, ε L 1 s s2 g4 2 id id id id id s s2 g7 g5 4 accept 5 s6 s8 6 (L) (L) (L) (L) (L) 7 L L L L L 8 s s2 g9 9 L L, L L, L L, L L, L L, C 412/41 pring 2007 Introduction to Compilers C 412/41 pring 2007 Introduction to Compilers 4 A Non-LR(0) Grammar Grammar for addition of bers: Left-associative version is LR(0) Right-associative version is not LR(0) [. ] [. ] [. ] [. ] LR(0) Parsing Table 1 2 What to do in state 2: shift or reduce? [. ] [. ] [. ] [. ] [. ] [. ] ε 2 s/ 4 [. ] 6 5 [. ] [. ] C 412/41 pring 2007 Introduction to Compilers 5 C 412/41 pring 2007 Introduction to Compilers 6 1

LR(1) Parsing LR Parsing = easy extension of LR(0) For each reduction A β, look at the next symbol c Apply reduction only if c is in FOLLOW(A), or c= ε and * γa LR parsing table eliminates some conflicts ame as LR(0) table except reduction rows Adds reductions A βonly in the columns of symbols in FOLLOW(A) xample: FOLLOW()={} but * γ ε 2 s LR Parsing Table Reductions do not fill entire rows Otherwise, same as LR(0) ε 2 s s4 g2 g5 4 5 6 s7 7 accept C 412/41 pring 2007 Introduction to Compilers 7 C 412/41 pring 2007 Introduction to Compilers 8 LR(k) Use the LR(0) machine states as rows of table Let Q be a state and u be a lookahead string Action(Q,u) = shift Goto(Q,b) if Q contains an item of the form [A β 1.bβ ], with u FIRT k (bβ FOLLOW k (A)) Action(Q,u) = accept if Q = { [. ] } and u=ε Action(Q,u) = reduce i if Q contains the item [A β.], where A βis the ith production of G and u FOLLOW k (A), or u=ε and * γa Action(Q,u) = error otherwise G is LR(k) iff the Action function given above is single-valued for all Q and u, i.e, there are no shift-reduce or reduce-reduce conflicts. C 412/41 pring 2007 Introduction to Compilers 9 LR(1) Parsing Get as much power as possible out of 1 lookahead symbol parsing table LR(1) grammar = recognizable by a shift/reduce parser with 1-symbol look-ahead LR(1) parsing uses similar concepts as LR(0) Parser states = sets of items LR(1) item = LR(0) item look-ahead symbol following the production LR(0) item : [. ] LR(1) item : [. ] C 412/41 pring 2007 Introduction to Compilers 10 LR(1) tates LR(1) state = set of LR(1) items LR(1) item = [ A α.β b], where b in Σ {ε} Meaning: α already matched at top of the stack; next expect to see βb horthand notation [ A α. B b 1,, b n ] means: [ A α. β b 1 ] [ A α. β b n ] xtend closure and goto operations [.,ε] [. ] LR(1) Closure LR(1) closure operation on set of items For each item in : [A α.bβ b] and for each production B γ, add the following item to : [B.γ FIRT(βb)], or [B.γ ε] if FIRT(βb)={} Repeat until nothing changes imilar to LR(0) closure, but also keeps track of the look-ahead symbol C 412/41 pring 2007 Introduction to Compilers 11 C 412/41 pring 2007 Introduction to Compilers 12 2

LR(1) tart tate Initial state: start with [. apply the closure operation xample: sum grammar ['. ε] closure ε], then ['. ε] [. ε] [.,ε] LR(1) Goto Operation LR(1) goto operation = describes transitions between LR(1) states Algorithm: for a state and a symbol Y ={[A αy.β b] [A α.yβ b] } Goto(, Y) = Closure( ) 1 [. ε] [. ε] Goto( 1, ) 2 Closure({ [. ε] }) C 412/41 pring 2007 Introduction to Compilers 1 C 412/41 pring 2007 Introduction to Compilers 14 LR(1) DFA Construction If = Goto(,X) then add an edge labeled X from to [ '. ε] [. ε] [.,ε] [. ε] [. ε] [.,ε] [. ε] [.,ε] [. ε] [. ε] [.,ε] LR(1) Reductions Reductions correspond to LR(1) items of the form [A β. x] [ '. ε] [. ε] [.,ε] [. ε] [. ε] [.,ε] [. ε] [.,ε] [. ε] [. ε] [.,ε] C 412/41 pring 2007 Introduction to Compilers 15 C 412/41 pring 2007 Introduction to Compilers 16 LR(1) Parsing Table Construction LR(1) Parsing Table xample ame as construction of LR(0) parsing table, except for reductions If [A β. b] state Q, then: Action(Q,b) is Reduce(A β) 1 2 [. ε] [. ε] [.,ε] [. ε] [. ε] [. ε] [. ε] [.,ε] Fragment of the Parsing table: ε 1 2 2 s C 412/41 pring 2007 Introduction to Compilers 17 C 412/41 pring 2007 Introduction to Compilers 18

LR(1) but not LR(1) Let G have productions aab Ac A a ε V(a) = { [ a.ab ] [ A a. ] [ A.a ] [ A. ] } FOLLOW(A) = {b,c} reduce-reduce conflict C 412/41 pring 2007 Introduction to Compilers 19 LALR(1) Grammars Problem with LR(1): too many states LALR(1) Parsing (Look-Ahead LR) Construct LR(1) DFA and then merge any two LR(1) states whose items are identical except look-ahead Results in smaller parser tables Theoretically less powerful than LR(1) [ id. ] [ id. ε] =? [. ε] [. ] LALR(1) Grammar = a grammar whose LALR(1) parsing table has no conflicts C 412/41 pring 2007 Introduction to Compilers 20 Classification of Grammars LL(1) LR(1) LALR(1) LR(1) LR(0) LR(k) LR(k1) LL(k) LL(k1) LL(k) LR(k) LR(0) LR(1) LALR(1) LR(1) C 412/41 pring 2007 Introduction to Compilers 21 Automate the Parsing Process Can automate: The construction of LR parsing tables The construction of shift-reduce parsers based on these parsing tables Automatic parser generators: yacc, bison, CUP LALR(1) parser generators Not much difference compared to LR(1) in practice maller parsing tables than LR(1) Augment LALR(1) grammar specification with declarations of precedence, associativity output: LALR(1) parser program C 412/41 pring 2007 Introduction to Compilers 22 Associativity hift/reduce Conflict [. ] [.,ε] What happens if we run this grammar through LALR construction? shift/reduce conflict shift: 1(2) reduce: (12) 12 ^ C 412/41 pring 2007 Introduction to Compilers 2 C 412/41 pring 2007 Introduction to Compilers 24 4

Grammar in CUP nonterminal ; terminal PLU, LPARN... precedence left PLU; when shifting a conflicts with reducing a production, choose reduce ::= PLU LPARN RPARN NUMBR ; Precedence CUP can also handle operator precedence T T T T ( ) ( ) C 412/41 pring 2007 Introduction to Compilers 25 C 412/41 pring 2007 Introduction to Compilers 26 Conflicts without Precedence ( ) [. ] [. ] [. ] [. ] C 412/41 pring 2007 Introduction to Compilers 27 Predecence in CUP precedence left PLU; precedence left TIM; // TIM > PLU ::= PLU TIM... RUL: in conflict, choose reduce if last terminal of production has higher precedence than symbol to be shifted; choose shift if vice-versa. In tie, use associativity (left or right) given by precedence rule [. ] [. ] [. ] [. ] reduce hift C 412/41 pring 2007 Introduction to Compilers 28 ummary Look-ahead information makes LR(1), LALR(1), LR(1) grammars expressive Automatic parser generators support LALR(1) grammars Precedence, associativity declarations simplify grammar writing C 412/41 pring 2007 Introduction to Compilers 29 5