This outline covers chapter four, and mentions a few additional
things. You are responsible for all of it.
- Legal names.
- Characters: Usually letters and numbers, starting with a letter.
- Others may be allowed
- Underscore is common. COBOL allows a dash.
- Use may be limited.
- Not at the ends.
- Ada allows underscore, but not two in a row.
- Case-sensitivity.
- Special names
- Keywords (e.g. while): Otherwise legal identifiers.
- Predefined words (e.g. printf): May be redefined.
- Keyword-in-context.
- Words that can be identifiers or keywords, depending on placement.
- FORTRAN.
- Markers distinguish variables: $ in perl and php.
- Variables.
- Binding: Name, address, type and scope.
- l-value and r-value.
- Some languages make these uses explicit.
- References.
- Allows a variable to hold the l-value of another variable.
- May (C, C++, most compiled) or may not (Java, many scripting)
require explicit deference.
- Scope.
- The collection of statements which can access that binding, without
qualification.
- Usual delimiters: {, }, begin, end
- Static or dynamic.
- Static: determined by program layout.
- Dynamic: determined by call order. Obsolete.
- Disjoint or nested.
Pascal
Example
Java Example
- Local and non-local references.
- Named scopes
- Scopes holding function bodies and control blocks are anonymous.
- Some types of scope are named.
- Classes. Contain data and operations defining a type, usually
used to create an ADT.
- Modules.
- Contain and group type declarations and operations
on those types.
- Is not a itself type.
- Name spaces. Contains any set of declarations, usually to
contain a library.
- Terms are not fixed.
- Name spaces and modules differ mainly by intended use.
They basically do the same thing.
- Modules usually have some form of access control, while
name spaces usually do not.
- Java packages are modules under the above terminology.
- Ada has packages which are modules.
- Some scripting languages (Python in particular) treat a
file as a name space, and call it a module.
- Allows qualified reference from outside.
- Modules class examples.
- Ada Integer Stack: Package
Implementation
Driver.
- Java Integer Stack: Class
Driver.
- C++ Integer Stack: Class
Implementation
Driver.
- Forward reference.
- Usually, definition must precede reference.
- Java is an exception.
- Declaration before definition.
- Usually an implicit global scope, usually with some
restrictions on what can go there.
- Resolving References.
- Start with current scope and work out through enclosing scopes.
- Names declared in a closer scope may hide outer ones of same name.
- Hidden name is invisible.
- Local declarations may hid parameters.
- Compiler keeps declarations in a symbol table.
- Usually a separate table for each scope.
- Nested scopes make a stack of tables.
- Overloading
- Giving multiple meanings to the same visible symbol.
- Most often functions or operators resolved by argument type.
Usually not return type, except Ada.
- Allowing the user to create overloads is fairly new.
- Overloading function names.
- Overloading operators (C++ and some others).
- Lifetime: The interval during which the variable is allocated some storage.
- Typically determined by scope, but a declaration may override.
- Static: lifetime of the program.
- Global variables.
- Declared so, as static in C.
- C Static Declaration
- Local variables: lifetime of their defining scope.
- In a function: entry to exit of the enclosing block.
- In a class.
- Object (usual) variables: Lifetime of the object that contains them.
- Static (class) variables: Lifetime of the class itself.
Often equals the lifetime of the program, but may not.
- Dynamic variables: from creation (new, malloc) to
deletion (delete, free, gc cleanup).
- Closure
- Referencing environment: All the variables you can use.
The sum of your scopes
- Closure: Executable code combined with a referencing environment to
to run it in.
- Lisp lambda, passed function references in some languages.