Bottom-Up Parsing. Lecture 11-12

Similar documents
Bottom-Up Parsing. Lecture 11-12

Lecture 8: Deterministic Bottom-Up Parsing

Lecture 7: Deterministic Bottom-Up Parsing

LR Parsing LALR Parser Generators

LR Parsing LALR Parser Generators

In One Slide. Outline. LR Parsing. Table Construction

LR Parsing. Table Construction

Conflicts in LR Parsing and More LR Parsing Types

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

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

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

More Bottom-Up 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

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

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

Lexical and Syntax Analysis. Bottom-Up Parsing

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

Bottom-Up Parsing II. Lecture 8

Introduction to Bottom-Up Parsing

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

Intro to Bottom-up Parsing. Lecture 9

CS 4120 Introduction to Compilers

Syntax-Directed Translation. Lecture 14

shift-reduce parsing

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

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

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

Top-Down Parsing and Intro to Bottom-Up Parsing. Lecture 7

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

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

Lecture Bottom-Up Parsing

Top-Down Parsing and Intro to Bottom-Up Parsing. Lecture 7

Principles of Programming Languages

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

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

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

Monday, September 13, Parsers

Introduction to Parsing. Lecture 5

Wednesday, August 31, Parsers

Context-free grammars

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

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

Wednesday, September 9, 15. Parsers

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

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

Introduction to Parsing. Lecture 8

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

Compiler Construction: Parsing

Lecture Notes on Bottom-Up LR Parsing

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.

Lecture Notes on Bottom-Up LR Parsing

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

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

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

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

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

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

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

Introduction to Parsing. Lecture 5

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

UNIT III & IV. Bottom up parsing

4. Lexical and Syntax Analysis

3. Parsing. Oscar Nierstrasz

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

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

4. Lexical and Syntax Analysis

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.

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

Concepts Introduced in Chapter 4

How do LL(1) Parsers Build Syntax Trees?

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

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

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

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

Formal Languages and Compilers Lecture VII Part 4: Syntactic A

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

Chapter 4. Lexical and Syntax Analysis

Configuration Sets for CSX- Lite. Parser Action Table

CSE 130 Programming Language Principles & Paradigms Lecture # 5. Chapter 4 Lexical and Syntax Analysis

Compiler Construction 2016/2017 Syntax Analysis

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

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

Outline. Regular languages revisited. Introduction to Parsing. Parser overview. Context-free grammars (CFG s) Lecture 5. Derivations.


CS415 Compilers. LR Parsing & Error Recovery

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

Bottom-Up Parsing LR Parsing

Chapter 4. Lexical and Syntax Analysis. Topics. Compilation. Language Implementation. Issues in Lexical and Syntax Analysis.

LR Parsing Techniques

( ) i 0. Outline. Regular languages revisited. Introduction to Parsing. Parser overview. Context-free grammars (CFG s) Lecture 5.

LR Parsing - The Items

Parsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones

CS2210: Compiler Construction Syntax Analysis Syntax Analysis

LL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012

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

The Parsing Problem (cont d) Recursive-Descent Parsing. Recursive-Descent Parsing (cont d) ICOM 4036 Programming Languages. The Complexity of Parsing

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

Syntax Analysis Part I

Midterm I - Solution CS164, Spring 2014

Introduction to Parsing. Lecture 5. Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

Transcription:

Bottom-Up Parsing Lecture 11-12 (From slides by G. Necula & R. Bodik) 2/20/08 Prof. Hilfinger CS164 Lecture 11 1

Administrivia Test I during class on 10 March. 2/20/08 Prof. Hilfinger CS164 Lecture 11 2

Bottom-Up Parsing Bottom-up parsing is more general than topdown parsing And just as efficient Builds on ideas in top-down parsing Most common form is LR parsing L means that tokens are read left to right R means that it constructs a rightmost derivation 2/20/08 Prof. Hilfinger CS164 Lecture 11 3

An Introductory xample LR parsers don t need left-factored grammars and can also handle left-recursive grammars Consider the following grammar: + ( ) int Why is this not LL(1)? Consider the string: int + ( int ) + ( int ) 2/20/08 Prof. Hilfinger CS164 Lecture 11 4

The Idea LR parsing reduces a string to the start symbol by inverting productions: sent input string of terminals while sent S: Identify first β in sent such that A β is a production and S * α A γ α β γ = sent Replace β by A in sent (so α A γ becomes new sent) Such α β s are called handles 2/20/08 Prof. Hilfinger CS164 Lecture 11 5

