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

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

CS453 : Shift Reduce Parsing Unambiguous Grammars LR(0) and SLR Parse Tables by Wim Bohm and Michelle Strout. CS453 Shift-reduce Parsing 1

Question Points Score

Context-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation

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

CS 4120 Introduction to Compilers

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

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

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

Downloaded from Page 1. LR Parsing

Syntax-Directed Translation. Lecture 14

Recursive Descent Parsers

CSCI312 Principles of Programming Languages

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

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

Part III : Parsing. From Regular to Context-Free Grammars. Deriving a Parser from a Context-Free Grammar. Scanners and Parsers.

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

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

Comp 411 Principles of Programming Languages Lecture 3 Parsing. Corky Cartwright January 11, 2019

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Action Table for CSX-Lite. LALR Parser Driver. Example of LALR(1) Parsing. GoTo Table for CSX-Lite

Wednesday, September 9, 15. Parsers

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

Introduction to Bottom-Up Parsing

Lecture 8: Deterministic Bottom-Up Parsing

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

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

Lecture 7: Deterministic Bottom-Up Parsing

Note that for recursive descent to work, if A ::= B1 B2 is a grammar rule we need First k (B1) disjoint from First k (B2).

JavaCC Parser. The Compilation Task. Automated? JavaCC Parser

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

Chapter 3. Parsing #1

Chapter 4. Lexical and Syntax Analysis

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

CS 314 Principles of Programming Languages

Top down vs. bottom up parsing

Building a Parser Part III

Wednesday, August 31, Parsers

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

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

COP4020 Programming Assignment 2 - Fall 2016

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

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

Context-free grammars

Bottom-Up Parsing. Lecture 11-12

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS

Bottom-up Parser. Jungsik Choi

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

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

Compiler Construction: Parsing

How do LL(1) Parsers Build Syntax Trees?

Some Thoughts on Grad School. Undergraduate Compilers Review

1. Explain the input buffer scheme for scanning the source program. How the use of sentinels can improve its performance? Describe in detail.

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

Intro to Bottom-up Parsing. Lecture 9

Programs as data Parsing cont d; first-order functional language, type checking

A clarification on terminology: Recognizer: accepts or rejects strings in a language. Parser: recognizes and generates parse trees (imminent topic)

Chapter 4: LR Parsing

Lexical and Syntax Analysis. Top-Down Parsing

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

Compiler construction in4303 lecture 3

COP4020 Programming Assignment 2 Spring 2011

Monday, September 13, Parsers

aabb S "SS S "asb S "bsa S "# Example: Parser derivation input Plan for Today Finish complexity argument for exhaustive search parsing

Introduction to Parsing. Comp 412

shift-reduce parsing

Bottom-Up Parsing. Lecture 11-12

Programming Language Syntax and Analysis

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

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

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

Lecture Notes on Bottom-Up LR Parsing

Parsing. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.

Compiler Design 1. Bottom-UP Parsing. Goutam Biswas. Lect 6

Using an LALR(1) Parser Generator

SYED AMMAL ENGINEERING COLLEGE (An ISO 9001:2008 Certified Institution) Dr. E.M. Abdullah Campus, Ramanathapuram

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

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

CS 406/534 Compiler Construction Putting It All Together

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

Lecture Notes on Bottom-Up LR Parsing

Building a Parser II. CS164 3:30-5:00 TT 10 Evans. Prof. Bodik CS 164 Lecture 6 1

LR Parsing - Conflicts

Lexical and Syntax Analysis

Syntax/semantics. Program <> program execution Compiler/interpreter Syntax Grammars Syntax diagrams Automata/State Machines Scanning/Parsing

Syntax Analysis Part I

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

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

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

Lecture Bottom-Up Parsing

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

LR Parsing - The Items

Context-sensitive Analysis Part IV Ad-hoc syntax-directed translation, Symbol Tables, andtypes

Topic 3: Syntax Analysis I

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

CS415 Compilers. LR Parsing & Error Recovery

Syntactic Analysis. Top-Down Parsing

Alternatives for semantic processing

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

COMP3131/9102: Programming Languages and Compilers

Transcription:

Plan for Today Review main idea syntax-directed evaluation and translation Recall syntax-directed interpretation in recursive descent parsers Syntax-directed evaluation and translation in shift-reduce parser Performs a right-most derivation in reverse Parsing unambiguous grammars Parsing ambiguous grammars using precedence and associativity rules LR parsing table algorithm LR parsing algorithm and syntax-directed evaluation and translation Syntax-directed code generation for PA3 CS453 Lecture Shift-reduce Parsing 1 Parse Tree Example CS453 Lecture Shift-reduce Parsing 2

