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

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

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

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

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

CS 4120 Introduction to Compilers

MODULE 14 SLR PARSER LR(0) ITEMS

Context-free grammars

Compiler Construction: Parsing

LR Parsers. Aditi Raste, CCOEW

Lecture 8: Deterministic Bottom-Up Parsing

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

Principles of Programming Languages

Lecture 7: Deterministic Bottom-Up Parsing

UNIT-III BOTTOM-UP PARSING

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

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

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

SLR parsers. LR(0) items

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

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

Wednesday, August 31, Parsers

Lexical and Syntax Analysis. Bottom-Up Parsing

Compiler Construction 2016/2017 Syntax Analysis

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

Bottom-Up Parsing II. Lecture 8

UNIT III & IV. Bottom up parsing

LR Parsing - The Items

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

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

Bottom-Up Parsing. Lecture 11-12

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

Wednesday, September 9, 15. Parsers

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

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

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

shift-reduce parsing

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

Monday, September 13, Parsers

How do LL(1) Parsers Build Syntax Trees?

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

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

Lecture Bottom-Up Parsing

CS453 : Shift Reduce Parsing Unambiguous Grammars LR(0) and SLR Parse Tables by Wim Bohm and Michelle Strout. CS453 Shift-reduce Parsing 1

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

Bottom-Up Parsing. Lecture 11-12

LR Parsing Techniques

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

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.

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

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


Formal Languages and Compilers Lecture VII Part 4: Syntactic A

Bottom-Up Parsing LR Parsing

LR Parsing LALR Parser Generators

4. Lexical and Syntax Analysis

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

Downloaded from Page 1. LR Parsing

4. Lexical and Syntax Analysis

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

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

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

Chapter 4. Lexical and Syntax Analysis

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

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

Bottom-up Parser. Jungsik Choi

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.

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

Chapter 4: LR Parsing

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

LR Parsing LALR Parser Generators

Review main idea syntax-directed evaluation and translation. Recall syntax-directed interpretation in recursive descent parsers


In One Slide. Outline. LR Parsing. Table Construction

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

Conflicts in LR Parsing and More LR Parsing Types

Prelude COMP 181 Tufts University Computer Science Last time Grammar issues Key structure meaning Tufts University Computer Science

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

3. Parsing. Oscar Nierstrasz

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

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

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

LALR Parsing. What Yacc and most compilers employ.

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

Syntactic Analysis. Top-Down Parsing. Parsing Techniques. Top-Down Parsing. Remember the Expression Grammar? Example. Example

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

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

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

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

Introduction to Parsing. Comp 412

CSCI312 Principles of Programming Languages

Syntax Analysis Part I

Intro to Bottom-up Parsing. Lecture 9

Concepts Introduced in Chapter 4

Top down vs. bottom up parsing

Parsing - 1. What is parsing? Shift-reduce parsing. Operator precedence parsing. Shift-reduce conflict Reduce-reduce conflict

Administrativia. WA1 due on Thu PA2 in a week. Building a Parser III. Slides on the web site. CS164 3:30-5:00 TT 10 Evans.

Syn S t yn a t x a Ana x lysi y s si 1

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

Part III : Parsing. From Regular to Context-Free Grammars. Deriving a Parser from a Context-Free Grammar. Scanners and Parsers.

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

Transcription:

Also known as Shift-Reduce parsing More powerful than top down Don t need left factored grammars Can handle left recursion Attempt to construct parse tree from an input string eginning at leaves and working to top Process of reducing strings to a non terminal shift-reduce Uses parse stack Contains symols already parsed Shift until match RHS of production Reduce to non-terminal on LHS ventually reduce to start symol Shift and Reduce Shift: Move the first input token to the top of the stack. Reduce: Choose a grammar rule X ɑ β γ pop γβɑ from the top of the stack push X onto the stack. Sentential Form A sentential form is a memer of N)* that can e derived in a finite numer of steps from the start symol S. A sentential form that contains no nonterminal symols i.e., is a memer of *) is called a sentence. Stack is initially empty and the parser is at the eginning of the input. Shifting $ is accepts. Handle Parse ree Intuition: reduce only if it leads to the start symol Handle has to match RHS of production and lead to rightmost derivation, if reduced to LHS of some rule S M M L M a L M a ) L ) S M L Definition: Let w e a sentential form where: is an aritrary string of symols X is a production w is a string of terminals hen at is a handle of w if S Xw w y a rightmost derivation Handles formalize the uition reduce to X), ut doesn t say how to find the handle Considering string: a a ) M a ) S M L M a ) a a ) ry to find handles and then reduce from sentential form via rightmost derivation a a ) M a ) L M S a 1

