Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 1 Chapter 5 Chapter 5 Variables: Names, Bindings, Type Checking and Scope.

Similar presentations


Presentation on theme: "CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 1 Chapter 5 Chapter 5 Variables: Names, Bindings, Type Checking and Scope."— Presentation transcript:

1 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 1 Chapter 5 Chapter 5 Variables: Names, Bindings, Type Checking and Scope

2 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 2 Introduction This chapter introduces the fundamental semantic issues of variables. –It covers the nature of names and special words in programming languages, attributes of variables, concepts of binding and binding times. –It investigates type checking, strong typing and type compatibility rules. –At the end it discusses named constraints and variable initialization techniques.

3 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 3 On Names Humans are the only species to have the concept of a name. It’s given philosophers, writers and computer scientists lots to do for many years. –The logician Frege’s famous morning star and evening star example. –"What's in a name? That which we call a rose by any other word would smell as sweet." -- Romeo and Juliet, W. Shakespeare –The semantic web proposes using URLs as names for everything. In programming languages, names are character strings used to refer to program entities – e.g., labels, procedures, parameters, storage locations, etc.

4 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 4 Names There are some basic design issues involving names: –Maximum length? –What characters are allowed? –Are names case sensitive? –Are special words reserved words or keywords? –Do names determine or suggest attributes

5 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 5 Names: length limitations? Some examples: –FORTRAN I: maximum 6 –COBOL: maximum 30 –FORTRAN 90 and ANSI C: maximum 31 –C++: no limit, but implementers often impose one –Java, Lisp, Prolog, Ada: no limit, and all are significant The trend has been for programming languages to allow longer or unlimited length names Is this always good? –What are some advantages and disadvantages of very long names?

6 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 6 Names: What characters are allowed? Typical scheme: –Names must start with an alpha and contain a mixture of alpha, digits and connectors –Some languages (e.g., Lisp) are even more relaxed Connectors: –Connectors might include underscore, hyphen, dot, … -Fortran 90 allowed spaces as connectors -Languages with infix operators typically only allow _, reserving., -, + as operators -LISP: first-name -C: first_name -“Camel notation” is popular in C, Java, C#... -Using upper case characters to break up a long name, e.g. firstName

7 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 7 Names: case sensitivity Foo = foo? The first languages only had upper case (why?) Case sensitivity was probably introduced by Unix and hence C (when?) Disadvantage: Poor readability, since names that look alike to a human are different; worse in Modula-2 because predefined names are mixed case (e.g. WriteCard) Advantages: Larger namespace, ability to use case to signify classes of variables (e.g., make constants be in uppercase) C, C++, Java, and Modula-2 names are case sensitive but the names in many other languages are not

8 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 8 Special words Def: A keyword is a word that is special only in certain contexts –Virtually all programming languages have keywords Def: A reserved word is a special word that cannot be used as a user-defined name –Some PLs have reserved words and others do not –PLs try to minimize the number of reserved words For languages which use keywords rather than reserved words –Disadvantage: poor readability thru possible confusion about the meaning of symbols –Advantage: flexibility – the programmer has fewer constraints on choice of names

9 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 9 Reserved words in C auto break case char const continue default do double else entry enum extern float for goto if int long register Return short signed sizeof static struct switch typedef union unsigned void volatile while Programming languages try to minimize the number of reserved words. Here are all of C’s reserved words.

10 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 10 Reserved words in Common Lisp Programming languages try to minimize the number of reserved words. Here are all of Lisp’s reserved words.

11 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 11 Names: implied attributes We often use conventions that associate attributes with name patterns. Some of these are just style conventions while others are part of the language spec and are used by compilers

12 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 12 Examples of implied attributes LISP: global variables begin and end with an asterisk, e.g., (if (> t *time-out-in-seconds*) (blue-screen)) JAVA: class names begin with an upper case character, field and method names with a lower case character for(Student s : theClass) s.setGrade(“A”); PERL: scalar variable names begin with a $, arrays with @, and hashtables with %, subroutines begin with a &. $d1 = “Monday”; $d2=“Wednesday”; $d3= “Friday”; @days = ($d1, $d2, $d3); FORTRAN: default variable type is float unless it begins with one of {I,J,K,L,M,N} in which case it’s integer DO 100 I = 1, N 100 ISUM = ISUM + I

13 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 13 Variables A variable is an abstraction of a memory cell Can represent complex data structures with lots of structure (e.g., records, arrays, objects, etc.) current_student

14 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 14 Attributes of Variables Variables can be characterized as a 6-tuple of attributes: –Name: identifier used to refer to the variable –Address: memory location(s) holding the variables value –Value: particular value at a moment –Type: range of possible values –Lifetime: when the variable can be accessed –Scope:where in the program it can be accessed

