Kamis, 28 Desember 2017

Names, Bindings, and Scopes

Names

Length
If too short, they cannot be connotative
Language examples:
FORTRAN 95: maximum of 31
C99: no limit but only the first 63 are significant; also, external names are limited to a maximum of 31
C#, Ada, and Java: no limit, and all are significant
C++: no limit, but implementers often impose one

Special character
PHP: all variable names must begin with dollar signs
Perl: all variable names begin with special characters, which specify the variable’s type
Ruby: variable names that begin with @ are instance variables; those that begin with @@ are class variables
Case sensitivity
Disadvantage: readability (names that look alike are different)
Names in the C-based languages are case sensitive
Names in others are not
Worse in C++, Java, and C#  because predefined  names are mixed case  (e.g. IndexOutOfBoundsException)

Possible Binding Times

Language design time --  bind operator symbols to operations
Language implementation time-- bind floating point type to a representation
Compile time -- bind a variable to a type in C or Java
Load time -- bind a C or C++ static variable to a memory cell)
Runtime -- bind a nonstatic local variable to a memory cell

Dynamic Type Binding

Dynamic Type Binding (JavaScript, Python, Ruby, PHP, and C# (limited))
Specified through an assignment statement e.g., JavaScript  
list = [2, 4.33, 6, 8];
  list = 17.3;
Advantage: flexibility (generic program units)
Disadvantages:

High cost (dynamic type checking and interpretation)
Type error detection by the compiler is difficult     

 Scope

The scope of a variable is the range of statements over which it is visible
The local variables of a program unit are those that are declared in that unit
The nonlocal variables of a program unit are those that are visible in the unit but not declared there
Global variables are a special category of nonlocal variables
The scope rules of a language determine how references to names are associated with variables
Variables can be hidden from a unit by having a "closer" variable with the same name
Ada allows access to these "hidden" variables
E.g.,  unit.name


Global Scope


C, C++, PHP, and Python support a program structure that consists of a sequence of function definitions in a file
These languages allow variable declarations to appear outside function definitions
C and C++have both declarations (just attributes) and definitions (attributes and storage)
            –A declaration outside a function definition specifies that it is defined in another file
PHP
Programs are embedded in HTML markup documents, in any number of fragments, some statements and some function definitions
The scope of a variable (implicitly) declared in a function is local to the function
The scope of a variable implicitly declared outside functions is from the declaration to the end of the program, but skips over any intervening functions
Global variables can be accessed in a function through the $GLOBALS array or by declaring it global


Dynamic Scope

Based on calling sequences of program units, not their textual layout (temporal versus spatial)
References to variables are connected to declarations by searching back through the chain of subprogram calls that forced execution to this point

Scope Example  


                  function big() {
                      function sub1()
                         var x = 7;
                      function sub2() {
                        var y = x;
                      }
                     var x = 3;
                   }
Static scoping
Reference to x in sub2 is to big's x
Dynamic scoping
Reference to x in sub2 is to sub1's x
                


Reference
Robert W. Sebesta - Concept of Programming Languages (Tenth Edition), Chapter 5






Tidak ada komentar:

Posting Komentar