Issues Grammar + * ) id Sentential form Handle Products id 1 + id 2 * id id 1 id + id 2 * id id 2 id + * id id id + * * * We need to locate the handle in the right sentential form and then decide what production to reduce it to which of the RHS of our grammar. Notice in right-most derivation, where right sentential form is: S 1 + + + M 2 Use to indicate where we are in string: id 1 + id 2 * id + id 2 * id + id 2 * id + * id + * id + * + Parsing never has to guess aout the middle of the string. he right side always contains terminals. hus, we can discover the rightmost derivation in reverse: 4 2 1 L M 4 a ) a Viale Prefix Consider our usual grammar and the prolem of when to reduce: + * ) Definition: is a viale prefix if here is a w where w is a right sentential form w is a configuration of a shift-reduced parser For the string: * + a a ) M a ) L M S Sentential form * + Production + Alternatively, a prefix of a rightmost derived sentential form is viale if it does not extend the right end of the handle. * + + + * * A prefix is viale ecause it can e extended y adding terminals to form a valid rightmost derived) sentential form + + As long as the parser has viale prefixes on the stack, no parsing error has een detected. Parser Structure Syntax Stack $ op Input okens: Parser Driver Read head Parse tale $ Output Operations 1. Shift shift input symol onto the stack 2. Reduce RHS of a non-terminal handle is at the top of the stack. Decide which non-terminal to reduce it to. Accept success 4. rror Parse S M M L M a L M a ) L ) String: a a ) $ Stack Input Action $ a a ) $ shift $ a a ) $ shift $ a a ) $ shift $ a a ) $ reduce $ M a ) $ shift $ M a ) $ shift $ M a ) $ reduce $ L $ reduce $ M $ shift $ M $ reduce $ Z $ accept 2

Amiguous Grammars Conflicts arise with amiguous grammars Amiguous grammars generate conflicts ut so do other types of grammars xample: Consider the amiguous grammar * + ) Sentential form * + * + + + + + Actions shift reduce * shift shift reduce reduce + Sentential form Actions * + shift * + shift * + shift * + reduce * + reduce + * reduce * Amiguity In the first step shown, we can either shift or reduce y * Choice ecause of precedence of + and * Same prolem with association of * and + We can always rewrite amiguous grammars of this sort to encode precedence and association in the grammar Sometimes this results in convoluted grammars. he tools we will use have other means to encode precedence and association We must get rid of conflicts! Know what a handle is ut not clear how to detect it Properties aout Handles always appear at the top of the stack Never in middle of stack Justifies use of stack in shift reduce parsing General shift reduce strategy If there is no handle on the stack, shift If there is a handle, reduce to the non-terminal Conflicts If it is legal to either shift or reduce then there is a shift-reduce conflict. If it is legal to reduce y two or more productions, then there is a reduce-reduce conflict. LR Parsers LR family of parsers LRk) L left to right R rightmost derivation in reverse k elements of look ahead Attractive LRk) is powerful virtually all language constructs fficient LLk) LRk) LR parsers can detect an error as soon as it is possile to do so Automatic technique to generate YACC, Bison, Java CUP LR and LL Parsers LR parser, each reduction needed for parse is detected on the asic of Left context Reducile phrase k terminals of look ahead LL parser Left context First k symols of what right hand side derive comined phrase and what is to right of phrase) ypes of LR Parsers SLR simple LR asiest to implement Not as powerful Canonical LR Most powerful xpensive to implement LALR Look ahead LR In etween the 2 previous ones in power and overhead Overall parsing algorithm is the same tale is different