A Bottom-up Parse in Detail (1) int + (int) + (int) int + ( int ) + ( int ) 2/20/08 Prof. Hilfinger CS164 Lecture 11 6

A Bottom-up Parse in Detail (2) int + (int) + (int) + (int) + (int) (handles in red) int + ( int ) + ( int ) 2/20/08 Prof. Hilfinger CS164 Lecture 11 7

A Bottom-up Parse in Detail (3) int + (int) + (int) + (int) + (int) + () + (int) int + ( int ) + ( int ) 2/20/08 Prof. Hilfinger CS164 Lecture 11 8

A Bottom-up Parse in Detail (4) int + (int) + (int) + (int) + (int) + () + (int) + (int) int + ( int ) + ( int ) 2/20/08 Prof. Hilfinger CS164 Lecture 11 9

A Bottom-up Parse in Detail (5) int + (int) + (int) + (int) + (int) + () + (int) + (int) + () int + ( int ) + ( int ) 2/20/08 Prof. Hilfinger CS164 Lecture 11 10

A Bottom-up Parse in Detail (6) int + (int) + (int) + (int) + (int) + () + (int) + (int) + () A reverse rightmost derivation int + ( int ) + ( int ) 2/20/08 Prof. Hilfinger CS164 Lecture 11 11

Where Do Reductions Happen Because an LR parser produces a reverse rightmost derivation: If αβγ is step of a bottom-up parse with handle αβ And the next reduction is by A β Then γ is a string of terminals! Because αaγ αβγ is a step in a right-most derivation Intuition: We make decisions about what reduction to use after seeing all symbols in handle, rather than before (as for LL(1)) 2/20/08 Prof. Hilfinger CS164 Lecture 11 12

Notation Idea: Split the string into two substrings Right substring (a string of terminals) is as yet unexamined by parser Left substring has terminals and non-terminals The dividing point is marked by a I The I is not part of the string Marks end of next potential handle Initially, all input is unexamined: Ix 1 x 2... x n 2/20/08 Prof. Hilfinger CS164 Lecture 11 13

Shift-Reduce Parsing Bottom-up parsing uses only two kinds of actions: Shift: Move I one place to the right, shifting a terminal to the left string + (I int ) + (int I ) Reduce: Apply an inverse production at the handle. If + ( ) is a production, then + ( + ( ) I ) +( I ) 2/20/08 Prof. Hilfinger CS164 Lecture 11 14

Shift-Reduce xample I int + (int) + (int)$ shift int + ( int ) + ( int )

Shift-Reduce xample I int + (int) + (int)$ shift int I + (int) + (int)$ red. int int + ( int ) + ( int )

Shift-Reduce xample I int + (int) + (int)$ shift int I + (int) + (int)$ red. int I + (int) + (int)$ shift 3 times int + ( int ) + ( int )

Shift-Reduce xample I int + (int) + (int)$ shift int I + (int) + (int)$ red. int I + (int) + (int)$ shift 3 times + (int I ) + (int)$ red. int int + ( int ) + ( int )

Shift-Reduce xample I int + (int) + (int)$ shift int I + (int) + (int)$ red. int I + (int) + (int)$ shift 3 times + (int I ) + (int)$ red. int + ( I ) + (int)$ shift int + ( int ) + ( int )

Shift-Reduce xample I int + (int) + (int)$ shift int I + (int) + (int)$ red. int I + (int) + (int)$ shift 3 times + (int I ) + (int)$ red. int + ( I ) + (int)$ shift + () I + (int)$ red. + () int + ( int ) + ( int )

Shift-Reduce xample I int + (int) + (int)$ shift int I + (int) + (int)$ red. int I + (int) + (int)$ shift 3 times + (int I ) + (int)$ red. int + ( I ) + (int)$ shift + () I + (int)$ red. + () I + (int)$ shift 3 times int + ( int ) + ( int )

Shift-Reduce xample I int + (int) + (int)$ shift int I + (int) + (int)$ red. int I + (int) + (int)$ shift 3 times + (int I ) + (int)$ red. int + ( I ) + (int)$ shift + () I + (int)$ red. + () I + (int)$ shift 3 times + (int I )$ red. int int + ( int ) + ( int )

