Presentation is loading. Please wait.

Presentation is loading. Please wait.

ISBN 0-321-49362-1 Chapter 5 Names, Bindings, Type Checking, and Scopes.

Similar presentations


Presentation on theme: "ISBN 0-321-49362-1 Chapter 5 Names, Bindings, Type Checking, and Scopes."— Presentation transcript:

1 ISBN 0-321-49362-1 Chapter 5 Names, Bindings, Type Checking, and Scopes

2 Copyright © 2007 Addison-Wesley. All rights reserved.1-2 Chapter 5 Topics Introduction Names Variables The Concept of Binding Type Checking Strong Typing Type Equivalence Scope Scope and Lifetime Referencing Environments Named Constants

3 Copyright © 2007 Addison-Wesley. All rights reserved.1-3 Introduction Imperative languages are abstractions of von Neumann architecture –Memory –Processor A variable is an abstraction of a memory cell Variables can be characterized as a sextuple of attributes: –Name –Address –Value –Type –Lifetime –Scope To design a type, must consider scope, lifetime, type checking, initialization, and type compatibility

4 Copyright © 2007 Addison-Wesley. All rights reserved.1-4 Variables Attributes – (Name, Address, Value, Type, Lifetime, Scope) Design issues for names: –Case sensitivity Disadvantage: readability (names that look alike are different) –Names in the C-based languages are case sensitive –Names in others are not –Names in C++, Java, and C# have predefined names with mixed case –Length If too short, they cannot be connotative Language examples: –FORTRAN I: maximum 6 –COBOL: maximum 30 –FORTRAN 90 and C89: maximum 31 –C99: maximum 63 –C#, Ada, and Java: no limit, and all are significant –C++: no limit, but implementers often impose one –Special words -- reserved words or keywords? An aid to readability; used to delimit or separate statement clauses –A keyword is a word that is special only in certain contexts, e.g., in Fortran »Real VarName ( Real is a data type followed with a name, therefore Real is a keyword) »Real = 3.4 ( Real is a variable) A reserved word is a special word that cannot be used as a user-defined name Potential problem with reserved words: If there are too many, many collisions occur (e.g., COBOL has 300 reserved words!)

5 Copyright © 2007 Addison-Wesley. All rights reserved.1-5 Variables Attributes – (Name, Address, Value, Type, Lifetime, Scope) Address - the memory address with which it is associated –A variable may have different addresses at different times during execution –A variable may have different addresses at different places in a program –If two variable names can be used to access the same memory location, they are called aliases Aliases are created via pointers, reference variables, C and C++ unions Aliases can be harmful to readability (program readers must remember all of them)

6 Copyright © 2007 Addison-Wesley. All rights reserved.1-6 Variables Attributes – (Name, Address, Value, Type, Lifetime, Scope) Value - the contents of the location with which the variable is associated - The l-value of a variable is its address - The r-value of a variable is its value Abstract memory cell - the physical cell or collection of cells associated with a variable

7 Copyright © 2007 Addison-Wesley. All rights reserved.1-7 Variables Attributes – (Name, Address, Value, Type, Lifetime, Scope) Type - determines the range of values of variables and the set of operations that are defined for values of that type; in the case of floating point, type also determines the precision Type Binding –How is a type specified? –When does the binding take place?

8 Copyright © 2007 Addison-Wesley. All rights reserved.1-8 First -- The Concept of Binding… A binding is an association, such as between an attribute and an entity, or between an operation and a symbol Binding time is the time at which a binding takes place. –Static Binding A binding is static if it first occurs before run time and remains unchanged throughout program execution. –Dynamic Binding A binding is dynamic if it first occurs during execution or can change during execution of the program

9 Copyright © 2007 Addison-Wesley. All rights reserved.1-9 Possible Binding Times Language design time –Binding of an operator symbol to its operation Language implementation time –Binding of the floating point data type to a specific representation Compile time –Binding of a variable to a type as in C or Java Load time –Binding of a variable to a memory cell as with C or C++ static variables Runtime –Binding of a a non static local variable to a memory cell

