CS415: Code Layout & While Loop with Different Semantics - Prof. Westley Weimer, Assignments of Programming Languages

A written assignment for cs415 students, focusing on code layout and operational semantics. The assignment includes questions on drawing the tree of activation records for a call to a recursive function and constructing a derivation for a given piece of code using operational semantics rules. Additionally, students are asked to create operational semantics for new array constructs in cool and modify the operational semantics for the while loop with alternative semantics.

Typology: Assignments

Pre 2010

Uploaded on 03/19/2009

koofers-user-csw
koofers-user-csw ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS415 โ€” Written Assignment 6
2007-03-26
This assignment asks you to prepare written answers to questions on code layout
and operational semantics. Each of the questions has a short answer. You may discuss
this assignment with other students and work on the problems together. However, your
write-up should be your own individual work.
Please print your name and email address on your homework!
We need this information so that we can give you credit for the assignment and so that
we can return it to you.
1. Consider the following piece of code:
fact(n : Int) : Int {
if n > 0 then n*fact(n-1) else 1 fi
};
Draw the tree of activation records for a call to fact(4).
2. Consider these six operational semantics rules:
(1) so, E, S `e1:Bool(f alse), S1
so, E, S `while e1loop e2pool :void, S1
(4)
E(id) = lid
S(lid) = v
so, E, S `id :v, S
(2)
so, E, S `e1:Bool(true),S1
so, E, S1`e2:v , S2
so, e, S2`while e1loop e2pool :void, S3
so, E, S `while e1loop e2pool :void, S3
(5)
so, E, S `e:v, S1
E(id) = lid
S2=S1[v/lid]
so, E, S `id โ†e:v, S2
(3)
so, E, S `e1:v1, S1
lnew =newloc(S1)
so, E[lnew /id], S1[v1/lnew]`e2:v2, S2
so, E, S `let id :Tโ†e1in e2:v2, S2
1
pf3

Partial preview of the text

Download CS415: Code Layout & While Loop with Different Semantics - Prof. Westley Weimer and more Assignments Programming Languages in PDF only on Docsity!

CS415 โ€” Written Assignment 6

This assignment asks you to prepare written answers to questions on code layout

and operational semantics. Each of the questions has a short answer. You may discuss

this assignment with other students and work on the problems together. However, your

write-up should be your own individual work.

Please print your name and email address on your homework!

We need this information so that we can give you credit for the assignment and so that

we can return it to you.

1. Consider the following piece of code:

fact(n : Int) : Int {

if n > 0 then n*fact(n-1) else 1 fi

Draw the tree of activation records for a call to fact(4).

2. Consider these six operational semantics rules:

so, E, S e 1 : Bool(f alse), S 1 so, E, S while e 1 loop e 2 pool : void, S 1 (4)

E(id) = lid S(lid) = v so, E, S ` id : v, S

so, E, S e 1 : Bool(true), S 1 so, E, S 1 e 2 : v, S 2 so, e, S 2 while e 1 loop e 2 pool : void, S 3 so, E, S while e 1 loop e 2 pool : void, S 3

so, E, S e : v, S 1 E(id) = lid S 2 = S 1 [v/lid] so, E, S id โ† e : v, S 2

so, E, S e 1 : v 1 , S 1 lnew = newloc(S 1 ) so, E[lnew/id], S 1 [v 1 /lnew] e 2 : v 2 , S 2 so, E, S ` let id : T โ† e 1 in e 2 : v 2 , S 2

so, E, S e 1 : Int(n 1 ), S 1 so, E, S 1 e 2 : Int(n 2 ), S 2

v =

Bool(true) if n 1 < n 2 Bool(f alse) if n 1 โ‰ฅ n 2

so, E, S ` e 1 < e 2 : v, S 2

Use these rules to construct a derivation for the following piece of code:

let x : Int <- 2 in

while 1 < x loop

x <- x - 1

pool

You may assume reasonable axioms, e.g. it is always true that so, E, S ` 2 โˆ’ 1 :

Int(1), S. Start your derivation using the let rule (3) as follows:

so, E, S ` 2 : Int(2), S

... so, E[lnew /x], S[Int(2)/lnew ] while 1 < x loop x โ† x โˆ’ 1 pool : void, Sf inal^ (2) so, E, S let x : Int โ† 2 in while 1 < x loop x โ† x โˆ’ 1 pool : void, Sf inal^ (3)

Note that you only need to expand hypotheses that need to be proved (i.e. those

containing `).

3. Suppose we wanted to add arrays to COOL, using the following syntax:

let a:T[e 1 ] in e 2 Create an array a with size e 1 of T s, usable in e 2

a[e 1 ] <- e 2 Assign e 2 to element e 1 in a

a[e] Get element e of a

Write the operational semantics for these three syntactic constructs. You may

find it helpful to think of an array of type T [n] as an object with n attributes of

type T.

4. The operational semantics for COOLโ€™s while expression show that result of

evaluating such an expression is always void.

However, we could have used the following alternative semantics:

  • If the loop body executes at least once, the result of the while expression

is the result from the last iteration of the loop body.

  • If the loop body never executes (i.e., the condition is false the first time it

is evaluated), then the result of the while expression is void.

For example, consider the following expression:

while (x < 10) loop x <- x+1 pool