CSCI Compiler Design

Similar documents
CSCI Compiler Design

CSCI Compiler Design

CSCI565 Compiler Design

CSCI Compiler Design

CSCI565 Compiler Design

Midterm I - Solution CS164, Spring 2014

Module 26 Backpatching and Procedures

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

Lecture 4: Syntax Specification

Intermediate Code Generation Part II

CSCI Compiler Design

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

Midterm I (Solutions) CS164, Spring 2002

Midterm II CS164, Spring 2006

CSE 401 Midterm Exam 11/5/10

Intermediate Code Generation

Intermediate Code Generation

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Question Bank. 10CS63:Compiler Design

Problem Score Max Score 1 Syntax directed translation & type

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

Compiler Construction

2. You are required to enter a password of up to 100 characters. The characters must be lower ASCII, printing characters.

Syntax Directed Translation

Question Points Score

Conflicts in LR Parsing and More LR Parsing Types

Syntactic Directed Translation

A Simple Syntax-Directed Translator

Decision Making in C

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

Syntactic Directed Translation

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

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

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

CSE P 501 Exam 8/5/04

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

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

CS154 Midterm Examination. May 4, 2010, 2:15-3:30PM

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

SYNTAX DIRECTED TRANSLATION FOR CONTROL STRUCTURES

Compiler Construction

Compiler Construction Assignment 3 Spring 2018

11. a b c d e. 12. a b c d e. 13. a b c d e. 14. a b c d e. 15. a b c d e

Name Student ID Department/Year. Midterm Examination. Introduction to Computer Networks Class#: 901 E31110 Fall 2006

Briefly describe the purpose of the lexical and syntax analysis phases in a compiler.

Homework 1 Answers. CS 322 Compiler Construction Winter Quarter 2006

CSE 582 Autumn 2002 Exam 11/26/02

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

COP4020 Spring 2011 Midterm Exam

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

CSE 582 Autumn 2002 Exam Sample Solution

CS143 Midterm Sample Solution Fall 2010

Midterm Exam II CIS 341: Foundations of Computer Science II Spring 2006, day section Prof. Marvin K. Nakayama

DO NOT OPEN UNTIL INSTRUCTED

Comp 311: Sample Midterm Examination

Unit-II Programming and Problem Solving (BE1/4 CSE-2)

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

CMSC330 Fall 2016 Midterm #2 2:00pm/3:30pm

Programming Lecture 3

CS 536 Midterm Exam Spring 2013

ECE251 Midterm practice questions, Fall 2010

CSCE 531, Spring 2015 Final Exam Answer Key

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

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

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

MIT Specifying Languages with Regular Expressions and Context-Free Grammars. Martin Rinard Massachusetts Institute of Technology

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

Language Reference Manual simplicity

CS164: Midterm I. Fall 2003

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.

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Compiler Design

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

CSci 1113 Midterm 1. Name: Student ID:

Non-deterministic Finite Automata (NFA)

CS606- compiler instruction Solved MCQS From Midterm Papers

2.2 Syntax Definition

Context-sensitive Analysis Part II

Theory Bridge Exam Example Questions Version of June 6, 2008

CS 415 Midterm Exam Spring 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)

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

CSE P 501 Exam Sample Solution 12/1/11

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

CS 3360 Design and Implementation of Programming Languages. Exam 1

CSE Qualifying Exam, Spring February 2, 2008

Intermediate Code Generation

Principle of Compilers Lecture IV Part 4: Syntactic Analysis. Alessandro Artale

CS 164 Handout 16. Final Examination. There are nine questions on the exam, some in multiple parts. You have 3 hours to work on the

COP5621 Exam 4 - Spring 2005

Semantics via Syntax. f (4) = if define f (x) =2 x + 55.

Midterm Exam. CSCI 3136: Principles of Programming Languages. February 20, Group 2

A simple syntax-directed

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

MIDTERM EXAMINATION - CS130 - Spring 2003

Part 5 Program Analysis Principles and Techniques

COP5621 Exam 3 - Spring 2005

Sri Vidya College of Engineering & Technology

Syntax-Directed Translation Part II

Curriculum Map Grade(s): Subject: AP Computer Science

CMSC430 Spring 2014 Midterm 2 Solutions

Transcription:

