Docsity

Programming Language - Final Exam | CSCI 4430, Exams of Programming Languages

Toggle sidebar
bg1
NAME__________________________
CSCI 4430 Final Exam 1 Fall 2008
Final Exam
CSCI 4430 Programming Languages
December 11, 2008
- DO NOT OPEN THIS EXAM UNTIL TOLD TO DO SO!
- READ THROUGH THE ENTIRE EXAM BEFORE STARTING TO WORK.
- YOU ARE ALLOWED 10 “CHEAT” SHEETS ONLY.
This exam is worth 380 points.
Make sure you have 15 pages counting this one; there are 10 questions. If you need
more room for an answer than is provided, please use the back of the page and indicate
that you have done so. If you re-do a question, please make clear what is your final
answer.
Read through each entire question before starting to answer it. Starred “**” questions
may take longer to answer/think through, so please budget your time appropriately. Be
clear and brief in your explanations (when the answer is text, write at most 2-3
sentences).
The following is for the use of graders
1. __________/20
2. __________/30
3. __________/30
4. __________/45
5. ___________/45
6. ___________/40
7. ___________/60
8. ___________/40
9. ___________/30
10. __________/40
TOTAL POINTS:___________/380
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Programming Language - Final Exam | CSCI 4430 and more Exams Programming Languages in PDF only on Docsity!

Final Exam

CSCI 4430 Programming Languages

December 11, 2008

- DO NOT OPEN THIS EXAM UNTIL TOLD TO DO SO!

- READ THROUGH THE ENTIRE EXAM BEFORE STARTING TO WORK.

- YOU ARE ALLOWED 10 “CHEAT” SHEETS ONLY.

This exam is worth 380 points.

Make sure you have 15 pages counting this one; there are 10 questions. If you need more room for an answer than is provided, please use the back of the page and indicate that you have done so. If you re-do a question, please make clear what is your final answer.

Read through each entire question before starting to answer it. Starred “” questions may take longer to answer/think through, so please budget your time appropriately. Be clear and brief in your explanations (when the answer is text, write at most 2- sentences).**

The following is for the use of graders

**1. __________/

  1. __________/
  2. __________/
  3. __________/
  4. ___________/
  5. ___________/
  6. ___________/
  7. ___________/
  8. ___________/
  9. __________/**

TOTAL POINTS:___________/

1. Grammars (20 points)

a) (10pts) Consider a grammar to generate Scheme lists, disallowing quotes on list elements. Assume that the scanner is going to recognize the integer and symbol constructs, so they do not need grammar productions. Add productions below to complete a legal context-free grammar.

List_ele ::= symbol | integer | List

b) (10pts)** Think about parsing the lists defined by this grammar. Can you write a predictive parser for this grammar? Briefly explain why or why not?

3. LR Parsing (30 points)

Consider the following grammar over terminals + , if and int :

S →→ →→ E

E →→ →→ int | E if E | E + E

a) (10 pts) This grammar is ambiguous. Show two parse trees for int + int if int

b) (20 pts) Consider the partial DFA (i.e., CFSM) for this grammar:

Complete state 0 by performing closure on the item listed.

Fill in the elements of states 3 and 4.

Fill in the missing labels (6 of them).

Fill in the “reduce by …” labels on states 5 and 6 below:

Label on state 5: “reduce by E → E + E on ________”

Label at state 6: “reduce by E → E if E on _________”

S → • E E → E • + E

E → E • if E

S → E •

E → int •

E → E + E •

E → E • + E

E → E • if E

E → E if E •

E → E • + E

E → E • if E

E

int

if

0

2

1

3

4

5

6

int

int

4. Scoping (45 points). Assume in our programming language we use lexical scoping for non-local variables and dynamic scoping for lookup of function definitions. (Note: this breaks the rules for lexical scoping of nested functions.) Use the run-time stack at the right, to hand-simulate the execution of this program and show the values of the variables, the dynamic chain, and the lexical chain.

a) (20pts) Show the run-time stack before exiting procedure r due to the call of r in main.q /****/ , writing qualified names for all procedures (e.g., main.q( )). Use as many of the frames as you need.

b) (25pts) Show the output of this execution below.

main()

{ integer: m=10, k=0;

procedure p()

{ integer: k=1;

procedure r()

{ integer: m=0;

k = 4;

}//end p.r

print “#2”, m, k;

q();

print “#3”, m, k;

}//end main.p

procedure r()

{ integer: m=6;

k = 5;

}//end main.r

procedure q()

{ k = 2;

r(); /****/

}//end main.q

print “#1”, m, k;

p();

}//end main

