Static (Lexical) Scoping vs Dynamic Scoping (Pseudocode)

后端 未结 3 1083
有刺的猬
有刺的猬 2020-11-29 16:20
Program A()
{
    x, y, z: integer;

    procedure B()
    {
        y: integer;
        y=0;
        x=z+1;
        z=y+2;
    }

    procedure C()
    {
        z:         


        
3条回答
  •  猫巷女王i
    2020-11-29 16:52

    Static scoping and Dynamic scoping are different ways to find a particular variable with a specific unique name in a program written in any language.

    Its specifically helpful for interpreter or compiler to decide on where and how to find the variable.

    Consider code is like below,

    f2(){
    
       f1(){
       }
    
       f3(){
        f1()
       }
    
    }
    

    Static:

    This is basically textual, first variable is defined or not will be checked in local function(lets name it f1()), if not in the local function f1(), then variable will be searched in function f2() that enclosed this function(by this I mean f1()), ...this continues...until variable is found.

    Dynamic:

    This is different from static, in the sense, as it is more runtime or dynamic, first variable is defined or not will be checked in local function,if not in the local function f1(),then variable will be searched in function f3() that called this function(by this I mean f1() again), ...this continues...until variable is found.

提交回复
热议问题