DOC PREVIEW
UMBC CMSC 331 - Variables: Names, Bindings, Type Checking and Scope

This preview shows page 1-2-3-4 out of 11 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 1Chapter 4Chapter 4Variables: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. 3NamesNamesDesign issues:Maximum length?Are connector characters allowed?Are names case sensitive?Are special words reserved words or keywords?LengthFORTRAN I: maximum 6COBOL: maximum 30FORTRAN 90 and ANSI C: maximum 31Ada: no limit, and all are significantC++: no limit, but implementors often impose oneConnectorsPascal, Modula-2, and FORTRAN 77 don't allowOthers doCMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 4Case sensitivity• Foo = Foo?• The first languages only had upper case• Case sensitivity was probably introduced by Unix and hence C.• 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. 5Special 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 nameCMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 6Variables• A variable is an abstraction of a memory cell• 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 accessibleScope: where in the program it can be accessedCMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 7Variables• 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. 8Aliases• How aliases can be created:• Pointers, reference variables, Pascal variant records, C and C++ unions, and FORTRAN EQUIVALENCE (and through parameters - discussed in Chapter 8)• Some of the original justifications for aliases are no longer valid; e.g. memory reuse in FORTRAN • replace them with dynamic allocationCMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 9Variables Type and ValueType - determines the range of values of variables and the set of operations that are defined for values of that type; in the case of floating point, type also determines the precisionValue - the contents of the location with which the variable is associated• Abstract memory cell - the physical cell or collection of cells associated with a variableCMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 10lvalue and rvalueAre 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 valueCMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 11BindingDef: A binding is an association, such as between an attribute and an entity, or between an operation and a symbolDef: Binding time is the time at which a binding takes place.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 cellCMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 12Type Bindings• Def: A binding is static if it occurs before run time and remains unchanged throughout program execution.• Def: A binding is dynamicif it occurs during execution or can change during execution of the program.• Type binding issues• How is a type specified?• When does the binding take place? • If static, type may be specified by either an explicit or an implicit declarationCMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 13Variable DeclarationsDef: An explicit declaration is a program statement used for declaring the types of variablesDef: An implicit declaration is a default mechanism for specifying types of variables (the first appearance of the variable in the program)– E.g.: in Perl, variables of type scalar, array and hash begin with a $, @ or %, respectively. – E.g.: In Fortran, variables beginning with I-N are assumed to be of type integer.– E.g.: ML (and other languages) use sophisticated type inference mechanismsAdvantages: writability, convenienceDisadvantages: reliabilityCMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 14Dynamic Type Binding• The type of a variable can chance during the course of the program and, in general, is re-determined on every assignment.• Usually associated with languages first implemented via an interpreter rather than a compiler.• Specified through an assignment statement, e.g. APLLIST <- 2 4 6 8LIST <- 17.3 23.5• Advantages:• Flexibility• Obviates the need for “polymorphic” types• Development of generic functions (e.g. sort)•


View Full Document

UMBC CMSC 331 - Variables: Names, Bindings, Type Checking and Scope

Documents in this Course
Semantics

Semantics

14 pages

Java

Java

12 pages

Java

Java

31 pages

V

V

46 pages

Semantics

Semantics

11 pages

Load more
Download Variables: Names, Bindings, Type Checking and 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 Variables: Names, Bindings, Type Checking and 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 Variables: Names, Bindings, Type Checking and 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?