Semantic Rules for Expression Example (JavaCUP) CS453 Lecture Shift-reduce Parsing 3 Semantic Rules for Expression Example (book notation) CS453 Lecture Shift-reduce Parsing 4

Recall recursive-descent parsing private void parse_elem() throws IOException, ParseException { switch (this.m_lookahead.tag) { // elem -> // RECT_START KW_X EQ NUM KW_Y EQ NUM KW_WIDTH EQ NUM KW_HEIGHT EQ NUM // KW_FILL EQ COLOR ELEM_END case RECT_START: this.match(token.tag.rect_start); this.match(token.tag.kw_x); this.match(token.tag.eq); Num x = (Num) this.match(token.tag.num); this.match(token.tag.kw_y); this.match(token.tag.eq); Num y = (Num) this.match(token.tag.num); this.match(token.tag.kw_width); this.match(token.tag.eq); Num width = (Num) this.match(token.tag.num); this.match(token.tag.kw_height); this.match(token.tag.eq); Num height = (Num) this.match(token.tag.num); this.match(token.tag.kw_fill); this.match(token.tag.eq); Color color = (Color) this.match(token.tag.color); this.match(token.tag.elem_end); this.m_drawer.draw_rect(x.value, y.value, width.value, height.value, color.value); this.m_reporter.report_rect(x.value, y.value, width.value, height.value, color.value); CS453 Lecture break; Top-Down Predictive Parsers 5 Recall Plan for Today Review main idea syntax-directed evaluation and translation Recall syntax-directed interpretation in recursive descent parsers Syntax-directed evaluation and translation in shift-reduce parser Performs a right-most derivation in reverse Parsing unambiguous grammars Parsing ambiguous grammars using precedence and associativity rules LR parsing table algorithm LR parsing algorithm and syntax-directed evaluation and translation Syntax-directed code generation for PA3 CS453 Lecture Shift-reduce Parsing 6

Shift-reduce parsing Form of bottom-up parsing Implicit parse tree is built bottom up Order is equivalent to a reverse right-most derivation A reduction is the reverse of a derivation step How it works Start with a stack and a token stream as input Based on the symbols at the top of the stack and the next token in input perform one of the following actions: Shift: consume the token from input and push onto stack Reduce: replace right-hand side of a production rule on the top of the stack with the left-hand side nonterminal for that rule Accept: indicate that parsing is complete Error: indicate there is a parsing error CS453 Lecture Shift-reduce Parsing 7 Shift reduce parsing applied to unambiguous grammars, ex1 (1) S -> E $ (2) E -> E + T (3) E -> T (4) T -> T * F (5) T -> F (6) F -> ( E ) (7) F -> NUM CS453 Lecture Top-Down Predictive Parsers 8

Shift reduce parsing applied to unambiguous grammars, ex2 [0] S -> ( S ) [1] S -> S EOF [2] S -> ID Start symbol is S CS453 Lecture Top-Down Predictive Parsers 9 Shift reduce parsing applied to ambiguous grammars, ex1 (1) S -> E $ (2) E -> E + E (3) E -> E * E (4) E -> ( E ) (5) E -> NUM CS453 Lecture Top-Down Predictive Parsers 10

Shift reduce parsing applied to ambiguous grammars, ex2 (1) S -> E $ (2) E -> E + E (3) E -> ( byte ) E (4) E -> NUM CS453 Lecture Top-Down Predictive Parsers 11 Shift-reduce parsing in an LR parser LR(k) parser Left-to-right parse Right-most derivation K-token look ahead LR parsing algorithm Performs a shift-reduce parse Look at state at top of stack and input symbol to find action in table shift(n): advance input, push state n on stack reduce(k): pop rhs of grammar rule k, look up state on top of stack and lhs for goto n, push lhs(k) and n onto stack accept: stop and success error: stop and fail CS453 Lecture Shift-reduce Parsing 12

Example LR Parse Table, Single Paren Nest [0] S -> ( S ) [1] S -> S EOF [2] S -> ID Action State ( ) $/EOF ID S 0 s3 s1 2 1 r2 r2 r2 r2 2 accept 3 s3 s1 4 4 s5 5 r0 r0 r0 r0 Goto CS453 Lecture Shift-reduce Parsing 13