University of Southern California CSCI565 Compiler Design Midterm Exam - Spring 26 CSCI 565 - Compiler Design Spring 26 Midterm Exam March, 26 at : PM in class (KAP 47) Duration: 2h 3 min. Please label all pages you turn in with your name and student number. Name: Number: Grade: Problem [3 points]: Problem 2 [2 points]: Problem 3 [2 points]: Problem 4 [3 points]: Total: INSTRUCTIONS:. This is a open-book exam and you may bring notes either typed or handwritten for your own personal use during the exam. 2. Please identify all the pages where you have answers that you wish to be graded. Also make sure to label each of the additional pages with the problem you are answering. 3. Use black or blue ink. No pencil answers allowed. 4. Staple or bind additional answer sheets together with this document to avoid being misplaced or worse, lost. Make sure this cover page is stapled at the front. 5. Please avoid laconic answers so that we can understand you understood the concepts being asked.

University of Southern California CSCI565 Compiler Design Midterm Exam - Spring 26 Problem : Context-Free-Grammars and Parsing Algorithms [3 points] Consider the context-free grammar (CFG) below for regular expression over the binary digit alphabet = {, with the concatenation operator., alternation operator and Kleene closure operator *. The start symbol is RegExp and the set of terminal and non-terminal symbols are respectively NT = { RegExp and T = { (, ),.,, *,, and the five productions are as shown below. () RegExp ( RegExp ) (2) RegExp RegExp * (3) RegExp RegExp. RegExp (4) RegExp RegExp RegExp (5) RegExp (6) RegExp Questions: a) [5 points] Is this grammar suitable to be parsed using a predictive () parsing algorithm? Why? b) [5 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] Typically the R() table construction algorithm leads to tables that are very 'dense'. Construct the SR parsing table by indicating the FOOW sets used to trim the entries on the table. Will this solve the conflicts? If not, comment on the nature of the conflict sand how you think you could avoid 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 RegExp $ and compute the set of R() items as depicted below for a total of 2 states. s S' RegExp $ RegExp RegExp '*' RegExp RegExp RegExp RegExp RegExp ' ' RegExp RegExp '(' RegExp ')' RegExp '' RegExp '' RegExp '(' '' '' s RegExp RegExp ' ' RegExp RegExp RegExp '*' RegExp RegExp RegExp RegExp RegExp ' ' RegExp RegExp '(' RegExp ')' RegExp '' RegExp '' RegExp '(' '' '' s RegExp '(' RegExp ')' RegExp RegExp '*' RegExp RegExp RegExp RegExp RegExp ' ' RegExp RegExp '(' RegExp ')' RegExp '' RegExp '' RegExp '(' '' '' s9 s S' RegExp $ RegExp RegExp '*' RegExp RegExp RegExp RegExp RegExp ' ' RegExp RegExp '' '*' ' ' RegExp RegExp RegExp RegExp RegExp '*' RegExp RegExp RegExp RegExp RegExp ' ' RegExp RegExp '(' RegExp ')' RegExp '' RegExp '' RegExp '(' '' '' s7 s9 RegExp '(' RegExp ')' RegExp RegExp '*' RegExp RegExp RegExp RegExp RegExp ' ' RegExp s RegExp '(' RegExp ')' ')' '*' ' ' s RegExp '' RegExp RegExp '*' s7 RegExp RegExp RegExp RegExp RegExp '*' RegExp RegExp RegExp RegExp RegExp ' ' RegExp '*' ' ' s RegExp RegExp ' ' RegExp RegExp RegExp '*' RegExp RegExp RegExp RegExp RegExp ' ' RegExp '*' ' ' ased on this DFA of states as sets of R() items, we can construct the R() parsing table below.

University of Southern California CSCI565 Compiler Design Midterm Exam - Spring 26 STATE s s s7 s9 s s ACTION '(' ')' ' ' '*' '' '' $ shift shift shift shift reduce r5 reduce r5 reduce r5 reduce r5 reduce r5 reduce r5 reduce r6 reduce r6 reduce r6 reduce r6 reduce r6 reduce r6 reduce r2 reduce r2 reduce r2 reduce r2 reduce r2 reduce r2 shift reduce r5 reduce r6 reduce r2 Accept reduce r5 reduce r6 reduce r2 shift shift shift shift reduce r3 reduce r3 shift reduce r3 shift reduce r3 shift reduce r3 reduce r3 reduce r3 reduce r3 shift shift shift s shift shift shift reduce r reduce r reduce r reduce r reduce r reduce r reduce r reduce r shift shift shift GOTO goto s goto s goto s7 goto s9 c) In this case one can attempt to resolve the conflicts in the R() parsing table using the FOOW set of RegExp. Unfortunately, FOOW(RegExp) = { *,.,, ) and as such there will still be a reduce operation for these columns. The problem is that this grammar is inherently ambiguous. A way to solve the ambiguity that arises by having consecutive. and operators is to include an additional precedence-enforcement non-terminal symbol and the corresponding productions (as we ve seen in class for the arithmetic grammar). A factor and a term non-terminals would give higher precedence to the concatenation. operator over alternation operator. STATE s s s7 s9 s s ACTION '(' ')' ' ' '*' '' '' $ shift shift shift reduce r5 reduce r5 reduce r5 reduce r5 reduce r6 reduce r6 reduce r6 reduce r6 reduce r2 reduce r2 reduce r2 reduce r2 reduce r3 shift shift shift reduce r3 reduce r3 reduce r3 shift s shift shift shift reduce r reduce r reduce r reduce r shift shift shift shift shift shift shift shift shift shift shift Accept GOTO goto s goto s goto s7 goto s9

University of Southern California CSCI565 Compiler Design Midterm Exam - Spring 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 setoff 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 two attributive grammars based on this CFG G to compute the decimal-number value of the attribute val of S. For example, the translation of the string. would result in a value of S.val of 5.25. Note that you are not supposed to modify the grammar. Questions: a) [ points] Design an -attributed syntax-directed definition for this grammar. As a suggestion, use an inherited attribute.side that reflects which side of the decimal point a bit is on and use that information to adjust the subsequent rules. b) [ points] Design an S-attributed syntax-directed definition for this grammar. Solution: a) In this solution we have 2 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 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) S.side = R;.depth = ; S.val =.val.val =.val * 2 (.depth).length = ; if (.side == ) then.depth =.depth + ;.val =.val +.val * 2 (.depth)

