Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

CS:3820 Progr. Lang. Concepts Midterm II Due: Monday, May 06, by 5:59pm.

Name:

Instructions
Please answer each question in the space provided.
The midterm needs to be turned in via ICON.
Be sure to review the syllabus for details about this course’s cheating policy.

1. (7 points) Provide a short answer (1-3 sentences) to each of the following questions.

1. Describe one advantage and one disadvantage of lazy evaluation versus eager evaluation
in programming languages.

2. In programming languages, describe characterizes a function f as (i ) being first-order or


as (ii ) being higher-order?

3. Explain the input/output functionality of a program like FSLex (i.e., what it takes as
input and what it produces as output).

4. A common compilation schema for imperative programming languages, such as C, uses


offsets to determine addresses of local variables. What is the advantage of using such
relative addresses for local variables rather than absolute addresses, as is done for global
variables?
2. (3 points) Consider the typing rule for function call in the first-order flavor of micro-ML, in
which ρ denotes the typing environment:

ρ(f ) = (f, x, er , ρfdecl ) ρ ` e ⇒ vx ρfdecl [x 7→ vx , f 7→ (f, x, er , ρfdecl )] ` er ⇒ v


ρ`f e⇒v

This typing rule assumes the language has static scope.

1. Provide a simplified typing rule based on the assumption that the language has dynamic
scope.

2. Explain the motivation of your modifications.

3. (6 points) Consider the following polymorphic tree data type and combinator
type ’a btree =
| Leaf
| Node of ’a btree * ’a * ’a btree

let rec treeFold f t e =


match t with
| Leaf -> e
| Node(t1, n, t2) -> f (treeFold f t1 e) n (treeFold f t2 e)

Page 2
Using treeFold and auxiliary non-recursive functions:

a) write functional implementations (i.e. without imperative constructs) defining non-recursively


each of the following functions;
b) describe why your solutions successfully implement the desired functions.

1. f : ’a btree -> int, which takes a btree and returns the depth of the tree, in which
a Leaf tree has depth 0 and a Node(t1, n, t2) tree has depth 1 plus the maximum of
the depths of t1 and t2. Ex: Node (Leaf,3,Node (Leaf,4,Leaf)) returns 2.

2. f : int btree -> string, which takes a btree and returns a string containing its node
values in inorder, i.e. left subtree, then root, then right subtree. The values must be
separated by semicolons, with no repeated occurrences of semicolons. The string must
start and finish with node values.
Ex: Node (Node (Leaf,3,Node (Leaf,4,Leaf)),2,Leaf) returns "3;4;2"

Page 3
4. (8 points) Given the task to extend the micro-C language to implement conditional expressions
of the form e1 ? e2 : e3, describe, for each of the following, which additions would you
make, at which point, and why:

1. the abstract syntax


2. the lexer
3. the parser
4. the compiler

The compilation of e1 ? e2 : e3 should produce code that evaluates e2 only if e1 is true


and evaluates e3 only if e1 is false. The value at the top of the stack after the evaluation of the
overall expression must be the value of evaluating e2 or e3, depending on which was executed.
Your additions should refer to the existing abstract syntax, lexer, parser and compiler in the
lecture code microc-machines.zip available in the lectures page the course website (section
of dates Apr 15 to Apr 24).
You do not need to provide a working implementation, but to describe in detail what to add,
where to add, and why.

Page 4
Page 5

You might also like