15 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 15 Variables: names and addresses Name - not all variables have them! Address - the memory address with which it is associated –A variable may have different addresses at different times during execution –A variable may have different addresses at different places in a program If two variable names can be used to access the same memory location, they are called aliases –Aliases are harmful to readability, but they are useful under certain circumstances

16 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 16 A variable which shall remain nameless Many languages allow one to create new data structures with only a pointer to refer to them. This is like a variable that does not really have a name (“that thing over there”), e.g.: Int *intNode;... intNode = new int;... Delete intNode; Some languages (e.g., shell scripts) assume you reference parameters not with names but by position (e.g., $1, $2) Some languages don’t have named variables at all! –E.g., if every function takes exactly one argument, we can dispense with the variable name –Arguments that naturally take several arguments (e.g., +) can be reduced to functions that take only a single argument. More on this when we talk of lisp, maybe.

17 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 17 Aliases When two names refer to the same memory location we can them aliases. Aliases can be created in many ways, depend- ing on the language. Pointers, reference variables, Pascal variant records, call by name, Prolog’s unification, C and C++ unions, and Fortran’s equivalence statemenr Aliases can be trouble. (how?) Some of the original justifications for aliases are no longer valid; e.g. memory reuse in FORTRAN Aliases can be powerful. Prolog’s unification offers new paradigms

18 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 18 Variables Type Typing is a rich subject we will talk about later. Most languages associate a variable with a single type The type determines the range of values and the operations allowed for a variable In the case of floating point, type usually also determines the precision (e.g., float vs. double) In some languages (e.g., Lisp, Prolog) a variable can take on values of any type. OO languages (e.g. Java) have a few primitive types (int, float, char) and everything else is a pointer to an object.

19 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 19 Variable Value The value is the contents of the memory location with which the variable is associated. Think of an abstract memory location, rather than a physical one. –Abstract memory cell - the physical cell or collection of cells associated with a variable A variable’s type will determine how the bits in the cell are interpreted to produce a value. We sometimes talk about lvalues and rvalues.

20 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 20 lvalue and rvalue Are the two occurrences of “a” in this expression the same? a := a + 1; In a sense, The one on the left of the assignment refers to the location of the variable whose name is a; The one on the right of the assignment refers to the value of the variable whose name is a; We sometimes speak of a variable’s lvalue and rvalue The lvalue of a variable is its address The rvalue of a variable is its value

21 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 21 Binding Def: A binding is an association, such as between an attribute and an entity, or between an operation and a symbol It’s like assignment, but more general We often talk of binding –a variable to a value, as in classSize is bound to the number of students –A symbol to an operator + is bound to the inner product operation Def: Binding time is the time at which a binding takes place.

22 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 22 Possible binding times Language design time, e.g., bind operator symbols to operations Language implementation time, e.g., bind floating point type to a representation Compile time, e.g., bind a variable to a type in C or Java Link time Load time, e.g., bind a FORTRAN 77 variable to memory cell (or a C static variable) Runtime, e.g., bind a nonstatic local variable to a memory cell

23 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 23 Type Bindings Def: A binding is static if it occurs before run time and remains unchanged throughout program execution. Def: A binding is dynamic if it occurs during execution or can change during execution of the program. Type binding issues include: How is a type specified? When does the binding take place? If static, type may be specified by either explicit or an implicit declarations

24 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 24 Variable Declarations Def: An explicit declaration is a program statement used for declaring the types of variables Def: An implicit declaration is a default mechanism for specifying types of variables (the first appearance of the variable in the program)

25 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 25 Implicit Variable Declarations Some examples of implicit type declarations –In C undeclared variables are assumed to be of type int –In Perl, variables of type scalar, array and hash begin with a $, @ or %, respectively. –Fortran variables beginning with I-N are assumed to be of type integer. –ML (and other languages) use sophisticated type inference mechanisms Advantages and disadvantages –Advantages: writability, convenience –Disadvantages: reliability – requiring explicit type declarations catches bugs revealed by type mis-matches

26 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 26 Dynamic Type Binding With dynamic binding, a variable’s type can change as the program runs and might be re- bound on every assignment. Used in scripting languages (Javascript, PHP) and some older languages (Lisp, Basic, Prolog, APL) In this APL example LIST is first a vector of integers and then of floats: LIST <- 2 4 6 8 LIST <- 17.3 23.5 Here’s a javascript example list = [2, 4.33, 6, 8]; list = 17.3;

27 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 27 Dynamic Type Binding The advantages of dynamic typing include –Flexibility for the programmer –Obviates the need for “polymorphic” types –Development of generic functions (e.g. sort) But there are disadvantages as well –Types have to be constantly checked at run time –A compiler can’t detect errors via type mis- matches Mostly used by scripting languages today

