7 Questions with Solution of Program Languages & Compilers | CS 421, Exams of Computer Science

Material Type: Exam; Class: Progrmg Languages & Compilers; Subject: Computer Science; University: University of Illinois - Urbana-Champaign; Term: Summer 2008;

Typology: Exams

Pre 2010

Uploaded on 03/16/2009

koofers-user-8p9
koofers-user-8p9 🇺🇸

9 documents

1 / 17

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 421—Summer 2006 1 CS421 Summer 2008 Final Exam SOLUTIONS
CS421 Summer 2008 Final Exam SOLUTIONS
CS 421 Programming Languages and Compilers
Summer Term 2006
1:00pm–3:00pm, Friday, August 1, 2008
The total time for this exam is 120 minutes.
Print your name and netid below. Also write your name at the bottom of each subsequent page.
Name:
Netid:
This is a closed-book exam. You are allowed one 3 inch by 5 inch card of notes prepared
by yourself. You may write on both sides of the card. This card is not to be shared. All
other materials, besides pens, pencils, and erasers, are to be put away.
Do not share anything with other students. Do not talk to other students. Do not look at
another student’s exam. Also, be careful to not expose your exam to easy viewing by other
students. Violation of any of these rules will count as cheating.
If you believe there is an error, or an ambiguous question, you must document your assump-
tions about what the question means. We are not allowed to answer questions about the exam
in class, and obviously cannot answer these questions for students taking the exam elsewhere.
Including this cover sheet, rules at the end, and scratch pages, there are 17 pages to the exam.
Please verify that you have all 17 pages. You are allowed to tear off the last 7 (rules and
scratch) if this makes it easier to take the test. To save time, there is no need to put your
name or netid on these final 7 pages.
Question Total points Score
1 16
2 14
3 10
4 20
5 10
6 20
7 10
TOTAL 100
University of Illinois at Urbana-Champaign Netid:
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download 7 Questions with Solution of Program Languages & Compilers | CS 421 and more Exams Computer Science in PDF only on Docsity!

CS421 Summer 2008 Final Exam SOLUTIONS

CS 421 — Programming Languages and Compilers

Summer Term 2006

1:00pm–3:00pm, Friday, August 1, 2008

The total time for this exam is 120 minutes.

Print your name and netid below. Also write your name at the bottom of each subsequent page.

Name:

Netid:

  • This is a closed-book exam. You are allowed one 3 inch by 5 inch card of notes prepared by yourself. You may write on both sides of the card. This card is not to be shared. All other materials, besides pens, pencils, and erasers, are to be put away.
  • Do not share anything with other students. Do not talk to other students. Do not look at another student’s exam. Also, be careful to not expose your exam to easy viewing by other students. Violation of any of these rules will count as cheating.
  • If you believe there is an error, or an ambiguous question, you must document your assump- tions about what the question means. We are not allowed to answer questions about the exam in class, and obviously cannot answer these questions for students taking the exam elsewhere.
  • Including this cover sheet, rules at the end, and scratch pages, there are 17 pages to the exam. Please verify that you have all 17 pages. You are allowed to tear off the last 7 (rules and scratch) if this makes it easier to take the test. To save time, there is no need to put your name or netid on these final 7 pages.

Question Total points Score 1 16 2 14 3 10 4 20 5 10 6 20 7 10 TOTAL 100

  1. Short Answer Questions (16 points):

(a) In the following, state TRUE or FALSE for each part separately; each part is 1 point (write your answers to the left): Data Abstraction: i. All items in an array are of the same type; TRUE ii. The items in a tuple have no inherent order – i.e., swapping them does not change the tuple; FALSE iii. ADTs limit access to the internal representation of the data type to enforce abstrac- tion; TRUE iv. ADTs include special syntax that allows a user to specify the order in which proce- dures should be called (push before pop on a stack, for instance). FALSE

(b) In the following, state TRUE or FALSE for each part separately; each part is 1 point (write your answers to the left): Object-Oriented Languages: i. Traits and mixins are alternate methods for reusing functionality; TRUE ii. In OO languages, all methods are virtual methods; FALSE iii. Overriding refers to the ability to give the same method name multiple signatures in the same class; FALSE iv. The “diamond problem” illustrates a potential problem with multiple inheritance. TRUE

