Gordon CPS 323 - NAMES, BINDING, LIFERIME ,SCOPE

Unformatted text preview:

CPS323 Lecture: Names, Binding, Lifetime, Scope 1/13/09Objectives:1. To introduce the concept of binding2. To discuss alternatives for name binding and object lifetime3. To discuss lexical and dynamic scope, and the related concept of referencing environments4. To discuss and distinguish the concepts of aliasing, overloading, and polymorphism Materials:1. Pascal redefinition of true example to project/demonstrate2. Projectable of Figure 3.10 in book2. Projectable of Figure 3.4 in bookI. Names- ----- A. As you have seen/will see in CPS311, at the machine language level, everything is represeted by numbers. But all programming languages (assembly as well as higher level) use symbolic names for a variety of purposes - e.g. variables, constants, executable code (methods, procedures, subroutines, functions, even whole programs), data types, classes, etc. B. In general, names are of two different types: 1. Special symbols: +, -, * ... 2. Identifiers: sequences of alphanumeric characters (in most cases beginning with a letter), plus in many cases a few special characters such as '_' or '$'. a. Programming languages are either case sensitive or case ! insensitive. In the former case, a and A are regarded as two ! distinct identifiers; in the latter case, a and A are the same. ! (Some languages only allow one case - e.g. until F90 only ! uppercase letters were allowed for identifiers in FORTRAN). b. Most programming languages impose some limit on the number of characters in an identifier - examples ASK c. Many programming languages have conventions regarding identifiers which, while not enforced by the compiler, are generally observed by "native speakers". i. Example: mixed case and lowercase/uppercase first letter conventions of Java ii. Example: COBOL allowed hyphen (-) in identifiers, and the convention was to use this to separate words - e.g. employee-name print-reportiii. Example: Ada is case-insensitive, but the Ada83 reference manual used the convention of lowercase for reserved words and uppercase for identifiers, with underscores used to separate words - e.g. if HOURS_WORKED > 40 then GROSS_PAY = GROSS_PAY + 0.5 * RATE * (HOURS_WORKED - 40); end if iv. In your projects, you should try to follow the conventions of whatever langauge you are using. C. Most programming languages have the concept of either reserved words or keywords. 1. A reserved word is an identifier that has a special meaning in the language, and cannot be used for any other purpose (e.g. "while" in Java) 2. A keyword is an identifier that has a special meaning in the language, but can be used as a name in other contexts. Example: The following is legal FORTRAN ! IF (IF.EQ.3) IF = 4 ! 3. Advantages of each? ASK a. An advantage of the reserved word approach is that programs are less confusing (though arguably something like the FORTRAN example I just gave represents really poor practice even if it is legal) b. An advantage of the keyword approach is that the introduction of a new reserved word in a language update does not "break" old code. Example: assert in Java. (It should be noted, though, that the designers of Java did intend anticipate problems like this by including in their reserved word list some words that Java does not use (like cast) but might use in the future. However, this was one they missed) D. Some programming languages have the concept of a predeclared identifier. 1. A predeclared identifier is a name that is "built-in" to the language, but which the programmer can change. (This differs from a keyword, in that a redefinition totally changes the meaning. In contrast, in the FORTRAN keyword example, IF actually meant two different things in the same statement depending on context.)2. Example: Consider the following Pascal fragment. What will it do? program MessedUp(input, output); const true = false; begin ! if true = false then writeln('Postmodernism reigns') end. Display, compile on laptop (gpc), run 3. A predeclared identifier behaves as if there were a declaration for it in an outer scope that surrounds the entire program. !II. Binding/Binding Time-- --------------- ---- A. Definition: 1. A binding is an association between two things. 2. Binding time is the time when an association is established. B. The concepts of binding and binding time are pervasive ones in computer science. 1. We consider these concepts here in the context of names in programming languages - e.g. the binding of a class name to a class or a variables name to a variable. 2. Other illustrative examples: a. Somthing other than a name being bound in a programming language: the binding of a data type to a variable in a programming language. b. A name being bound outside the context of a programming language: the binding of a name to a network socket (for which Unix actually provides a routine named bind). ! C. In the context of programming languages, there are quite a few! alternatives for binding time. ! ! 1. Language design time! ! ! Example: reserved words are bound to their meanings at the time a! ! given language is designed/redesigned! ! ! 2. Language Implementation Time! ! Example: the C language does not specify the range of values for! the type int. Implemntations on early microcomputers! typically used 16 bits for ints, yielding a range of! values from -32768 to +32767. On early large computers, and! on all computers today, C implementations typically! use 32 bits for int.! ! OTOH: in Java, the use of 32 bits for int is defined as part of the! language.! ! ! 3. Program write time! !! ! Example: many names are bound to specific meanings when a person! !


View Full Document

Gordon CPS 323 - NAMES, BINDING, LIFERIME ,SCOPE

Documents in this Course
Load more
Download NAMES, BINDING, LIFERIME ,SCOPE
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view NAMES, BINDING, LIFERIME ,SCOPE and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view NAMES, BINDING, LIFERIME ,SCOPE 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?