28 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 28 Type Inferencing Type Inferencing is used in some programming languages, including ML, Miranda, and Haskell. Types are determined from the context of the reference, rather than just by assignment statement. The compiler can trace how values flow through variables and function arguments The result is that the types of most variables can be deduced! –Any remaining ambiguity can be treated as an error the programmer must fix by adding explicit declarations Many feel it combines the advantages of dynamic typing and static typing

29 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 29 Type Inferencing in ML fun circumf(r) = 3.14159*r*r; // infer r is real fun time10(x) = 10*x; // infer r is integer fun square(x) = x*x; // can’t deduce types // default type is int We can explicitly type in several ways and enable the compiler to deduce that the function returns a real and takes a real argument. fun square(x):real = x*x; fun square(x:real) = x*x; fun square(x) = x:real*x; fun square(x) = x*x:real;

30 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 30 Storage Bindings and Lifetime Storage Bindings Allocation - getting a cell from some pool of available cells Deallocation - putting a cell back into the pool Def: The lifetime of a variable is the time during which it is bound to a particular memory cell Categories of variables by lifetimes Static Stack dynamic Explicit heap dynamic Implicit heap dynamic

31 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 31 Static Variables Static variables are bound to memory cells before execution begins and remains bound to the same memory cell throughout execution. Examples: All FORTRAN 77 variables C static variables Advantage: efficiency (direct addressing), history-sensitive subprogram support Disadvantage: lack of flexibility, no recursion!

32 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 32 Static Dynamic Variables Stack-dynamic variables -- Storage bindings are created for variables when their declaration statements are elaborated. If scalar, all attributes except address are statically bound –e.g. local variables in Pascal and C subprograms Advantages: –allows recursion –conserves storage Disadvantages: –Overhead of allocation and deallocation –Subprograms cannot be history sensitive –Inefficient references (indirect addressing)

33 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 33 Explicit heap-dynamic Explicit heap-dynamic variables are allocated and deallocated by explicit directives, specified by the programmer, which take effect during execution Referenced only through pointers or references e.g. dynamic objects in C++ (via new and delete ), all objects in Java Advantage: provides for dynamic storage management Disadvantage: inefficient and unreliable Example: int *intnode;... intnode = new int;... delete intnode;

34 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 34 Implicit heap-dynamic Implicit heap-dynamic variables -- Allocation and deallocation caused by assignment statements and types not determined until assignment. e.g. all variables in APL Advantage: –flexibility Disadvantages: –Inefficient, because all attributes are dynamic –Loss of error detection

35 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 35 Type Checking Generalize the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the operands of an operator are of compatible types A compatible type is one that is either legal for the operator, or is allowed under language rules to be implicitly converted, by compiler-generated code, to a legal type. This automatic conversion is called a coercion. A type error is the application of an operator to an operand of an inappropriate type Note: If all type bindings are static, nearly all checking can be static If type bindings are dynamic, type checking must be dynamic

36 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 36 Strong Typing A programming language is strongly typed if type errors are always detected There is strict enforcement of type rules with no exceptions. All types are known at compile time, i.e. are statically bound. With variables that can store values of more than one type, incorrect type usage can be detected at run-time. Strong typing catches more errors at compile time than weak typing, resulting in fewer run-time exceptions.

37 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 37 Which languages have strong typing? Fortran 77 isn’t because it doesn’t check parameters and because of variable equivalence statements. The languages Ada, Java, and Haskell are strongly typed. Pascal is (almost) strongly typed, but variant records screw it up. C and C++ are sometimes described as strongly typed, but are perhaps better described as weakly typed because parameter type checking can be avoided and unions are not type checked Coercion rules strongly affect strong typing—they can weaken it considerably (C++ versus Ada)

38 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 38 Type Compatibility Type compatibility by name means the two variables have compatible types if they are in either the same declaration or in declarations that use the same type name Easy to implement but highly restrictive: Subranges of integer types aren’t compatible with integer types Formal parameters must be the same type as their corresponding actual parameters (Pascal) Type compatibility by structure means that two variables have compatible types if their types have identical structures More flexible, but harder to implement

39 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 39 Type Compatibility Consider the problem of two structured types. Suppose they are circularly defined Are two record types compatible if they are structurally the same but use different field names? Are two array types compatible if they are the same except that the subscripts are different? (e.g. [1..10] and [-5..4]) Are two enumeration types compatible if their components are spelled differently? With structural type compatibility, you cannot differentiate between types of the same structure (e.g. different units of speed, both float)

40 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 40 Type Compatibility Language examples Pascal: usually structure, but in some cases name is used (formal parameters) C: structure, except for records Ada: restricted form of name –Derived types allow types with the same structure to be different –Anonymous types are all unique, even in: A, B : array (1..10) of INTEGER:

