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

Similar documents
Introduction to Bottom-Up Parsing

Intro to Bottom-up Parsing. Lecture 9

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

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

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

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

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. Lecture 11-12

Bottom-Up Parsing. Lecture 11-12

Bottom-Up Parsing II. Lecture 8

Lecture 8: Deterministic Bottom-Up Parsing

Lecture 7: Deterministic Bottom-Up Parsing

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

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

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

A bottom-up parser traces a rightmost derivation in reverse. Bottom-Up Parsing. Bottom-up parsing is more general than topdown.

CS2210: Compiler Construction Syntax Analysis Syntax Analysis

CS 4120 Introduction to Compilers

Top down vs. bottom up parsing

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

Ambiguity. Grammar E E + E E * E ( E ) int. The string int * int + int has two parse trees. * int

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

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

LR Parsing LALR Parser Generators


Lexical and Syntax Analysis. Top-Down Parsing

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

More Bottom-Up Parsing

3. Parsing. Oscar Nierstrasz

Lexical and Syntax Analysis. Bottom-Up Parsing

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

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

Chapter 4: LR Parsing

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

Table-Driven Parsing

University of Technology Department of Computer Sciences. Final Examination st Term. Subject:Compilers Design

Wednesday, August 31, Parsers

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

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

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.

Context-free grammars

LR Parsing LALR Parser Generators

In One Slide. Outline. LR Parsing. Table Construction

Syntax-Directed Translation. Lecture 14

Compiler Construction: Parsing

Monday, September 13, Parsers

Syntax Analysis. Martin Sulzmann. Martin Sulzmann Syntax Analysis 1 / 38

UNIT-III BOTTOM-UP PARSING

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

Lexical and Syntax Analysis

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

Parsing #1. Leonidas Fegaras. CSE 5317/4305 L3: Parsing #1 1

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

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

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

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

Lecture Bottom-Up Parsing

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

Wednesday, September 9, 15. Parsers

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

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

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

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.

A programming language requires two major definitions A simple one pass compiler

LL Parsing: A piece of cake after LR

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

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

CSCI312 Principles of Programming Languages

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

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).

Types of parsing. CMSC 430 Lecture 4, Page 1

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

CS502: Compilers & Programming Systems

Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5

Chapter 4. Lexical and Syntax Analysis

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

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

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

CS1622. Today. A Recursive Descent Parser. Preliminaries. Lecture 9 Parsing (4)

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.

CS 406/534 Compiler Construction Parsing Part I

8 Parsing. Parsing. Top Down Parsing Methods. Parsing complexity. Top down vs. bottom up parsing. Top down vs. bottom up parsing

Context-Free Grammars

FROWN An LALR(k) Parser Generator

4. Lexical and Syntax Analysis

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

Introduction to Parsing. Comp 412

4. Lexical and Syntax Analysis

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

Conflicts in LR Parsing and More LR Parsing Types

UNIT III & IV. Bottom up parsing

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

Parsing Part II (Top-down parsing, left-recursion removal)

Compiler Construction 2016/2017 Syntax Analysis

Syntactic Analysis. Top-Down Parsing

CS 314 Principles of Programming Languages

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

Transcription:

Outline Introduction to Bottom-Up Parsing Lecture Notes by Profs. Alex Aiken and George Necula (UCB) he strategy: -reduce parsing A key concept: handles Ambiguity and precedence declarations CS780(Prasad) L12BUP 1 CS780(Prasad) L12BUP 2 Predictive Parsing Summary First and Follow sets are used to construct predictive tables For non-terminal A and input t, use a production A α where t First(α) For non-terminal A and input t, if t Follow(α) and ε First(A), use a production A α where ε First(α) CS780(Prasad) L12BUP 3 Bottom-Up Parsing Bottom-up parsing is more general than topdown parsing. Don t need left-factored grammars. Left recursion fine. Just as efficient. Builds on ideas in top-down parsing. Bottom-up parsing is the preferred method in practice. Automatic parser generators: YACC, Bison, CS780(Prasad) L12BUP 4

An Introductory xample Revert to the natural grammar for our example: + () Consider the string: + he Idea Bottom-up parsing reduces a string to the start symbol by inverting productions: + + + + + + CS780(Prasad) L12BUP 5 CS780(Prasad) L12BUP 6 Observation Read the sequence of productions in reverse (from bottom to top) his is a rightmost derivation! Important Fact #1 Important Fact #1 about bottom-up parsing: + + + + + + A bottom-up parser traces a rightmost derivation in reverse. LR-parser CS780(Prasad) L12BUP 7 CS780(Prasad) L12BUP 8

A Bottom-up Parse A Bottom-up Parse in Detail (1) + + + + + + + + CS780(Prasad) L12BUP 9 CS780(Prasad) L12BUP 10 A Bottom-up Parse in Detail (2) A Bottom-up Parse in Detail (3) + + + + + + + CS780(Prasad) L12BUP 11 CS780(Prasad) L12BUP 12

A Bottom-up Parse in Detail (4) A Bottom-up Parse in Detail (5) + + + + + + + + + + + CS780(Prasad) L12BUP 13 CS780(Prasad) L12BUP 14 A Bottom-up Parse in Detail (6) A rivial Bottom-Up Parsing Algorithm + + + + + + Let I = input string repeat pick a non-empty substring β of I where X βis a production if no such β, backtrack replace one β by X in I until I = S (the start symbol) or all possibilities are exhausted CS780(Prasad) L12BUP 15 CS780(Prasad) L12BUP 16

