DOC PREVIEW
U of I CS 421 - Programming Languages and Compilers

This preview shows page 1-2-16-17-18-34-35 out of 35 pages.

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

Unformatted text preview:

Programming Languages andCompilers (CS 421)Elsa L Gunter2112 SC, UIUChttp://www.cs.uiuc.edu/class/fa06/cs421/Based in part on slides by Mattox Beckman, as updatedby Vikram Adve and Gul AghaElsa L. GunterVariables and Data Objects• Attributes of a variable:– Name– Scope– Lifetime– Data Object (abstract memory cell)• Each variable is associated with aunique data object• Data object may be associated withmore than one variable (aliasing)Elsa L. GunterData Objects• Structured storage location for data• Specification– Attributes– Values– Operations• Implementation– Storage location and layout– Access– OperationsElsa L. GunterData Object Attributes• Data type:–Set of values that may be storedin data object, and set of basicoperation which may beperformed on itElsa L. GunterData Object Attributes• Location:–Virtual machine may allowprogrammer to view as fixed, butmay be moved on actualhardware by storagemanagement of virtual machineElsa L. GunterData Object Attributes• Value• Name(s)• Components:– Binding of one or more data objectsas part of the given one (eg. pairs)• Lifetime– Allocation to garbage collectionElsa L. GunterData Object Versus DataValue• Data object:– A location in memory to hold dataaccessed by the variable name• Data value: 110110– Binary representation of the number54• Data value stored in data object:00110110Elsa L. GunterBinding of Data Objects• A variable is a binding of a name toa data object:– Data object is storage for values– Contents of the object may change• Different binding means differentvariableElsa L. GunterBindings of Program Elements• A binding of a program element isthe selection of a particular valuefor it from a set of possible values• The binding time is when thatselection is made• Binding time can depend onwhether we use a compiler or aninterpreterElsa L. GunterBinding Times• Language definition time: languagesyntax and semantics• Language implementation time:interpreter versus compiler, aspectsleft flexible in definition, set ofavailable librariesElsa L. GunterBinding Times• Compile time: some initial datalayout, internal data structures• Link time (load time): binding ofvalues to identifiers across programmodules• Run time (execution time): actualvalues assigned to non-constantidentifiersElsa L. GunterTo class• In expression int x = y + 5 whatbindings can you identify, and whenare they most likely made?Elsa L. GunterBinding of Data Objects andVariables• Attributes of data objects and variableshave different binding times• If a binding is made before run time andremains fixed through execution, it iscalled static• If the binding first occurs or can changeduring execution, it is called dynamicElsa L. GunterConstants and Variables• A named data object which may haveits data value changed is called avariable– Usually dynamically allocated• A named data object with a fixed datavalue is called a constant– Usually statically allocated• Some constants have names that wethink of as their values (eg 2 or “hi”).These are called literalsElsa L. GunterDeclarations• User defined variable and associateddata object, created at compile time• Declaration tells for a variable its:– Name– Type– Scope - which may also indicate itslifetime– Can be implicit as part of an initialassignmentElsa L. GunterUses for Declarations• Determine storage representations• Help with storage management• Resolve overloaded constants• Help type checkingElsa L. GunterStatic vs Dynamic TypeChecking• If types of all variables and signaturesof all procedures ar known at compiletime, type checking can be donestatically– Runtime code more efficient since noruntime type checks• If variable types only known at runtime,must use dynamic type checking– Variables may be used at multiple typesElsa L. GunterAssignment• Data object (and variable) has twovalues associated with it:– Location and value• In assignment X := Y we are sayingthe location of X gets the value of Y• Location called l-value of object (valueused when on left of assignment• Value called r-value (value used onright of assignmentElsa L. GunterInitialization• When a variable is declared, a dataobject is created for it• Before it is initialized it containsrandom data (bit strings)Elsa L. GunterInitialization• If used before initialized, programwill probably not recognize therandom string is junk and will use it• Hard to find such bugs• Good language design forcesinitialization as part of declarationElsa L. GunterScope of Variable• Range of program that can referencethat variable (ie access thecorresponding data object by thevariable’s name)• Variable is local to program or block if itis declared there• Variable is nonlocal to program unit if itis visible there but not declared thereElsa L. GunterLifetime• Lifetime of a variable is the duration ofthe program execution during which thevariable - binding of the name to dataobject - exists– Should contain the variable’s scope– May exceed the scope through hiding• Lifetime of data object is the period fromits allocation to its garbage collection– Needs to contain lifetime of all variablesassociated with it– Failure of this leads to dangling pointersElsa L. GunterStatic Scoping• Scope computed at compile time,based on program text• To determine the variable of aname used must find statementdeclaring variableElsa L. GunterStatic Scoping• Subprograms and blocks generatehierarchy of scopes–Subprogram or block thatdeclares current subprogram orcontains current block is its staticparentElsa L. GunterStatic Scoping• General procedure to finddeclaration:– First see if variable is local; if yes,done– If nonlocal to current subprogram orblock recursively search static parentuntil declaration is found– If no declaration is found this way,undeclared variable error detectedElsa L. GunterExampleprogram main; var x : integer; procedure sub1; var x : integer; begin { sub1 } … x … end; { sub1 }begin { main } … x … end; { main }Elsa L. GunterDynamic Scope• Now generally thought to havebeen a mistake• Main example of use: originalversions of LISP–Common LISP uses static scope–Perl allows variables to bedecalred to have dynamic scopeElsa L. GunterDynamic Scope• Determined by the callingsequence of program units, notstatic layout• Name bound to correspondingvariable most


View Full Document

U of I CS 421 - Programming Languages and Compilers

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 Programming Languages and Compilers
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 Programming Languages and Compilers 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 Programming Languages and Compilers 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?