Shift-Reduce xample I int + (int) + (int)$ shift int I + (int) + (int)$ red. int I + (int) + (int)$ shift 3 times + (int I ) + (int)$ red. int + ( I ) + (int)$ + () I + (int)$ shift red. + () I + (int)$ shift 3 times + (int I )$ red. int + ( I )$ shift int + ( int ) + ( int )

Shift-Reduce xample I int + (int) + (int)$ shift int I + (int) + (int)$ red. int I + (int) + (int)$ shift 3 times + (int I ) + (int)$ red. int + ( I ) + (int)$ + () I + (int)$ shift red. + () I + (int)$ shift 3 times + (int I )$ red. int + ( I )$ + () I $ shift red. + () int + ( int ) + ( int )

Shift-Reduce xample I int + (int) + (int)$ shift int I + (int) + (int)$ red. int I + (int) + (int)$ shift 3 times + (int I ) + (int)$ red. int + ( I ) + (int)$ + () I + (int)$ shift red. + () I + (int)$ shift 3 times + (int I )$ red. int + ( I )$ + () I $ shift red. + () I $ accept int + ( int ) + ( int )

The Stack Left string can be implemented as a stack Top of the stack is the I Shift pushes a terminal on the stack Reduce pops 0 or more symbols from the stack (production rhs) and pushes a non-terminal on the stack (production lhs) 2/20/08 Prof. Hilfinger CS164 Lecture 11 26

Key Issue: When to Shift or Reduce? Decide based on the left string (the stack) Idea: use a finite automaton (DFA) to decide when to shift or reduce The DFA input is the stack up to potential handle DFA alphabet consists of terminals and nonterminals DFA recognizes complete handles We run the DFA on the stack and we examine the resulting state X and the token tok after I If X has a transition labeled tok then shift If X is labeled with A β on tok then reduce 2/20/08 Prof. Hilfinger CS164 Lecture 11 27

accept on $ LR(1) Parsing. An xample + () on $, + int 0 1 + ( 2 3 4 7 8 + ) + 10 6 ( ) 5 11 int on $, + int 9 int on ), + int + () on ), + I int + (int) + (int)$ shift int I + (int) + (int)$ int I + (int) + (int)$ + (int I ) + (int)$ + ( I ) + (int)$ + () I + (int)$ I + (int)$ + (int I )$ + ( I )$ + () I $ I $ shift(x3) int shift +() shift (x3) int shift +() accept

Representing the DFA Parsers represent the DFA as a 2D table As for table-driven lexical analysis Lines correspond to DFA states Columns correspond to terminals and nonterminals In classical treatments, columns are split into: Those for terminals: action table Those for non-terminals: goto table 2/20/08 Prof. Hilfinger CS164 Lecture 11 29

Representing the DFA. xample The table for a fragment of our DFA: ( 3 4 6 ) 7 int 5 int on ), + + () on $, + 3 4 5 6 7 int s5 s8 r int r +() r +() 2/20/08 Prof. Hilfinger CS164 Lecture 11 30 + ( s4 s7 ) r int $ g6

The LR Parsing Algorithm After a shift or reduce action we rerun the DFA on the entire stack This is wasteful, since most of the work is repeated So record, for each stack element, state of the DFA after that state LR parser maintains a stack sym 1, state 1... sym n, state n state k is the final state of the DFA on sym 1 sym k 2/20/08 Prof. Hilfinger CS164 Lecture 11 31

The LR Parsing Algorithm Let I = w 1 w 2 w n $ be initial input Let j = 1 Let DFA state 0 be the start state Let stack = dummy, 0 repeat case action[top_state(stack), I[j]] of shift k: push I[j], k ; j += 1 reduce X α: pop α pairs, push X, Goto[top_state(stack), X] accept: halt normally error: halt and report error 2/20/08 Prof. Hilfinger CS164 Lecture 11 32

