Starting from:

$30

Homework 3 Solution

(20 pts) Consider the following fragment of code in C:



{int a, b, c;

...

{int d, e;

...

{int f;

...

}

...

}

...

{int g, h, i;

...

}

...

}




Assume that each integer variable occupies four bytes. How much total space is required for the variables in this code? Justify your answer.




(20 pts) Consider the following pseudocode, where procedures Q and R are nested inside procedure P:



procedure P (A,B: real)

X: real




procedure Q (B,C: real)

real
...




procedure R (A,C: real)

Z: real

... // (*)

...




Assuming static scope, what is the referencing environment (i.e., what names are known, and what do they refer to) at the location marked by (*)?










3. (30 pts) Consider the following pseudocode:

procedure main
a: integer := 1
b: integer := 2



procedure middle
b: integer := a



procedure inner
7. print a, b




a: integer := 3



// body of middle
inner()
print a, b



// body of main
middle()
print a, b



Suppose this was code for a language with the declaration-order rules of C (but with nested subroutines) - that is, names must be declared before use, and the scope of a name extends from its declaration through the end of the block. At each print statement, indicate which declarations of a and b are in the referencing environment. What does the program print (or will the compiler identify static semantic errors)?



Repeat the exercise for the declaration-order rules of C# (names must be declared before use, but the scope of a name is the entire block in which it is declared).
Repeat the exercise for the declaration-order rules Modula-3 (names can be declared in any order, and their scope is the entire block in which they are declared).



(30 pts) Consider the following pseudocode:



integer := 1
integer := 2



procedure add

x := x + y




procedure second (P: procedure)

integer := 2 P()



procedure first

integer := 3 second(add)



main program first()



write integer(x)




What does this program print if the language uses static scoping?



What does it print if the language uses dynamic scoping with deep binding?
What does it print if the language uses dynamic scoping with shallow binding?

More products