Monday, November 3, 2014

Assignment 5

Pada post kali ini, saya akan menjawab pertanyaan-pertanyaan Assignment #5 dari Chapter 5, dari buku "Concepts of Programming Languages" karya Robert W. Sebesta.

Review Questions

        1.  What is the l-value of a variable? What is the r-value?
l-value is an address/location of the variable. r-value is a content of the variable.

        2. Define binding and binding time.
Binding is an association between an attribute and an entity, such as between a variable and its type or value, or between an operation and a symbol.
Binding time is the time at which a binding takes place.

        3. After language design and implementation [what are the four times bindings can take place in a program?]
  • Language design time : bind operator symbols to operations
  • Language implementation time : bind floating point type to a representation
  • Compile time : bind a variable to a type in C or Java
  • Load time : bind a C or C++ static variable to a memory cell)
  • Runtime : bind a non-static local variable to a memory cell
        4. Define static binding and dynamic binding
  • Static binding : references which are resolved at the compile time
  • Dynamic binding : references which are resolved at run time
        5. What are the advantages and disadvantages of implicit declarations?
The advantage is writability.

Problem Sets

        6. Consider the following JavaScript skeletal program:
//the main program
var x;
function sub1(){
var x;
function sub2(){
...
}
}
function sub3(){
...
}
Assume that the execution of this program is in the following unit order:
main calls sub1
sub1 calls sub2
sub2 calls sub3


a. Assuming static scoping, in the following, which declaration of x is the correct one for a reference to x?
i. sub1
ii. sub2
iii. sub3


b. Repeat part a, but assume dynamic scoping.
a.) i.) sub1 ii.) sub1 iii.) Main
b.) i.) sub1 ii.) sub1 iii.) sub1


        7. Assume the following JavaScript program was interpreted using static-scoping rules. What value of x is displayed in function sub1? Under dynamic scoping rules, what value of x is displayed in function sub1?


var x;
function sub1(){
document.write("x = " + x + "<br />");
}
functin sub2(){
var x;
x = 10;
sub1();
}
x = 5;
sub2();

Static scoping: x=5;
Dynamic scoping: x=10;

        8. Consider the following JavaScript program:Answer :
var x, y, z;
function sub1() {
var a, y, z;
function sub2() {
var a, b, z;
. . .
}
. . .
}
function sub3() {
var a, x, w;
. . .
}
List all the variables, along with the program units where they are declared, that are visible in the bodies of sub1, sub2, and sub3, assuming static scoping is use
d.
Sub1: a(sub1), y(sub1), z(sub1), x(main)
Sub2: a(sub2), b(sub2), z(sub2), y(sub1), x(main)
Sub3: a(sub3), x(sub3), w(sub3), y(main), z(main)

        9. Consider the following Python program:
Answer :
x = 1;
y = 3;
z = 5;
def sub1():
a = 7;
y = 9;
z = 11;
. . .
def sub2():
global x;
a = 13;
x = 15;
w = 17;
. . .
def sub3():
nonlocal a;
a = 19;
b = 21;
z = 23;
. . .
. . .
List all the variables, along with the program units where they are
declared, that are visible in the bodies of sub1, sub2, and sub3, assuming static scoping is used.
point 1 : x = 1(main), y = 9 (sub1), z = 11(sub1) ,a = 7(sub1);
point 2 : x =15(sub2), w = 17(sub2), a = 13(sub2), y = 9(sub1);
point 3 : x = 15(sub2), b = 21(sub3), a = 19(sub1), z = 23(sub3), w = 17(sub 2);
point 4 : x = 15(sub2), b = 21(sub3), a = 19(sub1), z = 23(sub3), w = 17(sub 2);

        10. Consider the following C program:
void fun(void){
int a,b,c; /* definition 1 */

while(…){
int b,c,d; /* definition 2 */
… <—————–1
while(…){
int c,d,e; /*definition 3 */
… <—————–2
}
… <—————–3
}
… <—————–4
}
For each of the four marked points in this function, list each visible variable, along with the number of the definition statement that defines it.
1. int a,b,c in definition 1, and int b,c,d in definition 2
2. int a,b,c in definition 1, and int b,c,d in definition 2, and int c, d, e in definition 3
3. int a,b,c in definition 1, and int b,c,d in definition 2
4. int a,b,c in definition 1

No comments:

Post a Comment