CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 1Chapter 5Chapter 5Variables:Names, Bindings, Type Checking and ScopeCMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 2IntroductionThis 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.CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 3Names• Humans are probably 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.CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 4NamesThere 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 attributesCMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 5Names: 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 to allow longer or unlimited length names• Is this always good? – What are some advantages and disadvantages of very long names?CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 6Names: 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 _, ., -, …- Fortran 90 allowed spaces as connectors.- Languages with infix operators typically only allow _, reserving ., -, + as operators- LISP: first-name- C: first_name- “Camel notation” popular in C, Java, C#...- Using upper case characters to break up a long name, e.g.-firstNameCMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 7Names: 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 notCMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 8Special wordsDef: A keyword is a word that is special only in certain contexts–Disadvantage: poor readability–Advantage: flexibilityDef: A reserved word is a special word that cannot be used as a user-defined nameSome PLs have reserved words and others do not.CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 9Names: implied attributes• We often use conventions that associate attributes with name patterns, e.g.:• LISP: global variables begin and end with a * as in (if (> t *time-out-in-seconds*) …)• JAVA: class names begin with an upper case character, field and method names begin with a lower case char• PERL: scalar variable ‘names’ begin with a $, arrays with @ , and hashtables with a %. Subroutines begin with a &.• FORTRAN: default type of a variable is float unless it begins with an I, J, K, L, M, or N.• Some of these are just programming conventions, and others are part of the language spec. (which?)CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 10Variables•A variable is an abstraction of a memory cell• Of course, variables can represent complex data structures with lots of structure (e.g., records, arrays)• Variables can be characterized as a 6-tuple of attributes:Name: identifierAddress: memory location(s)Value: particular value at a momentType: range of possible valuesLifetime: when the variable can be accessedScope: where in the program it can be accessedCMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 11Variables: names and addresses• Name - not all variables have them (examples?)• 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 circumstancesCMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 12A variable who 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 a variable name – Arguments that naturally take several arguments (e.g., +) can bereduced to functions that take only a single argument– More on this when we talk of lisp, maybe.CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 13Aliases• When two names refer to the same memory location we can them aliases.• Aliases can be created in many ways, depending on the language.• Pointers, reference variables, Pascal variant records, Call by name, prolog’s unification, C and C++ unions, and FORTRAN EQUIVALENCE• Aliases can be trouble. (how?)• Some of the original justifications for aliases
View Full Document