Solutions to Final COMS W4115 Programming Languages and Translators Monday, May 4, :10-5:25pm, 309 Havemeyer

Similar documents
CSC 220: Computer Organization Unit 11 Basic Computer Organization and Design

EE University of Minnesota. Midterm Exam #1. Prof. Matthew O'Keefe TA: Eric Seppanen. Department of Electrical and Computer Engineering

COSC 1P03. Ch 7 Recursion. Introduction to Data Structures 8.1

CS553 Lecture Reuse Optimization: Common SubExpr Elim 3

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Algorithms and Data Structures

COP4020 Programming Languages. Compilers and Interpreters Prof. Robert van Engelen

Python Programming: An Introduction to Computer Science

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Homework 1 Solutions MA 522 Fall 2017

Chapter 9. Pointers and Dynamic Arrays. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

CS 11 C track: lecture 1

Today s objectives. CSE401: Introduction to Compiler Construction. What is a compiler? Administrative Details. Why study compilers?

9.1. Sequences and Series. Sequences. What you should learn. Why you should learn it. Definition of Sequence

CMPT 125 Assignment 2 Solutions

EE 459/500 HDL Based Digital Design with Programmable Logic. Lecture 13 Control and Sequencing: Hardwired and Microprogrammed Control

Ones Assignment Method for Solving Traveling Salesman Problem

Computers and Scientific Thinking

Lecture 1: Introduction and Strassen s Algorithm

Chapter 4 The Datapath

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

n Some thoughts on software development n The idea of a calculator n Using a grammar n Expression evaluation n Program organization n Analysis

Behavioral Modeling in Verilog

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

COMP Parallel Computing. PRAM (1): The PRAM model and complexity measures

Project 2.5 Improved Euler Implementation

condition w i B i S maximum u i

CS211 Fall 2003 Prelim 2 Solutions and Grading Guide

EE260: Digital Design, Spring /16/18. n Example: m 0 (=x 1 x 2 ) is adjacent to m 1 (=x 1 x 2 ) and m 2 (=x 1 x 2 ) but NOT m 3 (=x 1 x 2 )

Big-O Analysis. Asymptotics

Reversible Realization of Quaternary Decoder, Multiplexer, and Demultiplexer Circuits

Pseudocode ( 1.1) Analysis of Algorithms. Primitive Operations. Pseudocode Details. Running Time ( 1.1) Estimating performance

Task scenarios Outline. Scenarios in Knowledge Extraction. Proposed Framework for Scenario to Design Diagram Transformation

Python Programming: An Introduction to Computer Science

Chapter 3 Classification of FFT Processor Algorithms

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring, Instructions:

Creating Exact Bezier Representations of CST Shapes. David D. Marshall. California Polytechnic State University, San Luis Obispo, CA , USA

Recursion. Recursion. Mathematical induction: example. Recursion. The sum of the first n odd numbers is n 2 : Informal proof: Principle:

Big-O Analysis. Asymptotics

Math 10C Long Range Plans

10/23/18. File class in Java. Scanner reminder. Files. Opening a file for reading. Scanner reminder. File Input and Output

The number n of subintervals times the length h of subintervals gives length of interval (b-a).

South Slave Divisional Education Council. Math 10C

Bezier curves. Figure 2 shows cubic Bezier curves for various control points. In a Bezier curve, only

From last week. Lecture 5. Outline. Principles of programming languages

top() Applications of Stacks

Elementary Educational Computer

Chapter 2. C++ Basics. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Improving Template Based Spike Detection

Recursion. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Review: Method Frames

Abstract. Chapter 4 Computation. Overview 8/13/18. Bjarne Stroustrup Note:

Mathematics. Programming

Sorting in Linear Time. Data Structures and Algorithms Andrei Bulatov

n Bottom-up (LR) parsing n Handle n LR item n Characteristic Finite State Machine (CFSM) n SLR(1) parsing tables n Conflicts in SLR(1)

CSC165H1 Worksheet: Tutorial 8 Algorithm analysis (SOLUTIONS)

Lecture 6. Lecturer: Ronitt Rubinfeld Scribes: Chen Ziv, Eliav Buchnik, Ophir Arie, Jonathan Gradstein

Lecture 28: Data Link Layer

Midterm Solutions COMS W4115 Programming Languages and Translators Wednesday, March 25, :10-5:25pm, 309 Havemeyer

Chapter 3. More Flow of Control. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Recursive Procedures. How can you model the relationship between consecutive terms of a sequence?

BOOLEAN MATHEMATICS: GENERAL THEORY

Arithmetic Sequences

