CS4215 Programming Language Implementation

Similar documents
CS4215 Programming Language Implementation. Martin Henz

Midterm II CS164, Spring 2006

1 Lexical Considerations

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

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

The role of semantic analysis in a compiler

Lexical Considerations

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

CS4215 Programming Language Implementation. Martin Henz

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

The basic operations defined on a symbol table include: free to remove all entries and free the storage of a symbol table

M1-R4: Programing and Problem Solving using C (JAN 2019)

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

Module 27 Switch-case statements and Run-time storage management

CS143 Final Fall 2009

Lexical Considerations

Functions and Recursion

COMPILER DESIGN. For COMPUTER SCIENCE

COS 320. Compiling Techniques

CS 6353 Compiler Construction Project Assignments

Operational Semantics of Cool

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

Program Abstractions, Language Paradigms. CS152. Chris Pollett. Aug. 27, 2008.

COS 126 General Computer Science Spring Written Exam 1

MIDTERM EXAMINATION - CS130 - Spring 2005

Operational Semantics. One-Slide Summary. Lecture Outline

Lecture #19: Code Generation

Intermediate Languages and Machine Languages. Lecture #31: Code Generation. Stack Machine with Accumulator. Stack Machines as Virtual Machines

Lecture #31: Code Generation

Sardar Vallabhbhai Patel Institute of Technology (SVIT), Vasad M.C.A. Department COSMOS LECTURE SERIES ( ) (ODD) Code Optimization

Semantic Analysis and Type Checking

Today's Topics. CISC 458 Winter J.R. Cordy

Examination Questions Midterm 1

Programming Languages Third Edition. Chapter 7 Basic Semantics

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

Short Notes of CS201

CS 406/534 Compiler Construction Putting It All Together

CSE 307: Principles of Programming Languages

8. Control statements

CS201 - Introduction to Programming Glossary By

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Types and Type Inference

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

The Substitution Model

Question No: 1 ( Marks: 1 ) - Please choose one One difference LISP and PROLOG is. AI Puzzle Game All f the given

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division

CS 6353 Compiler Construction Project Assignments

Parsing Scheme (+ (* 2 3) 1) * 1

CS143 Final Spring 2016

CS302: Self Check Quiz 2

Outline. Introduction Concepts and terminology The case for static typing. Implementing a static type system Basic typing relations Adding context

Type Checking and Type Equality

CS415 Compilers. Procedure Abstractions. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Types and Type Inference

Final Examination May 5, 2005

CS 164, Midterm #2, Spring O, M, C - e1 : Bool O, M, C - e2 : t2 O, M, C - e2 : T3 O, M, C - if e1 then e2 else e3 fi : T2 _ T3

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

Signature: 1. (10 points) Basic Microcontroller Concepts

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

8. Functions (II) Control Structures: Arguments passed by value and by reference int x=5, y=3, z; z = addition ( x, y );

LAB 7 Writing Assembly Code

Compilers CS S-05 Semantic Analysis

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

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

CS164: Programming Assignment 5 Decaf Semantic Analysis and Code Generation

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

UVa ID: NAME (print): CS 4501 LDI Midterm 1

COP4020 Fall 2006 Final Exam

CS 314 Exam 2 Spring

Writing an Interpreter Thoughts on Assignment 6

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

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Review for Test 1 (Chapter 1-5)

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

Types, Type Inference and Unification

Concepts Introduced in Chapter 6

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Lecture 13. Notation. The rules. Evaluation Rules So Far

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

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

Functions and Recursion. Dr. Philip Cannata 1

CS558 Programming Languages

CS-XXX: Graduate Programming Languages. Lecture 9 Simply Typed Lambda Calculus. Dan Grossman 2012

Combining Analyses, Combining Optimizations - Summary

CS 307 Final Fall 2009

Semantic Processing. Semantic Errors. Semantics - Part 1. Semantics - Part 1

Introduction to C Language (M3-R )

UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE. M.Sc. in Computational Science & Engineering

Introduction to C. Why C? Difference between Python and C C compiler stages Basic syntax in C

CSCE 206: Structured Programming in C++

Run-time Environments - 2

Lecture 3: C Programm

CS 2630 Computer Organization. Meeting 10/11: data structures in MIPS Brandon Myers University of Iowa

Reference Grammar Meta-notation: hfooi means foo is a nonterminal. foo (in bold font) means that foo is a terminal i.e., a token or a part of a token.

Informatica 3 Syntax and Semantics

The current topic: Python. Announcements. Python. Python

Operational Semantics of Cool

CS 415 Midterm Exam Fall 2003

Transcription:

CS4215 Programming Language Implementation You have 45 minutes to complete the exam. Use a B2 pencil to fill up the provided MCQ form. Leave Section A blank. Fill up Sections B and C. After finishing, place the MCQ sheet on top of the question sheet and leave both on the table, when you exit the room. 1

Question 1: Which one of the following statements about the programming language processor families of assemblers, hardware emulators, compilers and decompilers is false? Assemblers are translators. Hardware emulators are translators. Compilers are translators. Decompilers are translators. Answer 1: Hardware emulators are interpreters, not transators. All other mentioned language processor families contain translators. Question 2: Consider two alternative implementations of a language X using a processor Y. Compile the programs from X to the machine code of Y. Interpret the programs written in X using an interpreter written in the machine code of Y. Which statement characterizes the relationship between the interpretation-based implementation and the compiler-based implementation of X. Interpretation is always faster than running compiled code. Interpretation is always slower than running compiled code. Interpretation avoids the compilation step. Compilation avoids the execution of any machine code. The compilation cannot perform type checking of programs written in X. Answer 2: It lies in the nature of an interpreter to not compile the program in a separate step. There may be internal compilation happening, such as in just-in-time compilation, but that would be interleaved with interpretation, and not in a compilation step. 2

