Chapter 5 Programming Language Concepts R Sebesta
Nama: Billy
NIM: 1801374785
NIM: 1801374785
Review Questions
6. What is the l-value of a variable? What is the r-value?
L-value is the address of a variable , because the address is what is required when the name of a variable appears in the left side of an assignment.
R-value is a name for a variable's value because it is what is required when the name of the variable appears in the right side of an assignment statement
7. Define binding and binding time.
A 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.
8. After language design and implementation [what are the four times bindings can take place in a program?
-compile time (bind a variable to a type in C or Java)
-link time
-load time (bind a C or C++ static variable to a memory cell)
-run time (bind a nonstatic local variable to a memory cell)
9. Define static binding and dynamic binding.
A static binding is if it first occurs before run time and remains unchanged throughout program execution. A dynamic binding is if it first occurs during execution or can change during execution of the program.
10. What are the advantages and disadvantages of implicit declarations?
Advantages:
-Can make it easier for the programmer to write code, since he/she doesn’t have to also write the declarations
-Maintainability can be easier too, since the information about a variable’s type is not written down in a part of the program distant from where the variable is used
-Readability can be better since a reader can probably infer the variable’s name or its context
Disadvantage:
-Reliability will probably suffer since the programmer may not always realize the type that the compiler assigned a variable
Problem Set
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. In sub1: sub1
In sub2: sub1
In sub3: main
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.
In sub2: sub1
In sub3: main
b.In sub1: sub1
In sub2: sub1
In sub3: sub1.
In sub2: sub1
In sub3: 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 />");
}
function sub2() {
var x;
x = 10;
sub1();
}
x = 5;
sub2();
*Static scope: x=5
Dynamic scope: x=10
8. Consider the following JavaScript program:
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, assum- ing static scoping is used.
*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)
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:
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, assum- ing static scoping is used.
* Variable Where Declared
In sub1:
a sub1
y sub1
z sub1
x main
In sub2:
a sub2
x sub2
w sub2
y main
z main
In sub3:
a sub3
b sub3
z sub3
w sub2
x sub2
y main
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 vari- able, along with the number of the definition statement that defines it.
* Point 1: a:1 b:2 c:2 d:2
Point 2: a:1 b:2 c:3 d:3 e:3
Point 3: a:1 b:2 c:2 d:2
Point 4: a:1 b:1 c:1
No comments:
Post a Comment