CSCI Compiler Design

Similar documents
CSCI Compiler Design

CSCI Compiler Design

CSCI Compiler Design

Syntax-Directed Translation Part II

Syntactic Directed Translation

CSCI565 Compiler Design

Context-sensitive Analysis Part II Chapter 4 (up to Section 4.3)

CSCI565 Compiler Design

Syntax Directed Translation

1. Consider the following program in a PCAT-like language.

Context-sensitive Analysis Part II

1. (a) What are the closure properties of Regular sets? Explain. (b) Briefly explain the logical phases of a compiler model. [8+8]

Context-sensitive Analysis

CS143 Midterm Sample Solution Fall 2010

Syntactic Directed Translation

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Compiler Design. Fall Control-Flow Analysis. Prof. Pedro C. Diniz

Context-sensitive Analysis

COL728 Minor1 Exam Compiler Design Sem II, Answer all 5 questions Max. Marks: 20

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Spring 2013 CSE Qualifying Exam Core Subjects. March 23, 2013

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

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

Question Bank. 10CS63:Compiler Design

UNIVERSITY OF CALIFORNIA

CSCE 531, Spring 2015 Final Exam Answer Key

Problem Score Max Score 1 Syntax directed translation & type

LR Parsing - Conflicts

COP4020 Spring 2011 Midterm Exam

Conflicts in LR Parsing and More LR Parsing Types

Programming Lecture 3

CSE 582 Autumn 2002 Exam 11/26/02

Principle of Compilers Lecture VIII: Intermediate Code Generation. Alessandro Artale

COP5621 Exam 3 - Spring 2005

Downloaded from Page 1. LR Parsing

CS 164 Handout 11. Midterm Examination. There are seven questions on the exam, each worth between 10 and 20 points.

CSE 582 Autumn 2002 Exam Sample Solution

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

Context-sensitive Analysis. Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.

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

NARESHKUMAR.R, AP\CSE, MAHALAKSHMI ENGINEERING COLLEGE, TRICHY Page 1

Syntax-Directed Translation

Computing Inside The Parser Syntax-Directed Translation. Comp 412

CS 134 Midterm Fall 2006

MASSACHUSETTS INSTITUTE OF TECHNOLOGY Fall Test I

Class Information ANNOUCEMENTS

Intermediate Code Generation

Compiler Construction

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Subject Name: CS2352 Principles of Compiler Design Year/Sem : III/VI

Types, Expressions, and States

CS606- compiler instruction Solved MCQS From Midterm Papers

Decision Making in C

UNIT IV INTERMEDIATE CODE GENERATION

CSE3322 Programming Languages and Implementation

COMPILER DESIGN. For COMPUTER SCIENCE

Computing Inside The Parser Syntax-Directed Translation. Comp 412 COMP 412 FALL Chapter 4 in EaC2e. source code. IR IR target.

CS 432 Fall Mike Lam, Professor. Code Generation

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

Loop Optimizations. Outline. Loop Invariant Code Motion. Induction Variables. Loop Invariant Code Motion. Loop Invariant Code Motion

COP5621 Exam 4 - Spring 2005

Sample Examination. Family Name:... Other Names:... Signature:... Student Number:...

Intermediate Code Generation

Fall 2015 CSE Qualifying Exam Core Subjects

Partial Exam Compilation and Program Analysis (CAP) October, 24th, 2016 Duration: 2 Hours

Grammars. CS434 Lecture 15 Spring 2005 Department of Computer Science University of Alabama Joel Jones

CS 406/534 Compiler Construction Putting It All Together

Highlights. - Making threads. - Waiting for threads. - Review (classes, pointers, inheritance)

10/18/18. Outline. Semantic Analysis. Two types of semantic rules. Syntax vs. Semantics. Static Semantics. Static Semantics.

UNIT-3. (if we were doing an infix to postfix translator) Figure: conceptual view of syntax directed translation.

CSCI 565: Compiler Design and Implementation Spring 2006

Maciej Sobieraj. Lecture 1

LECTURE 17. Expressions and Assignment

