Introduction to Bottom-Up Parsing

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

Intro to Bottom-up Parsing. Lecture 9

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

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

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

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

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 II. Lecture 8

Bottom-Up Parsing. Lecture 11-12

Bottom-Up Parsing. Lecture 11-12

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

Lecture 8: Deterministic Bottom-Up Parsing

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

More Bottom-Up Parsing

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

CS 4120 Introduction to Compilers

Lecture 7: Deterministic Bottom-Up Parsing

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

Lexical and Syntax Analysis. Top-Down Parsing

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

LR Parsing LALR Parser Generators

3. Parsing. Oscar Nierstrasz

LR Parsing LALR Parser Generators

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.

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

Table-Driven Parsing

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.

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

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

LL Parsing: A piece of cake after LR

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

Lexical and Syntax Analysis. Bottom-Up Parsing

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

Top down vs. bottom up parsing

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

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

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

In One Slide. Outline. LR Parsing. Table Construction

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

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

Lexical and Syntax Analysis

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

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

UNIT-III BOTTOM-UP PARSING

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

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

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

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

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

CSCI312 Principles of Programming Languages

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

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

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

It parses an input string of tokens by tracing out the steps in a leftmost derivation.

Chapter 4: LR Parsing

LR Parsing E T + E T 1 T

Lecture Bottom-Up Parsing

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & 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.

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


CS 314 Principles of Programming Languages

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

Context-free grammars

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

Compiler Construction: Parsing

Syntax-Directed Translation. Lecture 14

Abstract Syntax Trees & Top-Down Parsing

1 Introduction. 2 Recursive descent parsing. Predicative parsing. Computer Language Implementation Lecture Note 3 February 4, 2004

Wednesday, August 31, Parsers

CS502: Compilers & Programming Systems

Introduction to Parsing. Comp 412

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

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

Types of parsing. CMSC 430 Lecture 4, Page 1

Administrativia. PA2 assigned today. WA1 assigned today. Building a Parser II. CS164 3:30-5:00 TT 10 Evans. First midterm. Grammars.

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

Conflicts in LR Parsing and More LR Parsing Types

Announcements. Written Assignment 1 out, due Friday, July 6th at 5PM.

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

FROWN An LALR(k) Parser Generator

Ambiguity. Lecture 8. CS 536 Spring

CMSC 330: Organization of Programming Languages

CS 321 Programming Languages and Compilers. VI. Parsing

Compiler Design Concepts. Syntax Analysis

Syntax Analysis/Parsing. Context-free grammars (CFG s) Context-free grammars vs. Regular Expressions. BNF description of PL/0 syntax

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

CS 406/534 Compiler Construction Parsing Part I

Concepts Introduced in Chapter 4

Monday, September 13, Parsers

LR Parsers. Aditi Raste, CCOEW

Algorithms for NLP. Chart Parsing. Reading: James Allen, Natural Language Understanding. Section 3.4, pp

LR Parsing. Table Construction

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

Chapter 3. Parsing #1

Parsing Part II. (Ambiguity, Top-down parsing, Left-recursion Removal)

컴파일러입문 제 6 장 구문분석

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

Transcription:

Introduction to Bottom-Up Parsing Lecture 11 CS 536 Spring 2001 1

Outline he strategy: shift-reduce parsing Ambiguity and precedence declarations Next lecture: bottom-up parsing algorithms CS 536 Spring 2001 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 ε First(A) and t Follow(α), then use a production A α where ε First(α) We ll see First and Follow sets again... CS 536 Spring 2001 3

Bottom-Up Parsing Bottom-up parsing is more general than topdown parsing And just as efficient Builds on ideas in top-down parsing Bottom-up is the preferred method in practice Concepts today, algorithms next time CS 536 Spring 2001 4

An Introductory Example Bottom-up parsers don t need left-factored grammars Hence we can revert to the natural grammar for our example: E + E * (E) Consider the string: * + CS 536 Spring 2001 5

he Idea Bottom-up parsing reduces a string to the start symbol by inverting productions: * + * + + + + E E * E E + E CS 536 Spring 2001 6

ES YOURSELF #1 Question: find the rightmost derivation of the string * + CS 536 Spring 2001 7

Observation Read the productions found by bottom-up parse in reverse (i.e., from bottom to top) his is a rightmost derivation! * + * + + + + E E * E E + E CS 536 Spring 2001 8

Important Fact #1 Important Fact #1 about bottom-up parsing: A bottom-up parser traces a rightmost derivation in reverse CS 536 Spring 2001 9

A Bottom-up Parse * + E * + + E + + E E * + CS 536 Spring 2001 10

A Bottom-up Parse in Detail (1) * + * + CS 536 Spring 2001 11

A Bottom-up Parse in Detail (2) * + * + * + CS 536 Spring 2001 12