10 Copyright © 2007 Addison-Wesley. All rights reserved.1-10 Binding Times - Example count = count + 5; Type of count bound at compile time Set of possible values for count bound at compiler design time Meaning of operator + symbol bound at compile time, when the types of its operands have been determined Internal representation of the literal 5 bound at compiler design time Value of count bound at execution time

11 Copyright © 2007 Addison-Wesley. All rights reserved.1-11 Variables Attributes – (Name, Address, Value, Type, Lifetime, Scope) Static Type Binding -- Explicit/Implicit Declaration An explicit declaration is a program statement used for declaring the types of variables –int count; An implicit declaration is a default mechanism for specifying types of variables (the first appearance of the variable in the program) –Pre 1960s - FORTRAN, PL/I, BASIC –Post 1960s –JavaScript, Ruby, and ML –Perl uses special characters to denote type ($ scalar, @ for array and % for hash –Advantage: writability –Disadvantage: reliability –Forgotten declarations in fortran example

12 Copyright © 2007 Addison-Wesley. All rights reserved.1-12 Variables Attributes – (Name, Address, Value, Type, Lifetime, Scope) Dynamic Type Binding –When the assignment statement is executed, the name on the lhs is bound to the type of the value of the rhs JavaScript list = [2, 4.33, 6, 8]; list = 17.3; –Advantage: flexibility (generic program units) –Disadvantages: High cost (dynamic type checking and interpretation) Type error detection by the compiler is difficult

13 Copyright © 2007 Addison-Wesley. All rights reserved.1-13 Variables Attributes – (Name, Address, Value, Type, Lifetime, Scope) Type Inferencing (ML, Miranda, and Haskell) –Rather than by assignment statement, types are determined (by the compiler) from the context of the reference fun circumf = 3.14159 * r * r; inferred as real fun times10(x) = 10 * x; inferred as int fun square(x) = x * x; default type of int fun square (x) : real = x * x

14 Copyright © 2007 Addison-Wesley. All rights reserved.1-14 Variables Attributes – (Name, Address, Value, Type, Lifetime, Scope) Storage Bindings & Lifetime –Allocation - getting a cell from some pool of available cells –Deallocation - putting a cell back into the pool The lifetime of a variable is the time during which it is bound to a particular memory cell –Static static int count = 0; –Stack-dynamic public int method1 (int arg1); –Explicit heap-dynamic Student s = new Student(); –Implicit heap-dynamic Highs = [74, 80, 85, 90]

15 Copyright © 2007 Addison-Wesley. All rights reserved.1-15 Variables Attributes – (Name, Address, Value, Type, Lifetime, Scope) Static –Bound to memory cells before program execution begins and remains bound to the same memory cell throughout execution. C and C++ static variables –Advantages: efficiency – static constants in Java efficiency - (direct addressing) and no run-time allocate/deallocate history-sensitive support –Disadvantage: lack of flexibility (no recursion)

16 Copyright © 2007 Addison-Wesley. All rights reserved.1-16 Variables Attributes – (Name, Address, Value, Type, Lifetime, Scope) Stack-dynamic –Storage bindings are created for variables when their declaration statements are elaborated. A declaration is elaborated when the executable code associated with it is executed. Local variables of a method in Java Advantages –allows recursion –conserves storage Disadvantages –Overhead of allocation and deallocation –Subprograms cannot be history sensitive –Inefficient references (indirect addressing)

17 Copyright © 2007 Addison-Wesley. All rights reserved.1-17 Variables Attributes – (Name, Address, Value, Type, Lifetime, Scope) Explicit heap-dynamic –allocated and deallocated by explicit directives, specified by the programmer, which take effect during execution Referenced only through pointers or references dynamic objects in C++ (via new and delete ) Int *intnode; … Intnode = new int; … Delete intnode; Objects in Java –Advantage: provides for dynamic storage management –Disadvantages: inefficient and can be unreliable due to the complexities of storage management

18 Copyright © 2007 Addison-Wesley. All rights reserved.1-18 Variables Attributes – (Name, Address, Value, Type, Lifetime, Scope) Implicit heap-dynamic –Bound to heap storage only when assigned a value allocation and deallocation caused by assignment statements –all variables in APL; all strings and arrays in Perl, JavaScript, and PHP »Highs = [74, 80, 85, 90] Advantage: flexibility (generic code) Disadvantages: –Inefficient, because all attributes are dynamic –Loss of error detection

19 Copyright © 2007 Addison-Wesley. All rights reserved.1-19 Type Checking Generalize the concept of operands and operators to include subprograms and assignments. Type checking is the activity of ensuring that the operands of an operator are of compatible types A type error is the application of an operator to an operand of an inappropriate type A compatible type is one that is either legal for the operator, or is allowed under language rules to be implicitly converted by the compiler- generated code, to a legal type. –This automatic conversion is called a coercion. If all type bindings are static, nearly all type checking can be static If type bindings are dynamic, type checking must be dynamic A programming language is strongly typed if type errors are always detected Advantage of strong typing: –allows the detection of the misuses of variables that result in type errors

20 Copyright © 2007 Addison-Wesley. All rights reserved.1-20 Strong Typing Language examples: –FORTRAN 95 is not strongly typed due to: EQUIVALENCE –C and C++ are not strongly typed: unions are not type checked –Ada is nearly strongly typed ( UNCHECKED CONVERSION is loophole) –Java and C# are similar to Ada »Allow casts and mixed mode expressions Coercion rules can weaken “strong typing” considerably Java has half the assignment coercions of C++ but its strong typing is still far less effective than that of Ada

21 Copyright © 2007 Addison-Wesley. All rights reserved.1-21 Name Type Equivalence Name type equivalence –two variables have equivalent types –if They are in the same declaration –or They are in declarations that use the same type name Easy to implement but highly restrictive: –Subranges of integer types are not equivalent with integer types. Consider in ADA: Type Indextype is 1..100; count : Integer; index: Indextype; –Formal parameters must be the same type as their corresponding actual parameters

22 Copyright © 2007 Addison-Wesley. All rights reserved.1-22 Structure Type Equivalence Structure type equivalence –Two variables have equivalent types if –their types have identical structures More flexible, but harder to implement –Consider the problem of two structured types: –Are two record types equivalent if they are structurally the same but use different field names? –Are two array types equivalent if they are the same except that the subscripts are different? (e.g. [1..10] and [0..9]) –Are two enumeration types equivalent if their components are spelled differently? –With structural type equivalence, you cannot differentiate between types of the same structure (e.g. different units of speed, both float) Also, disallows differentiating between types with the same structure. Consider: –Type celsius = float; fahrenheit = float;

23 Copyright © 2007 Addison-Wesley. All rights reserved.1-23 Variables Attributes – (Name, Address, Value, Type, Lifetime, Scope ) 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 The scope rules of a language determine how references to names are associated with variables

24 Copyright © 2007 Addison-Wesley. All rights reserved.1-24 Variables Attributes – (Name, Address, Value, Type, Lifetime, Scope ) 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, and PHP)