41 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 41 Variable Scope The scope of a variable is the range of statements in a program over which it’s visible Typical cases: Explicitly declared => local variables Explicitly passed to a subprogram => parameters The nonlocal variables of a program unit are those that are visible but not declared. Global variables => visible everywhere. The scope rules of a language determine how references to names are associated with variables. The two major schemes are static scoping and dynamic scoping

42 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 42 Static Scope Aka “lexical scope” Based on program text and can be determined prior to execution (e.g., at compile time) To connect a name reference to a variable, you (or the compiler) must find the declaration Search process: search declarations, first locally, then in increasingly larger enclosing scopes, until one is found for the given name Enclosing static scopes (to a specific scope) are called its static ancestors; the nearest static ancestor is called a static parent

43 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 43 Blocks A block is a section of code in which local variables are allocated/deallocated at the start/end of the block. Provides a method of creating static scopes inside program units Introduced by ALGOL 60 and found in most PLs. Variables can be hidden from a unit by having a "closer" variable with same name C++ and Ada allow access to these "hidden" variables

44 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 44 Examples of Blocks C and C++: for (...) { int index;... } Ada: declare LCL:FLOAT; begin... end Common Lisp: (let ((a 1) (b foo) (c)) (setq a (* a a)) (bar a b c))

45 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 45 Static scoping example MAIN A B C D E MAIN A C D B E MAIN A B C D E MAIN calls A and B A calls C and D B calls A and E

46 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 46 Evaluation of Static Scoping Suppose the spec is changed so that D must now access some data in B Solutions: 1. Put D in B (but then C can no longer call it and D cannot access A's variables) 2. Move the data from B that D needs to MAIN (but then all procedures can access them) Same problem for procedure access! Overall: static scoping often encourages many globals

47 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 47 Dynamic Scope Based on calling sequences of program units, not their textual layout (temporal versus spatial) References to variables are connected to declarations by searching back through the chain of subprogram calls that forced execution to this point Used in APL, Snobol and LISP –Note that these languages were all (initially) implemented as interpreters rather than compilers. Consensus is that PLs with dynamic scoping leads to programs which are difficult to read and maintain. –Lisp switch to using static scoping as it’s default circa 1980, though dynamic scoping is still possible as an option.

48 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 48 Static vs. dynamic scope Define MAIN declare x Define SUB1 declare x... call SUB2... Define SUB2... reference x... call SUB1... MAIN calls SUB1 SUB1 calls SUB2 SUB2 uses x Static scoping - reference to x is to MAIN's x Dynamic scoping - reference to x is to SUB1's x

49 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 49 Dynamic Scoping Evaluation of Dynamic Scoping: Advantage: convenience Disadvantage: poor readability

50 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 50 Scope vs. Lifetime While these two issues seem related, they can differ In Pascal, the scope of a local variable and the lifetime of a local variable seem the same In C/C++, a local variable in a function might be declared static but its lifetime extends over the entire execution of the program and therefore, even though it is inaccessible, it is still in memory

51 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 51 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, that is the local variables plus all of the visible variables in all of the enclosing scopes. See book example (p. 184) A subprogram is active if its execution has begun but has not yet terminated In a dynamic-scoped language, the referencing environment is the local variables plus all visible variables in all active subprograms.

52 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 52 Named Constants A named constant is a variable that is bound to a value only when it is bound to storage. The value of a named constant can’t be changed while the program is running. The binding of values to named constants can be either static (called manifest constants) or dynamic Languages: Pascal: literals only Modula-2 and FORTRAN 90: constant-valued expressions Ada, C++, and Java: expressions of any kind Advantages: increased readability and modifiability without loss of efficiency

53 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 53 Example in Pascal Procedure example; type a1[1..100] of integer; a2[1..100] of real;... begin... for I := 1 to 100 do begin... end;... for j := 1 to 100 do begin... end;... avg = sum div 100;... Procedure example; type const MAX 100; a1[1..MAX] of integer; a2[1..MAX] of real;... begin... for I := 1 to MAX do begin... end;... for j := 1 to MAX do begin... end;... avg = sum div MAX;...

54 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 54 Variable Initialization For convenience, variable initialization can occur prior to execution FORTRAN: Integer Sum Data Sum /0/ Ada: Sum : Integer := 0; ALGOL 68: int first := 10; Java: int num = 5; LISP (Let (x y (z 10) (sum 0) )... )

55 CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 55 Summary In this chapter, we see the following concepts being described Variable Naming, Aliases Binding and Lifetimes Type variables Scoping Referencing environments Named Constants Type Compatibility Rules


Download ppt "CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 1 Chapter 5 Chapter 5 Variables: Names, Bindings, Type Checking and Scope."

Similar presentations


Ads by Google