The Gateway to Computer Science Excellence
+3 votes
4.1k views

Consider the following psuedocode:

x: integer := 1  
y: integer := 2   
procedure add  
    x:= x + y    
procedure second (P: Procedure)  
    x: integer := 2      
    p()   
procedure first  
    y: integer := 3
    second (add) 
first () 
write_integer(x)

What does it print if the language uses dynamic scoping with deep binding?

  1. $2$
  2. $3$
  3. $4$
  4. $5$
in Compiler Design by Boss (30.8k points) | 4.1k views

3 Answers

+13 votes
Best answer

Deep binding binds the environment at the time the procedure is passed as an argument - relevant only for functional languages where functions can be passed as parameter to functions. 

x: integer := 1  
y: integer := 2   
procedure add  
    x:= x + y    
procedure second (P: Procedure)  
    x: integer := 2      
    p()   
procedure first  
    y: integer := 3
    second (add) 
first () 
write_integer(x)

Here, execution starts from line number 11. first() is called and control goes to line number 8. Here a new variable "y" is created (this will hide the global variable "y"). Now second() is called and function "add" is passed as an argument. Due to deep-binding, the scope of variables in "add" gets assigned here. So, usage of "y" in "add" will correspond to the "y" declared in "first" and not the global "y" (if a variable named "y" is declared in "add" that gets preference as usual). Same for variable "x". Now, control goes to "second". A variable "x" is declared here and function p, which is "add" is called. Had we followed shallow binding, due to dynamic scoping, "x" in add will be the "x" in the calling function which is having value 2. But due to deep binding, "x" is already binded to the "global x". So, in function add, global "x" will be updates as $1+3 = 4.$ 

While Shallow binding binds the environment at the time the procedure is actually called.

Shallow binding just traverses up the activation records until it finds the nearest variable that corresponds to the name.

So, for shallow binding, the update the value as  $2+3=5$.

Using dynamic scoping with deep binding value of $x$ will be $4$.

by Boss (38.7k points)
edited by
0

As per the key Provided by ISRO - 2013

Answer is C

0
yes off course 4 .i mistakenly write the result of shallow binding.
0
@manoj what are the sequene of function calls can u plz tell me...i m not able to cache
0
Why in case of Shallow binding answer is not x = 2+2=4 because at the time of calling function add(), x =2 and y =2 (the global variable). Why y is taken as 2
0
Please correct me if I am wrong....

since from this function call second(add);

add is passed passed as an argument and at that time it will ask for the variable x and because variable at this time is not available in calling functin i.e. first, so it will directly access the global variable x.

and when it will ask for y it is already present in the calling function i.e. first, so it will access the variable y of first.

and hence the answer is 1 + 3 = 4.

ryt ????
0
@Manoj if shallow binding is used, then the 'x' variable of second() will be updated to 5. But write_integer(x) prints the global variable 'x' whose value is '1'. So with shallow binding the output should be '1' right?
+1 vote

Deep binding binds the environment at the time the procedure is passed as an argument

Shallow binding binds the environment at the time the procedure is actually called

So for dynamic scoping with deep binding when add is passed into second the environment is x = 1, y = 3 and the x is the global x so it writes 4 into the global x, which is the one picked up by the write_integer.

 

Answer is C

by Active (3.9k points)
0
what is the sequence of function calls can u pplz tell me ?
0 votes
It is 5  I think :O

Deep access method find the value of non local variables in the First Activation record that would be  coming on the stack containing it !
by Loyal (9.9k points)
Answer:

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,737 questions
57,324 answers
198,404 comments
105,169 users