Question 3: Assume a given programming language L with a typing relation : and one-step evaluation. Furthermore, we assume a given set V L called values. Assume that L is type-safe with respect to :, and the notion of values given by V. Which of the following statements is false? There may be a well-typed expression E L such that there is no value v V with E v. There may be a non well-typed expression E L such that there is a value v V with E v. There may be a well-typed expression E L such that E V and there is no expression E with E E. There may be a well-typed expression E L such that E V and there is a value v with E v. None of the above. Answer 3: This statement contradicts the progress property of type-safety. All other statements are correct. Question 4: What is the result of evaluating the following simpl expression, according to the dynamic semantics (based on substitution)? if 1 < 2 then (fun {int -> int} x -> x + 1 end (1 + (5-3) / 0)) else 4 end (fun {(int -> int)} x -> (x + 1) end (1 + (2 / 0))) 4 (1 + (2 / 0)) + 1 (fun {(int -> int)} x -> (x + 1) end (1 + ((5-3) / 0))) if 1 < 2 then (fun {int -> int} x -> x + 1 end (1 + ((5-3) / 0))) else 4 end Answer 4: The division by 0 gets stuck; neither the addition nor the application can proceed. 3

Question 5: What is the result of evaluating the following simpl expression, according to the dynamic semantics (based on substitution)? ((fun {int -> int -> int} x -> fun {int -> int} y -> x + y end end true) 4) true 4 (fun {int -> int} y -> true + y end 4) ((fun {int -> int -> int} x -> fun {int -> int} y -> x + y end end true) 4) (true + 4) Answer 5: The applications proceed, according to the dynamic semantics. At the end, addition gets stuck because the first argument is a non-integer value. 4

Question 6: What is the result of evaluating the following simpl expression according to the denotational semantics of simpl? (recfun f {int -> int} x -> (fun {int -> int} y -> if x=y then (f x - 1) else y end end 2) end 0) 2 1 0 infinite loop None of the above. Answer 6: Question 7: What is the result of type-checking the following simpl program, using the type checker that was implemented in Lab Task 4? if true then 3 else 4 / 0 end int -> int Type error int Runtime error 3 Answer 7: 5

Question 8: What is the result of type-checking the following simpl program, using the type checker that was implemented in Lab Task 4? if true then 3 else 4 / (3-3) end 3 Type error Runtime error int -> int int Answer 8: Question 9: The type checker that we implemented in the lab tasks flags out the first type error it finds, using Java s exception handling mechanism. More realistic type checkers try to report as many type errors as possible to the user. What is the best way to modify the existing type checker to achieve this goal? Change the class TypeError such that it prints out all error messages encountered during type checking. Change the function check such that it returns data structure that contains a type along with a list of type errors. Change the main function in the class simpltypechecker.typecheck, and run the function check again, whenever an exception occurs during type checking. This is not possible using the type checking approach chosen for the language simpl. Change the class TypeEnvironment to print out an error message whenever a variable is not found. F Change the class Type to print out the incorrect types and continue the type checking without exception. Answer 9: 6

This is the only way to capture multiple type errors in the current structure of the type checker. Question 10: Consider a new instruction MYST (for mystery ), which comes (like GOTO ) with an ADDRESS, referring to the index of an instruction in the instruction array. We decide to implement the instruction as follows: case OPCODES.MYST: { int x = (((IntValue) os.pop()).value); int y = (((IntValue) os.pop()).value); if (x > y) { pc = pc + 1; } else { pc = ((MYST)i).ADDRESS; } break; } What is the result of executing this instruction with an operand stack after we first push a value v on the operand stack, and then a value w? ADDRESS if v is greater than w, and execute the instruction after MYST, otherwise. The instruction removes both v and w from the operand stack. ADDRESS if w is greater than v, and execute the instruction after MYST, otherwise. The instruction removes both v and w from the operand stack. ADDRESS if w is less than or equal to v, and execute the instruction after MYST, otherwise. The instruction removes both v and w from the operand stack. ADDRESS if v is greater than w, and execute the instruction after MYST, otherwise. The operand stack remains unchanged. ADDRESS if w is greater than v, and execute the instruction after MYST, otherwise. The operand stack remains unchanged. Answer 10: 7

Question 11: What will be the result of modifying our recursive interpreter, based on the denotational semantics of simpl, by replacing the function extend in the class Environment with the following function: public Environment extend(string v, Value d) { if (containskey(v)) { new ErrorValue(); } else { Environment e = (Environment)clone(); e.put(v,d); return e; } Variables cannot be redefined in nested function definitions and let-expressions. Variable occurrences must be distinct; you cannot write (f x x) any longer. All variable declarations have to use different variable names. There is no effect of this change observable at runtime. None of these. Answer 11: In simpl, the function extend will extend the environment that represents the variable bindings of global variables of functions by a binding of parameters to values. So if we prevent variables from being defined if they are already present in an environment, we are preventing redefinition of variables. 8

Question 12: Let us say the operand stack os of the virtual machine for the language simpl has the form 10, 20, 30 with 10 on top of the stack, and the environment e is of the form e = [x 3][y 4] What is the value on top of the operand stack after execution of the following instruction sequence? 1 230 1 70 1 320 1 260 [LDS x, PLUS, TIMES] Answer 12: 1 LDS pushes the value 3, PLUS pops 3 and 10 and pushes 13, and TIMES pops 13 and 20 and pushes 260. 9