WORKED EXAMPLE 7.1. Producing a Mass Mailing. We want to automate the process of producing mass mailings. A typical letter might look as follows:

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Major CSL Write your name and entry no on every sheet of the answer script. Time 2 Hrs Max Marks 70

n Bottom-up (LR) parsing n Characteristic Finite State Machine (CFSM) n SLR(1) parsing table n Conflicts in SLR(1) n LR parsing variants

Lecture 9: Exam I Review

Chapter 1. Introduction to Computers and C++ Programming. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

EVALUATION OF TRIGONOMETRIC FUNCTIONS

Inductive Definition to Recursive Function

Data Structures Week #9. Sorting

Programming with Shared Memory PART II. HPC Spring 2017 Prof. Robert van Engelen

5.3 Recursive definitions and structural induction

How do we evaluate algorithms?

Classes and Objects. Again: Distance between points within the first quadrant. José Valente de Oliveira 4-1

CIS 121 Data Structures and Algorithms with Java Spring Stacks and Queues Monday, February 12 / Tuesday, February 13

Computer Science Foundation Exam. August 12, Computer Science. Section 1A. No Calculators! KEY. Solutions and Grading Criteria.

1.2 Binomial Coefficients and Subsets

Chapter 10. Defining Classes. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

1. The lines intersect. There is one solution, the point where they intersect. The system is called a consistent system.

1.8 What Comes Next? What Comes Later?

n n B. How many subsets of C are there of cardinality n. We are selecting elements for such a

Chapter 11. Friends, Overloaded Operators, and Arrays in Classes. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

CIS 121. Introduction to Trees

Chapter 5: Processor Design Advanced Topics. Microprogramming: Basic Idea

Implementing Dynamic Programming Recurrences in Constraint Handling Rules with Rule Priorities

The CS job market. Building Java Programs Chapter 1. Programming languages. What is programming? Introduction to Java Programming

Threads and Concurrency in Java: Part 1

DEFINITION OF CELL BEHAVIOUR. Actions and Behaviour. CELL = a CELL CELL = b CELL

The Graphs of Polynomial Functions

CS 111: Program Design I Lecture 16: Module Review, Encodings, Lists

Threads and Concurrency in Java: Part 1

CS 111: Program Design I Lecture 15: Objects, Pandas, Modules. Robert H. Sloan & Richard Warner University of Illinois at Chicago October 13, 2016

Outline. Research Definition. Motivation. Foundation of Reverse Engineering. Dynamic Analysis and Design Pattern Detection in Java Programs

Module 8-7: Pascal s Triangle and the Binomial Theorem

15-859E: Advanced Algorithms CMU, Spring 2015 Lecture #2: Randomized MST and MST Verification January 14, 2015

End Semester Examination CSE, III Yr. (I Sem), 30002: Computer Organization

the beginning of the program in order for it to work correctly. Similarly, a Confirm

UNIT 1 RECURRENCE RELATIONS

CS 111: Program Design I Lecture # 7: First Loop, Web Crawler, Functions

12-5A. Equivalent Fractions and Decimals. 1 Daily Common Core Review. Common Core. Lesson. Lesson Overview. Math Background

Transcription:

Departmet of Computer ciece Columbia Uiversity olutios to Fial COM W45 Programmig Laguages ad Traslators Moday, May 4, 2009 4:0-5:25pm, 309 Havemeyer Closed book, o aids. Do questios 5. Each questio is worth 20 poits. Questio 6 is optioal, extra credit, 0 poits.. Here is a fragmet of C code: struct studet { it id; char studet[30]; } studet; a) Explai the roles of the three uses of the idetifier studet. struct studet { // here studet is a structure tag it id; char studet[30]; // here studet is a structure member } studet; // here studet is a variable b) Are these three uses i the same scope? Explai. The three uses are i the same scope but i differet ame spaces. 2. Cosider the cotext-free grammar G: ab ba a) Describe L(G). how two parse trees for the setece abab i L(G). L(G) is the set of all strigs of a s ad b s with the same umber of a s as b s. a b a b b a a b

b) Costruct the LR() parsig actio ad goto tables for L(G). how the behavior of a LR() parser usig these tables o the iput abab. The sets of items for the augmeted grammar are: I0: ab ba I3: b a ab ba I: I2: a b ab ba I4: a b I5: b a I6: ab ab ba I9: ab I7: ba ab ba I8: ab The parsig actio ad goto tables costructed from these sets of items are: tate Actio Goto a b $ 0 s2/r (3) s3/r (3) r(3) accept 2 s2/r (3) s3/r (3) r(3) 4 3 s2/r (3) s3/r (3) r(3) 5 4 s6 5 s7 6 s2/r (3) s3/r (3) r(3) 8 7 s2/r (3) s3/r (3) r(3) 9 8 r() r() r() 9 r(2) r(2) r(2) Note the multiple shift-reduce coflicts that arise from the ambiguity i the grammar. O the iput abab, here is oe sequece of moves a LR() parser usig these tables ca make: 2

