Intro to Bottom-up Parsing. Lecture 9

Similar documents
Introduction to Bottom-Up Parsing

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

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

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

Bottom-Up Parsing II. Lecture 8

Bottom-Up Parsing. Lecture 11-12

Lexical and Syntax Analysis. Bottom-Up Parsing

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

Lecture 8: Deterministic Bottom-Up Parsing

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

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

Lecture 7: Deterministic Bottom-Up Parsing

CS 4120 Introduction to Compilers

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

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

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

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

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

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

Downloaded from Page 1. LR Parsing

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

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

Lecture Bottom-Up Parsing

UNIT-III BOTTOM-UP PARSING

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

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

Syntax-Directed Translation. Lecture 14

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

Chapter 4: LR Parsing

FROWN An LALR(k) Parser Generator

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

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

LR Parsing E T + E T 1 T

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

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

UNIT III & IV. Bottom up parsing

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

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

More Bottom-Up Parsing

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

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

LR Parsing Techniques

In One Slide. Outline. LR Parsing. Table Construction

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

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

Syntax Analysis, V Bottom-up Parsing & The Magic of Handles Comp 412

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

LR Parsing LALR Parser Generators

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


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.

Lexical and Syntax Analysis. Top-Down Parsing

shift-reduce parsing

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

LR Parsing LALR Parser Generators

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.

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

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

How do LL(1) Parsers Build Syntax Trees?

LR Parsers. Aditi Raste, CCOEW

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

Principles of Programming Languages

Lecture Notes on Shift-Reduce Parsing

Wednesday, August 31, Parsers

MODULE 14 SLR PARSER LR(0) ITEMS

Context-free grammars

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

Compiler Construction: Parsing

Top down vs. bottom up parsing

Syntax Analysis, VI Examples from LR Parsing. Comp 412

3. Parsing. Oscar Nierstrasz

Chapter 4. Lexical and Syntax Analysis

Derivations vs Parses. Example. Parse Tree. Ambiguity. Different Parse Trees. Context Free Grammars 9/18/2012

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

LL Parsing: A piece of cake after LR

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

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

CSC 4181 Compiler Construction. Parsing. Outline. Introduction

Monday, September 13, Parsers

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

CSCI312 Principles of Programming Languages

Introduction to Parsing. Comp 412

Formal Languages and Compilers Lecture VII Part 4: Syntactic A

Configuration Sets for CSX- Lite. Parser Action Table

Lecture Notes on Bottom-Up LR Parsing

Table-Driven Parsing

Wednesday, September 9, 15. Parsers

CSE 105 THEORY OF COMPUTATION

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

Parsing Techniques. CS152. Chris Pollett. Sep. 24, 2008.

CS 406/534 Compiler Construction Putting It All Together

Bottom-Up Parsing LR Parsing

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

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

Fall Compiler Principles Lecture 2: LL parsing. Roman Manevich Ben-Gurion University of the Negev

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

Transcription:

Intro to Bottom-up Parsing Lecture 9

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 Concepts today, algorithms next time 2

An Introductory Example Bottom-up parsers don t need left-factored grammars Revert to the natural grammar for our example: E + E (E) Consider the string: + 3

he Idea Bottom-up parsing reduces a string to the start symbol by inverting productions: + + + + + E E E E + E 4

Observation Read the productions in reverse (from bottom to top) his is a rightmost derivation! + + + + + E E E E + E 5

Important Fact #1 Important Fact #1 about bottom-up parsing: A bottom-up parser traces a rightmost derivation in reverse 6

A Bottom-up Parse + E + + E + + E E + 7

A Bottom-up Parse in Detail (1) + + 8

A Bottom-up Parse in Detail (2) + + + 9

A Bottom-up Parse in Detail (3) + + + + 10

A Bottom-up Parse in Detail (4) + + + + + 11

A Bottom-up Parse in Detail (5) + + + E + + E + 12

A Bottom-up Parse in Detail (6) + E + + E + + E E + 13

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 14

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? 15

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 16

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 17

Shift-Reduce Parsing Bottom-up parsing uses only two kinds of actions: Shift Reduce 18

Shift Shift: Move one place to the right Shifts a terminal to the left string ABC xyz ABCx yz 19

Reduce Apply an inverse production at the right end of the left string If A xy is a production, then Cbxy ijk CbA ijk 20

he Example with Reductions Only + + reduce reduce + + + E reduce reduce E reduce E + E E 21

he Example with Shift-Reduce Parsing + + + + + + + + + + E shift shift shift reduce reduce shift shift reduce reduce E reduce E + E E 22

A Shift-Reduce Parse in Detail (1) + + 23

A Shift-Reduce Parse in Detail (2) + + + 24

A Shift-Reduce Parse in Detail (3) + + + + 25

A Shift-Reduce Parse in Detail (4) + + + + + 26

A Shift-Reduce Parse in Detail (5) + + + + + + 27

A Shift-Reduce Parse in Detail (6) + + + + + + + 28

A Shift-Reduce Parse in Detail (7) + + + + + + + + 29

A Shift-Reduce Parse in Detail (8) + + + + + + + + + 30

A Shift-Reduce Parse in Detail (9) + + + + + + + + + + 31

A Shift-Reduce Parse in Detail (10) + + + + + E + + + + + E + 32

A Shift-Reduce Parse in Detail (11) + + + + + + + + + + E E E E + 33

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

Conflicts In a given state, more than one action (shift or reduce) may lead to a valid parse If it is legal to shift or reduce, there is a shiftreduce conflict If it is legal to reduce by two different productions, there is a reduce-reduce conflict 35