25 Copyright © 2007 Addison-Wesley. All rights reserved.1-25 Variables Attributes – (Name, Address, Value, Type, Lifetime, Scope ) Static Scope continuted.. –Variables can be hidden from a unit by having a "closer" variable with the same name –C++ and Ada allow access to these "hidden" variables void sub() { int count; … while (…) { int count; count++; }.. } In Ada: unit.name In C++: class_name::name

26 Copyright © 2007 Addison-Wesley. All rights reserved.1-26 Variables Attributes – (Name, Address, Value, Type, Lifetime, Scope ) Static Scope continued.. Block –A method of creating static scopes inside program units-- from ALGOL 60 –Examples: C-based languages: while (...) { int index;... } Ada: declare Temp : Float; begin... end

27 Copyright © 2007 Addison-Wesley. All rights reserved.1-27 Variables Attributes – (Name, Address, Value, Type, Lifetime, Scope ) Evaluation of Static Scoping Assume MAIN calls A and B A calls C and D B calls A and E MAIN E A C D B AB CDE

28 Copyright © 2007 Addison-Wesley. All rights reserved.1-28 Static Scope Example MAIN AB CDE A C B ED Graph of desirable calls vs. graph of potential calls.

29 Copyright © 2007 Addison-Wesley. All rights reserved.1-29 Static Scope Example MAIN AB CDE A C B ED Suppose the spec is changed so that D must now access some data in B Solutions: –Put D in B (but then C can no longer call it and D cannot access A's variables) –Move the data from B that D needs to MAIN (but then all procedures can access them) Same problem for procedure access Overall: static scoping often encourages many globals

30 Copyright © 2007 Addison-Wesley. All rights reserved.1-30 Variables Attributes – (Name, Address, Value, Type, Lifetime, Scope ) Dynamic Scope Based on calling sequences of program units, not their textual layout –i.e. 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 Evaluation of Dynamic Scoping: –Advantage: convenience (called subprogram is executed in the context of the caller) –Disadvantage: poor readability

31 Copyright © 2007 Addison-Wesley. All rights reserved.1-31 Example Big - declaration of X Sub1 - declaration of X -... call Sub2... Sub2... - reference to X -... call Sub1 … Scenario: Big calls Sub1 Sub1 calls Sub2 Sub2 uses X Static scoping Reference to X is to X in Big Dynamic scoping Reference to X is to X in Sub1

32 Copyright © 2007 Addison-Wesley. All rights reserved.1-32 Scope and Lifetime Scope and lifetime are sometimes closely related, but are different concepts –Consider a static variable in a C or C++ function –Another example in C++: void printheader()() { … } void compute() { int sum;.. printheader(); }

33 Copyright © 2007 Addison-Wesley. All rights reserved.1-33 Referencing Environments 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 of the visible variables in all of the enclosing scopes. See Example on page 235 –In a dynamic-scoped language, the referencing environment 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. See Example on page 236

34 Copyright © 2007 Addison-Wesley. All rights reserved.1-34 Homework Page 241- –1, 5, 6, 8, 9, 10, 14

35 Copyright © 2007 Addison-Wesley. All rights reserved.1-35 Named Constants A named constant is a variable that is bound to a value only once –Advantages: readability and modifiability –Used to parameterize programs -Classic Example The binding of values to named constants can be either static (called manifest constants) or dynamic Languages: FORTRAN 95: constant-valued expressions only; uses static binding of values to named constants Ada, C++, and Java: expressions of any kind; uses dynamic binding C# has two kinds, readonly and const - the values of const named constants are bound at compile time (i.e. statically bound) - The values of readonly named constants are dynamically bound void example() { final int len = 100; int [] intlist = new int[len]; … for (i = 0; i, len ; i++) {…} …. for (i = 0; i, len ; i++) {…} … avg = sum / len ; } void example() { int [] intlist = new int[100]; … for (i = 0; i, 100; i++) {…} …. for (i = 0; i, 100; i++) {…} … avg = sum / 100; }

36 Copyright © 2007 Addison-Wesley. All rights reserved.1-36 Variable Initialization initialization - the binding of a variable to a value at the time it is bound to storage. For statically bound constants (manifest contants) –the rhs must be a literal or expression consisting of literals and/or already defined named constants For dynamically bound constants –the rhs can be any expression. Initialization is often done on the declaration statement, e.g., in Java int sum = 0;

37 Copyright © 2007 Addison-Wesley. All rights reserved.1-37 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: static, stack dynamic, explicit heap dynamic, implicit heap dynamic Strong typing means detecting all type errors


Download ppt "ISBN 0-321-49362-1 Chapter 5 Names, Bindings, Type Checking, and Scopes."

Similar presentations


Ads by Google