Output:

procedure

___main_____

lexical-link xxxx

dynamic-link xxxx

procedure ________

lexical-link

dynamic-link

procedure ________

lexical-link

dynamic-link

procedure ________

lexical-link

dynamic-link

procedure ________

lexical-link

dynamic-link

b) (5pts) How would you change the call to p at ()* so that after returning from that call, a[2] would have been modified to 6?

call p ( ___________, ______________, ____________);

**c) (20pts) Complete the following program fragment in the shortest possible way (fewest and shortest statements) so that the output will be different if parameter passing was by reference versus by value- result. Indicate in each case what the output would be.

procedure p(integer x)

________________________________

________________________________

________________________________

________________________________

main()

/* begin main program */

integer A = 10, B = 20;

p(A);

print(“A=”,A,”B=”,B);

i. Output in case of pass by-reference: ________________________________

ii. Output in case of pass by-value-result: ________________________________

6. Types and Type equivalence (40 points)

a (25pts) Consider the following type and variable declarations.

type TI = integer; //equivalent to C’s typedef int TI; type TTI = TI; type TPI = ref integer;//equivalent to C’s typedef int * TPI type TPI2 = ref TI; type TPPTI = ref TPI; type AI = [0:10] integer; type ATPI = [1:20] TPI; type ATI = [0:10][0:10] TI; var a : integer; var b : TTI; var c : TPI; var d : TPI2; var e : TTI; var f : ref float; var g : ref float; var h : TPPTI; var j : ATPI; var k : ATI; var l : AI;

Assume that the array type includes the array dimensionality, but not array bounds.

For each of the pairs of variables in the first column of the table above, put YES in the column labeled NameEq exactly when their types are equivalent according to the rules of name equivalence, and YES in the column StructEq when their types are equivalent according to the rules of structural type equivalence.

b (15pts) Consider the following type declarations in C:

struct B{ char x; int y;}; typedef struct B A;

**struct { A a; A next;} aa; struct { struct B a; struct B next;} bb;

*struct { struct B a; struct B next;} cc;

A a; struct B b;

Circle the assignments that result in “incompatible types” error? Why?

a = b;

aa = bb;

bb = cc;

7. Comparative Programming Languages (60 points, 20 each)

NameEq StructEq

a, b

c, d

b, e

h, j

c, f

f, g

k, l

b. Now write a Python function to accomplish the same task. Note; the following built-in operations in

Python on dictionaries may be of use : keys(), values(), has_key(),

update(dict), len(dict), indexing or the following built-in operations on Python lists:

+ (concatenate), append(ele), del list[k] (element deletion),

indexing [..]. Function returns list of variables in dict of type type.

def get_vars (type, dict)__________________________

c. Now write the predicate get_vars(Type,Sym,Vars) in Prolog as a predicate with three

parameters. The first is the type to be searched for, the second is the symbol table (as a list) to search, and the third is the list of variables found to be of that type. So when the predicate is executed on the list shown, for example,

?- get_vars(int, [[x,int],[y,real],[z,list],[w, int]],V)

we expect the answer V = [x, w].

get_vars(Type,Sym,Vars):-

c (20pts)** Suppose we want to create a different set of pairs, that is all pairs whose first element comes from the first list and whose second element comes from the second list. For example, (all_pairs ‘(1 2) ‘(3 4)) returns ((1 3) (1 4) (2 3) (2 4)) (Note: order of the pairs in the list does not matter.) Write the function all_pairs below.

Hint: In writing all_pairs, think about using a helper function that applies multiple functions to a list

of values.

(define (all_pairs xs ys)

9. Lambda Calculus (30 points)

a (10pts) Reduce the term ( λλλλ x. x x) (( λλλλ y. y) ( λλλλ z. z)) using normal order (i.e., by-name) reduction.

b (10pts) Reduce the term ( λλλλ x. x x) (( λλλλ y. y) ( λλλλ z. z)) using applicative order (i.e., by-value) reduction.

c (10pts) Consider the following combinators:

true = λλλλ x. λλλλ y.x

false = λλλλ x. λλλλ y.y

not = λλλλ x.((x false) true)

Show that not (not x) →→→→ … →→→→ x where x is true or false. Hint: to save writing, first reduce not (not x) as much as possible.