(c) In the following, state TRUE or FALSE for each part separately; each part is 1 point (write your answers to the left): Control Flow: i. Coroutines allow multiple execution contexts to run at the same time; FALSE ii. Only checked exceptions (i.e., exceptions that are required to be caught) are used in object-oriented languages like Java and C#; FALSE iii. Continuations provide the ability to quickly transfer control to another part of a program, even while in the middle of another computation; TRUE iv. Exceptions are only available in object-oriented languages. FALSE

(d) In the following, state TRUE or FALSE for each part separately; each part is 1 point (write your answers to the left): Garbage Collection: i. One disadvantage of copying collectors is that they cause memory fragmentation; FALSE ii. An advantage of reference counting collectors is that they can spread the cost of garbage collection across the program by collecting individual objects as they become garbage; TRUE iii. Garbage collection does not address the dangling reference problem; FALSE iv. Garbage collection methods, like mark/sweep, that find live objects in memory by determining objects reachable from the roots, are not able to deal with cyclic data structures. FALSE

  1. Type Derivations (10 points): Provide a full type derivation for the following term. Feel free to write on the paper sideways – the trees tend to be wider than they are tall. You can also write out type environments separately and just reference them in the rules (so you could say Γ 1 = {x : int} and then just reference Γ 1 in the derivation) – this is especially useful if you use the same environment in multiple places. Typing rules are on page 11.

let rec g = fun a −> ((if a = 0 then 1 else (g 0)) + 10 )

Note: I realized there was an error in this after it was too late to fix it. There should be an in component to the let rec, unlike the construct in OCaml. The solution below treats let rec like OCaml would. We took account of this when grading your solutions, so as long as you did something reasonable (most everyone just left the in part off), you received credit.

Solution:

Γ′^ ⊢ a : int Γ′^ ⊢ 0 : int Γ′^ ⊢ a = 0 : bool Γ′^ ⊢ 1 : int

Γ′^ ⊢ g : int → int Γ′^ ⊢ 0 : int Γ′^ ⊢ g 0 : int Γ′^ ⊢ if a = 0 then 1 else (g 0) : int Γ′^ ⊢ 10 : int Γ′^ = Γ ∪ [g : int → int, a : int] ⊢ (if a = 0 then 1 else (g 0)) + 10 : int Γ ∪ [g : int → int] ⊢ fun a −> ((if a = 0 then 1 else (g 0)) + 10 ) : int → int Γ ⊢ let rec g = fun a −> ((if a = 0 then 1 else (g 0)) + 10 ) : int → int

Note here that Γ starts out empty, but could really be anything, so you can use either Γ, without further saying what it is, or use {} to represent an empty type environment. If you used {x : int}, which was just an example, we did not count off points.

  1. Lambda Calculus (20 points):

(a) (5 points) Use β-reduction with a lazy evaluation order to reduce the following term as fully as possible: (λa b.b) ((λz.z z)((λf.f)(λg.g))) m

Solution: (λa b.b) ((λz.z z)((λf.f)(λg.g))) m →β (λb.b) m →β m

(b) (5 points) Use β-reduction with an eager evaluation order to reduce the following term as fully as possible: (λf.f g h) (((λa.a a) (λb.b)) (λc d.d))

Solution: (λf.f g h) (((λa.a a) (λb.b)) (λc d.d)) →β (λf.f g h) (((λb.b)(λb.b)) (λc d.d)) →β (λf.f g h) ((λb.b) (λc d.d)) →β (λf.f g h) (λc d.d) →β (λc d.d) g h →β (λd.d) h →β h

  1. Context-Free Grammars, Derivations, and Parse Trees (10 points): The following ex- ercises make use of this grammar for arithmetic expressions. Here, the start symbol is E, id refers to identifiers made up of one lowercase character (a, b, · · · , y, z), and num refers to any integer:

E → E + T T → T ∗ F F → id E → E − T T → T /F F → num E → T T → F F → (E)

(a) (5 points) Show a leftmost derivation for the following term: x ∗ (y − (z + 3))

Solution: E ⇒ T ⇒ T ∗F ⇒ F ∗F ⇒ x∗F ⇒ x∗(E) ⇒ x∗(E −T ) ⇒ x∗(T −T ) ⇒ x∗(F −T ) ⇒ x ∗ (y − T ) ⇒ x ∗ (y − F ) ⇒ x ∗ (y − (E)) ⇒ x ∗ (y − (E + T )) ⇒ x ∗ (y − (T + T )) ⇒ x ∗ (y − (F + T )) ⇒ x ∗ (y − (z + T )) ⇒ x ∗ (y − (z + F )) ⇒ x ∗ (y − (z + 3))

