1 / 138

Chapter 5

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.

jamesrgreen
Download Presentation

Chapter 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 5 Names, Bindings, Type Checking, and Scopes

  2. Chapter 5 Topics • Introduction • Names • Variables • The Concept of Binding • Type Checking • Strong Typing • Type Compatibility • Scope and Lifetime • Referencing Environments • Named Constants

  3. 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

  4. 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 directly in one or more bytes of memory. • In other cases, the abstractions are far removed from the organization of the hardware memory, • as with a three-dimensional array, which requires a software mapping function to support the abstraction.

  5. Attributes of Variables • A variable can be characterized by a collection of properties, or attributes. • The most important of variable attributes is type, a fundamental concept in programming languages.

  6. C-based Languages • In this book, the author uses the phrase C-based languages to refer to C, C++, Java, and C#.

  7. Name • One of the fundamental attributes of variables: names, which have broader use than simply for variables. • A name is a string of characters used to identify some entity in a program.

  8. 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.

  9. Design Issues for Names • Are names case sensitive? • Are special wordsreserved words or keywords?

  10. 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.

  11. 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

  12. 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

  13. Case Sensitivity • In many languages, notably the C-based languages, uppercase and lowercase letters in names are distinct • For example, the following three names are distinct in C++: rose, ROSE, and Rose.

  14. Drawbacks of Case Sensitivity • detrimental 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. • detrimental to writability • The need to remember specific case usage makes it more difficult to write correct programs.

  15. 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, special 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.

  16. Keywords • A keyword is a word of a programming language that is special only in certain contexts.

  17. 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.

  18. Reserved Words • A reserved word is a special word of a programming language that can NOT be used as a name.

  19. 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.

  20. 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.

  21. Variables • A program variable is an abstraction of a computer memory cell or collection of cells. • Variables can be characterized as a sixtuple of attributes: • Name • Address • Value • Type • Lifetime • Scope

  22. Benefits of Using Variables • One of the major adjustments from machine languages to assembly languages was to replace absolute numeric memory addresses with names, making programs far more readable and thus easier to write and maintain. • That above step also provided an escape from the problem of manual absolute addressing, because the translator that converted the names to actual addresses also chose those addresses.

  23. Address • The address of a variable is the memory address with which it is associated. • In many languages, it is possible for the same variable name to be associated with different addresses • at different places and • at different times in the program.

  24. How Parameters and Local Variables Are Represented in an Object File? abc: function prologue *(%ebp-4)=*(%ebp+8) function epilogue abc(int aa) {int bb; bb=aa; : : } aa return address previous frame point ebp a C function equivalent assembly code P.S.: function prologueand function epilogueare added by a compiler bb

  25. 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.

  26. The Same Names in Different Executions May Be Associated with Different Addresses • If a subprogram has a local variable that is allocated from the run-time stack when the subprogram is called, different calls may result in that variable having different addresses. • These are in a sense different instantiations of the same variable.

  27. Memory Allocation of Local Variables stack G(int a) { int i; H(3); add_g: i++; } H(int b) {char c[100]; int i; while((c[i++]=getch())!=EOF) { } } high address G’s stack frame i b return address add_g H’s stack frame address of G’s frame point C[99] C[0] i low address

  28. 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.

  29. Aliases • It is possible to have multiple variables that have the same address. • When more than one variable name can be used to access a single memory location, the names are called aliases.

  30. 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 total and sum are aliases, any change to total also changes sum and vice versa. • A reader of the program must always remember that total and sum are different names for the same memory cell. • Because there can be any number of aliases in a program, this is very difficult in practice. • Aliasing also makes program verification more difficult.

  31. Ways to Create Aliases • Aliases can be created in programs in several different ways. • C and C++: union types. • Two pointer variables are aliases when they point to the same memory location. • Reference variables • When a C++ pointer is set to point at a variable, the pointer, when dereferenced, and the variable’s name are aliases.

  32. Type • The type of a variable determines • the range of values the variable can store 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.

  33. Value • The value of a variable is the contents of the memory cell or cells associated with the variable.

  34. Abstract Cells • It is convenient to think of computer memory in terms of abstract cells, rather than physical cells. • The physical 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.

  35. 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.

  36. 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.

  37. 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.

  38. Possible Binding Time • Bindings can take place at: • language design time, • language implementation time, • compile time, • link time, • load time, • run time.

  39. Static Binding • A binding is static if • it first occurs before run time and • remains unchanged throughout program execution.

  40. Dynamic Binding • If a binding • first occurs during run time or • can change in the course of program execution, it is called dynamic.

  41. Type Binding • 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.

  42. 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. • Perl, JavaScript, Ruby, and ML are some exceptions.

  43. 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. • Several widely used languages whose initial designs were done before the late 1960s – notably Fortran, PL/I, and BASIC – have implicit declarations.

  44. 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, or their lowercase versions, it is implicitly declared to be Integer type. • In all other cases, it is implicitly declared to be Real type.

  45. 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.

  46. 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.

  47. 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. • any name beginning with @ 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.

  48. 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,

  49. Number of Declarations and Definitions • For a specific name, a C program can have ANY number of compatible declarations, but only a SINGLE definition.

More Related