Chapter 5 Names Binding and Type Checking Variables
Chapter 5 Names, Binding, and Type Checking
Variables A variable abstracts a collection of memory cells Based on the von Neumann architecture Variables are characterized by: ▪ ▪ ▪ Name Memory address Value Data type Lifetime Scope 2
Variables - Names A name is a string of characters that identifies some entity in a program ▪ ▪ ▪ Variables Labels Subprograms Functions, methods, . . Classes, structures, . . etc. double f(double q) { return q * q; } main() { double x, y; here: scanf("%lf", &x); if (x < 0. 0) { goto here; } y = f(x) * PI; printf("%lfn", y); } 3
Variables – Names continued Early computers were initially used for solving complex and repetitive mathematical equations. Early programming languages looked a lot like algebra. In mathematics, you can append notes when you need to break out of the sea of symbols to explain what the symbols stand for. Variable names in programming should be more meaningful. 4
Variables – Names continued The first BASIC interpreter limited variables to a single letter and an optional digit ( A , B 2 , C 3 , etc. ) FORTRAN gave the programmer six characters to play with--really 5 1/2, since the first character denoted the default type. That meant that instead of using I for an index, you could use a name like INDEX. This was progress. (One problem with FORTRAN was that it used the first letter of the variable name for type information. So people would use KOUNT for "count" so that the type would be integer. ) 5
Special Words A keyword is a word that is special only in certain contexts but a reserved word is a special word that cannot be used as a user-defined name Not all languages have the same numbers of reserved words. For example, Java (and other C derivatives) has a rather sparse complement of reserved words approximately 25 -whereas COBOL has around 400. At the other end of the spectrum, pure Prolog has none at all. The number of reserved words in a language has little to do with how “powerful” a language is 6
Special Words – Continued COBOL was designed in the 1950 s as a business language and was made to be selfdocumenting using English-like structural elements such as verbs, clauses, sentences, sections and divisions. C, on the other hand, was written to be very terse (syntactically) and to get more text on the screen 7
Special Words - Example // Calculation in Java: * Calculation in COBOL: if (salaried) amount = 40 * payrate; else amount = hours * payrate; IF Salaried THEN MULTIPLY Payrate BY 40 GIVING Amount ELSE MULTIPLY Payrate BY Hours GIVING Amount END-IF. 8
Variables – Address & Value The address of a variable is often called its l-value, because it appears on the left -hand side of an assignment statement The value of a variable is often called its r-value, because it appears on the righthand side of an assignment statement 9
Binding time The time at which association occurs, e. g. between an operation and a symbol Possible binding times: ▪ Language design time - when the language is designed, e. g. , meaning of operators. ▪ Compiler design time – when the compiler is designed, e. g. , internal representations. ▪ Compile time – when a module is compiled, e. g. , type of variable. ▪ Load time ▪ Link time – when the modules are linked together, e. g. , address of called subroutine. ▪ Runtime – when the program is being run, e. g. , value of variable. ▪ etc. . 10
Static & Dynamic Binding A binding is static if it occurs before run time and remains unchanged throughout program execution A binding is dynamic if it occurs during execution or can change during execution of the program In many ways, binding times for various attributes determine the flavor of a language As binding time gets earlier: efficiency goes up safety goes up flexibility goes down 11
Static Type Binding An explicit declaration is a program statement used for declaring the types of variables ▪ var x: int ▪ Advantage: safer, cheaper ▪ Disadvantage: less flexible An implicit declaration is a default mechanism for specifying types of variables (the first appearance of the variable in the program) ▪ I-N => integer in Fortran ▪ Advantage: writability ▪ Disadvantage: reliability 12
Static Type Binding Examples Defining constants: ▪ ▪ ▪ const int Q = 15; // C/C++ const double PI = 3. 1415927; public static final int JACK = 11; // Java public static final int QUEEN = 12; public static final int KING = 13; Binding variables to their respective data types: ▪ int count, x, y, z; // C/C++ ▪ char letter; ▪ double radius, area, volume; 13
Dynamic Type Binding Specified through an assignment statement (APL, Smalltalk, etc. ) ▪ LIST <- 2 4 6 8 ▪ LIST <- 17. 3 Advantage: flexibility (generic program units) Disadvantages: ▪ Type error detection by the compiler is difficult ▪ High cost (dynamic type checking and interpretation) or low safety 14
Dynamic Type Binding - Example Variable binding to a memory address: ▪ (can be static, too) Binding a value to a variable Dynamic memory allocation Object creation 15
Variables – Data Type specifies a class of values & operations to create, access, & modify languages usually include some predefined types ▪ e. g. , Boolean, integer, floating point, character often allow programmers to define new types 16
Variables - Lifetime The lifetime of a variable is the time during which it is bound to a particular memory location Variables begin their lifetime upon declaration Variables end their lifetime when they go out of scope or when the program ends execution 17
Variables – Lifetime continued Memory is allocated from a pool of available memory when the variable is first bound Memory is deallocated when a variable ends its lifetime and the allocated memory is given back to the pool Categories of Variables by Lifetime: ▪ ▪ Static Stack-Dynamic Explicit Heap-Dynamic Implicit Heap-Dynamic 18
Static Variables Those that are bound to memory cells before program execution begins and remain bound to those same memory cells until program execution terminates FORTRAN I, II, and IV contain only static variables, and C++ and Java allow the developer to specify static variables with the static keyword; however, Pascal contains no static variables 19
Static Variables - Continued Advantages: • History sensitive • Increased efficiency Disadvantages: • Reduced flexibility • Storage cannot be shared among variables 20
Static Variables Example /** C **/ int calculate(int y) { static int x = 5; } if (x > 0) { x--; return y * y; } /** disallowed **/ return -1; 21
Stack-Dynamic Variables bound when the declaration statement is executed, and it is deallocated when the procedure returns ALGOL 60 and successor languages use stack -dynamic variables as well as FORTRAN 77 and FORTRAN 90. Local variables in C and C++ are stack-dynamics variables by default. 22
Stack-Dynamic Variables Example variables defined in methods in Java, C++, C# main() /** C **/ { int x; . . . while (f > 0) { int q = 0; . . . } } 23
Stack-Dynamic Variables Continued Advantages • all subprograms share the same memory space for their locals. Disadvantages • run-time overhead of allocation and deallocation • cannot be history sensitive. 24
Explicit Heap-Dynamic Variables bound during an explicit run-time instruction All objects in Java are explicit heap-dynamic variables and are destroyed by 'garbage collection Ada and C++ use operators to create and destroy explicit heap-dynamic variables 25
Explicit Heap-Dynamic Variables Example dynamic objects in C++, objects in Java, use of malloc() in C void main() { char *str; … str = malloc(100); /* allocates 100 bytes of dynamic heap storage */ … free(str); /* deallocates 100 bytes of dynamic heap storage*/ } 26
Explicit Heap-Dynamic Variables Continued Advantages • used for dynamic structures which can be built conveniently using pointers or references and explicit heap dynamic variables Disadvantages • difficulty of using pointer and reference variables correctly • the cost of references to the variables, allocations, and deallocations. 27
Implicit Heap-Dynamic Variables bound to heap storage when the variable is assigned a value Languages such as APL, ALGOL 68, and LISP use implicit heap-dynamic variables 28
Implicit Heap-Dynamic Variables Example variables in APL, strings and arrays in Perl and Java. Script(and hashes in Perl) # Perl %q 2 mapping = ( "April" => 30, "May" => 31, "June" => 30 ); %q 2 mapping = ( "q 2" => 4000 ); // Java. Script numbers = [ 5, 7, 9, 11 ]; numbers = "Hello. "; numbers = 100; 29
Implicit Heap-Dynamic Variables Continued Advantages • highest degree of flexibility, allowing highly generic code to be written. Disadvantages • run-time overhead of maintaining all the dynamic attributes • the loss of some error detection by the compiler. 30
Type Checking Type checking ensures that the operands of an operator are of compatible types Types are compatible when: ▪ it is legal to use the type with the operator ▪ or the type is allowed under implicit conversion rules of the language (a process called coercion) Type errors occur when an operator is applied to an operand of an inappropriate type 31
Type Checking - Continued Type checking occurs with variable binding If type bindings are static, nearly all type checking can be static (i. e. at compile time) If type bindings are dynamic, type checking must be dynamic (i. e. at run time) A programming language is strongly typed if type errors are always detected Detection occurs at both compile time and run time Impacts the reliability of a language 32
- Slides: 32