tack ymbols Iput Actio 0 abab$ shift 2 02 a bab$ shift 3 023 ab ab$ reduce by 0235 ab ab$ shift 7 02357 aba b$ reduce by 023579 aba b$ reduce by ba 024 a b$ shift 6 0246 ab $ reduce by 02468 ab $ reduce by ab 0 $ accept This sequece of moves correspods to the first parse tree above. There is aother sequece of moves correspodig to the secod parse tree. 3. Cosider the followig partial sytax-directed defiitio for traslatig ifstatemets ito three address code: Productio P assig if ( B ) ematic Rules.ext = ewlabel() P.code =.code label(.ext).code = assig.code B.true = ewlabel() B.false =.ext.ext =.ext.code = B.code label(b.true).code B B && B 2? B ot B? B true? B false? Here the fuctio ewlabel() creates a ew label each time it is called ad label(l) attaches label L to the ext three-address istructio to be geerated. a) Fill i the sematic rules for the B-productios (they represet boolea expressios) usig jumps to true ad false labels. ee ALU, p. 404. b) how how your DD traslates the if-statemet if (true && ot false) assig 3

ito three-address istructios by costructig a aotated parse tree for the if-statemet. P P.code = "goto L3 L3: goto L2 L2: assig.code L:".ext = "L" P.code = "goto L3 L3: goto L2 L2: assig.code" if ( B ) B.true = "L2".ext = "L" B.false = "L" B.code = "goto L3 L3: goto L2".code = "assig.code" B && B B.true = "L3" B.true = "L2" B.false = "L" B.false = "L" B.code = "goto L3" B.code = "goto L2" assig true ot B B.true = "L" B.false = "L2" B.code = "goto L2" false 4

4. Cosider the arithmetic expressio a * b + c / (d e) ad a register machie with istructios of the form LD reg, src T dst, reg OP reg, reg2, reg3 // the registers eed ot be distict a) Draw a sytax tree for the expressio ad label the odes with Ershov umbers. The sytax tree of this expressio is isomorphic to the oe o p. 568, ALU. b) Geerate machie code for the expressio o a machie with two registers miimizig the umber of spills. This expressio eeds oe spill o a two-register machie. ee ALU, p. 572, for a isomorphic code sequece. 5. Cosider the followig sequece of three-address code: x = 0 i = 0 L: t = i * 4 t2 = a[t] t3 = i * 4 t4 = b[t3] t5 = t2 * t4 x = x + t5 i = i + if i < goto L a) Draw a flow graph for this three-address code. 5

ENTRY () x = 0 (2) i = 0 B (3)L:t = i * 4 (4) t2 = a[t] (5) t3 = i * 4 (6) t4 = b[t3] (7) t5 = t2 * t4 (8) x = x + t5 (9) i = i + (0) if i< goto L B2 EXIT b) Optimize this code by elimiatig commo subexpressios, performig reductio i stregth o iductio variables, ad elimiatig all the iductio variables that you ca. tate what trasformatios you are usig at each optimizatio step. First, we ca elimiate the commo subexpressio i*4 i lies (3) ad (4) by usig t i place of t3 i lie (6) ad elimiatig lie (5). Next, t ad i are both iductio variables i block B2. We ca elimiate either oe of these iductio variables i the loop. We choose to elimiate t by replacig lie (9) by i = i + 4, addig the statemet t6 = 4 *, to the ed of block B, replacig the test i< i lie (0) by i<t6, ad usig i to idex the arrays. The resultig optimized flow graph is: 6

ENTRY x = 0 i = 0 t6 = 4 * B t2 = a[i] t4 = b[i] t5 = t2 * t4 x = x + t5 i = i + 4 if i<t6 goto L B2 EXIT 6. Optioal [extra credit, 0 poits]. Cosider agai the cotext-free grammar G from questio 2: ab ba a) How may parse trees are there for the setece ababab? There are 5 parse trees. b) Write a recurrece relatio for the umber of parse trees for the setece (ab). Let T() be the umber of parse trees for (ab). By symmetry T() is also the umber of parse trees for (ba). For the base cases, we have T(0) = T() = ice we ca write (ab) as a(ba) i b(ab) --i, we have the recurrece 7

) ( ) ( ) ( 0 i T i T T i Σ = = c) What is the solutio to your recurrece? The solutio is the Catala umbers, + = T 2 ) (. 8