(b) (5 points) Show an AST (not a parse tree) for the same term:

Solution:

x -

y (^) +

z (^3)

  1. Semantics (20 points):

(a) (10 points) Derivations: Using natural semantics (rules are on pages 14–15), provide a derivation for the following term: while i = 1 do (if n = 10 then i := i + 1 else skip) Assume the starting σ = {i 7 → 1 , n 7 → 10 }. Feel free to turn the page sideways, or to number parts of the derivation and provide them separately (so you can say # somewhere in the derivation and then elsewhere provide the definition for the #1 part of the derivation tree).

Solution:

〈i, σ〉 ⇓ 1 〈 1 , σ〉 ⇓ 1 〈i = 1 , σ〉 ⇓ true

〈n, σ〉 ⇓ 10 〈 10 , σ〉 ⇓ 10 〈n = 10 , σ〉 ⇓ true

〈i, σ〉 ⇓ 1 〈 1 , σ〉 ⇓ 1 〈i + 1 , σ〉 ⇓ 2 〈i := i + 1 , σ〉 ⇓ σ′^ = σ[2/i] 〈if n = 10 then i := i + 1 else skip, σ〉 ⇓ σ′^ (#1, see below) 〈while i = 1 do (if n = 10 then i := i + 1 else skip), σ〉 ⇓ σ′

〈i, σ′〉 ⇓ 2 〈 1 , σ′〉 ⇓ 1 〈i = 1 , σ′〉 ⇓ false #1 = 〈while i = 1 do (if n = 10 then i := i + 1 else skip), σ′〉 ⇓ σ′

  1. Opinion/Open (10 points):

These questions are fairly open, and designed to be (hopefully) a little fun. Please try to support your answers, so we have something to go on where we’re grading (i.e., please don’t just write “Yes” or “No” and leave it at that). Don’t feel the need to write for too long on these, though – an answer that fits in the space provided is acceptable, but feel free to continue onto the back of the page.

(a) (5 points) Garbage Collection: You are working on the software that is used to control the landing gear of a commercial airliner. One of the other members of the project has just read about copying collectors, and believes that you should use one as part of this system. Is this a good idea? Why or why not? If not, what would you use instead, and why?

Solution: You could argue either way on this, and, as long as you backed your argument up, you were given credit. In one sense, this really isn’t a good idea, because of the problem we talked about in class with copy collectors, where (at least in the model we examined) they require the mutator, i.e., the program, to pause while collection occurs. If this happened at a bad time, it could prevent the landing gear from coming down, which would make for an interesting landing. At the same time, you generally don’t lower landing gear at the last minute, so it should be the case that, by the time you need them, they will be down (they are usually lowered several minutes in advance). If you don’t use this, there are a couple of other options. One is to use a reference counting collector. This would keep the cost of collection down, spreading it across all operations. Another would be to just use manual memory management; this would have a smaller footprint, but could lead to its own safety issues because of issues we talked about in class, such as with dangling references. Again, any answer that explored (at least partially) these points should receive credit.

(b) (5 points) Language Design: You have the opportunity to add a new language feature to one of your favorite languages. What feature do you select? Describe the semantics and typing rules either formally or informally, depending somewhat on the language (describing a typing rule formally in OCaml is easier, perhaps, than doing the same in BASIC). Show a short example using this feature, illustrating how it would be useful.

Solution: This one was wide open – as long as you put something in, gave it some justification, and explained how the feature works, either formally or informally, you should have received credit. You lost points if you either didn’t explain how your feature would work, or if you never justified it, either with text or a useful example.

Rules for type derivations:

Constants

⊢ n : int (assuming n is an int)

⊢ true : bool

⊢ false : bool

Variables

Γ ⊢ x : τ if(x : τ ) ∈ Γ

Arithmetic Operators Γ ⊢ e 1 : int Γ ⊢ e 2 : int Γ ⊢ e 1 ⊕ e 2 : int

(⊕ ∈ {+, −, ∗, /, · · · })

Relational Operators Γ ⊢ e 1 : int Γ ⊢ e 2 : int Γ ⊢ e 1 ∼ e 2 : bool

(∼ ∈ {<, >, ≤, ≥, =, 6 =, · · · })

Booleans Γ ⊢ e 1 : bool Γ ⊢ e 2 : bool Γ ⊢ e 1 && e 2 : bool Γ ⊢ e 1 : bool Γ ⊢ e 2 : bool Γ ⊢ e 1 || e 2 : bool Γ ⊢ e 1 : bool Γ ⊢! e 1 : bool If Γ ⊢ e 1 : bool Γ ⊢ e 2 : τ Γ ⊢ e 3 : τ Γ ⊢ if e 1 then e 2 else e 3 : τ Function Abstraction Γ ∪ [x : τ 1 ] ⊢ e : τ 2 Γ ⊢ fun x−>e : τ 1 → τ 2 Function Application Γ ⊢ e 1 : τ 1 → τ 2 Γ ⊢ e 2 : τ 1 Γ ⊢ e 1 e 2 : τ 2 Let Γ ⊢ e 1 : τ Γ ∪ [x : τ ] ⊢ e 2 : τ ′ Γ ⊢ let x = e 1 in e 2 : τ ′ Letrec Γ ∪ [x : τ ] ⊢ e 1 : τ Γ ∪ [x : τ ] ⊢ e 2 : τ ′ Γ ⊢ let rec x = e 1 in e 2 : τ ′

Assignment 〈a, σ〉 → 〈a′, σ〉 〈X := a, σ〉 → 〈X := a′, σ〉

〈X := n, σ〉 → σ[n/X]

Sequencing 〈c 0 , σ〉 → 〈c′ 0 , σ′〉 〈c 0 ; c 1 , σ〉 → 〈c′ 0 ; c 1 , σ′〉

〈c 0 , σ〉 → σ′ 〈c 0 ; c 1 , σ〉 → 〈c 1 , σ′〉

If 〈b, σ〉 → 〈b′, σ〉 〈if b then c 0 else c 1 , σ〉 → 〈if b′^ then c 0 else c 1 , σ〉

〈if true then c 0 else c 1 , σ〉 → 〈c 0 , σ〉

〈if false then c 0 else c 1 , σ〉 → 〈c 1 , σ〉

While 〈while b do c, σ〉 → 〈if b then (c; while b do c) else skip, σ〉

Natural Semantics, Dynamic Semantics Rules:

Aexp a ::= n | X | a 0 + a 1 | a 0 − a 1 | a 0 × a 1 Bexp b ::= true | false | a 0 = a 1 | a 0 ≤ a 1 | ¬b | b 0 ∧ b 1 | b 0 ∨ b 1 Com c ::= skip | X := a | c 0 ; c 1 | if b then c 0 else c 1 | while b do c

Identifiers 〈X, σ〉 ⇓ σ(X) Constants 〈n, σ〉 ⇓ n 〈true, σ〉 ⇓ true 〈false, σ〉 ⇓ false

Arithmetic Operators 〈a 0 , σ〉 ⇓ n 0 〈a 1 , σ〉 ⇓ n 1 n = n 0 opint n 1 〈a 0 op a 1 , σ〉 ⇓ n

(op ∈ {+, −, ×})

Relational Operators 〈a 0 , σ〉 ⇓ n 0 〈a 1 , σ〉 ⇓ n 1 b = n 0 opint n 1 〈a 0 op a 1 , σ〉 ⇓ b

(op ∈ {=, ≤})

Logical Operators 〈b 0 , σ〉 ⇓ false 〈b 0 ∧ b 1 , σ〉 ⇓ false

〈b 0 , σ〉 ⇓ true 〈b 1 , σ〉 ⇓ b 〈b 0 ∧ b 1 , σ〉 ⇓ b

〈b 0 , σ〉 ⇓ true 〈b 0 ∨ b 1 , σ〉 ⇓ true

〈b 0 , σ〉 ⇓ false 〈b 1 , σ〉 ⇓ b 〈b 0 ∨ b 1 , σ〉 ⇓ b

〈b, σ〉 ⇓ false 〈¬b, σ〉 ⇓ true

〈b, σ〉 ⇓ true 〈¬b, σ〉 ⇓ false

Skip {skip, σ} ⇓ σ

  • Scratch Page
  • Scratch Page