Problem Score Max Score 1 Syntax directed translation & type

Similar documents
CMSC430 Spring 2014 Midterm 2 Solutions

CMSC430 Spring 2009 Midterm 2 (Solutions)

CMSC330 Spring 2014 Midterm #2. Score 1 OCaml types & type

CMSC 330, Fall 2018 Midterm 2

CMSC330 Spring 2017 Midterm 2

CMSC330 Spring 2016 Midterm #2 9:30am/12:30pm/3:30pm

CMSC330 Fall 2009 Final Exam

COP5621 Exam 4 - Spring 2005

CMSC330 Spring 2008 Final Exam

CMSC 330, Fall 2018 Midterm 2

CMSC411 Fall 2013 Midterm 1

CMSC330 Spring 2015 Final Exam 9:30am/11:00am/12:30pm

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

CMSC330 Spring 2017 Midterm #1 Solution

CMSC330 Spring 2018 Midterm 2 9:30am/ 11:00am/ 3:30pm

LECTURE 3. Compiler Phases

First Midterm Exam CS164, Fall 2007 Oct 2, 2007

This test is not formatted for your answers. Submit your answers via to:

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

Final Exam. COMP Summer I June 26, points

CS 536 Midterm Exam Spring 2013

Compiler Structure. Data Flow Analysis. Control-Flow Graph. Available Expressions. Data Flow Facts

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

CMSC330 Spring 2017 Midterm #1 9:30am/11:00am/12:30pm/5:00pm

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

Intermediate Code Generation

Compilers. Compiler Construction Tutorial The Front-end

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

Question Points Score

CSE 582 Autumn 2002 Exam 11/26/02

COP5621 Exam 3 - Spring 2005

MIDTERM EXAMINATION - CS130 - Spring 2003

Midterm 2. CMSC 430 Introduction to Compilers Fall Instructions Total 100. Name: November 21, 2016

Syntax-Directed Translation. Lecture 14

MIDTERM EXAM (Solutions)

CS164: Midterm I. Fall 2003

CMSC330 Fall 2014 Final Exam

Midterm II CS164, Spring 2006

CMSC 330 Exam #2 Fall 2008

1 Lexical Considerations

CSCE 531 Spring 2009 Final Exam

CMSC330 Spring 2016 Midterm #1 9:30am/12:30pm/3:30pm

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

COS 126 General Computer Science Spring Written Exam 1

Type Inference Systems. Type Judgments. Deriving a Type Judgment. Deriving a Judgment. Hypothetical Type Judgments CS412/CS413

A Simple Syntax-Directed Translator

Principles of Compiler Design

CMSC330 Spring 2015 Midterm #1 9:30am/11:00am/12:30pm

Midterm 2. CMSC 430 Introduction to Compilers Fall 2018

Syntax and Grammars 1 / 21

Project 2 Interpreter for Snail. 2 The Snail Programming Language

CSE302: Compiler Design

CMSC330 Fall 2015 Midterm #1 12:30pm/2:00pm/5:00pm

CSE341 Autumn 2017, Midterm Examination October 30, 2017

CS 415 Midterm Exam Spring SOLUTION

University of Maryland College Park Dept of Computer Science CMSC106 Fall 2016 Midterm I

CMSC330 Spring 2015 Midterm #1 9:30am/11:00am/12:30pm

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

CMSC330 Fall 2014 Midterm 2 Solutions

CSE P 501 Exam 8/5/04

CSE 401 Midterm Exam Sample Solution 2/11/15

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Computing Science 114 Solutions to Midterm Examination Tuesday October 19, In Questions 1 20, Circle EXACTLY ONE choice as the best answer

CMSC330 Spring 2014 Midterm 2 Solutions

CPSC 211, Sections : Data Structures and Implementations, Honors Final Exam May 4, 2001

CMSC330 Fall 2017 Final Exam

CIS 341 Midterm February 28, Name (printed): Pennkey (login id): SOLUTIONS

The role of semantic analysis in a compiler

CSCE 314 Programming Languages. Type System

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

CSE341 Spring 2016, Midterm Examination April 29, 2016

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

CSE 401 Final Exam. December 16, 2010

CSE341 Spring 2016, Midterm Examination April 29, 2016

COS 126 Midterm 1 Written Exam Fall 2011

CSE 5317 Midterm Examination 4 March Solutions

CMSC330 Spring 2016 Midterm #1 9:30am/12:30pm/3:30pm Solution

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

CIS 110 Introduction to Computer Programming. February 29, 2012 Midterm

CS 139 Practice Midterm Questions #2

Semantic Analysis. Lecture 9. February 7, 2018

CS112 Spring 2012 Dr. Kinga Dobolyi. Exam 2. Do not open this exam until you are told. Read these instructions:

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

CSE P 501 Exam 12/1/11

LECTURE 18. Control Flow

CS5363 Final Review. cs5363 1

CSE413 Midterm. Question Max Points Total 100

CS 415 Midterm Exam Fall 2003

Midterm Exam CS 251, Intermediate Programming March 6, 2015

University of Maryland College Park Dept of Computer Science CMSC106 Fall 2013 Midterm I Key

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

Comp 311: Sample Midterm Examination

University of Maryland College Park Dept of Computer Science

CSE 401 Final Exam. March 14, 2017 Happy π Day! (3/14) This exam is closed book, closed notes, closed electronics, closed neighbors, open mind,...

CS 415 Midterm Exam Spring 2002

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

A simple syntax-directed

University of Virginia Department of Computer Science. CS 4501: Information Retrieval Fall 2015

