Session 3 : Names, Binding, Scope

Name
- If name length are too short, it can't be connotative. For each language, they have their own criteria, for examples FORTRAN 95 that has maximum of 31 characters, C  ++ has no limit, but implementers often impose one, C#, Ada, Java has no limit.
- Names in most programming languages have the same form: a letter followed by a string consisting of letters, digits, and underscore characters ( _ ).
- Names are case sensitive, and the disadvantage is readability which names that look alike are different. Examples : Names base in C languages are case sensitive,   names in other languages are not case sensitive. So, we have to pay attention using the capital letters in C based languages.
- Special characters
  Special words in programming languages are used to make programs more
  readable by naming actions to be performed. They also are used to separate the
  syntactic parts of statements and programs. In most languages, special words are
  classified as reserved words, which means they cannot be redefined by programmers,
  but in some they are only keywords, which means they can be redefined.
  examples :
  1. PHP: all variable names must begin with dollar signs
  2. Perl: all variable names begin with special characters, which specify the variable’s type
  3. Ruby: variable names that begin with @ are instance variables; those that begin with @@ are class variables
Variable => an abstraction of a computer memory cell or collection of cells.
Variables can be characterized as a sextuple of attributes:
- Name
- Address
- Value
- Type
- Lifetime
- Scope

Binding
- Binding is an association between an entity and an attribute, such as between a variable and its type or value, or between an operation and a symbol.
- Binding time is the time at which a binding takes place
- 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
- Advantage: flexibility (generic program units)
- Disadvantages:
- High cost (dynamic type checking and interpretation)
- Type error detection by the compiler is difficult
Variable Attributes : Lifetime.
The lifetime of a variable is the time during which it is bound to a particular memory cell.
It has some categories, such as :
- Static : bound to memory cells before execution begins and remains bound to the same memory cell throughout execution
Advantages: efficiency  (direct addressing), history-sensitive subprogram support
Disadvantage: lack of flexibility  (no recursion)
- Stack-dynamic : Storage bindings are created for variables when their declaration statements are elaborated.
Advantage: allows recursion; conserves storage
Disadvantages:
- Overhead of allocation and deallocation
- Subprograms cannot be history sensitive
- Inefficient references (indirect addressing)
- Explicit heap-dynamic :  Allocated and deallocated by explicit directives, specified by the programmer, which take effect during execution
Advantage: provides for dynamic storage management
Disadvantage: inefficient and unreliable
- Implicit heap-dynamic : Allocation and deallocation caused by assignment statements
Advantage: flexibility (generic code)
Disadvantages:
- Inefficient, because all attributes are dynamic
- Loss of error detection

Scope
One of the important factors in understanding variables is scope. The scope of a variable is the range of statements in which the variable is visible. A variable is visible in a statement if it can be referenced in that statement.
Static scope
- Based on program text
- To connect a name reference to a variable, you (or the compiler) must find the declaration
- Search process: search declarations, first locally, then in increasingly larger enclosing scopes, until one is found for the given name
- Enclosing static scopes (to a specific scope) are called its static ancestors; the nearest static ancestor is called a static parent
- Some languages allow nested subprogram definitions, which create nested static scopes (e.g., Ada, JavaScript, Common LISP, Scheme, Fortran 2003+, F#, and Python)
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

Komentar