Parsing Contexts Consider the state describing the situation at the I in the stack + ( I int ) + ( int ) Context: We are looking for an + ( ) Have have seen + ( from the right-hand side We are also looking for int or + ( ) Have seen nothing from the right-hand side One DFA state describes a set of such contexts (Traditionally, use to show where the I is.) 2/20/08 Prof. Hilfinger CS164 Lecture 11 33

LR(1) Items An LR(1) item is a pair: X α β, a X αβ is a production a is a terminal (the lookahead terminal) LR(1) means 1 lookahead terminal [X α β, a] describes a context of the parser We are trying to find an X followed by an a, and We have α already on top of the stack Thus we need to see next a prefix derived from βa 2/20/08 Prof. Hilfinger CS164 Lecture 11 34

Convention We add to our grammar a fresh new start symbol S and a production S Where is the old start symbol No need to do this if had only one production The initial parsing context contains: S, $ Trying to find an S as a string derived from $ The stack is empty 2/20/08 Prof. Hilfinger CS164 Lecture 11 35

Constructing the Parsing DFA. xample. 2 S, $ 0 +(), $/+ int int, $/+ + S, $ +(), $/+ accept on $ 6 +( ), $/+ +(), )/+ and so on 1 int, $/+ + (), $/+ +( ), $/+ +(), )/+ int, )/+ int on $, + int, )/+ 2/20/08 Prof. Hilfinger CS164 Lecture 11 36 ( int 4 3 5 int on ), +

LR Parsing Tables. Notes 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.g., they report errors in terms of sets of items What kind of errors can we expect? 2/20/08 Prof. Hilfinger CS164 Lecture 11 37

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 2/20/08 Prof. Hilfinger CS164 Lecture 11 38

Shift/Reduce Conflicts Typically due to ambiguities in the grammar Classic example: the dangling else S if then S if then S else S OTHR Will have DFA state containing [S if then S, else] [S if then S else S, $] If else follows then we can shift or reduce 2/20/08 Prof. Hilfinger CS164 Lecture 11 39

More Shift/Reduce Conflicts Consider the ambiguous grammar + * int We will have the states containing [ *, +] [ *, +] [ +, +] [ +, +] Again we have a shift/reduce on input + We need to reduce (* binds more tightly than +) Solution: declare the precedence of * and + 2/20/08 Prof. Hilfinger CS164 Lecture 11 40

More Shift/Reduce Conflicts In bison declare precedence and associativity of terminal symbols: %left + %left * Precedence of a rule = that of its last terminal See bison manual for ways to override this default Resolve shift/reduce conflict with a shift if: input terminal has higher precedence than the rule the precedences are the same and right associative 2/20/08 Prof. Hilfinger CS164 Lecture 11 41

Using Precedence to Solve S/R Conflicts Back to our example: [ *, +] [ *, +] [ +, +] [ +, +] Will choose reduce because precedence of rule * is higher than of terminal + 2/20/08 Prof. Hilfinger CS164 Lecture 11 42

Using Precedence to Solve S/R Conflicts Same grammar as before + * int We will also have the states [ +, +] [ +, +] [ +, +] [ +, +] Now we also have a shift/reduce on input + We choose reduce because + and + have the same precedence and + is left-associative 2/20/08 Prof. Hilfinger CS164 Lecture 11 43

Using Precedence to Solve S/R Conflicts Back to our dangling else example [S if then S, else] [S if then S else S, x] Can eliminate conflict by declaring else with higher precedence than then However, best to avoid overuse of precedence declarations or you ll end with unexpected parse trees 2/20/08 Prof. Hilfinger CS164 Lecture 11 44

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 2/20/08 Prof. Hilfinger CS164 Lecture 11 45

Reduce/Reduce Conflicts Usually due to gross ambiguity in the grammar xample: 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? 2/20/08 Prof. Hilfinger CS164 Lecture 11 46

More on Reduce/Reduce Conflicts Consider the states [S id, $] [S S, $] [S id S, $] [S, $] id [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 2/20/08 Prof. Hilfinger CS164 Lecture 11 47

Relation to Bison Bison builds this kind of machine. However, for efficiency concerns, collapses many of the states together. Causes some additional conflicts, but not many. The machines discussed here are LR(1) engines. Bison s optimized versions are LALR(1) engines. 2/20/08 Prof. Hilfinger CS164 Lecture 11 48

A Hierarchy of Grammar Classes From Andrew Appel, Modern Compiler Implementation in Java 2/20/08 Prof. Hilfinger CS164 Lecture 11 49

Notes on Parsing Parsing A solid foundation: context-free grammars A simple parser: LL(1) A more powerful parser: LR(1) An efficiency hack: LALR(1) We use LALR(1) parser generators But let s also see a more powerful engine. 2/20/08 Prof. Hilfinger CS164 Lecture 11 50