Do not start the test until instructed to do so!

CMSC 330, Fall 2018 Midterm 1

Transcription:

CMSC430 Spring 2014 Midterm 2 Name Instructions You have 75 minutes for to take this exam. This exam has a total of 100 points. An average of 45 seconds per point. This is a closed book exam. No notes or other aids are allowed. If you have a question, please raise your hand and wait for the instructor. Answer essay questions concisely using 1-2 sentences. Longer answers are not necessary and a penalty may be applied. In order to be eligible for partial credit, show all of your work and clearly indicate your answers. Write neatly. Credit cannot be given for illegible answers. Problem Score Max Score 1 Syntax directed translation & type 12 checking 2 Symbol tables 4 3 Intermediate representations 12 4 Code generation 26 5 Control flow analysis 10 6 CFG ordering, dominators, loops 12 7 Dataflow analysis 24 Total 100 HONOR PLEDGE: I pledge on my honor that I have not given or received any unauthorized assistance on this assignment/ examination. SIGNATURE:

1. (12 pts) Syntax directed translation & type checking Consider the following grammar fragment for an expression for C--: exp CONST IDENT 1 IDENT 2 [ exp 1 ] Assume that the nonterminal exp and terminals IDENT and CONST have an attribute type that can be assigned a range of type values, including typeboolean, typeint, typeerror, etc. In addition, the terminal IDENT has an attribute isarray that can be true or false. Add syntax-directed type checking rules to the production exp IDENT 2 [ exp 1 ] to enforce the following: a. (2 pts) IDENT 2 must be an array. Otherwise call parser.msg( Misuse of array ). b. (2 pts) The subscript expression exp 1 must be typeint. Otherwise call parser.msg( Integer required ). c. (2 pts) The expression type is assigned the type of IDENT 2 if both (a) and (b) are true, else it is assigned typeerror. Answer: exp IDENT 2 [ exp 1 ] {: :}

d. (6 pts) Given the parse tree for x[y[2]], where x is a BOOL array, y is an T array, and 2 is an T, show how syntax directed translation can calculate the type of the expression using your type checking rules by annotating the parse tree. Show the value calculated for each attribute, and draw arrows linking the attributes used in the calculation. 2. (4 pts) Symbol tables Show the state of lexically nested symbol tables when the compiler reaches the point marked HERE in the following code fragment: int a,b; void foo( ) { int b; { int c;} } void bar( ) { int a, { int d,e;} /* HERE */ }

3. (12 pts) Intermediate Representations Translate the following arithmetic expression: x = b ( a * a ) (2 pts) abstract syntax tree (AST) (2 pts) directed acyclic graph (DAG) (4 pts) 3-address code (4 pts) Java stack code

if stack is X Y ( Y is at top of stack ) then if_icmpeq L pop X, Y from stack, jump to L if X = Y if_icmpgt L pop X, Y from stack, jump to L if X > Y if_icmplt L pop X, Y from stack, jump to L if X < Y 4. (26 pts) Code generation. Your compiler generates Java byte codes using attributes (code) and helper functions (geninst, append) found in the table on previous page. Add support in your compiler for a Ruby style STEP loop, where x.step(y,z) { } will iterate with index starting at x, incrementing index by z each iteration, and repeating while index y. Note the loop will not be executed if x > y. Write syntax-directed actions needed to generate code for a Ruby style STEP loop: stmt exp 1. STEP ( exp 2, exp 3 ) { stmtlist } {: stmt.code =?? :} 3.step(2,2) { puts x } 3.step(3,2) { puts x } x 3.step(4,2) { puts x } x 3.step(5,2) { puts x } xx 3.step(6,2) { puts x } xx

5. (10 pts) Control flow analysis Consider the following code: <I1> a := b <I2> L1: b := c <I3> L2: if (...) goto L4 <I4> c := b <I5> L3: d := a <I6> L4: goto L1 <I7> L5: b := a <I8> L6: if (...) goto L2 <I9> c := a a. (8 pts) Find basic blocks and draw the control flow graph (CFG). b. (2 pts) What is the compiler term used to describe statement I7, I8, and I9?

6. (12 pts) CFG ordering, dominators, loops Consider the following control flow graph (CFG), where B1 is the start of the program. a. (2 pts) List all basic blocks dominated by B3. b. (2 pts) List all basic blocks dominated by B4. c. (4 pts) List all the loops in the control flow graph. For each loop list all basic blocks in the loop, as well any back edges in the loop. d. (4 pts) Give TWO reverse Postorder numberings of the CFG for Problem 7 on the next page.

B1 I1: a = 7. (24 pts) Dataflow analysis Calculate Live Variables for the following code: <I1> a := <I2> L1: c := a <I3> if (...) goto L4 <I4> a := b <I5> L2: b := a+c <I6> goto L1 <I7> L4: b := a <I8> if (...) goto L2 <I9> a := b a. (8 pts) Calculate GEN/KILL for each basic block Basic Block B1 B2 B3 B4 B5 b. (16 pts) Initializing /OUT to, solve live variables in the order B6, B5, B4, B3, B2, B1, showing /OUT for each pass. Stop when a fixed point is reached. GEN KILL Basic Block B6 B5 OUT Initial Pass 1 Pass 2 Pass 3 Pass 4 OUT B3 B2 I4: a = b B4 I2: c = a I3: if ( ) goto L4 B5 I5: b = a+c I6: goto L1 I7: b = a I8: if ( ) goto L2 B6 I9: a = b B6 B4 OUT B3 OUT B2 OUT B1 OUT