11/22/1999 7pm - 9pm. Name: Login Name: Preceptor Name: Precept Number:

Syntax Directed Translation

Formal Languages and Compilers Lecture X Intermediate Code Generation

Compilers. Compiler Construction Tutorial The Front-end

CSE P 501 Exam 8/5/04 Sample Solution. 1. (10 points) Write a regular expression or regular expressions that generate the following sets of strings.

10/26/17. Attribute Evaluation Order. Attribute Grammar for CE LL(1) CFG. Attribute Grammar for Constant Expressions based on LL(1) CFG

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

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

LECTURE 11. Semantic Analysis and Yacc

Program Assignment 2 Due date: 10/20 12:30pm

Computer Science 320: Final Examination

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

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

Control Structures. Boolean Expressions. CSc 453. Compilers and Systems Software. 16 : Intermediate Code IV

Department of Computer Science Purdue University, West Lafayette

Compiler Construction

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

CMSC430 Spring 2014 Midterm 2 Solutions

Midterm 1. CMSC 430 Introduction to Compilers Spring Instructions Total 100. Name: March 14, 2012

Intermediate Code Generation

Comp 204: Computer Systems and Their Implementation. Lecture 22: Code Generation and Optimisation

Motivation was to facilitate development of systems software, especially OS development.

Chapter 3. Describing Syntax and Semantics

Two hours UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE. Date: Friday 20th May 2016 Time: 14:00-16:00

Midterm I (Solutions) CS164, Spring 2002

Compiler Construction Assignment 3 Spring 2018

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

Syntax Analysis Part IV

Transcription:

University of Southern California CSCI565 Compiler Design Midterm Exam - Fall 26 CSCI 565 - Compiler Design Fall 26 Midterm Exam Solution Problem : Context-Free-Grammars and Parsing Algorithms [4 points] Consider the CFG with non-terminal symbols N={S, E, A, with start symbol S, terminal symbols T={ id, ';', '=' and the productions P listed below. () S E $ (2) E E ';' A (3) E A ';' (4) A id (5) A A '=' A Questions: a) [ points] As specified, can this grammar be parsed using a predictive algorithm? Why or why not? b) [2 points] Compute the DFA that recognizes the R() sets of items for this grammar and construct the corresponding R() parsing table. Comment on the nature of the conflicts, if any. c) [ points] How different would the SR table look like? If there were conflicts in the original table will this table construction algorithm resolve them? Solution: a) This grammar is clearly not () as it is left-associative as shown by productions 2, 3 and 4 of the grammar. b) We star by augmenting the grammar with an initial production () S E $ and compute the set of R() items as depicted below for a total of 9 states. s S E $ E E ',' A ',' s7 E E ',' A A id A A '=' A s S E $ E E ',' A E A ';' A id A A '=' A E A id s2 E A ';' A A '=' A ';' s3 E A ';' s5 A id '=' id id s8 E E ',' A A A '=' A s4 A A '=' A A id A A '=' A '=' A '=' A s6 A A '=' A A A '=' A

