









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










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. 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 :
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:
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.
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?
**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.
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
b. Now write a Python function to accomplish the same task. Note; the following built-in operations in
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,
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.
of values.
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.