Dynamic Scoping - Deep Binding vs Shallow Binding

Viewed 34225

I've been trying to get my head around shallow binding and deep binding, wikipedia doesn't do a good job of explaining it properly. Say I have the following code, what would the output be if the language uses dynamic scoping with

a) deep binding

b) shallow binding?

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)

----main starts here---
first()
write_integer(x)
3 Answers

a) In deep binding we deal with the environment of add, in which x refers to the global x and y refers to the y local to first (the last executed function which has a declaration of y)

x (global) = x (global) + y (local) : x = 1 + 3 = 4

b) In shallow binding, we deal with the environment of second, in which x refers to the x local to second and y refers to the y local to first (the last executed function which has a declaration of y)

x (local) = x (local) + y (local) : x = 2 + 3 = 5

BUT : write_integer(x) outputs the global x which is equal to 1

Finally

a) 4

b) 1

Related