------------------------------------------------------------------------------
MC logo
Names and Scope
[^] Chapter Outlines
------------------------------------------------------------------------------
[Ch. 1: Overview and History] [Syntax] [Names and Scope] [Types and Type Systems] [Semantics] [Functions] [Memory Management] [Imperitive Programs and Functional Abstraction] [Modular and Class Abstraction] [Functional Programming] [Logic Programming]
[Java Class Scope] [Ada Int Stack Package] [Ada Int Stack Impl] [Ada Int Stack Driver] [Java Int Stack Class] [Java Int Stack Driver] [C++ Int Stack Class] [C++ Int Stack Impl] [C++ Int Stack Driver]
<<Syntax Types and Type Systems>>
This outline covers chapter four, and mentions a few additional things. You are responsible for all of it.
  1. Legal names.
    1. Characters: Usually letters and numbers, starting with a letter.
    2. Others may be allowed
      1. Underscore is common. COBOL allows a dash.
      2. Use may be limited.
        1. Not at the ends.
        2. Ada allows underscore, but not two in a row.
    3. Case-sensitivity.
    4. Special names
      1. Keywords (e.g. while): Otherwise legal identifiers.
      2. Predefined words (e.g. printf): May be redefined.
      3. Keyword-in-context.
        1. Words that can be identifiers or keywords, depending on placement.
        2. FORTRAN.
      4. Markers distinguish variables: $ in perl and php.
  2. Variables.
    1. Binding: Name, address, type and scope.
    2. l-value and r-value.
    3. Some languages make these uses explicit.
    4. References.
      1. Allows a variable to hold the l-value of another variable.
      2. May (C, C++, most compiled) or may not (Java, many scripting) require explicit deference.
  3. Scope.
    1. The collection of statements which can access that binding, without qualification.
    2. Usual delimiters: {, }, begin, end
    3. Static or dynamic.
      1. Static: determined by program layout.
      2. Dynamic: determined by call order. Obsolete.
    4. Disjoint or nested.
      Pascal Example
      Java Example
    5. Local and non-local references.
    6. Named scopes
      1. Modules and classes.
        1. Ada Integer Stack: Package Implementation Driver.
        2. Java Integer Stack: Class Driver.
        3. C++ Integer Stack: Class Implementation Driver.
      2. Allows qualified reference from outside.
    7. Forward reference.
      1. Usually, definition must precede reference.
      2. Java is an exception.
      3. Declaration before definition.
    8. Usually an implicit global scope.
  4. Resolving References.
    1. Start with current scope and work out through enclosing scopes.
    2. Names declared in a closer scope may hide outer ones of same name.
      1. Hidden name is invisible.
      2. Local declarations may hid parameters.
    3. Compiler keeps declarations in a symbol table.
      1. Usually a separate table for each scope.
      2. Nested scopes make a stack of tables.
  5. Overloading
    1. Giving multiple meanings to the same visible symbol.
    2. Most often functions or operators resolved by argument type.
      Usually not return type, except Ada.
    3. Allowing the user to create overloads is fairly new.
      1. Overloading function names.
      2. Overloading operators (C++ and some others).
  6. Lifetime: The interval during which the variable is allocated some storage.
    1. Typically determined by scope, but a declaration may override.
    2. Static: lifetime of the program.
      1. Global variables.
      2. Declared so, as static in C.
      3. static.c
    3. Local variables: lifetime of their defining scope.
      1. In a function: entry to exit of the enclosing block.
      2. In a class.
        1. Object (usual) variables: Lifetime of the object that contains them.
        2. Static (class) variables: Lifetime of the class itself.
          Often equals the lifetime of the program, but may not.
    4. Dynamic variables: from creation (new, malloc) to deletion (delete, free, gc cleanup).
  7. Closure
    1. Referencing environment: All the variables you can use.
      The sum of your scopes
    2. Closure: Executable code combined with a referencing environment to to run it in.
    3. Lisp lambda, passed function references in some languages.
<<Syntax Types and Type Systems>>