340 likes | 363 Views
Explore the fundamentals of names, variables, type checking, and scopes in programming languages, covering concepts like bindings, strong typing, and type compatibility. Understand the importance of scope and lifetime management for efficient programming.
E N D
Chapter 5 Names, Bindings, Type Checking, and Scopes
Chapter 5Outline Introduction Names Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope Scope and Lifetime Referencing Environments Named Constants
Names Definition: A name is a string of characters used to identify some entity in a program Design Issues: Maximum Length (Allowed/Significant) Non-Alphanumerics (Alphabet) Case sensitivity (Alphabet) Special words (Use Restriction)
Variables A variable is an abstraction of a memory cell. Practically, it is a sextuple of attributes: name address type value lifetime scope
Variables The sextuple of attributes can be considered to be a triple of pairs: name address type value lifetime scope
Variables The sextuple of attributes can be considered to be a pair of triples: name address type value lifetime scope
Variables The pair of triples is also useful when considering the L-value and R-value : name address = L-value type value = R-value lifetime scope
The Concept of Binding Definition:A binding is an association, such as between an attribute and an entity, or between an operation and a symbol Definition:Binding time is the time at which a binding takes place. Possible binding times: 1. Language design time 2. Language implementation time 3. Compile time 4. Load time 5. Runtime
Type Checking • Definition: Type checking is the activity of verifying and enforcing data type constraints • Operands of an operator must be compatible • with the operator and • with each other • Type checking may occur either • at compile-time (static type checking), or • at run-time (dynamic type checking). • Definition: A type error is a violation of type constraints, frequently the application of an operator to an operand of an inappropriate type
Type Checking If a language enforces type rules strongly (that is, generally allowing only those automatic type conversions which do not lose information), one can refer to the process as strongly typed, if not, as weakly typed. Definition: A compatible type is one that is either legal for the operator, or is allowed under language rules to be implicitly converted, by compiler-generated code, to a legal type (coercion)
Strong Typing • Definition: • A programming language is strongly typed if • each name in a program has a single data type associated with it, and • that type is known at compile time • Advantages: • This allows detection of type errors.
Strong Typing Language Examples • FORTRAN 95, C, C++ not strongly typed • Ada, Java, C# almost strongly typed • Pascal most strongly typed • Coercion rules greatly affect strong typing: • They can weaken it considerably • C++ versus Ada • Although Java has just half the assignment coercions of C++, its strong typing is still far less effective than that of Ada
Type Compatibility There are two different kinds of type compatibility: Type Compatibility by Name Type Compatibility by Structure
The Two Kinds of Type Compatibility Definition: Two variables are type compatible by name if they are either in the same declaration or in declarations that use the same type name - Easy to implement but highly restrictive Definition: Two variables are type compatible by structure if their types have identical structures - More flexible, but harder to implement
Type Compatibility by Structure • Consider the problem of two structured types: • What if they are circularly defined? • Are two record types compatible if they are structurally the same but use different field names? • Are two array types compatible if they have the same cardinality but the subscripts are different? • (e.g. [1..10] and [-5..4]) • Are two enumeration types compatible if their components are spelled differently?
Type Compatibility With structural type compatibility, you cannot differentiate between types of the same structure Example: different units of speed Most speed units are stored as float Miles per hour Feet per second Meters per second Inches per minute Furlongs per fortnight
Type Compatibility Language examples • C: structure, except for struct • Ada: restricted form of name • Derived types allow types with the same structure to be different • Anonymous types are all unique, even in: • A,B: array (1..10) of INTEGER; • C++: name for everything except objects • Java, C++, C# - object compatibility – Chapter 12
Scope • Static Scope • Blocks • Evaluation of Static Scoping • Dynamic Scope • Evaluation of Dynamic Scoping
Scope Definitions The scope of a variable is the range of statements over which it is visible The nonlocal variables of a program unit are those that are visible but not declared there
Static Scope Based on program text Created by nested subprograms and/or blocks To connect a name reference to a variable, find the declaration Enclosing static scopes (to a specific scope) are called its static ancestors The nearest static ancestor is the static parent
Scope Hiding Variables can be hidden from a unit by having a "closer" variable with the same name Both C++ and Ada allow access to these "hidden" variables In Ada: unit.name In C++: class_name::name
Blocks A method of creating static scopes inside program units First used in ALGOL 60 Examples: C, C++, Java, C#: for(...){ int index; ... } Ada: declare LCL:FLOAT; begin ... end
Static Scope Example Assume MAIN calls A and B A calls C and D B calls A and E
Static Scope Example Assume MAIN calls A and B A calls C and D B calls A and E
Evaluation of Static Scoping Suppose the spec is changed: now D must access some data in B Solutions 1. Put D in B C can no longer call D D cannot access A's variables 2. Move the data from B that D needs to MAIN but then all procedures can access them Same problem for procedure access! Evaluation static scoping often encourages many globals
DynamicScope Based on calling sequences of program units, not their textual layout (temporal versus spatial) Example: Ada Big - declaration of x Sub1 - declaration of x ... call Sub2 ... Sub2 ... - reference to x ... ... call Sub1 ... Big calls Sub1 Sub1 calls Sub2 Sub2 uses x Evaluation Advantage convenience Disadvantage poor readability
Scope and Lifetime Related or Unrelated? Scope and lifetime are sometimes closely related, but are different concepts!! Consider a static variable in a C or C++ function
Referencing Environments Definition: The referencing environment of a statement is the collection of all names that are visible in the statement In a static-scoped language, it is the local variables, plus all visible variables in all enclosing scopes In a dynamic-scoped language, it is the local variables, plus all visible variables in all active subprograms A subprogram is active if its execution has begun but has not yet terminated
Named Constants Definition: a variable that is bound to a value only when it is bound to storage Used to parameterize programs The binding of values to named constants can be either static (called manifest constants) or dynamic Advantages: readability and modifiability Examples Pascal literals only FORTRAN 95 constant-valued expressions Ada, C++, Java expressions of any kind
Variable Initialization Definition The binding of a variable to a value at the time it is bound to storage is called initialization Examples Ada: SUM:FLOAT := 0.0; Java: int sum = 0;
Summary • Case sensitivity and the relationship of names to special words represent design issues of names • Variables are characterized by the sextuples: { name, address, value, type, lifetime, scope } • Binding is the association of attributes with program entities • Scalar variables are categorized as: • 1) static • 2) stack dynamic • 3) explicit heap dynamic • 4) implicit heap dynamic • Strong typing means detecting all type errors