University of Southern California CSCI565 Compiler Design Midterm Exam - Spring 26 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 parsetree for the input string.. S val = 5.25! side =! val = 5! depth =! side = R! val =.25! depth =! side =! val = 4! depth =! side = R! depth = -! val =! side =! val = 4! length =! depth = 2! val =! side = R! length =! depth = -2! b) 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 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) S.val =.val.val =.val.length = ;.length =.length + ;.length = 2 *.val +.val;

University of Southern California CSCI565 Compiler Design Midterm Exam - Spring 26.val =.val = S val = 5.25! val = 5! val =! val = 2! length = 2 val =! val =! length =! length = 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 = ;

University of Southern California CSCI565 Compiler Design Midterm Exam - Spring 26.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. S val = 5.25! S val = 5.5! val = 5! plnz = 3! val =! plnz = 3 val = 5! plnz = 3! val =! plnz = val =! length =! plnz = val =! val = 2! plnz = val =! length =! plnz = plnz = val =! val =! length =! plnz = val =! val = 2! plnz = val =! length =! plnz = plnz = val = input: "." input: "."

University of Southern California CSCI565 Compiler Design Midterm Exam - Spring 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 φ- functions. Also discuss what sort of optimizations could be exploited in this specific case. b = ; d = ; a = ; i =...; loop: if(i > ){ if(a < ){ b = i; else { b = ; i = i - ; if(i < ) { goto break; if (b == ){ goto break else { a = a + d; goto loop; break: x = a; y = b; Solution: b = ;! d = ;! a = ;! i = ;! oop: i = ɸ(i, i )! 2 if ( i 2 > ) False True a = ɸ(a, a 2 ) if (a < ) False b = True b = i 2 2 b = ɸ(b, b )! 3 2 i = i 2 +! if (i > ) True False if (b == ) 3 True False a = a + d 2 reak: a = ɸ(a, a ) 3! b = ɸ(b, b 3 ) 4 x = a! 3 y = b 4 + d

University of Southern California CSCI565 Compiler Design Midterm Exam - Spring 26 Problem 4: ack-patching of oop Constructs [3 points] In class we have covered SDT schemes to generated code using the back-patching technique for a while and other loop constructs. In this exercise you are going to focus on the control-flow constructs such as the continue, break and return statement in imperative language such as C. In the case of the continue statement, the control is returned to the following iteration of the current looping construct. The break statement returns control to the first statement following the looping construct (i.e., ends the current loop construct and possibly returns to the immediately enclosing looping construct in the case of a nesting scenario). astly, the return statement returns control to the epilogue of the enclosing procedure, in effect terminating the execution of the currently executing procedure. It does bypasses all nesting looping constructs. In this exercises you are asked to develop a syntax-directed definition scheme using the grammar productions listed below where for simplicity we have omitted the productions for the Exp non-terminal symbols in this grammar and have omitted some details about assignment statements. Use exclusively synthesized attributed and back-patching to generate code that correctly handles break, continue and return statements in terms of control flow. Do not focus on the structured control-flow of conditional if statement as this was covered in class. () Stat while Exp { ist (2) Stat if Exp { ist else { ist (3) Stat return Exp (4) Stat continue (5) Stat break (6) Stat id = Exp (7) ist Stat ; ist (8) ist Stat ; Do not forget to show the augmented production with the marker non-terminal symbols, M and possibly N along with the corresponding rules for the additional symbols and productions. Argue for the correctness of your solution for the sample code below. Note that you are not being asked to draw the entire parse tree for this example, but rather to explain how the attributes of the return statement are carried over the parse tree and use for the purpose of backpatching. Solution: while exp { a = exp; if exp { continue; else { break; if exp { return exp; b = exp; This solution is very similar to the one described in class. The really only difference is the use of another synthesized attribute, the returnlist to be back-patched only at the top-most production in the procedure. Only at this stage, and knowing with the exit landing pad or the epilogue for the procedure is, can the compiler know where to jump to in case of a return statement. As such, this returnlist attribute is propagated up the parse tree possibly being augmented with other return statements.

University of Southern California CSCI565 Compiler Design Midterm Exam - Spring 26 () Stat while M Exp { M 2 ist { backpatch(ist.nextlist, M.quad); backpatch(exp.truelist. M 2.quad); Stat.nextlist = merge(exp.falselist, ist.breaklist); Stat.breaklist = nil; Stat.returnlist = ist.returnlist; (2) Stat if Exp { M ist N else { M 2 ist 2 { backpatch(exp.truelist. M.quad); backpatch(exp.truelist. M 2.quad); Stat.nextlist = merge(ist.nextlist, ist 2.nextlist,N.nextlist); Stat.breaklist = merge(ist.breaklist, ist 2.breaklist); Stat.returnlist = merge(ist.returnlist, ist 2.returnlist); (3) Stat return Exp { (4) Stat continue; { (5) Stat break; { (6) Stat id = Exp { (7) ist Stat ; M 2 ist 2 { (8) ist Stat { Stat.nextlist = nil; Stat.breaklist = nil; Stat.returnlist = newist(nextaddr()); emit( goto ); S.nextlist = newist(nextaddr()); emit( goto ); S.breaklist = nil; S.returnlist = nil; S.nextlist = nil; S.breaklist = newist(nextaddr()); emit( goto ); S.returnlist = nil; Stat.nextlist = null; Stat.breaklist = null; Stat.returnlist = null; backpatch(stat.nextlist, M 2.quad); ist.nextlist = ist 2.nextlist; ist.breaklist = merge(stat.breaklist,ist 2.breaklist); ist.returnlist = merge(stat.returnlist,ist 2.returnlist); ist.nextlist = Stat.nextlist; ist.breaklist = Stat.breaklist; ist.returnlist = Stat.returnlist (9) M ε { M.quad = nextaddr; () M 2 M 2 ε { M 2.quad = nextaddr;

University of Southern California CSCI565 Compiler Design Midterm Exam - Spring 26 () N ε { N.nextlist := makelist(nextaddr(); emit( goto _ ); The returnlist attribute is always propagated up the parse tree being merged along the way with other return statements locations of to-be-filled goto instructions. As such, and when reaching the top node of a procedures code, the corresponding rule can then backpatch all these locations with the appropriate value. This works well as there is a single exit point the epilogue of a code (in principle) where all the return instructions should jump to before the epilogue code executed all the instructions regarding cleaning up the called procedure execution environment.