Skip to main content

What is lexical scope?


I want a brief intro to lexical scope



Source: Tips4allCCNA FINAL EXAM

Comments

  1. I understand them through examples :)

    Static or Lexical Scope, in C-like syntax ::

    void fun()
    {
    int x = 5;

    void fun2()
    {
    printf("%d", x);
    }
    }


    every inner level can access its outer levels.

    there is another way, called Dynamic Scope used by first implementation of Lisp,
    again in C-like Syntax ::

    void fun()
    {
    printf("%d", x);
    }

    void dummy1()
    {
    int x = 5;

    fun();
    }

    void dummy2()
    {
    int x = 10;

    fun();
    }


    here fun can either access x in dummy1 or dummy2, or any x in any function that call fun with x declared in it.

    dummy1();


    will print 5

    dummy2();


    will print 10

    the first one is called static because it can be deduced at compile-time,
    the second is called dynamic because the the outer scope is dynamic and depends on the chain call of the functions.

    I find static scoping easier for the eye. Most languages went this way eventually even Lisp (can do both, right?). Dynamic scoping is like passing references of all variables to the called function.
    An example of why the compiler can not deduce the outer dynamic scope of a function, consider our last example, if we write something like this ::

    if(/* some condition */)
    dummy1();
    else
    dummy2();


    the call chain depends on a run time condition. If it is true, then the call chain looks like ::

    dummy1 --> fun()


    If the condition is false ::

    dummy2 --> fun()


    The outer scope of fun in both cases is the caller plus the caller of the caller and so on.

    Just to mention that C language does not allow nested functions nor dynamic scoping.

    ReplyDelete
  2. Lexical (AKA static) scoping refers to determining a variable's scope based solely on its position within the textual corpus of code. A variable always refers to its top-level environment. It's good to understand it in relation to dynamic scope.

    ReplyDelete
  3. Scope defines the area, where functions, variables and such are available. The availability of a variable for example is defined within its the context, let's say the function, file, or object, they are defined in. We usually call these local variables.

    The lexical part means that you can derive the scope from reading the source code.

    Lexical scope is also known as static scope.

    Dynamic scope defines global variables that can be called or referenced from anywhere after being defined. Sometimes they are called global variables, even though global variables in most programmin languages are of lexical scope. This means, it can be derived from reading the code that the variable is available in this context. Maybe one has to follow a uses or includes clause to find the instatiation or definition, but the code/compiler knows about the variable in this place.

    In dynamic scoping, by contrast, you search in the local function first, then you search in the function that called the local function, then you search in the function that called that function, and so on, up the call stack. "Dynamic" refers to change, in that the call stack can be different every time a given function is called, and so the function might hit different variables depending on where it is called from. (see here)

    To see an interesting example for dynamic scope see here.

    For further details see here and here.

    Some examples in Delphi/Object Pascal

    Delphi has lexical scope.

    unit Main;
    uses aUnit; // makes available all variables in interface section of aUnit

    interface

    var aGlobal: string; // global in the scope of all units that use Main;
    type
    TmyClass = class
    strict private aPrivateVar: Integer; // only known by objects of this class type
    // lexical: within class definition,
    // reserved word private
    public aPublicVar: double; // known to everyboday that has access to a
    // object of this class type
    end;

    implementation

    var aLocalGlobal: string; // known to all functions following
    // the definition in this unit

    end.


    The closest Delphi gets to dynamic scope is the RegisterClass()/GetClass() function pair. For its use see here.

    Let's say that the time RegisterClass([TmyClass]) is called to register a certain class cannot be predicted by reading the code (it gets called in a button click method called by the user), code calling GetClass('TmyClass') will get a result or not. The call to RegisterClass() does not have to be in the lexical scope of the unit using GetClass();

    Another possibility for dynamic scope are anonymous methods (closures) in Delphi 2009, as they know the variables of their calling function. It does not follow the calling path from there recursively and therefore is not fully dynamic.

    ReplyDelete
  4. Lets try the shortest possible definition:

    Lexical Scoping (aka Closure) defines how variable names are resolved in nested functions: inner functions contain the scope of parent functions even if the parent function has returned.

    That's all there is to it! To help myself understand what this means, I wrote an in depth blog post about function scope and lexical scoping in JavaScript which can be found here. Maybe this could serve someone else too.

    ReplyDelete
  5. Lexical scoping: Variables declared outside of a function are global variables and are visible everywhere in a JavaScript program. Variables declared inside a function have function scope and are visible only to code that appears inside that function.

    ReplyDelete
  6. Here is an explanation in the case of R.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex