Lexical and Syntax Analysis. Bottom-Up Parsing

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

Bottom-Up Parsing. Lecture 11-12

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

Bottom-Up Parsing. Lecture 11-12

LR Parsing LALR Parser Generators

Formal Languages and Compilers Lecture VII Part 4: Syntactic A

LR Parsing LALR Parser Generators

In One Slide. Outline. LR Parsing. Table Construction

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

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

Context-free grammars

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

shift-reduce parsing

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

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

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

LR Parsing Techniques

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

UNIT III & IV. Bottom up parsing

Conflicts in LR Parsing and More LR Parsing Types

SLR parsers. LR(0) items

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

Lecture 8: Deterministic Bottom-Up Parsing

LR Parsing - The Items

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

CS 4120 Introduction to Compilers

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

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

Lecture 7: Deterministic Bottom-Up Parsing

MODULE 14 SLR PARSER LR(0) ITEMS

Wednesday, August 31, Parsers

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

Wednesday, September 9, 15. Parsers

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

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.

Principles of Programming Languages

LR Parsing. Table Construction

Lecture Bottom-Up Parsing


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.

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

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

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

LR Parsing E T + E T 1 T

Monday, September 13, Parsers

Compiler Construction 2016/2017 Syntax Analysis

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

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

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

Bottom-Up Parsing II. Lecture 8

LR Parsers. Aditi Raste, CCOEW

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

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

4. Lexical and Syntax Analysis

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

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

LR Parsing Techniques

4. Lexical and Syntax Analysis

More Bottom-Up Parsing

Downloaded from Page 1. LR Parsing

Chapter 4. Lexical and Syntax Analysis

Chapter 4: LR Parsing

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

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

Lexical and Syntax Analysis. Top-Down Parsing

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

Intro to Bottom-up Parsing. Lecture 9

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

How do LL(1) Parsers Build Syntax Trees?


Context-Free Grammars and Parsers. Peter S. Housel January 2001

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

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

UNIT-III BOTTOM-UP PARSING

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

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

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

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

Lecture Notes on Bottom-Up LR Parsing

Top down vs. bottom up parsing

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

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

LALR Parsing. What Yacc and most compilers employ.

Lecture Notes on Bottom-Up LR Parsing

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

CSE302: Compiler Design

Syntax Analysis Part I

Algorithms for NLP. LR Parsing. Reading: Hopcroft and Ullman, Intro. to Automata Theory, Lang. and Comp. Section , pp.

Parsing III. (Top-down parsing: recursive descent & LL(1) )

CS2210: Compiler Construction Syntax Analysis Syntax Analysis

Bottom-up Parser. Jungsik Choi

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

VIVA QUESTIONS WITH ANSWERS

Lexical and Syntax Analysis

Configuration Sets for CSX- Lite. Parser Action Table

FROWN An LALR(k) Parser Generator

Introduction to Bottom-Up Parsing

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

Transcription:

Lexical and Syntax Analysis Bottom-Up Parsing

Parsing There are two ways to construct derivation of a grammar. Top-Down: begin with start symbol; repeatedly replace an instance of a production s LHS with its RHS. Bottom-Up: begin with terminal symbols; repeatedly replace an instance of a production s RHS with its LHS. (See animation.)

Derivation in reverse Bottom-up parsers construct a derivation in reverse. Example: ( x + x ) ( v + x ) ( e + x ) ( e + v ) ( e + e ) e (Here we have a rightmost derivation in reverse.)

Shift-Reduce Parsing A non-deterministic procedure for bottom-up parsing involving: a stack holding grammar symbols; an input buffer holding the rest of the string to be parsed. Symbol $ is used to mark the bottom of the stack and the end of the input buffer.

Shift-Reduce Parsing There are four primary operations: 1. Shift. Consume the next input symbol and push it onto the stack. 2. Reduce. If the RHS of a production appears on top of the stack, then replace it with the LHS. 3. Accept. Announce success when the stack contains only the start symbol and the input is empty. 4. Error. Fail when the input is empty and no reduction can be applied.

Exercise 1 Consider the following grammar G. e e + t t t t * f f f ( e ) x Show steps taken by a shift-reduce parser to successfully parse the following input. x * x

Shift-reduce parsing is complete If an input string conforms to a grammar, then there exists at least one series of shift-reduce steps leading to acceptance. (See Aho et al. for a proof.)

Problem But how do we decide when to shift and when to reduce? Stack Input Action $ x * x $ Shift $ x * x $ Reduce by f x $ f * x $ Reduce by t f $ t * x $ Shift or Reduce? Both shift and reduce (by e t) are possible, but reduce leads to failure.

Possible solutions How do we decide when to shift and when to reduce? Solution 1: guess, and backtrack upon failure. (Inefficient!) Solution 2: LR parsing. L for left-to-right scan of the input, and R for constructing a rightmost derivation in reverse.

