DOC PREVIEW
UA CSC 520 - Study Notes

This preview shows page 1 out of 3 pages.

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

Unformatted text preview:

Static vs. Dynamic ScopeDynamic ScopeDynamic Scopeldots Dynamic Scope --- ProblemsDynamic Scope --- AdvantagesDynamic Scope --- Advantagesldots Dynamic Scope --- Advantagesldots Readings and References520—Spring 2005—34CSc 520Principles of ProgrammingLanguages34: Procedures — Dynamic ScopeChristian [email protected] of Computer ScienceUniversity of ArizonaCopyrightc 2005 Christian Collberg[1]520—Spring 2005—34Static vs. Dynamic ScopePascal is lexically scoped. We can look (textually, or atcompile-time) at a procedure and determine to whichobject an identifier refers.Some languages (Snobol, APL, Perl, some dialects ofLISP) aredynamically scoped. The binding between anidentifier and the object it refers to is not decided untilrun-time.[2]520—Spring 2005—34Dynamic ScopeThe current binding for an identifier is the one last seenduring execution and whose scope has yet to bedestroyed.Consider the example on the next slide.static scope: the program prints 1.dynamic scope: the program prints 2.Static scope rules match the use of an identifier with theclosest lexically enclosing declaration.Dynamic scope rules choose the most recent activedeclaration at runtime.[3]520—Spring 2005—34Dynamic Scope...var a : integer;procedure first();a := 1;procedure second();var a : integer;first();begina := 2;second();write(a);end[4]520—Spring 2005—34Dynamic Scope — Problemsvar max : integer;procedure scale(x : integer) : real;return x/max;procedure compute(y : integer);var max : integer;write(scale(y));Dynamic scope makes it is easy to accidentally redefinea variable.[5]520—Spring 2005—34Dynamic Scope — Advantagesprocedure A(base : integer)printInt(base, 245);procedure B(base : integer)A();procedure C(base : integer)B();begin C(16); endWe often have to pass around state so that deeplynested procedures can make use of it.DEBUG-flags is acommon example.[6]520—Spring 2005—34Dynamic Scope — Advantages...var base : integer := 10;procedure A()printInt(base, 245);procedure B()A();procedure C()B();beginvar lastbase := base;base := 16; C();base := last base;endWe can, of course, use global variables.[7]520—Spring 2005—34Dynamic Scope — Advantages...procedure A()printInt(base, 245);procedure B()A();procedure C()B();beginvar base : integer := 16;C();endDynamic scope makes it is easy customize the behaviorof procedures.[8]520—Spring 2005—34Readings and ReferencesRead Scott, pp. 115–116, 129–132, 139–144, 298,471–479Dynamic Variables, David R. Hanson and Todd A.Proebsting, PLDI


View Full Document

UA CSC 520 - Study Notes

Documents in this Course
Handout

Handout

13 pages

Semantics

Semantics

15 pages

Haskell

Haskell

15 pages

Recursion

Recursion

18 pages

Semantics

Semantics

12 pages

Scheme

Scheme

32 pages

Syllabus

Syllabus

40 pages

Haskell

Haskell

17 pages

Scheme

Scheme

27 pages

Scheme

Scheme

9 pages

TypeS

TypeS

13 pages

Scheme

Scheme

27 pages

Syllabus

Syllabus

10 pages

Types

Types

16 pages

FORTRAN

FORTRAN

10 pages

Load more
Download Study Notes
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 Study Notes 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 Study Notes 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?