Questions Does this algorithm terminate? How fast is the algorithm? Does the algorithm deal with all cases? How do we choose the substring to reduce at each step? Where Do Reductions Happen Important Fact #1 has an eresting consequence: Let αβω be a step of a bottom-up parse. Assume the next reduction is by X β. hen ω is a string of terminals. Why? Because αxω αβωis a step in a rightmost derivation. CS780(Prasad) L12BUP 17 CS780(Prasad) L12BUP 18 Notation Idea: Split string o two substrings. Right substring is as yet unexamined by parser (hence is a string of terminals). Left substring has terminals and non-terminals. he dividing po is marked by a he is not part of the string. Initially, all input is unexamined. x 1 x 2... x n Shift-Reduce Parsing Bottom-up parsing uses only two kinds of actions: Shift: Move one place to the right. Shifts a terminal to the left string ABC xyz ABCx yz Reduce: Apply an inverse production at the right end of the left string. If A xy is a production, then Cbxy ijk CbA ijk CS780(Prasad) L12BUP 19 CS780(Prasad) L12BUP 20

he xample with Reductions Only he xample with Shift-Reduce Parsing + reduce + reduce + reduce + reduce + reduce + CS780(Prasad) L12BUP 21 + + + + + + + + + + reduce reduce reduce reduce reduce + 22 A Shift-Reduce Parse in Detail (1) + A Shift-Reduce Parse in Detail (2) + + + + CS780(Prasad) L12BUP 23 CS780(Prasad) L12BUP 24

A Shift-Reduce Parse in Detail (3) + + + A Shift-Reduce Parse in Detail (4) + + + + + + CS780(Prasad) L12BUP 25 CS780(Prasad) L12BUP 26 A Shift-Reduce Parse in Detail (5) A Shift-Reduce Parse in Detail (6) + + + + + + + + + + + + + CS780(Prasad) L12BUP 27 CS780(Prasad) L12BUP 28

A Shift-Reduce Parse in Detail (7) A Shift-Reduce Parse in Detail (8) + + + + + + + + + + + + + + + + + CS780(Prasad) L12BUP 29 CS780(Prasad) L12BUP 30 A Shift-Reduce Parse in Detail (9) A Shift-Reduce Parse in Detail (10) + + + + + + + + + + + + + + + + + + + + + CS780(Prasad) L12BUP 31 CS780(Prasad) L12BUP 32

A Shift-Reduce Parse in Detail (11) he Stack + + + + + + + + + + + L12BUP 33 Left string can be implemented by a stack op of the stack is the Shift pushes a terminal on the stack. Reduce pops 0 or more symbols off the stack (production rhs) and pushes a non-terminal on the stack (production lhs). CS780(Prasad) L12BUP 34 Shift-Reduce Parser Key Issue Stack stack Parser ngine Parser Action How do we decide when to or reduce? Consider step + We could reduce by giving + A fatal mistake: Because there is no way to reduce to the start symbol. Current Symbol + () CS780(Prasad) L12BUP 36

Handles Intuition: Want to reduce only if the result can still be reduced to the start symbol. Assume a rightmost derivation: S => αxω αβω hen αβ is a handle of αβω. Handles (Cont.) A handle is a string that can be reduced, and that also allows further reductions back to the start symbol. We only want to reduce at handles. Note: We have said what a handle is, not how to find handles. CS780(Prasad) L12BUP 37 CS780(Prasad) L12BUP 38 Important Fact #2 Important Fact #2 about bottom-up parsing: In -reduce parsing, handles appear only at the top of the stack, never inside. Why? Informal induction on # of reduce moves: rue initially, stack is empty Immediately after reducing a handle right-most non-terminal on top of the stack. next handle must be to right of right-most nonterminal, because this is a right-most derivation. Sequence of moves reaches next handle. CS780(Prasad) L12BUP 39 CS780(Prasad) L12BUP 40

Summary of Handles In -reduce parsing, handles always appear at the top of the stack. Handles are never to the left of the rightmost non-terminal. herefore, -reduce moves are sufficient; the need never move left. Bottom-up parsing algorithms are based on recognizing handles. Conflicts Generic -reduce strategy: If there is a handle on top of the stack, reduce Otherwise, But what if there is a choice? If it is legal to or reduce, there is a reduce conflict. If it is legal to reduce by two different productions, there is a reduce-reduce conflict. CS780(Prasad) L12BUP 41 CS780(Prasad) L12BUP 42 Source of Conflicts One Shift-Reduce Parse Ambiguous grammars always cause conflicts. But beware, so do many non-ambiguous grammars. Consider our favorite ambiguous grammar: + () +... + + + + +... reduce reduce reduce + CS780(Prasad) L12BUP 43 CS780(Prasad) L12BUP 44

Another Shift-Reduce Parse xample Notes +... + + + +... reduce reduce + reduce In the second step +, we can either or reduce by. Choice determines associativity and precedence of + and. As noted previously, grammar can be rewritten to enforce precedence. Precedence declarations are an alternative. CS780(Prasad) L12BUP 45 CS780(Prasad) L12BUP 46 Precedence Declarations Revisited Precedence declarations cause -reduce parsers to resolve conflicts in certain ways. Declaring has greater precedence than + causes parser to reduce at +. More precisely, precedence declaration is used to resolve conflict between reducing a and ing a + CS780(Prasad) L12BUP 47