CS415 Compilers Syntax Analysis Bottom-up Parsing

Similar documents
LR(1) Parsers Part II. Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved.

Parsing VI LR(1) Parsers

LR(1) Parsers Part II. Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved.

LR(1) Parsers Part III Last Parsing Lecture. Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved.

Syntax Analysis, VII The Canonical LR(1) Table Construction. Comp 412

Syntax Analysis, VII The Canonical LR(1) Table Construction. Comp 412 COMP 412 FALL Chapter 3 in EaC2e. source code. IR IR target.

Computer Science 160 Translation of Programming Languages

Syntax Analysis, VI Examples from LR Parsing. Comp 412

CS415 Compilers Syntax Analysis Top-down Parsing

Bottom-Up Parsing. Ÿ rm E + F *idÿ rm E +id*idÿ rm T +id*id. Ÿ rm F +id*id Ÿ rm id + id * id

Administrivia. Test I during class on 10 March. Bottom-Up Parsing. Lecture An Introductory Example

Lecture VII Part 2: Syntactic Analysis Bottom-up Parsing: LR Parsing. Prof. Bodik CS Berkley University 1

Compiling Techniques

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

Lexical Analysis Part II: Constructing a Scanner from Regular Expressions

Parsing -3. A View During TD Parsing

CA Compiler Construction

Syntactic Analysis. Top-Down Parsing

CS 314 Principles of Programming Languages

LR2: LR(0) Parsing. LR Parsing. CMPT 379: Compilers Instructor: Anoop Sarkar. anoopsarkar.github.io/compilers-class

Compiler Design Spring 2017

CS 406: Bottom-Up Parsing

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

Syntax Analysis (Part 2)

Parsing Algorithms. CS 4447/CS Stephen Watt University of Western Ontario

CSE 105 THEORY OF COMPUTATION

Chapter 4: Bottom-up Analysis 106 / 338