University of Southern California CSCI565 Compiler Design Midterm Exam - Fall 26 ased on this DFA of states as sets of R() items, we can construct the R() parsing table below. STATE s s s2 s3 s4 s5 s6 s7 s8 ACTION id '=' ';' ',' $ shift s8 Accept shift s4 shift s3 reduce r3 reduce r3 reduce r3 reduce r3 reduce r3 reduce r4 reduce r4 reduce r4 reduce r4 reduce r4 reduce r5 shift s4 reduce r5 reduce r5 reduce r5 reduce r5 reduce r2 shift s4 reduce r2 reduce r2 reduce r2 reduce r2 GOTO A E goto s2 goto s goto s6 goto s7 c) In this case one can attempt to resolve the conflicts in the R() parsing table using the FOOW set of A ad E. Unfortunately, FOOW(A) = { $, ;, = and FOOW(E) = { $,, and as such there will still be a shift/reduce conflict for state s6 on input = as shown below. STATE s s s2 s3 s4 s5 s6 s7 s8 ACTION id '=' ';' ',' $ shift s8 Accept shift s4 shift s3 reduce r3 reduce r3 reduce r3 reduce r3 reduce r3 reduce r4 reduce r4 reduce r4 reduce r4 reduce r4 shift s4 reduce r5 reduce r5 shift s4 reduce r2 reduce r2 GOTO A E goto s2 goto s goto s6 goto s7

University of Southern California CSCI565 Compiler Design Midterm Exam - Fall 26 Problem 2: Syntax-Directed Translation [2 points] Consider the CFG G whose productions P are listed below with start symbol S, non-terminal symbols NT = {S,, and terminal symbols T = {,,.. This grammar captures the set off strings over the binary alphabet = {, with an integer and a fractional part. () S. (2) S (3) (4) (5) (6) In this problem you are asked to develop an attributive grammars based on G to compute the decimal-number value of the attribute val of S and check if the value is greater than a specific input threshold decimal value. For instance, is the input is. and the input threshold is 5, then the output to be captured as an attribute of S, should be true. Note that you are not supposed to modify the grammar. Solution: In this solution we have 3 inherited and 2 synthesized attributes, namely {side, depth and {val, length. The table below summarizes the type and associated non-terminal symbols for each attribute. Attribute Name Type Non-Terminal Symbol side enumerated: {, R val real S,, length integer depth integer threshold real S check bool S The following are the productions and the corresponding rules for the -attributed grammar. S. 2.side = R;.depth = ; 2.side = ; 2.depth = ; S.val =.val + 2.val * 2 (- 2.depth) if (S.val > S.threshold) S.check = true; else S.check = false S.side = R;.depth = ; S.val =.val.val =.val * 2 (.depth).length = ;

University of Southern California CSCI565 Compiler Design Midterm Exam - Fall 26 if (.side == ) then.depth =.depth + ;.val =.val +.val * 2 (.depth) else.depth =.depth - ;.val =.val +.val * 2 (.length).side =.side.length =.length + ;.val =.val = Using this -attributed grammar we depicted below the set of values for the various attributes for the parse-tree for the input string.. S threshold = 5.! val = 5.25! check = true! side =! val = 5! depth =! '.' side = R! val =.25! depth =! side =! val = 4! depth =! side = R! depth = -! side =! val = 4! length =! depth = 2! side = R! length =! depth = -2! A possible S-attributed grammar requires that all the attributes be synthesized. On the right side of the parse three, that is for the integer part of the binary number representation, a possible approach is to propagate the value of the bits seen so far along the tree and multiply them by 2 at each level and then adding the value for the bit at the current level. Cumulatively, at the top you will get the correct amount. The fractional part relies on the same approach but you need to propagate up the length of this fractional section and then multiply the entire cumulative value by a negative power of two. The S-attributed grammar below depicts this solution. Attribute Name Type Non-Terminal Symbol val real S,, length integer threshold real S check boolean S The following are the productions and the corresponding rules for the S-attributed grammar. S. 2 S S.val =.val + 2.val * 2 (- 2.length) if (S.val > S.threshold) S.check = false; else S.check = true S.val =.val

University of Southern California CSCI565 Compiler Design Midterm Exam - Fall 26.val =.val.length = ;.length =.length + ;.length = 2 *.val +.val;.val =.val = As you might have noticed, this solution has a flaw exemplified by the input string.. The length of the fractional part is computed as 3 and as a result the value computed using the S-attributed solution described above is 5.25 rather than the correct value of 5.5. A correct solution needs to capture the effective length of the fractional part or the position of the last non-zero bit. As such we keep two counters, the length of the string (as before) and the position-of-the-last-non-zero bit ( plnz ) that is set to be the value of length when at the corresponding position of the bit, the bit value is. The revised solution below captures this and as you can check, it produces the correct result for the input string. as effectively all trailing bits are ignored. Attribute Name Type Non-Terminal Symbol val real S,, length integer plnz integer The following are the productions and the corresponding rules for this revised S-attributed grammar: S. 2 S S.val =.val + 2.val * 2 (- 2.plnz) S.val =.val.val =.val if (.val == ) then.plnz = else.plnz = ;.length = ;.length =.length + ; if (.val == ) then.plnz =.length else.plnz =.plnz;.length = 2 *.val +.val;.val =.val = elow we depict two parse trees for two inputs, respectively. and. showing that the S- attributed grammar correctly computed the desired value for the val attribute of the goal symbol of the grammar.

University of Southern California CSCI565 Compiler Design Midterm Exam - Fall 26 S threshold = 5.! val = 5.25! check = true! S threshold = 5.! val = 5.5! check = true! val = 5! plnz = 3! '.' plnz = 3 val = 5! plnz = 3! '.' plnz = length =! plnz = val = 2! plnz = length =! plnz = plnz = length =! plnz = val = 2! plnz = length =! plnz = plnz = val = input: "." input: "."

University of Southern California CSCI565 Compiler Design Midterm Exam - Fall 26 Problem 3. SSA Representation [2 points] For the sequence of instructions shown below depict an SSA-form representation (as there could be more than one). Do not forget to include the f- functions. Also discuss what sort of optimizations could be exploited in this specific case, meaning, using the information about which definitions reach which uses and derive specific variable values. b = ; d = ; a = ; i =...; loop: if(i > ){ if(a < ){ b = i; else { b = ; i = i - ; if(i < ) { i = ; goto break; if (b == ){ goto break; else { a = a + d; goto loop; break: x = a; y = b; Solution: b = ; d = ; a = ; i =...; loop: i 3 = f(i, i 2 ) break: a 3 = f(a, a 2 ) b 3 = f(b, b 6 ) if(i 3 > ){ if(a 3 < ){ b 4 = i; else { b 5 = ; b 6 = f(b 4, b 5 ) i 4 = i - ; if(i 4 < ) { i 5 = ; goto break; if (b 6 == ){ goto break; else { a 2 = a 3 + d ; goto loop; b 7 = f(b 3, b 6 ) x = a 3 ; y = b 7 ; In this specific case, the assignment a = a + d always used the very first definition of the variable d, as the zero value. As such this assignment is in fact void, as the variable a s value remains unchanged. As a consequence, the variable a always retains the same value for all the code s execution, that is. astly, the assignment at the end of the code to the x variable thus assumes the value as well.

University of Southern California CSCI565 Compiler Design Midterm Exam - Fall 26 Problem 4: Intermediate Code Generation [2 points] In class we covered intermediate code generation for a variety of high-level programming constructs such as boolean expressions, assignments and loop constructs using three-address instructions. In this problem you are asked to use the SDT schemes presented in class and show the generated code for the snippet of code below using the short-circuit scheme for the code generation of predicates. For this code, suggest an improvement that will result in fewer instructions being executed. State the assumption you make and use symbolic labels for targets of jump as you see fit and draw a memory layout of the code. i = ; while ((a < b) and (i < )) { if (c[i] < ) { break; i = i + ; Solution: The layout of this code is fairly simple as suggested in one of the lectures about control-flow. At the top we will have an if statement along with a loop label followed by the body of the while-loop. At the bottom of the loop there is an unconditional jump to the test section of the loop s header. In terms of code generation, we assume there is a symbolic label denoting where the control flows when the loop terminates. We can name this label as S.next as an inherited symbolic attribute. The three-address intermediate code below is a direct application of the SDT schemes described in class assuming 3 = S.next. i = t = : if a < b goto 2 goto 3 2: if i < t goto 4 goto 3 4: t2 = c[i] t3 = if t2 < t3 goto 3 t4 = i + i = t4 goto 3: The key observation in making this code better in terms of the number of executed instructions is that the portion of the while loop predicate (a < b) is invariant with respect to the loop s execution. Its value does not change throughout the execution and thus needs to be evaluated only once. The code can be rewritten as shown below. i = t = if a >= b goto 3 2: if i < t goto 4 goto 3 4: t2 = c[i] t3 = if t2 < t3 goto 3 t4 = i + i = t4 goto 2 3: