DOC PREVIEW
U of I CS 421 - Lecture 16

This preview shows page 1-2 out of 5 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 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 5 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

OutlineNames and VariablesBindingBinding CategoriesScopeCS421 Lecture 16: Names and Variables1Mark [email protected] of Illinois at Urbana-ChampaignJuly 17, 20061Based on slides by Mattox Beckman, as updated by Vikram Adve, GulAgha, and Elsa GunterMark Hills CS421 Lecture 16: Names and VariablesOutlineNames and VariablesBindingBinding CategoriesScopeNames and VariablesBindingBinding CategoriesScopeMark Hills CS421 Lecture 16: Names and VariablesOutlineNames and VariablesBindingBinding CategoriesScopeObjectivesBy the end of this lecture, you should◮understand the attributes of variables in a programminglanguage, including concepts such as type and value◮understand the distinction between static and dynamicproperties of variables◮be familiar with the concept of scope, specifically differencesbetween static and dynamic scopeMark Hills CS421 Lecture 16: Names and VariablesOutlineNames and VariablesBindingBinding CategoriesScopeOverviewVariable AttributesNamesDef: A name is a string of characters used to identify some entityin a program2◮Names (or identifiers) give us a way to name programconcepts, such as types, values, classes, functions, etc.◮Different languages have different rules for names◮Case-sensitive (C, C++, Java) or not (Pascal, Lisp)?◮Are there special words in a language that are reserved words,or are there just keywords◮Naming requirements/conventions◮Name length and allowed characters2Sebesta,2nd edition,p.207Mark Hills CS421 Lecture 16: Names and VariablesOutlineNames and VariablesBindingBinding CategoriesScopeOverviewVariable AttributesVariablesA variable is just an abstraction of memory. We can define avariable as a sextuple of attributes:◮Name◮Address◮Value◮Type◮Lifetime◮ScopeMark Hills CS421 Lecture 16: Names and VariablesOutlineNames and VariablesBindingBinding CategoriesScopeOverviewVariable AttributesVariables: NamesRemember, names are defined with certain characters andconventions◮In Haskell and ML, variables start with lowercase letters◮In Haskell and Ml, type constructors start with uppercaseletters◮C-family languages have no similar restrictions, but generallyuse conventions (Java camel-case, etc)◮Older Fortran had a 6-character limit, all uppercase names,and “type inference” for variables based on the first letterMost variables have names, although some do not (heap-allocatedvariables may not, more on this later)Mark Hills CS421 Lecture 16: Names and VariablesOutlineNames and VariablesBindingBinding CategoriesScopeOverviewVariable AttributesVariables: AddressThe address is the machine memory address that is associated withthe variable. This can be trickier than it sounds:◮Multiple variables may refer to the same address – aliasing◮pointers and references◮Equivalence-type statements and unions◮function calls◮The same name may have multiple addresses at differenttimes◮OS-based – different ph ysical address, same virtual address◮language-based – variables in functions may have differentaddresses on each callSometimes called an l-value, since a variable’s address is used onthe left-hand side of an assignment.Mark Hills CS421 Lecture 16: Names and VariablesOutlineNames and VariablesBindingBinding CategoriesScopeOverviewVariable AttributesVariables: Typ e sThe type of a variable determines what kinds of values the variablecan refer to. We covered this in Lecture 6.Mark Hills CS421 Lecture 16: Names and VariablesOutlineNames and VariablesBindingBinding CategoriesScopeOverviewVariable AttributesVariables: ValuesThe value is what we typically think of when we talk aboutvariables. This is just the data associated with the variable.◮We will think in terms of abstract memory cells to hold thevalue – that way we don’t have to worry aboutrepresentational issues (word size, packing, bit formats, etc)◮The value is sometimes called the r-value, since it is used onthe right-hand side of an assignment; we must know thel-value to get the r-valueMark Hills CS421 Lecture 16: Names and VariablesOutlineNames and VariablesBindingBinding CategoriesScopeBindingBinding TimesStatic and DynamicDeclarations, Storage, and LifetimeBindingWhen we talk about associating attributes with variables, we aretalking about binding.◮Def: In the general sense, a binding is an associationbetween two items, such as varibles and attributes◮Def: The time this association occurs is the binding timeMark Hills CS421 Lecture 16: Names and VariablesOutlineNames and VariablesBindingBinding CategoriesScopeBindingBinding TimesStatic and DynamicDeclarations, Storage, and LifetimeBinding TimesBinding can take place at:◮Language design time (identifying ∗ as multiplication)◮Language implementation time (an int takes 4 bytes)◮Compile time (variable x is an int)◮Load time (variable x is in storage location 10)◮Link time (function f is in load module m)◮Run time (x has the value 10)Mark Hills CS421 Lecture 16: Names and VariablesOutlineNames and VariablesBindingBinding CategoriesScopeBindingBinding TimesStatic and DynamicDeclarations, Storage, and LifetimeExampleWhat are the bindings here, and when do they occur?count = count + 5;Mark Hills CS421 Lecture 16: Names and VariablesOutlineNames and VariablesBindingBinding CategoriesScopeBindingBinding TimesStatic and DynamicDeclarations, Storage, and LifetimeStatic vs. Dynamic BindingsBindings are either static or dynamic:◮Static bindings first occur before run time and remainunchanged during program execution◮Dynamic bindings either occur during run time or occur earlierbut can be changed during run timeWe’ve already seen one example of this in Lecture 6 – dynamictyping, where the type is either not computed prior to execution orcan change during execution.Mark Hills CS421 Lecture 16: Names and VariablesOutlineNames and VariablesBindingBinding CategoriesScopeBindingBinding TimesStatic and DynamicDeclarations, Storage, and LifetimeAn Aside: ConstantsConstants are similar to variables except the value, once assigned,cannot be changed.◮Usually they can be allocated statically◮Some languages allow constants that are defined once atruntime but then not changed (sometimes called readonly)◮Literals can be thought of as constants (i.e. 5, “hello”, etc.)Mark Hills CS421 Lecture 16: Names and VariablesOutlineNames and VariablesBindingBinding CategoriesScopeBindingBinding TimesStatic and DynamicDeclarations, Storage, and LifetimeDeclarationsWe can use a


View Full Document

U of I CS 421 - Lecture 16

Documents in this Course
Lecture 2

Lecture 2

12 pages

Exams

Exams

20 pages

Lecture

Lecture

32 pages

Lecture

Lecture

21 pages

Lecture

Lecture

15 pages

Lecture

Lecture

4 pages

Lecture

Lecture

68 pages

Lecture

Lecture

68 pages

Lecture

Lecture

84 pages

s

s

32 pages

Parsing

Parsing

52 pages

Lecture 2

Lecture 2

45 pages

Midterm

Midterm

13 pages

LECTURE

LECTURE

10 pages

Lecture

Lecture

5 pages

Lecture

Lecture

39 pages

Load more
Download Lecture 16
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 Lecture 16 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 Lecture 16 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?