A Bottom-up Parse in Detail (3) * + * + + * + CS 536 Spring 2001 13

A Bottom-up Parse in Detail (4) * + * + + + * + CS 536 Spring 2001 14

A Bottom-up Parse in Detail (5) * + * + + E + + E * + CS 536 Spring 2001 15

A Bottom-up Parse in Detail (6) * + E * + + E + + E E * + CS 536 Spring 2001 16

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 CS 536 Spring 2001 17

Questions Does this algorithm terminate? How fast is the algorithm? Does the algorithm handle all cases? How do we choose the substring to reduce at each step? CS 536 Spring 2001 18

How to build the house of cards? CS 536 Spring 2001 19

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 CS 536 Spring 2001 20

Notation Idea: Split string o two substrings Right substring is as yet unexamined by parsing (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 CS 536 Spring 2001 21

Shift-Reduce Parsing Bottom-up parsing uses only two kinds of actions: Shift Reduce CS 536 Spring 2001 22

Shift Shift: Move one place to the right Shifts a terminal to the left string ABC xyz ABCx yz CS 536 Spring 2001 23

Reduce Apply an inverse production at the right end of the left string If A xy is a production, then Cbxy ijk CbA ijk CS 536 Spring 2001 24

he Example with Reductions Only * + * + reduce reduce * + reduce + reduce E + E reduce E + E E CS 536 Spring 2001 25

he Example with Shift-Reduce Parsing * + shift * + shift * + shift * + reduce * + reduce * + shift + shift + reduce + reduce E + E reduce E + E E CS 536 Spring 2001 26

A Shift-Reduce Parse in Detail (1) * + * + CS 536 Spring 2001 27

A Shift-Reduce Parse in Detail (2) * + * + * + CS 536 Spring 2001 28

A Shift-Reduce Parse in Detail (3) * + * + * + * + CS 536 Spring 2001 29

A Shift-Reduce Parse in Detail (4) * + * + * + * + * + CS 536 Spring 2001 30

A Shift-Reduce Parse in Detail (5) * + * + * + * + * + * + CS 536 Spring 2001 31

A Shift-Reduce Parse in Detail (6) * + * + * + * + * + + * + CS 536 Spring 2001 32

A Shift-Reduce Parse in Detail (7) * + * + * + * + * + + + * + CS 536 Spring 2001 33

A Shift-Reduce Parse in Detail (8) * + * + * + * + * + + + + * + CS 536 Spring 2001 34

A Shift-Reduce Parse in Detail (9) * + * + * + * + * + + + + + * + CS 536 Spring 2001 35

A Shift-Reduce Parse in Detail (10) * + * + * + * + * + E + + + + + E * + CS 536 Spring 2001 36

A Shift-Reduce Parse in Detail (11) * + * + * + * + * + + + + + + E E E E * + CS 536 Spring 2001 37

he Stack 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 of the stack (production rhs) and pushes a nonterminal on the stack (production lhs) CS 536 Spring 2001 38

Key Issue (will be resolved by algorithms) How do we decide when to shift or reduce? Consider step * + We could reduce by giving * + A fatal mistake: No way to reduce to the start symbol E CS 536 Spring 2001 39

Conflicts Generic shift-reduce strategy: If there is a handle on top of the stack, reduce Otherwise, shift But what if there is a choice? If it is legal to shift or reduce, there is a shift-reduce conflict If it is legal to reduce by two different productions, there is a reduce-reduce conflict CS 536 Spring 2001 40

Source of Conflicts Ambiguous grammars always cause conflicts But beware, so do many non-ambiguous grammars CS 536 Spring 2001 41

Conflict Example Consider our favorite ambiguous grammar: E E + E E * E (E) CS 536 Spring 2001 42

One Shift-Reduce Parse * +... E * E + E + E + E + E + E E shift... reduce E E * E shift shift reduce E reduce E E + E CS 536 Spring 2001 43

Another Shift-Reduce Parse * +... E * E + E * E + E * E + E * E + E E * E E shift... shift shift reduce E reduce E E + E reduce E E * E CS 536 Spring 2001 44

Example Notes In the second step E * E + we can either shift or reduce by E E * E Choice determines associativity of + and * As noted previously, grammar can be rewritten to enforce precedence Precedence declarations are an alternative CS 536 Spring 2001 45

Precedence Declarations Revisited Precedence declarations cause shift-reduce parsers to resolve conflicts in certain ways Declaring * has greater precedence than + causes parser to reduce at E * E + More precisely, precedence declaration is used to resolve conflict between reducing a * and shifting a + CS 536 Spring 2001 46

Precedence Declarations Revisited (Cont.) he term precedence declaration is misleading hese declarations do not define precedence; they define conflict resolutions Not quite the same thing! CS 536 Spring 2001 47