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

Similar documents
Intro to Bottom-up Parsing. Lecture 9

Introduction to Bottom-Up Parsing

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

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

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

Lecture 8: Deterministic Bottom-Up Parsing

Lecture 7: Deterministic Bottom-Up Parsing

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

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

Syntax-Directed Translation. Lecture 14

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

Lexical and Syntax Analysis. Bottom-Up Parsing

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

Wednesday, August 31, Parsers

Wednesday, September 9, 15. Parsers

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

Lecture Bottom-Up Parsing

S Y N T A X A N A L Y S I S 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

Monday, September 13, Parsers

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

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

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

LR Parsing LALR Parser Generators

LR Parsing LALR Parser Generators

Top down vs. bottom up parsing

Downloaded from Page 1. LR Parsing

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


In One Slide. Outline. LR Parsing. Table Construction

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

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

CS 4120 Introduction to Compilers

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

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

CMSC 330: Organization of Programming Languages

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

Chapter 4. Lexical and Syntax Analysis

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

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

LR Parsing Techniques

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

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

Using an LALR(1) Parser Generator

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

CS2210: Compiler Construction Syntax Analysis Syntax Analysis

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

Compiler Construction: Parsing

UNIT-III BOTTOM-UP PARSING

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

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

3. Parsing. Oscar Nierstrasz

LR Parsers. Aditi Raste, CCOEW

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

Chapter 4: LR Parsing

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

4. Lexical and Syntax Analysis

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

G53CMP: Lecture 4. Syntactic Analysis: Parser Generators. Henrik Nilsson. University of Nottingham, UK. G53CMP: Lecture 4 p.1/32

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

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

FROWN An LALR(k) Parser Generator

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

4. Lexical and Syntax Analysis

shift-reduce parsing

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

How do LL(1) Parsers Build Syntax Trees?

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

Introduction to Parsing. Lecture 8

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

CMSC 330: Organization of Programming Languages. Context Free Grammars

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

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.

Context-free grammars (CFG s)

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

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

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

More Bottom-Up Parsing

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

Parsing Algorithms. Parsing: continued. Top Down Parsing. Predictive Parser. David Notkin Autumn 2008

CMSC 330: Organization of Programming Languages

Types of parsing. CMSC 430 Lecture 4, Page 1

The Parsing Problem (cont d) Recursive-Descent Parsing. Recursive-Descent Parsing (cont d) ICOM 4036 Programming Languages. The Complexity of Parsing

LL Parsing: A piece of cake after LR

Context-Free Grammars

LR Parsing - The Items

Academic Formalities. CS Modern Compilers: Theory and Practise. Images of the day. What, When and Why of Compilers

Abstract Syntax Trees & Top-Down Parsing

Architecture of Compilers, Interpreters. CMSC 330: Organization of Programming Languages. Front End Scanner and Parser. Implementing the Front End

LR Parsing. Table Construction

Lexical and Syntax Analysis. Top-Down Parsing

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

Outline. Limitations of regular languages Parser overview Context-free grammars (CFG s) Derivations Syntax-Directed Translation

Transcription:

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

Outline Parser Overview op-down Parsers (Covered largely through labs) Bottom-up Parsers 2

he Functionality of the Parser Input: sequence of tokens from lexer Output: parse tree/as of the program Actions, e.g., semantic analysis, type checking Intermediate representation 3

Comparison with Lexical Analysis Phase Input Output Lexer Parser String of characters String of tokens String of tokens Parse tree/ AS 4

Bottom-up vs. op-down Successful Parse Example of grammars Example of parser technique Ease of implementation Issue with leftrecursion Issues with leftfactoring op-down Parsers From start symbol of grammar to the string LL(k) Left-to-right, Leftmost derivation first Recursive-descent Literally recursive descent Yes Yes Bottom-up Parsers From the string to the start symbol of the grammar LR(k), LALR Left-to-right, Rightmost derivation first (in reverse) Shift-reduce Many grammar generators available (Yacc, Bison, ) No No 5

Review: Bottom-Up Parsing Bottom-up parsing is more general than traditional top-down parsing And just as efficient Doesn t have issues with left-recursion Doesn t require left-factoring Many well-known parser generators (Yacc, Bison, ) Can handle many more grammars without backtracking than otherwise PEG parsers are top-down, more general than traditional top-down and bottom-up parsers 6

Review: 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: * + 7

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

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

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

A Bottom-up Parse * + E * + + E + + E E * + 11

A Bottom-up Parse in Detail (1) * + * + 12

A Bottom-up Parse in Detail (2) * + * + * + 13

A Bottom-up Parse in Detail (3) * + * + + * + 14

A Bottom-up Parse in Detail (4) * + * + + + * + 15

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

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

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 18

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

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

Shift Shift: Move one place to the right Shifts a terminal to the left string ABC xyz ABCx yz 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 24

he Example with Reductions Only * + * + reduce reduce * + + + E reduce reduce E reduce E + E E 25

he Example with Shift-Reduce Parsing * + * + * + * + * + + + + + + E shift shift shift reduce reduce * shift shift reduce reduce E reduce E + E E 26

A Shift-Reduce Parse in Detail (1) * + * + 27

A Shift-Reduce Parse in Detail (2) * + * + * + 28

A Shift-Reduce Parse in Detail (3) * + * + * + * + 29

A Shift-Reduce Parse in Detail (4) * + * + * + * + * + 30

A Shift-Reduce Parse in Detail (5) * + * + * + * + * + * + 31

A Shift-Reduce Parse in Detail (6) * + * + * + * + * + + * + 32

A Shift-Reduce Parse in Detail (7) * + * + * + * + * + + + * + 33

A Shift-Reduce Parse in Detail (8) * + * + * + * + * + + + + * + 34

A Shift-Reduce Parse in Detail (9) * + * + * + * + * + + + + + * + 35

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

A Shift-Reduce Parse in Detail (11) * + * + E * + * + * + E + + + + + E * + E 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) 38

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 39

Key Issue: o Shift or Reduce? How do we decide when to shift or reduce? Example grammar: E + E * (E) Consider step * + We could reduce by giving * + A fatal mistake! No way to reduce to the start symbol E 40

Handles: Symbols replaced by Reduction Intuition: Want to reduce only if the result can still be reduced to the start symbol Handle: Informally, represents the RHS of a production. Let X β be a production in G. hen β in the position after α is a handle of αβω 41

Handles (Cont.) Handles formalize the uition A handle is a string that can be reduced and also allows further reductions back to the start symbol (using a particular production at a specific spot) We only want to reduce at handles Note: We have said what a handle is, not how to find handles 42

Important Fact #2 Important Fact #2 about bottom-up parsing: In shift-reduce parsing, handles appear only at the top of the stack, never inside Using the symbol already shifted o the stack (left context) and the next k lookahead symbols (right context), decide to shift or reduce 43