PART 1: LR(0) PARSING (When to shift and when to reduce) LR(0) Items Reducible Items LR(0) States LR(0) Automata Constructing an LR(0) Automaton LR(0) Parsing

LR(0) Items An item of a grammar H is a production of H with a dot at some point in the body. For example, the production e e + t has four possible items: e e + t e e + t e e + t e e + t

LR(0) Items Note that the production n ε has just one possible item: n

Intuition An item indicates how much of a production we have seen at some point during parsing. For example, the item e e + t indicates that we have just seen a substring derivable from e and that next we hope to see one derivable from + t.

Reducible items An item is reducible if it has a dot at the rightmost position. For example: t t * f Intuition: we have reached a point where we are ready to reduce t * f to t. Exception: an item defining the start symbol is not reducible.

LR(0) States A state of an LR parser is a set of items. For example, here is a state S 2 : S 2 e e + v v x v y In state S 2 we could be in the process of parsing one of three possible productions.

LR(0) Automaton An LR(0) automaton has states (sets of LR(0) items); and transitions labelled by grammar symbols. For example, here is a fragment of an LR(0) automaton: x S 2 e e + v v x v y S 5 v x v y S 6 v y S 3 e e + v

Constructing an LR(0) automaton An LR(0) automaton is defined by two main functions: GOTO(L, X): the LR(0) state reached by taking an X transition in state L. STATES(G): the set of all LR(0) states of grammar G. To define these we need an auxiliary function called CLOSURE.

The CLOSURE function If L is a set of items then CLOSURE(L) is the set of items such that: every item in L is in CLOSURE(L); if item n α m β is in CLOSURE(L) and m γ is a production then m γ is also in CLOSURE(L).

Exercise 2 Consider the following grammar P. s e e e + v v v x y Compute the CLOSURE of the set of items { s e }.

The GOTO function If L is a set of items and X is a grammar symbol and n α X β L then GOTO(L, X) is the CLOSURE of the set of all items n α X β

Exercise 3 Consider the following LR(0) state. S 0 s e e e + v e v v x v y Give the states that can be reached from S 0 on each of the following grammar symbols: e, v, x and y.

Exercise 4 Consider the following LR(0) state. S 1 s e e e + v Give the states that can be reached from S 1 on symbol +.

Constructing the LR(0) states Aim: to compute C, the set of all LR(0) states for some grammar G. Pre-processing step: if s is the start symbol of G, introduce a new start symbol s' and a new production s' s. (Motivation: success is detected when s is reduced to s'.)

The STATES function STATES(G) { } C := { CLOSURE({ s' s }) } repeat for each L C for each grammar symbol X C := C { GOTO(L, X) } until C is unchanging return C

Constructing the LR(0) automaton States: obtained by the STATES function. Transitions: for each state L and grammar symbol X compute GOTO(X, L). Final state: if s' is the start symbol and s' s is an item in state L then add a transition labelled $ from L to the accepting state.

Exercise 5 Construct an LR(0) automaton for the following grammar. s e e e + v v v x y (A new start symbol s has already been introduced.)

LR(0) Parsing An LR(0) parser has: an LR(0) automaton (the GOTO function in particular); a pointer next to the next character in the input string; a stack of states S, initially containing the start state of the LR(0) automaton; a current state, always at the top of the stack.

LR(0) Parsing while top(s) accept if n α is a reducible item in top(s) then pop α elements push( GOTO(top(S), n ) ) else if GOTO(top(S), *next) = L then next++ push(l) else raise syntax error Shift! Reduce!

Exercise 6 (page 1) Consider the LR(0) automaton: start v S 0 s e e e + v e v v x v y S 1 s e e e + v $ e accept + x y S 4 e v S 5 v x x S 2 e e + v v x v y v y S 6 v y S 3 e e + v

Exercise 6 (page 2) Show the steps taken by an LR(0) parser on the following input string. x + x For each step, show the stack, the input buffer, and the action taken.

Shift-Reduce Conflicts If at some state L there is a reducible item and a transition on a terminal symbol then: there exists a shift-reduce conflict; and the grammar is not an LR(0) grammar. Grammar P contains no shiftreduce conflicts.

Reduce-Reduce Conflicts If at some state L there is a more than one reducible item then: there exists a reduce-reduce conflict; and the grammar is not an LR(0) grammar. Grammar P contains no reducereduce conflicts.

PART 2: LR(1) PARSING (When to shift and when to reduce) LR(1) Items Reducible Items SLR(1) Parsing LR(1) Parsing LALR(1) Parsing

Motivation LR(0) automata say when to shift and when to reduce. But conflicts are very common: many grammars are not LR(0) grammars. If we enrich the structure of LR(0) items, we can decrease conflicts significantly.

LR(1) Items Idea: extend LR(0) items to have the form n α β { σ } where { σ } is a set of terminal symbols called the lookahead set.

Reducible items An item is reducible if it has a dot at the rightmost position and the next input symbol is in the lookahead set. Example: t t * f { *, +, ) } Here we have reached a point where we can reduce t * f to t provided that the next input symbol is *, + or ).

LR(1) parsing LR(1) parsing is like LR(0) parsing except that: LR(1) states contain LR(1) items rather than LR(0) items. An item is only reducible if the next input symbol is in the lookahead set.

Aside: an equivalence If an LR(1) state contains several items differing only in their lookahead sets, e.g. t t * f { *, + } t t * f { +, ) } then these are equivalent to the single item formed by unioning the lookahead sets, e.g. t t * f { *, +, ) }

