1.16k likes | 1.18k Views
Explore the importance of names, bindings, and type checking in programming languages. Learn about variable attributes, the concept of binding, and the impact of type compatibility. Understand how scopes, lifetime, and referencing environments play a crucial role in variable management.
E N D
Chapter 5 Names, Bindings, Type Checking, and Scopes
Chapter 5 Topics • Introduction • Names • Variables • The Concept of Binding • Type Checking • Strong Typing • Type Compatibility • Scope and Lifetime • Referencing Environments • Named Constants
Imperative Languages • Imperative languages are abstractions of von Neumann architecture • Memory • stores both instructions and data • Processor • provides operations for modifying the contents of the memory
Memory Cells and Variables • The abstractions in a language for the memory cells of the machine are variables. • In some cases, the characteristics of the abstractions are very close to the characteristics of the cells; • an example of this is an integer variable, which is usually represented exactly as in an individual hardware memory word. • In other cases, the abstractions are far removed from the cells, • as with a three-dimensional array, which requires a software mapping function to support the abstraction.
Attributes of Variables • Variables are characterized by attributes and among them the most important one is type. • Type: • to design, must consider scope, lifetime, type checking, initialization, and type compatibility
A Fundamental Attribute of Variables --Name • A name is a string of characters used to identify some entity in a program.
Other Usage of the Name Attribute of Variables • Names are also associated with labels, subprograms, formal parameters, and other program constructs. • The term identifier is often used interchangeably with name.
Names • Design issues for names: • Maximum length? • Are names case sensitive? • Are special words reserved words or keywords?
Length of Names • Length • If too short, they cannot be connotative • Length examples: • FORTRAN I: maximum 6 • C 89: • no length limitation on its internal names • Only the first 31 are significant • external names (defined outside functions and handled by linkers) • are restricted to 6 characters. • C# and Java: no limit, and all are significant • C++: no limit, but implementers often impose one • They do this so the symbol table in which identifiers are stored during compilation need not be too large, and also to simplify the maintenance of that table.
Name Forms • Names in most programming languages have the same form: a letter followed by a string consisting of letters, digits, and underscore character (_). • In the 1970s and 1980s, underscore characters were widely used to form names. • E.g. my_stack • Nowadays, in the C-based languages, underscore form names are largely replaced by camel notation. • E.g. myStack
Embedded Spaces in Names • In versions of Fortran prior to Fortran 90, names could have embedded spaces, which were ignored. • For example, the following two names were equivalent: Sum Of Salaries SumOfSalaries
Case Sensitivity • Uppercase and lowercase letters in names are distinct • For example, the following three names are distinct in C++: rose, ROSE, and Rose.
Drawbacks of Case Sensitivity • detriment to readability • Names that look very similar in fact denote different entities. • Case sensitivity violates the design principle that language constructs that look the same should have the same meaning.
Special Words • Special words in programming languages are used • to make programs more readable by naming actions to be performed. • to separate the syntactic entities of programs. • In most languages, these words are classified as reserved words, but in some they are only keywords. • P.S.: In program code examples in this book, special words are presented in boldface.
Keywords • A keyword is a word of a programming language that is special only in certain contexts.
Example of Keywords • Fortran is one of the languages whose special words are keywords. • In Fortran, the word Real, when found at the beginning of a statement and followed by a name, is considered a keyword that indicates the statement is a declarative statement. • However, if the word Real is followed by the assignment operator, it is considered a variable name. • These two uses are illustrated in the following: Real Apple Real = 3.4 • Fortran compilers and Fortran program readers must recognize the difference between names and special words by context.
Reserved Words • A reserved word is a special word of a programming language that can NOT be used as a name.
Advantages of Reserved Words • As a language design choice, reserved words are better than keywords because the ability to redefine keywords can lead to readability problems.
Drawback Example of Keywords • In Fortran, one could have the statements • Integer Real • Real Integer which declare the program variable Real to be of Integer type and the variable Integer to be of Real type. • In addition to the strange appearance of these declaration statements, the appearance of Real and Integer as variable names elsewhere in the program could be misleading to program readers.
Variables • A program variable is an abstraction of a computer memory cell or collection of cells. • Variables can be characterized as a sextuple of attributes: • Name • Address • Value • Type • Lifetime • Scope
Benefits of Using Variables • The move from machine languages to assembly languages was largely one of replacing absolute numeric memory addresses with names, making programs far more readable and thus easier to write and maintain. • That step also provided an escape from the problem of absolute addressing, because the translator that converted the names to actual addresses also chose those addresses.
Address • The address of a variable is the memory address with which it is associated. • In many languages, it is possible for the same name to be associated with different addresses at different places and at different times in the program.
The Same Names in Different Functions Are Associated with Different Addresses • A program can have two subprograms, subl and sub2, each of which defines a variable that uses the same name, say sum. Because these two variables are independent of each other, a reference to sum in subl is unrelated to a reference to sum in sub2.
The Same Names in Different Executions May Be Associated with Different Addresses • Similarly, most languages allow the same variable to be associated with different addresses at different times during program execution. • For Example: • Local variables and runtime stack.
L-value • The address of a variable is sometimes called its L-value, because that is what is required when a variable appears in the left side of an assignment statement.
Aliases • It is possible to have multiple identifiers reference the same address. • When more than one variable name can be used to access a single memory location, the names are called aliases.
Disadvantages of Aliases • Aliasing is a hindrance to readability because it allows a variable to have its value changed by an assignment to a different variable. • For example, if variables A and B are aliases, any change to A also changes B and vice versa. A reader of the program must always remember that A and B are different names for the same memory cell. This is very difficult in practice. • Aliasing also makes program verification more difficult.
Ways to Create Aliases • Aliases can be created in programs in several different ways. • C and C++: union types. • Two pointer variables are aliases whey they point to the same memory location. • Reference variables: when a C++ pointer is set to point at a named variable, the pointer, when dereferenced, and the variable’s name are aliases.
Type • The type of a variable determines the range of values the variable can have and the set of operations that are defined for values of the type. • For example, the type int in Java specifies a value range of -2147483648 to 2147483647 and arithmetic operations for addition, subtraction, multiplication, division, and modulus.
Value • The value of a variable is the contents of the memory cell or cells associated with the variable.
Abstract Cells • It is convenient to think of computer memory in terms of abstract cells, rather than physical cells. • The cells, or individually addressable units, of most contemporary computer memories are byte-sized, with a byte usually being eight bits in length. • This size is too small for most program variables. • We define an abstract memory cell to have the size required by the variable with which it is associated.
Example • Although floating-point values may occupy four physical bytes in a particular implementation of a particular language, we think of a floating-point value as occupying a single abstract memory cell. • We consider the value of each simple nonstructured type to occupy a single abstract cell. Henceforth, when we use the term memory cell, we mean abstract memory cell.
r-value • A variable's value is sometimes called its r-value because it is what is required when the variable is used on the right side of an assignment statement. • To access the r-value, the L-value must be determined first. • Such determinations are not always simple. • For example, scoping rules can greatly complicate matters, as is discussed in Section 5.8.
Binding and Binding Time • In a general sense, a binding is an association, such as between an attribute and an entity or between an operation and a symbol. • The time at which a binding takes place is called binding time.
Possible Binding Time • Bindings can take place at: • language design time, • language implementation time, • compile time, • link time, • load time, • run time.
Different Language Syntactic Unit Has Different Binding Time for Different Attribute • The asterisk symbol (*) is usually bound to the multiplication operation at language design time. • A data type, such as int in C, is bound to a range of possible values at language implementation time. • A variable in a Java program is bound to a particular data type at compile time. • Symbol tables are created at compile time. • A variable may be bound to a storage cell when the program is loaded into memory. • global variables and static variables. • A call to a library subprogram is bound to the subprogram code at link time.
Example • Consider the following C assignment statement count = count + 5; • Some of the bindings and their binding times for the parts of this assignment statement are as follows: • The type of count is bound at compile time • The set of possible values of count is bound at compiler design time/language implementation time. • The meaning of the operator symbol + is bound at compile time, when the types of its operands have been determined. • The symbol + may have different usage in a programming Language, such as addition of integers or addition of floating point numbers. • The internal representation of the literal 5 is bound at compiler design time • The value of count is bound at execution time with this assignment
Static Binding • A binding is static if it first occurs before run time and remains unchanged throughout program execution.
Dynamic Binding • If a binding first occurs during run time or can change in the course of program execution, it is called dynamic.
Type Bindings • Before a variable can be referenced in a program, it must be bound to a data type. • Two important aspects of type bindings are • how the type is specified. • when the binding takes place. • Types can be specified statically through some form of • explicit declaration • implicit declaration • Both explicit and implicit declarations create static bindings to types.
Explicit Declarations • An explicit declaration is a statement in a program that lists variable names and specifies that they are a particular type. • Most programming languages designed since the mid-1960s require explicit declarations of ALL variables.
Implicit Declarations • An implicit declaration is a means of associating variables with types through default conventions instead of declaration statements. • In this case, the FIRST appearance of a variable name in a program constitutes its implicit declaration.
Implicit Declaration Example • In Fortran, an identifier that appears in a program that is not explicitly declared is implicitly declared according to the following convention: • If the identifier begins with one of the letters i, J, K, L, M, or N, it is implicitly declared to be Integer type. • In all other cases, it is implicitly declared to be Real type.
Drawbacks of Implicit Declarations • Although they are a minor convenience to programmers, implicit declarations can be detrimental to reliability because they prevent the compilation process from detecting some typographical and programmer errors. • For example, in Fortran, variables that are accidentally left undeclared by the programmer are given default types and unexpected attributes, which could cause subtle errors that are difficult to diagnose.
Disable Implicit Declarations in Fortran • Many Fortran programmers now include the declaration – Implicit none – in their programs. This declaration instructs the compiler to no implicitly declare any variables.
Method to Avoid Implicit Declarations • Some of the problems with implicit declarations can be avoided by requiring names for specific types to begin with particular special characters. • For example, in Perl any name that begins with $ is a scalar, which can store either a string or a numeric value. • If a name begins with @, it is an array. • The above rules create different name spaces for different type variables. In this scenario, the names @apple and %apple are unrelated, because each is from a different name space. • Furthermore, a program reader always knows the type of a variable when reading its name.
Declarations and Definitions • In C and C++, one must sometimes distinguish between declarations and definitions. • Declarations specify types and other attributes but do not cause allocation of storage. • Definitions specify attributes and cause storage allocation,
Number of Declarations and Definitions • For a specific name, a C program can have ANY number of compatible declarations, but only a SINGLE definition. • One purpose of variable declarations in C is to provide the type of a variable defined external to a function that is used in the function. It tells the compiler the type of a variable and that it is defined elsewhere.
Function Definition and Function Prototype • The idea in previous slides carries over to the functions in C and C++, where prototypes declare names and interfaces, but not the code of functions. • Function definitions, on the other hand, are complete.
Dynamic Type Binding • With dynamic type binding: • the type is not specified by a declaration statement. • the variable is bound to a type when it is assigned a value in an assignment statement. • When the assignment statement is executed, the variable being assigned is bound to the type of the value of the expression on the right side of the assignment.