开发者

Static and Dynamic Scoping Problem

开发者 https://www.devze.com 2023-02-02 11:11 出处:网络
I\'m solving following code in Static and Dynamic Scoping. I got following answer but I need someone to confirm if I\'m correct or not since I\'m a bit confusing. I really appreciate if anyone can exp

I'm solving following code in Static and Dynamic Scoping. I got following answer but I need someone to confirm if I'm correct or not since I'm a bit confusing. I really appreciate if anyone can explain in simple way!

Static  =>开发者_Go百科; (1)8  (2)27
Dynamic => (1)10 (2)27

proc main
var x,y,z;
    proc sub1
        var x,z
        x := 6;
        z := 7;
        sub2;
        x := y*z + x;
        print(x);   ---- (2)
    end;
    proc sub2
        var x,y
        x := 1;
        y := x+z+2;
        print(y);   ---- (1)
    end;
begin
    x := 1; y:=3; z:=5;
    sub1;
end 


Scoping defines the rules by which the variable identifiers in your code (i.e. names) are mapped to the actual variables.

Static (or lexical) scoping determines the mapping, by which variable names are mapped based on the program code structure. First the immediate scope (i.e. the block between proc..end in your pseudo-language), that is the scope where the variable is referenced, is searched for the variable declaration. If it's there, the name is bound to this variable. If it's not, the name is looked up in the parent scope (based on the code structure - i.e. on the program text), in the grandparent, etc.

In your case, in sub2, in the expression x+z+2, x always refers to the local variable (defined in the same scope), z always refers to the variable defined in the main proc - this mapping does not depend on the actual program execution. Therefore, with static scoping, x refers to local variable with value of 1, z refers to the variable in main with the value of 5, so the result is 5+1+2=8. Note that in sub1, x and z are local variables - i.e. z := 7 does not change the z in main.

Dynamic scoping is like the static scoping in that it first looks for the variable in the immediate scope, and then continues to look in the scope ancestors. However, while with static scoping the scope hierarchy is determined by program text, with dynamic scoping the hierarchy is determined by the hierarchy of subroutine calls your program makes - i.e. scope hierarchy and callstack are alike.

With dynamic scoping, at the point (1), the callstack is as follows: main -> sub1 -> sub2. Thus the names in sub2 are first looked up in sub2 (the x name resolves to x local variable, as with static scoping), then in sub1 (the z name resolves to the z variable in sub1), then in main.

So x resolves to the variable with value 1, z resolves to the variable from sub2 with the value 7, and the result is 10. Finally, the y in sub2 is looked in sub2, then in main (because the call stack is main -> sub2), so the expression result at (2) is 3*7+6=27 - this is the same as with static scoping because the static (lexical) scope hierarchy and the dynamic (call-based) hierarchy are equal at this point of execution.

0

精彩评论

暂无评论...
验证码 换一张
取 消