Pregunta

What will be the output of the given pseudo-code using DYNAMIC SCOPING ? Here I want to know what will be the values of x that will be printed.

It is just simple pseudo code in a language that resembles C but has dynamic scoping.

    integer x,y;

    p(integer n){
        x=(n+2)/(n-3);
    }

    q(){
        integer x,y;
        x=3;
        y=4;
        p(y);
        write(x);
    }

    main(){
        x=7;
        y=8;
        q();
        write(x);
    }
¿Fue útil?

Solución 2

With dynamic scope, each identifier has a global stack of bindings. Introducing a local variable with name x pushes a binding onto the global x stack (which may have been empty), which is popped off when the control flow leaves the scope. Evaluating x in any context always yields the top binding. In other words, a global identifier refers to the identifier associated with the most recent environment. Note that this cannot be done at compile time because the binding stack only exists at runtime, which is why this type of scoping is called dynamic scoping. http://en.wikipedia.org/wiki/Scope_(computer_science)

Having read this theory.. if I answer this question using dynamic scoping First when main() is called x=7 and y=8 are pushed in stack then when q() is called it pushes x=3 and y=4 ; now p(n) doesn't have x declared so it will use the last invocation of x as declaration which is in q() and will update its value to '6'.

then when scope of q() will end its values will be popped out of stack and globally invoked values of x=7 and x=8 will remain there so x=7 will be printed.

ANS : 6 and 7 are printed

Otros consejos

Since I first misunderstood your question and provided an answer for C lexical scope, let's keep it as a comparison.

C-style scope

the symbols x and y will correspond to 2 possible variables

Let's call x0, y0 the variables declared at toplevel and xq, yq the ones declared inside q.

x0 = 7
y0 = 8
q -> 
   xq = 3
   yq = 4
   p(4) ->
      x0 = 6 // (4+2)/(4-3)
   write(xq) // OUTPUT: 3
write(x0)    // OUTPUT: 6

dynamic scope

We will show the symbol binding stack to keep track of the symbol current values

x0 = 7 // x:(x0)
y0 = 8 // y:(y0)
q ->        // declaring x,y in q -> x:(x0,xq) y:(y0,yq) 
   xq = 3   // x:(x0,xq)
   yq = 4   // y:(y0,yq)
   p(4) ->
      xq = 6 // x:(x0,xq)
   write(xq) // OUTPUT: 6
             // leaving q -> x:(x0) y:(y0)
write(x0)    // OUTPUT: 7

This example illustrates well the dangerosity/complexity of dynamic scoping:

from within p, the value of x depends on the execution path and, looking at the code, the only way to know whether we are modifying a local or a global variable is to recreate mentally the flow of the program. This can quickly become quite difficult in moderately more complex cases.

Non-dynamic scoping allows to follow the scope of a given identifier much more easily (simply by tracing back the static scope chain).

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top