a. What is the referencing environment at location 1 in the program below? Use static scope rules. Include what functions the variables are declared in.
void g ( int w)
{
var b = 1; var x = 8;
void f()
{
varb = 3;
x = b + 1;
print “f: x = “ , x ;
…. < - - - - - - - - - - //Used in Prob a //location 1
}
f();
print “g: w = “ , w ;
print “ g: b = “, b ;
print “g: x = “, x ;
}
void main()
{
var x = 5;
g ( 3);
print “main: x = “, x;
}
b. What is the referencing environment at location 1 in the program below, when the program first gets to location 1? Use dynamic scope rules. Include what functions the variables are declared in.
var a = 20;//global
var x = 17;//global
function sub1( ) {
var x = 7;sub2();
}
function sub2 ( ) {
var y = x +1;a = a + 1;
print “sub2: sum = “, (x +y + a) ;
… < - - - - - - - - - - -//location 1 (used in Problem b)
}
main( ){
var x = 3;var a = 5;sub1(); print “main: a = “, a
}
Problem a:
Static Scoping Rule: If an undeclared variable is encountered, it's definition is checked in the outer block. Note that a block is referred to statements between '{' and '}'.
In the location1, there are 2 variables, b is defined whereas x is undeclared/undefined. The definition of x is checked in the outer block, that is function g. And x is accordingly updated for function g.
Problem b:
Dynamic Scoping Rule: If an undeclared variable is encountered, it's definition is checked in the block from where it is called.
In location 1, we have variables, x, y, and a used in that particular block. Variable y is defined whereas variables x and a are not defined or declared. So their definitions are checked in the block from where they are called. In this code, the calling takes place in this fashion:
main() --> sub1() --> sub2()
Hence, the definition of undefined variables are checked in sub1(). In sub1(), we found the definition of x but not of a. Hence, we will move further to the block from where sub1() is called, that is, main() block. In main block, we found the definition of a. Note that even though there is another definition of variable x in main, we are not going to use it in sub2(). The definition of variable x in sub1() will only be used in sub2().
a. What is the referencing environment at location 1 in the program below? Use static scope...
Consider the following program, written in JavaScript-like syntax: // main program var x, y, z; function sub1() { var a, y, z; . . . } function sub2() { var a, b, z; . . . } function sub3() { var a, x, w; . . . } Given the following calling sequences and assuming that dynamic scoping is used, what variables are visible during execution of the last subprogram activated? Include with each visible variable the name of the...
Consider the following JavaScript skeletal program: //the main program var x: function sub1 () { var x: function sub2 () { } } function sub3 () { } Assume the execution of this program is in the following unit order: main calls sub1 sub1 calls sub2 sub2 calls sub3 a. Assuming static scoping, which declaration of x is the correct one for a reference to x in: i. sub1 ii. sub2 iii. sub3 b. Repeat part a, but assume dynamic...
CONCEPTS OF PROGRAMMING LANGUAGE Consider the following program skeleton Procedure Main is X, Y, Z: Integer; procedure Sub1 is A, Y, Z: Integer; begin -- of Sub1 ... end; -- of Sub1 procedure Sub2 is A, B, Z: Integer; begin -- of Sub2 ... end; -- of Sub2 procedure Sub3 is A, X, W: Integer; begin -- of Sub3 ... end; -- of Sub3 begin - of Main ... end; -- of Main Given the following calling sequences and assuming...
CONCEPTS OF PROGRAMMING LANGUAGE Consider the following skeleton program skeleton. List all the variables, along with the program units where they are declared, that are visible in the bodies of Sub1, Sub2, and Sub 3, assuming static scoping is used. Procedure Main is X, Y, Z: Integer; procedure Sub1 is A, Y, Z: Integer; begin -- of Sub1 ... end; -- of Sub1 procedure Sub2 is A, X, W: Integer; procedure Sub3 is A, B, Z: Integer; begin -- of...
1) Given the following ‘generic’ program. var aVar; aVar = 10; sub1(); function sub1 () { Sub2(); Print (“ aVar = “ aVar + “\n”); } function sub2() { var aVar; aVar = 4; } What would be output under static-scoping rules? What would be output under dynamic-scoping rules? 2) Discuss and compare the following memory allocation strategies for variables, give an example of how Java and/or Python uses each: Stack-Dynamic Explicit Heap-Dynamic Implicit Heap-Dynamic 3) What is a descriptor?...
4.The following questions refer to the skeletal C++ program shown below. a.Assume that static scoping is used. List all variables, along with the functions in which they are declared, that are visible at Line 1 in the program. b. Repeat part (a), but list the variables along with the functions in which they are declared that are visible at Line 2. c. Repeat part (a), but list the variables along with the functions in which they are declared that are...
Given the following Ada program: procedure Main is X, Y: Integer; procedure Sub1 is Y, Z: Integer; begin -- of Sub1 point 1 Sub2; end; -- of Sub1 procedure Sub2 is X: Integer; procedure sub3 (B: Integer) is W: Integer; begin -- of Sub3 point 2 end; -- of Sub3 begin -- of Sub2 point 3 Sub3 (X); end; -- of Sub2 begin -- of Main point 4 Sub1; end; -- of Main For each of the four marked points,...
Write a C++ program that simulates coin tossing. For each toss of the coin the program should print Heads or Tails. The program should toss a coin 100 times. Count the number of times each side of the coin appears and print the results at the end of the 100 tosses. The program should have the following functions as a minimum: void toss() - called from main() and will randomly toss the coin and set a variable equal to the...
How do I do this C++ in a Unix Environment assignment Given dot1m.c 1. The program (dot1m.c) uses mutex to lock and unlock the shared resource (dotstr.sum) for access control as shown below. pthread_mutex_lock (&mutexsum); dotstr.sum += mysum; printf("Thread %ld did %d to %d: mysum=%f global sum=%f\n", offset,start,end,mysum,dotstr.sum); pthread_mutex_unlock (&mutexsum); 2. Modify dot1m.c program to use reader-writer lock (instead of mutex). Replace the codes (for mutex) by the codes (for reader-writer lock). To initialize reader-writer lock, pthread_rwlock_initializer. At the end,...
CODE ONE #include #include using namespace std; string x =" I am global"; // Global x int main() { string x = " I am local"; // Local x cout< cout<<::x< return 0; } CODE TWO #include #include using namespace std; string str = "i am global ";// global int main() { string srt = "i am local to main() ";//local to main() cout << str << "---" << ::str << "\n";// LINE 5555. int x=5; int y=6; if...