LR Parser Actions How does the LR parser know when to shift and when to reduce? By using a DFA! he edges of the DFA are laeled y the symols terminals and non-terminals) that can appear on the stack. Five kinds of actions: 1. sn Shift o state n; 2. gn Goto state n;. rk Reduce y rule k; 4. a Accept; 5. rror LR Parser Actions Shiftn): Advance input one token; push n on stack. Reducek): Pop stack as many times as the numer of symols on the right-hand side of rule k Let X e the left-hand-side symol of rule k In the state now on top of stack, look up X to get goto n Push n on top of stack. Accept: Stop parsing, report success. rror: Stop parsing, report failure. LR Parsers Can tell handle y looking at stack top: grammar symol, state) and k input symols index our FSA tale In practice, k<=1 How to construct LR parse tale from grammar: 1. First construct SLR parser 2. LR and LALR are augmented asic SLR techniques. 2 phases to construct tale: I. Build deterministic finite state automation to go from state to state II. Build tale from DFA ach state how do we know from grammar where we are in the parse. Production already seen. Notion of an LR0) item An item is a production with a distinguished position on the right hand side. his position indicates how much of the production already seen. xample: S a B S is a production Items for the production: S a B S S a B S S a B S S a B S Basic idea: Construct a DFA that recognizes the viale prefixes group items o sets Construction of LR0) items Create augmented grammar G G: S G : S S S What else is needed: A c d Indicate a new state y consuming symol d: need goto function A c d What are all possile things to see all possile derivations from? Add strings derivale from closure function A c d reduce to A and goto another state Compute functions closure and goto will e used to determine the action and goto parts of the parsing tale closure essentially defines what is expected goto moves from one state to another y consuming symol LR0) States Start with our usual grammar: 1.) + 2.) *.) ) Add a special start symol, S, that goes to our original start symol and $: 0.) S $ he LR0) start state will e the set of LR0) items: S $ + 4

LR0) States What happens if we shift an onto the stack from the start state 1)? What happens if we shift a onto the stack from this start state 1)? LR0) States What happens if we parse some string derived from nonterminal? We will execute a goto for in state 1, yielding state 4. S $ + * 2 1 ) + S $ + * 2 1 ) + S $ 4 LR0) States LR0) Operations In state 8, we find that the parsing position is at the end of the item. his means that the top of the stack has the complete RHS of a production on its top. Compute closurei) and gotoi, X), where I is a set of items and X is a grammar symol terminal or nonterminal). his is a reduce action. ) ) 8 ClosureI) = repeat for any item A α Xβ in I for any production X γ I I {X γ } until I does not change. return I GotoI, X) = J {} for any item A α Xβ in I add A αx β to J return ClosureJ) Closure adds more items to a set of items when there is a dot to the left of a nonterminal Goto moves the dot past the symol X in all items. LR0) Parser Construction 1. Augment the grammar with an auxiliary start production S S$. 2. Let e the set of states seen so far,. Let the set of shift or goto) edges found so far. 4. Make an accept action for the symol $ do not compute GotoI, $)) Initialize to {Closure{S S$})} Initialize to {} LR0) Reduce Actions R is the set of reduce actions R {} for each state I in for each item A α in I R R {I, A α)} repeat for each state I in for each item A α Xβ in I let J e GotoI, X) {J} {I X J} until and did not change in this iteration 5

DFA S $ + ) + 1 2 S $ + + 4 * 5 * 6 ) 10 * * ) 7 11 ) + + 7 9 + 8 Building the LR0) ale for each edge I X J if X is a terminal M[I,X] = shift J if X is a nonterminal M[I,X] = goto J for each state I containing an item S S.$ M[I,$] = accept for a state containing an item A γ // A production n with the dot at the end for every token Y M[I,Y] = reduce n LR0) Parse ale LR0) Parse ale 1 2 4 5 6 7 8 9 10 11 ) * + $ ) * + $ 1 s4 s5 g2 g 2 a s8 4 s6 5 s4 s5 g10 g 6 s4 s5 g7 7 r2 r2 r2 r2 r2 r2 8 g9 g 9 r1 r1 r1 r1 r1 r1 10 s11 11 r r r r r r 6