8,035 views
4 votes
4 votes

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$

3 Answers

Best answer
16 votes
16 votes

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

edited by
1 votes
1 votes

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

0 votes
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 !
Answer:

Related questions

8 votes
8 votes
3 answers
1
makhdoom ghaya asked Apr 25, 2016
6,572 views
Which of the following productions eliminate left recursion in the productions given below:$S \rightarrow Aa \mid b$$A \rightarrow Ac \mid Sd \mid \epsilon$$S \rightarrow...
5 votes
5 votes
1 answer
2
makhdoom ghaya asked Apr 25, 2016
4,438 views
Shift reduce parsing belongs to a class ofBottom up parsing.Top down parsing.Recursive parsing.Predictive parsing.
5 votes
5 votes
2 answers
4