CS3360 Design and Implementation of Programming Languages Final Exam May 16, 2013 10pm to 12:45pm (2 hour and 40 minutes) NAME: GRADE = / 140 1. Choice of Programming Languages / 25 2. Programming languages: Design Issues / 30 3. Programming / 20 4. Grammars / 20 5. Dynamic Semantics / 10 6. Storage and Type Binding /20 7. Parameter Passing /15 Rules: This exam is to be done individually. Class notes, cheat sheets, and books of any kind are NOT allowed. You must write legibly and in a structured manner: unreadable answers will be graded 0; messy / hard-to-follow answers will be penalized by taking off up to 75% of the points of the corresponding question. Cell phones should be turned off, no headsets allowed. Turn in your exam by 12:45pm sharp: no exam will be accepted past this time. 1
1 Choice of Programming Languages/Paradigms [25 points + 5 points of extra-credit] 1.1 What does AspectJ bring to Java? Which dimension(s) of the evaluation of a programming language can it enhance, and how? [10 points] 1.2 Suppose that you have to design a piece of software to determine the best schedule of courses in your department. You know which courses need to be offered, who can teach what, how many courses does each instructor teach, which classes can each instructor teach, etc. Which programming language/paradigm would you recommend using and why?. [10 points] 2
1.3 Name and discuss a programming language of your choice, specify the programming paradigm it belongs to, and evaluate it critically using the dimensions and criteria we presented and discussed in class. [10 points] 2 Programming Languages: Design issues [30 points + 10 points of extra-credit] 2.1 Discuss how aliasing affects (positively or negatively) the reliability of programming languages that allow it. [5 points] 2.2 Give an example of implicit heap dynamic variables along with the corresponding programming language. [5 points] 3
2.3 Discuss the advantages and inconvenients of static variables. [10 points] 2.4 Discuss the advantages and inconvenients of dynamically-typed variables. [10 points] 2.5 Discuss the feasibility of implementing a in-out parameter-passing method in a programming language that does not allow aliasing. [10 points] 4
3 Programming [20 points] 3.1 Functional Programming [10 points] Consider the following piece of code: module Main where mystery n = if n == 0 then 1 else n * mystery (n - 1) main = do putstrln What is mystery(5)?" x readln if x == mystery 5 then putstrln You re right!" else putstrln You re wrong!" What does mystery do? What does the above main function do? What can you say about variable x? (e.g., type binding, storage binding) 3.2 Logic Programming [10 points] 1. Consider the following Prolog code: mystery(x,[ ]):-!. mystery(x,[y L]):- X \ = Y, mystery(x,l). 5
What does this predicate do? What is the result of mystery(2,[1,3,5]).? 2. Consider the following Prolog code: What does this predicate do? mystery2([ ],[H,R]). mystery2([x T],[H,R]):- (X = H; X = R), mystery2(t,[h,r]). What is mystery2([2,4,6],[2,4]).? How can you improve the above code so that the compiler will not throw any warning? 6
4 Grammars [20 points + 5 points of extra credit] 4.1 Translate the following grammar from EBNF to BNF. [10 points] <sequence> <letter> {(<letter> <digit>)} @ <letter> {(<letter> <digit>)}. (fr com edu) 4.2 Attribute Grammar. [15 points] Consider the following grammar that defines binary trees. <BT> (<BT> <value> <BT>) nil <value> 0 1... 9 where nil stands for the empty tree. Draw the parse tree of the following tree [5 points]: 7
Augment the above grammar with attributes so that only balanced binary trees are allowed (balanced binary trees are trees such each node in the tree is such that the number of levels of its left subtree does not differ from the number of levels of its right subtree by more than 1) [10 points]. Note: use the following attribute of <BT>: actual-number-of-levels. 8
5 Dynamic semantics [10 points + 20 points of extra-credit] 5.1 Operational semantics [10 points] Write the operational semantics of if B then C else D. 5.2 Denotational semantics [10 points] Write the denotational semantics of do C while B. 5.3 Axiomatic semantics [10 points] Consider the following fragment of code with its given general post-condition: {???} y = 2x + y; if (y is even) then x = 2y; else x = 2y + 1; { x > 0} Determine the weakest precondition of this fragment of code. Justify your answer and detail your reasoning. 9
6 Storage and Type Binding [20 points] 6.1 Answer the following questions [20 points] 1. When talking about stack-dynamic variables, heap-dynamic variables, what do we refer to? storage binding type binding other; explain: 2. Stack-dynamic variables are: statically bound to type dynamically bound to type other; explain: 3. Heap-dynamic variables are: statically bound to type dynamically bound to type other; explain: 4. When it comes to type binding, what are the three main types of type binding: (a)... (b)... (c)... 10
7 Parameter-passing methods [15 points + 5 points of extra credit] 7.1 What is the mode of pass-by-reference parameter-passing methods? [5 points] 7.2 Discuss the differences and pros-cons of pass-by-reference and pass-byvalue-result parameter-passing methods? [10 points] 7.3 Consider the following program is run using pass-by-value-result parameterpassing mode. What are the values in the variables at the end of execution? [5 points] void fun (int first, int second) { first=second; second=10; } void main () { int list[2] = {1,3}; fun(list[0],list[1]); } 11