Question

a. What is the referencing environment at location 1 in the program below? Use static scope...

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

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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().

Add a comment
Know the answer?
Add Answer to:
a. What is the referencing environment at location 1 in the program below? Use static scope...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider the following program, written in JavaScript-like syntax: // main program var x, y, z; function...

    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...

    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...

  • 1) Given the following ‘generic’ program. var aVar; aVar = 10; sub1(); function sub1 () {  &...

    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?...

  • Given the following Ada program: procedure Main is X, Y: Integer; procedure Sub1 is Y, Z:...

    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...

    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...

    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...

    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...

  • Using the program segment below, write two short functions to complete the program. Use the test...

    Using the program segment below, write two short functions to complete the program. Use the test cases to ensure the program works properly. Prototypes: void int2bin(int, int[8]); The first function will convert an integer that is between 0 - 255 to its binary representation. You are to store the binary number in the array passed to the function. void printBinary(int[8]); The second function will print the binary number stored in the array passed to the function. XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Test 1: 13...

  • program. 13. What's the output of the following program? #include< iostream> #include&math> using namespace std; int...

    program. 13. What's the output of the following program? #include< iostream> #include&math> using namespace std; int p 7; void main) extern double var int p abs(-90); system( "pause"); double var = 55; 14. How many times does "#" print? forlint i 0;j< 10; +ti) if(i-2141 6) continue; cout<< "#"; 15. Write the function declaration/prototype for a, pow function b floor function 16. State True or False a. b. c. d. e. isupper function returns a double value for loop is...

  • A) Fix any errors to get the following program to run in your environment.               B)...

    A) Fix any errors to get the following program to run in your environment.               B) Document each line of code with comments and describe any changes you had to make to the original code to get it to work. C) Write a summary of what your final version of the program does. You may also add white space or reorder the code to suit your own style as long as the intended function does not change. Program 3 #include...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT