Jump to content
Urch Forums

Static/Dynamic Scope


wood

Recommended Posts

 

Consider the following procedure declaration:

  procedure p; 
     x: integer; 
     procedure q; 
        begin x := x+1 end; 
     procedure r; 
        x: integer; 
        begin x := 1; q; write(x) end; 
     begin 
     x:= 2; 
     r 
     end;

What is the output produced by calling p in

1) A language with static scope

2) A language with dynamic scope

 

Link to comment
Share on other sites

int x = 1;

 

procedure P(i) {

int x = 1;

i++;

Q(i);

}

 

procedure Q(j) {

j = j + x;

}

 

 

P(x)

print x

P(x)

print x

 

 

pass by value, Static scope

prints 1 1

pass by value, Dynamic scope

prints 1 1

 

pass by reference, Static scope

prints 4 10

pass by reference, Dynamic scope

prints 3 5

 

Correct???

 

Link to comment
Share on other sites

Originally posted by munnymun

 

hi guys, i'm new here. umm.. I don't understand this question. could you please tell me why X is not just 1. coz when I look at the code, it doesn't change the value of X, so why print X is not just 1. Thanks

 

Wood was asking us to try out his code using dynamic scoping and static scoping. These are explained in depth in Aho's "Dragon Book" concerning compilers.

 

The basic gist is that when you statically scope a variable, its binding is inherited from the enclosing scope. This what Pascal does, for instance.

 

However, when you dynamically scope a variable, its binding is inherited from the calling function, even if that function isn't "outside" this one. For example,

 

function a() {

int x = 2;

b();

}

 

function b() {

print x;

}

 

In function b, x isn't even defined. However, some languages allow you to get along with this. They simply assume that if function a calls function b, then function a has to define x before it can call function b. Then function b will inherit the same variable.

 

That's called "dynamic" scoping.

 

It's called "dynamic"

 

1) Because it makes for a much more exciting practice problem

and

2) Because it cannot be resolved when the code is compiled but must instead be figured out "on the fly" by the language runtime.

 

I hope this helps.

 

Link to comment
Share on other sites

nope I think it is correct as is

Let's see, trace the execution for pass-by-reference, static scoping

 

int x = 1;

procedure P(i) {     (i points to global x = 1)
    int x = 1;
    i++;                (i = global x = 2) 
   Q(i);
}                        (go to print)

procedure Q(j) {  (j points to global x = 2)
  j = j + x;         ( j = globale x = 4) 
}


P(x)       (x is global x)
print x    (output is 4)

I guess you can do the other call of P(x) by yourself

Link to comment
Share on other sites

Originally posted by randhawa

 

Thats what I think happens in dynamic...

Actually I wasn't able to get the ans for dynamic binding (it seems that the global variable is not been changed in the ans you gave... why is that??)

 

Dynamic scope

int x = 1;

 

procedure P(i) { (i points to global x = 1)

int x = 1;

i++; (i = global x = 2)

Q(i);

} (go to print)

 

procedure Q(j) { (j points to global x = 2)

j = j + x; (x = x in P, therefore j = 2 + 1 = 3)

}

 

 

P(x) (x is global x)

print x (output is 3)

 

Link to comment
Share on other sites

  • 5 years later...

procedure Main is

x : Integer ;

procedure sub 1 is

begin -- of sub 1

put(x) ;

end ; --- of sub 1

procedure sub 2 is

x : Integer ;

begin -- of sub 2

x := 10 ;

sub1

end ; --- of sub 2

begin -- of Main

x:= 5;

sub2

end -- of main

 

what value of x is printed in sub 1 ?? ( assumeing ststic scoping rules )

what value of x is printed in sub 1 ?? ( assuming dynamic scoping rules )

 

can anybody help me please???????

 

thank you ...

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...