Shift-Reduce parser E + (E + (E) E [a-z] In each stage, we shift a symbol from the input to the stack, or reduce according to one of the rules.

CMSC 330: Organization of Programming Languages. Pushdown Automata Parsing

MA/CSSE 474 Theory of Computation

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

Compiler Design 1. LR Parsing. Goutam Biswas. Lect 7

Introduction to Bottom-Up Parsing

Bottom up parsing. General idea LR(0) SLR LR(1) LALR To best exploit JavaCUP, should understand the theoretical basis (LR parsing);

n Top-down parsing vs. bottom-up parsing n Top-down parsing n Introduction n A top-down depth-first parser (with backtracking)

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών. Lecture 7a Syntax Analysis Elias Athanasopoulos

Introduction to Bottom-Up Parsing

Introduction to Bottom-Up Parsing

Syntax Analysis Part I

CSE302: Compiler Design

Syntax Analysis Part I. Position of a Parser in the Compiler Model. The Parser. Chapter 4

THEORY OF COMPILATION

CFLs and Regular Languages. CFLs and Regular Languages. CFLs and Regular Languages. Will show that all Regular Languages are CFLs. Union.

Syntax Analysis Part I

Introduction to Bottom-Up Parsing

Compiler Construction Lent Term 2015 Lectures (of 16)

Compiler Construction Lent Term 2015 Lectures (of 16)

Context free languages

Compiler Construction Lectures 13 16

Announcements. H6 posted 2 days ago (due on Tue) Midterm went well: Very nice curve. Average 65% High score 74/75

Top-Down Parsing and Intro to Bottom-Up Parsing

CS Pushdown Automata

I 1 : {S S } I 2 : {S X ay, Y X } I 3 : {S Y } I 4 : {X b Y, Y X, X by, X c} I 5 : {X c } I 6 : {S Xa Y, Y X, X by, X c} I 7 : {X by } I 8 : {Y X }

Syntax Directed Transla1on

Computer Science 160 Translation of Programming Languages

Announcement: Midterm Prep

Lecture 11 Sections 4.5, 4.7. Wed, Feb 18, 2009

(pp ) PDAs and CFGs (Sec. 2.2)

Everything You Always Wanted to Know About Parsing

FORMAL LANGUAGES, AUTOMATA AND COMPUTATION

Compilers. Lexical analysis. Yannis Smaragdakis, U. Athens (original slides by Sam

Compiling Techniques

CS Rewriting System - grammars, fa, and PDA

Lexical Analysis: DFA Minimization & Wrap Up

Bottom-Up Syntax Analysis

Compiler Principles, PS4

Why augment the grammar?

Theory of Computation

CS20a: summary (Oct 24, 2002)

Predictive parsing as a specific subclass of recursive descent parsing complexity comparisons with general parsing

Harvard CS 121 and CSCI E-207 Lecture 10: CFLs: PDAs, Closure Properties, and Non-CFLs

Compiling Techniques

Intro to Theory of Computation

Handout 8: Computation & Hierarchical parsing II. Compute initial state set S 0 Compute initial state set S 0

Translator Design Lecture 16 Constructing SLR Parsing Tables

CS5371 Theory of Computation. Lecture 7: Automata Theory V (CFG, CFL, CNF)

CFGs and PDAs are Equivalent. We provide algorithms to convert a CFG to a PDA and vice versa.

Parsing. Left-Corner Parsing. Laura Kallmeyer. Winter 2017/18. Heinrich-Heine-Universität Düsseldorf 1 / 17

Context-Free Grammars (and Languages) Lecture 7

Pushdown Automata: Introduction (2)

EXAM. Please read all instructions, including these, carefully NAME : Problem Max points Points 1 10 TOTAL 100

(pp ) PDAs and CFGs (Sec. 2.2)

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

Material Covered on the Final

Curs 8. LR(k) parsing. S.Motogna - FL&CD

Harvard CS 121 and CSCI E-207 Lecture 10: Ambiguity, Pushdown Automata

CS153: Compilers Lecture 5: LL Parsing

Lecture 10: Data Flow Analysis II

Construction of Static Single-Assignment Form

Syntax Analysis Part III

CS 301. Lecture 18 Decidable languages. Stephen Checkoway. April 2, 2018

Type 3 languages. Type 2 languages. Regular grammars Finite automata. Regular expressions. Type 2 grammars. Deterministic Nondeterministic.

Bottom-Up Syntax Analysis

Bottom-up Analysis. Theorem: Proof: Let a grammar G be reduced and left-recursive, then G is not LL(k) for any k.

Pushdown Automata (Pre Lecture)

1. For the following sub-problems, consider the following context-free grammar: S AA$ (1) A xa (2) A B (3) B yb (4)

Please give details of your answer. A direct answer without explanation is not counted.

CISC4090: Theory of Computation

Foundations of Informatics: a Bridging Course

THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET

Transcription:

CS415 Compilers Syntax Analysis Bottom-up Parsing These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Review: LR(k) Shift-reduce Parsing Shift reduce parsers are easily built and easily understood A shift-reduce parser has just four actions Shift next word is shifted onto the stack Reduce right end of handle is at top of stack Locate left end of handle within the stack Pop handle off stack & push appropriate lhs Accept stop parsing & report success Error call an error reporting/recovery routine Accept & Error are simple Shift is just a push and a call to the scanner Reduce takes rhs pops (or 2* rhs pops) & 1 push If handle-finding requires state, put it in the stack 2x work 2

Review - LR(k) items The LR(1) table construction algorithm uses LR(1) items to represent valid configurations of an LR(1) parser An LR(k) item is a pair [P, δ], where P is a production A β with a at some position in the rhs δ is a lookahead string of length k (words or EOF) The in an item indicates the position of the top of the stack LR(1): [A βγ,a] means that the input seen so far is consistent with the use of A βγ immediately after the symbol on top of the stack [A β γ,a] means that the input seen so far is consistent with the use of A βγ at this point in the parse, and that the parser has already recognized β. [A βγ,a] means that the parser has seen βγ, and that a lookahead symbol of a is consistent with reducing to A. 3

Review LR(1) Table Construction High-level overview 1 Build the canonical collection of sets of LR(k) Items, I a Begin in an appropriate state, s 0 Assume: S S, and S is unique start symbol that does not occur on any RHS of a production (extended CFG - ECFG) [S S,EOF], along with any equivalent items Derive equivalent items as closure( s 0 ) b Repeatedly compute, for each s k, and each X, goto(s k,x) If the set is not already in the collection, add it Record all the transitions created by goto( ) This eventually reaches a fixed point 2 Fill in the table from the collection of sets of LR(1) items The canonical collection completely encodes the transition diagram for the handle-finding DFA 4

Review - Computing Closures Closure(s) adds all the items implied by items already in s Any item [A β Bδ,a] implies [B τ,x] for each production with B on the lhs, and each x FIRST(δa) for LR(1) item The algorithm Closure( s ) while ( s is still changing ) items [A β Bδ,a] s productions B τ P b FIRST(δa) // δ might be ε if [B τ,b] s then add [B τ,b] to s Ø Classic fixed-point method Ø Halts because s ITEMS Closure fills out a state 5

Review - Computing Gotos Goto(s,x) computes the state that the parser would reach if it recognized an x while in state s Goto( { [A β Xδ,a] }, X ) produces [A βx δ,a] (easy part) Should also includes closure( [A βx δ,a] ) (fill out the state) The algorithm Goto( s, X ) new Ø items [A β Xδ,a] s new new [A βx δ,a] return closure(new) Ø Not a fixed-point method! Ø Straightforward computation Ø Uses closure ( ) Goto() moves forward 6

Review - Building the Canonical Collection Start from s 0 = closure( [S S,EOF ] ) Repeatedly construct new states, until all are found The algorithm cc 0 closure ( [S S, EOF] ) CC { cc 0 } while ( new sets are still being added to CC) for each unmarked set cc j CC mark cc j as processed for each x following a in an item in cc j temp goto(cc j, x) if temp CC then CC CC { temp } record transitions from cc j to temp on x Ø Fixed-point computation (worklist version) Ø Loop adds to CC Ø CC 2 ITEMS, so CC is finite 7

One Example 8

One Example 9

One Example 10

One Example 11

One Example 12

One Example 13

One Example 14

One Example LR(0) states 15

Another Example LR(1) states Simplified, right recursive expression grammar 1: Goal Expr 2: Expr Term Expr 3: Expr Term 4: Term Factor * Term 5: Term Factor 6: Factor ident Symbol FIRST Goal { ident } Expr { ident } Term { ident } Factor { ident } { } * { * } ident { ident } 16

Another Example (building the collection) 1: Goal Expr 2: Expr Term Expr 3: Expr Term 4: Term Factor * Term 5: Term Factor 6: Factor ident Initialization Step Symbol FIRST Goal { ident } Expr { ident } Term { ident } Factor { ident } { } * { * } ident { ident } s 0 closure( { [Goal Expr, EOF] } ) = { [Expr à Term Expr, EOF], [Expr à Term, EOF], [Term à Factor * Term, -], [Term à Factor, -], [Term à Factor * Term, EOF], [Term à Factor, EOF], [Factor à ident, *], [Factor à ident, -], [Factor à ident, EOF]} S { S 0 } 17

Example (building the collection) s 0 closure( { [Goal Expr, EOF] } ) { [Goal Expr, EOF], [Expr Term Expr, EOF], [Expr Term, EOF], [Term Factor * Term, EOF], [Term Factor * Term, ], [Term Factor, EOF], [Term Factor, ], [Factor ident, EOF], [Factor ident, ], [Factor ident, *] } Iteration 1 s 1 goto(s 0, Expr) s 2 goto(s 0, Term) s 3 goto(s 0, Factor) s 4 goto(s 0, ident ) 18

Example (building the collection) s 0 closure( { [Goal Expr, EOF] } ) { [Goal Expr, EOF], [Expr Term Expr, EOF], [Expr Term, EOF], [Term Factor * Term, EOF], [Term Factor * Term, ], [Term Factor, EOF], [Term Factor, ], [Factor ident, EOF], [Factor ident, ], [Factor ident, *] } Iteration 1 s 1 goto(s 0, Expr) = { [Goal Expr, EOF] } s 2 goto(s 0, Term) = { [Expr Term Expr, EOF], [Expr Term, EOF] } s 3 goto(s 0, Factor) = { [Term Factor * Term, EOF],[Term Factor * Term, ], [Term Factor, EOF], [Term Factor, ] } s 4 goto(s 0, ident ) = { [Factor ident, EOF],[Factor ident, ], [Factor ident, *] } 19

Example (building the collection) Iteration 1 s 1 goto(s 0, Expr) = { [Goal Expr, EOF] } s 2 goto(s 0, Term) = { [Expr Term Expr, EOF], [Expr Term, EOF] } s 3 goto(s 0, Factor) = { [Term Factor * Term, EOF],[Term Factor * Term, ], [Term Factor, EOF], [Term Factor, ] } s 4 goto(s 0, ident ) = { [Factor ident, EOF],[Factor ident, ], [Factor ident, *] } Iteration 2 s 5 goto(s 2, ) s 6 goto(s 3, * ) 20

Example (building the collection) Iteration 1 s 1 goto(s 0, Expr) = { [Goal Expr, EOF] } s 2 goto(s 0, Term) = { [Expr Term Expr, EOF], [Expr Term, EOF] } s 3 goto(s 0, Factor) = { [Term Factor * Term, EOF],[Term Factor * Term, ], [Term Factor, EOF], [Term Factor, ] } s 4 goto(s 0, ident ) = { [Factor ident, EOF],[Factor ident, ], [Factor ident, *] } Iteration 2 s 5 goto(s 2, ) = { [Expr Term Expr, EOF], [Expr Term Expr, EOF], [Expr Term, EOF], [Term Factor * Term, ], [Term Factor, ], [Term Factor * Term, EOF], [Term Factor, EOF], [Factor ident, *], [Factor ident, ], [Factor ident, EOF] } s 6 goto(s 3, * ) = see next page 21

Example (building the collection) Iteration 2 s 5 goto(s 2, ) = { [Expr Term Expr, EOF], [Expr Term Expr, EOF], [Expr Term, EOF], [Term Factor * Term, ], [Term Factor * Term, EOF], [Term Factor, ], [Term Factor, EOF], [Factor ident, *], [Factor ident, ], [Factor ident, EOF] } s 6 goto(s 3, * ) = { [Term Factor * Term, EOF], [Term Factor * Term, ], [Term Factor * Term, EOF], [Term Factor * Term, ], [Term Factor, EOF], [Term Factor, ], [Factor ident, EOF], [Factor ident, ], [Factor ident, *] } Iteration 3 s 7 goto(s 5, Expr ) = { [Expr Term Expr, EOF] } s 8 goto(s 6, Term ) = { [Term Factor * Term, EOF], [Term Factor * Term, ] } goto(s 5, Term) = S 2, goto(s 5, factor) = s 3, goto(s 5, ident) = s 4 goto(s 6, Factor) = s 3, goto(s 6, ident) = s 4 22

Example (Summary) S 0 : { [Goal Expr, EOF], [Expr Term Expr, EOF], [Expr Term, EOF], [Term Factor * Term, EOF], [Term Factor * Term, ], [Term Factor, EOF], [Term Factor, ], [Factor ident, EOF], [Factor ident, ], [Factor ident, *] } S 1 : { [Goal Expr, EOF] } S 2 : { [Expr Term Expr, EOF], [Expr Term, EOF] } S 3 : { [Term Factor * Term, EOF],[Term Factor * Term, ], [Term Factor, EOF], [Term Factor, ] } S 4 : { [Factor ident, EOF],[Factor ident, ], [Factor ident, *] } S 5 : { [Expr Term Expr, EOF], [Expr Term Expr, EOF], [Expr Term, EOF], [Term Factor * Term, ], [Term Factor, ], [Term Factor * Term, EOF], [Term Factor, EOF], [Factor ident, *], [Factor ident, ], [Factor ident, EOF] } 23

Example (Summary) S 6 : { [Term Factor * Term, EOF], [Term Factor * Term, ], [Term Factor * Term, EOF], [Term Factor * Term, ], [Term Factor, EOF], [Term Factor, ], [Factor ident, EOF], [Factor ident, ], [Factor ident, *] } S 7 : { [Expr Term Expr, EOF] } S 8 : { [Term Factor * Term, EOF], [Term Factor * Term, ] } 24

Example (DFA) term s 1 s 2 s 7 term - expr expr ident ident s 0 s 4 s 5 term ident factor s 6 * s 3 s 8 factor factor The State Transition Table State Expr Term Factor - * Ident 0 1 2 3 4 1 2 5 3 6 4 5 7 2 3 4 6 8 3 4 7 8 25

Filling in the ACTION and GOTO Tables The algorithm set s x S item i s x if i is [A β ad,b] and goto(s x,a) = s k, a T then ACTION[x,a] shift k else if i is [S S,EOF] then ACTION[x, EOF] accept else if i is [A β,a] then ACTION[x,a] reduce A β n NT if goto(s x,n) = s k then GOTO[x,n] k Many items generate no table entry 26

Next class Wrap Up Syntax Analysis Context-Sensitive Analysis Read EaC: Chapters 3.4, 4.1 4.3 27