LR(1) parsing There are three main forms of LR(1) parsing, each differing in how the lookahead sets of items are defined. LR(1) LALR(1) SLR(1)

SLR(1) PARSING SLR(1) stands for simple LR(1)

SLR(1) Automata An SLR(1) automaton is an LR(0) automaton with every item n α β replaced by n α β { σ } where {σ } is the follow set of n.

Exercise 7 Compute the follow set of each non-terminal in the following grammar. s e e e + v v v x y

Answer follow( s ) = { $ } follow( e ) = { +, $ } follow( v ) = { +, $ }

Example of an SLR(1) automaton start v S 4 e v {+, $} S 0 s e {$} e e + v {+, $} e v {+, $} v x {+, $} v y {+, $} e S 1 s e {$} e e + v {+, $} $ accept + x y S 5 v x {+, $} S 6 v y {+, $} x S y 2 e e + v {+, $} v x {+, $} v y {+, $} v S 3 e e + v {+, $}

Power of SLR(1) SLR(1) Grammars LR(0) Grammars An SLR(1) parser observes fewer reducible items than an LR(0) parser. Therefore, fewer reduce-reduce and shift-reduce conflicts.

LR(1) PARSING More powerful than SLR(1)

Idea of LR(1) In SLR(1), the lookahead set of an item is computed in the context of any possible derivation. Idea: have different lookahead sets for item n α β for each occurrence site of n in the grammar.

Example Consider the following grammar. s c c c c c d The follow sets are: follow( s ) = { $ } follow( c ) = { c, d, $ }

Example (continued) In SLR(1) parsing, we have items such as c d { c, d, $ } But depending on whether we are parsing the first or second c in an s, we want items such as c d { c, d } or c d { $ }

Construction an LR(1) automaton To construct an LR(1) automaton, we make minor modifications to the CLOSURE and GOTO functions.

The CLOSURE function every item in L is in CLOSURE(L); if item n α m β {σ} is in CLOSURE(L) and m γ is a production then m γ {σ'} is in CLOSURE(L) where σ' contains every terminal in first(β) and also, if ε first(β), every terminal in σ.

Exercise 8 Consider the following grammar Q. s' s s c c c c c d Compute the CLOSURE of the set of items { s' s {$} }.

The GOTO function If L is a set of items and X is a grammar symbol and n α X β {σ} L then GOTO(L, X) is the CLOSURE of the set of all items n α X β {σ}

Exercise 9 Consider the following LR(1) state. S 0 s' s {$} s c c {$} c c c {c, d} c d {c, d} Give the states that can be reached from S 0 on each of the following grammar symbols: s, c, c and d.

Exercise 10 Construct an LR(1) automaton for the following grammar. s' s s c c c c c d (A new start symbol s' has already been introduced.)

LALR(1) PARSING Merging LR(1) States

Problem LR(1) automata can contain very many states, requiring large amounts of memory to store.

LALR(1) Parsing Idea: first construct an LR(1) automaton and then merge states where possible. Two LR(1) states are merged if, by ignoring the lookahead sets, they contain the same items. The merged state contains the union of all the items in the original two states.

Example Ignoring lookahead sets, states S 4 and S 7 have the same items. S 4 c d {c, d} S 7 c d {$} After merging: S 47 c d {c, d} c d {$} Or more simply: S 47 c d {$, c, d}

Exercise 11 Construct an LALR(1) automaton for the following grammar. s' s s c c c c c d (A new start symbol s' has already been introduced.)

Facts about LALR(1) LALR(1) and LR(0) automata contain the same number of states. For the C language, the LALR(1) automaton contains hundreds of states whereas the LR(1) automaton contains thousands.

More facts about LALR(1) If the LR(1) automaton for a grammar contains no shiftreduce conflicts, then neither does the LALR(1) automaton. But, an LALR(1) automaton may contain reduce-reduce conflicts not present in an LR(1) automaton.

Expressive power of LR parsing techniques LR(1) LALR(1) SLR(1) LR(0)

Concluding remarks LR parsers can handle a larger class of grammars than LL parsers. But LR parsers are often too complex to construct by hand. So the main use of LR parsing is in automatic parser generator tools such as Bison.

Acknowledgements