Consider the following code:
int a:=10 //global int b:=12 //global proc F a:= a-b proc P (M:proc) int a:=2 M() proc K int b:=3 P(F) K() //main program print(a) //built in function
a- what does this code print if it uses dynamic scoping and deep binding?
b- what does this code print if it uses dynamic scoping and shallow binding?
Solution:
Let's have a definition of deep binding and shallow binding first.
Deep binding:
Deep binding captures the environment at the point. and it takes the environment of the parent function.
Shallow binding:
Shallow binding takes the environment of the final calling function.
a)
int a:=10 //global int b:=12 //global proc F a:= a-b proc P (M:proc) int a:=2 M() proc K int b:=3 P(F) K() //main program print(a) //built in function
main method is where K() is called Now in the K method body b of int type is defined and initialized with the value 3.
Now F is called now a=a-b, here the value of a and b will be called from parent function which means the global value of a and b which is 10 and 12 respectively. now a= -2.
now the control is back to the main an print(a) will print -2.
b)
Here also
main method is where K() is called Now in the K method body b of int type is defined and initialized with the value 3.
Now F is called now a=a-b, here the value of a and b will be called from the function which called the function where we are at; this means the value of b will be taken from the calling function which is 3 and the value of a will be taken from the global value which is 10 now a= 7
now the control is back to the main an print(a) will print 7.
Hit the thumbs up if you liked the answer. :)
Consider the following code: int a:=10 //global int b:=12 //global proc F a:= a-b proc P...
Consider the following pseudocode which uses dynamic scoping. What does the program print if the language uses shallow binding? What does it print with deep binding? x: integer//global procedure print_x write_integer (x) procedure first x:= x * 3 procedure second (F: procedure) x: integer x: = 5 F () print_x () x: = 7 second (first) print_x ()
Question 7 (1 point) Suppose the program below outputs 10 30. The language uses int vari int var2 = 10 40 def bar() : print varl var2 def foo (f): int var2 = 30 f() def qux(): int varl = 20 foo (bar) # execution starts here qux () # execution starts here qux ( ) dynamic scoping with shallow binding lexical scoping dynamic scoping with deep binding None of these options
2. Consider the following programs (using C's syntax): #include <stdio.h> int a- 1, b-2; int foo (int x) 1 return (x+a); void ba r () { printf("%d, %d\n",a,b); void foobar) } printf("%d, %d\n", a, b) ; int a -4; bar bfoo (b); bar int main)( int b 8; foobar printf ("%d, %d\n", a, b) ; return 0; (a) What does the program print under static scoping? (b) What does the program print under dynamic scoping?
Consider the following program written in C syntax int a , b , c ; // first declaration void g() { print(a,b,c); } int f(int a) // parameter declaration { int b; // second declaration b = a + 1; g(); // first call { int a; // third declaration int c; // fourth declaration c = b; a = b + c; g(); // second call } g(); // third call return a + b ; } int main()...
x: integer -- global procedure second x: integer --local x:= 2 second --main program x:= 0 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?
Consider the following pseudocode Xinteger procedure set x(n global integer) Xin procedure print x write integer(x) procedure first set x(1) print x procedure second xinteger set x(2) print x set x(e) first() print x second print x What does this program print if the language uses static scoping? What does it print with dynamic scoping? Why?
Consider the following pseudocode. int a = 9; //global variable void go {a = a +2; print a; } main { int a = 4; 80: a = a +1; print a; } a. What is printed out if we are using static scope rules? Show runtime stack. b. What is printed out if we are using dynamic scope rules? Show calling tree.
8. (10%) Consider the following JavaScript program: // The main program var x,y; function f10{ var y, z; function f20{ var x, Z, P; function f30{ var x, z, ; Assume that the execution of this program is in the following unit order: main calls f3, f3 calls f1, f1 calls 12. a) Assuming that static scoping is in effect, indicate which version of each of the following variables will be visible in f2 by filling in each blank with...
(10pts). Consider the following: void fun1(void); void fun2(void); void fun3(void); void main() { Int a,b,c; … } void fun1(void){ Int b,c,d; … } void fun2(void){ Int c,d,e; … } void fun3(void){ Int d,e,f; … } Given the following calling sequences and assuming that dynamic scoping is used. What variables are visible during execution of the last function called? Include with each visible variable the name of the function in which it was...
B) The following code is given. Determine the outputs if scoping is static and dynamic. static scope output dynamic scope output #include <stdio.h> void function1(int); void function(int); int function(void); intx=10; main() { functioni(x); function2x); void function1(int y) { int x=y+5; printf("x=%d in function1 \n",function()); void function(int y){ int x=y+10; printf("x=%d in function1 \n", function()); int function(){ return x;