Download presentation
Presentation is loading. Please wait.
1
Type Checking, and Scopes
Chapter 5 Names, Bindings, Type Checking, and Scopes
2
Chapter 5 Outline Introduction Names Variables The Concept of Binding
Type Checking Strong Typing Type Compatibility Scope Scope and Lifetime Referencing Environments Named Constants Type Checking Strong Typing Type Compatibility Scope Static Scope Blocks Evaluation of Static Scoping Dynamic Scope Evaluation of Dynamic Scoping Scope and Lifetime Referencing Environments Named Constants
3
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)
4
Variables A variable is an abstraction of a memory cell.
Practically, it is a sextuple of attributes: name address type value lifetime scope
5
Variables The sextuple of attributes can be
considered to be a triple of pairs: name address type value lifetime scope
6
Variables The sextuple of attributes can be
considered to be a pair of triples: name address type value lifetime scope
7
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
8
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
9
The Four Categories of Variables (by Lifetimes)
Static Bound to memory cells before execution begins and remains bound to the same memory cell throughout execution. Stack- dynamic Storage bindings are created for variables when their declaration statements are elaborated. Explicit heap- Allocated and deallocated by explicit directives, specified by the programmer, which take effect during execution. Implicit Allocation and deallocation caused by assignment statements.
10
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
11
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)
12
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.
13
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
14
Type Compatibility by Name Type Compatibility by Structure
There are two different kinds of type compatibility: Type Compatibility by Name Type Compatibility by Structure
15
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 Two variables are type compatible by structure if their types have identical structures - More flexible, but harder to implement
16
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?
17
Furlongs per fortnight
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
18
A,B: array (1..10) of INTEGER;
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
19
Scope Static Scope Blocks Evaluation of Static Scoping Dynamic Scope
Evaluation of Dynamic Scoping
20
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
21
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
22
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
23
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
24
Assume MAIN calls A and B
Static Scope Example Assume MAIN calls A and B A calls C and D B calls A and E
25
Assume MAIN calls A and B
Static Scope Example Assume MAIN calls A and B A calls C and D B calls A and E
26
Static Scope Example
27
Static Scope Example
28
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
29
Dynamic Scope Based on calling sequences of program units,
not their textual layout (temporal versus spatial) Example: Ada Big - declaration of x Sub1 ... call Sub2 Sub2 - reference to x call Sub1 Big calls Sub1 Sub1 calls Sub2 Sub2 uses x Evaluation Advantage convenience Disadvantage poor readability
30
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
31
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, in all active subprograms A subprogram is active if its execution has begun but has not yet terminated
32
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
33
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;
34
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.