Chapter Names, Binding, Type Checking and Scopes ISBN 0-321-33025-0 Chapter Topics • • • • • • • • • • • Introduction Names Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime Referencing Environments Named Constants Variable Initialization Copyright © 2006 Addison-Wesley All rights reserved 1-2 Introduction • Fundamental semantic issues of variables – Imperative languages are abstractions of von Neumann architecture – Variables characterized by attributes, the most important of which is data type • The design of the data types of a language requires that a variety of issues be considered – The scope and lifetime of variables – Type checking and initialization – Type compatibility, … Copyright © 2006 Addison-Wesley All rights reserved 1-3 Names • A name is a string of characters used to identify some entity in a program • Names are associated with: – Variables – Labels – Subprograms – Formal parameters, and other program constructs • The term identifier is often used interchangeably with name Copyright © 2006 Addison-Wesley All rights reserved 1-4 Design Issues for Names • Maximum length? • Are connector characters allowed? • Are names case sensitive? • Are special words reserved words or keywords? Copyright © 2006 Addison-Wesley All rights reserved 1-5 Name Forms • Length: if too short, they cannot be connotative • Language examples: – FORTRAN I: maximum – COBOL: maximum 30 – FORTRAN 90 and ANSI C: maximum 31 – Ada and Java: no limit – C++: indetermination • Connectors: – Modula-2 and FORTRAN 77 don't allow – Others Copyright © 2006 Addison-Wesley All rights reserved 1-6 Name Forms (cont.) • Case sensitivity: – A serious detriment to readability because names that look alike are different – Easy typo problems vs IDE autocomplete solves it • Example: – Case sensitivity example: C/C++, Modula, Java, … – Non-case sensitivity example: Eiffel, Ada, … – Mixed up at times: PHP (user variables vs functions) Copyright © 2006 Addison-Wesley All rights reserved 1-7 Special words • Special words are used to make programs more readable • A keyword is a word that is special only in certain contexts • A reserved word is a special word that cannot be used as a user-defined name • A predefined name, which is in a sense between reserved word and user-defined name It has predefined meaning but can be redefined by the user Copyright © 2006 Addison-Wesley All rights reserved 1-8 Variables • A variable is an abstraction of a memory cell – Abstract memory cell - the physical cell or collection of cells associated with a variable – Replace absolute numeric memory addresses with name – Escape from the problem of absolute addressing • Variables can be characterized as a sextuple of attributes: (Name, Address, Value, Type, Lifetime, Scope) – Name: considerations of length, case, character, … Copyright © 2006 Addison-Wesley All rights reserved 1-9 Variables (cont.) – Address (also called L-value): the memory address with which it is associated The same variable name may have different addresses at different places in the program A variable may have different addresses at different times during program execution If two or more variable names can be used to access the same memory location, they are called aliases • Aliasing is harmful to readability • Aliasing makes program verification more difficult Copyright © 2006 Addison-Wesley All rights reserved 1-10 Example Ada declare TEMP: integer; begin TEMP := FIRST; FIRST := SECOND; SECOND := TEMP; end; Copyright © 2006 Addison-Wesley All rights reserved C/C++, Java if (FIRST > SECOND) { int TEMP; TEMP = FIRST; FIRST = SECOND; SECOND = TEMP; } 1-53 Evaluation of Static Scoping main A main C A D C B B D E E Assume MAIN calls A and B, A calls C and D, B calls A and E, D calls C Copyright © 2006 Addison-Wesley All rights reserved 1-54 Dynamic Scope • Dynamic scoping is based on the calling sequence of subprograms, not on their spatial relationship to each other • The scope can be determined only at run-time • References to variables are connected to declarations by searching back through the chain of subprogram calls that forced execution to this point Copyright © 2006 Addison-Wesley All rights reserved 1-55 Example procedure big; var x: integer; procedure sub1; begin … x … end; procedure sub2; var x: integer; begin … end; begin … end; Copyright © 2006 Addison-Wesley All rights reserved Call sequence: big sub2 sub1 The search proceeds from the local procedure, sub1, to its caller, sub2, where a declaration for x is found So the reference to x in sub1 is to the x declared in sub2 1-56 Example procedure big; var x: integer; procedure sub1; begin … x … end; procedure sub2; var x: integer; begin … end; begin … end; Copyright © 2006 Addison-Wesley All rights reserved Call sequence: big sub2 sub1 The search proceeds from the local procedure, sub1, to its caller, sub2, where a declaration for x is found So the reference to x in sub1 is to the x declared in sub2 Call sequence: big sub1 The dynamic parent of sub1 is big, and the reference is to the x declared in big 1-57 Evaluation of Dynamic Scoping • The local variables of the active subprogram are all visible to any other executing subprogram, regardless of its textual proximity • The inability to statically type check references to nonlocals • Poor readability and reliability • Accesses to nonlocal variables in dynamic scoped languages take far longer than accesses to nonlocals when static scoping is used • Advantage: In some cases, the passing parameters are not needed Copyright © 2006 Addison-Wesley All rights reserved 1-58 Scope and Lifetime • Scope and lifetime are sometimes closely related, but are different concepts – Static scope is a textual, or spatial, concept – Lifetime is a temporal concept • Example 1: A variable that is declared in a Pascal procedure that contains no subprogram calls – Scope - from variable’s declaration to the reserved word end of the procedure – Lifetime - the period of time beginning when the procedure is entered and ending when execution of the procedure reaches the end Copyright © 2006 Addison-Wesley All rights reserved 1-59 Scope and Lifetime (cont.) • Example 2: A local static variable in C/C++ – Scope - local to the function – Lifetime - extend over the entire execution of the program of which it is a part • Example 3: Scope and lifetime are also unrelated when subprogram calls are involved void A() { … } void B() { int x; … A(); … } – The scope of the variable x is completely contained within the B() – The lifetime of x extends over the time during which A() executes Copyright © 2006 Addison-Wesley All rights reserved 1-60 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 • 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 Copyright © 2006 Addison-Wesley All rights reserved 1-61 Example (static-scoped language) program example; var a, b: integer; procedure sub1; var x, y: integer; begin … end; procedure sub2; var x: integer; procedure sub3; var x: integer; begin … end; begin … end; begin … end; Copyright © 2006 Addison-Wesley All rights reserved Point Referencing environment x, y of sub1; a, b of example x of sub3 (x of sub2 is hidden); a, b of example x of sub2; a, b of example a, b of example 1-62 Example (dynamic-scoped language) void sub1() { int a, b; … } void sub2() { int b, c; … sub1(); } void main() { int c, d; … sub2(); } main sub2 sub1 Copyright © 2006 Addison-Wesley All rights reserved Point Referencing environment a, b of sub1; c of sub2; d of main (b of sub2, c of main are hidden) b, c of sub2; d of main (c of main is hidden) c, d of main 1-63 Named Constants • A named constant is a variable that is bound to a value only once • Advantages: readability and modifiability • The binding of values to named constants can be either static (called manifest constants) or dynamic • Languages: – Pascal: literals only – FORTRAN 90: constant-valued expressions – Ada, C++, and Java: expressions of any kind Copyright © 2006 Addison-Wesley All rights reserved 1-64 Example • In FORTRAN integer size parameter (size = 100 ) double precision pi parameter ( pi = 3.14159265358979d0 ) • In C++ const int result = * width + 1; • In Pascal const pi: real = 3.14; Copyright © 2006 Addison-Wesley All rights reserved 1-65 Variable Initialization • The binding of a variable to a value at the time it is bound to storage is called initialization • Initialization is often done on the declaration statement • Example: – In FORTRAN (compile-time) REAL PI INTEGER SUM DATA SUM /0/, PI /3.14159/ – Local variable in C/C++ (run-time) Copyright © 2006 Addison-Wesley All rights reserved 1-66 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 Copyright © 2006 Addison-Wesley All rights reserved 1-67 ... to a legal type – A type error is the application of an operator to an operand of an inappropriate type – If all type bindings are static, nearly all type checking can be static If type bindings... dynamic, type checking must be dynamic Copyright © 2006 Addison-Wesley All rights reserved 1-28 Type Checking (cont.) • Type checking will touch on the topics of: – Type equivalence – Type compatibility... represents a derived type or a subtype – A subtype is compatible with its base type (Subtypes of the same base type are also compatible with each other) – A derived type is incompatible subtype int is