Nested Scopes and Declaration Order

Nested Scopes and Declaration Order

Class 14: Nested Scopes and Declaration Order SI 413 - Programming Languages and Implementation Dr. Daniel S. Roche United States Naval Academy Fall 2011 Roche (USNA) SI413 - Class 14 Fall 2011 1 / 10 Homework Review i n t x = 10; i n t foo( i n t y){ x=y+5; print(x); } i n t main(){ i n t x = 8; foo(9); print(x); } What happens in a dynamic vs. lexically scoped language? Roche (USNA) SI413 - Class 14 Fall 2011 2 / 10 Another dynamic/lexical example i n t width = 10; char justification= 'L' ; v o i d print(strings){ i n t space= width- length(s); i f (justification == 'L' ) print(s); f o r ( i n t i=0;i<space; ++i) print( ' ' ); i f (justification == 'R' ) print(s); } Suppose we want a function foo that prints a series of names, using the existing print function, all right-justified to 20 characters width. How would we write this in a dynamic vs. a lexically scoped language? Roche (USNA) SI413 - Class 14 Fall 2011 3 / 10 What would the effect of nested function calls be on the above strategies? Another dynamic/lexical example In a dynamically scoped language, we could just write v o i d foo(string names[], i n t n){ i n t width = 20; char justification= 'R' ; f o r ( i n t i=0;i<n; ++i) print(names[i]); } In a lexically-scoped language, we would have to change the global values of width and justification, and then (to be nice) change them back before returning. Roche (USNA) SI413 - Class 14 Fall 2011 4 / 10 Another dynamic/lexical example In a dynamically scoped language, we could just write v o i d foo(string names[], i n t n){ i n t width = 20; char justification= 'R' ; f o r ( i n t i=0;i<n; ++i) print(names[i]); } In a lexically-scoped language, we would have to change the global values of width and justification, and then (to be nice) change them back before returning. What would the effect of nested function calls be on the above strategies? Roche (USNA) SI413 - Class 14 Fall 2011 4 / 10 Nested scopes Certain language structures create a new scope. For example: i n t temp= 5; // Sorts a two-element array. void t w o s o r t( i n t A[]) f i f (A[0] > A[1]) f i n t temp=A[0]; A[0] =A[1]; A[1] = temp; g g i n t main() f i n t a r r= f2 , 1 g ; t w o s o r t(arr); cout << temp;// Prints 5, even with dynamic scoping! g Roche (USNA) SI413 - Class 14 Fall 2011 5 / 10 Nested Scopes In C++, nested scopes are made using curly braces (f and g). The scope resolution operator :: allows jumping between scopes manually. In most languages, function bodies are a nested scope. Often, control structure blocks also form nested scopes (e.g. for, if , etc.) Lexical scoping creates a tree structure with the nested scopes. Every name that is visible within some scope is either defined locally within that scope, or is defined above somewhere on the path from the root. Roche (USNA) SI413 - Class 14 Fall 2011 6 / 10 15; 14; 13; 12; 11 Nested Functions With nested functions, we have to consider scope and allocation rules. void f( i n t a, i n t b) f i n t g( i n t c) f return a+c; g i f (a == 0) return ; p r i n t(g(g(b))); f(a −1,b+1); g What integers are printed from the call f(5,5)? Roche (USNA) SI413 - Class 14 Fall 2011 7 / 10 Nested Functions With nested functions, we have to consider scope and allocation rules. void f( i n t a, i n t b) f i n t g( i n t c) f return a+c; g i f (a == 0) return ; p r i n t(g(g(b))); f(a −1,b+1); g What integers are printed from the call f(5,5)? 15; 14; 13; 12; 11 Roche (USNA) SI413 - Class 14 Fall 2011 7 / 10 Declaration Order In many languages, variables must be declared before they are used. (Otherwise, the first use of a variable constitutes a declaration.) In C/C++, the scope of a name starts at its declaration and goes to the end of the scope. Every name must be declared before its first use, because names are resolved as they are encountered. C++ and Java make an exception for names in class scope. Scheme doesn't resolve names until they are evaulated. Roche (USNA) SI413 - Class 14 Fall 2011 8 / 10 Declaration Order and Mutual Recursion Consider the following familar code: void exp() f atom(); exptail(); g void atom() f switch (peek()) f case LP: match(LP); exp(); match(RP); break ; //... g g Mutual recursion in C/C++ requires forward declarations, i.e., function prototypes. These wouldn't be needed within a class definition or in Scheme. C# and Pascal solve the problem in a different way. Roche (USNA) SI413 - Class 14 Fall 2011 9 / 10 Class outcomes You should know: Relative advantages of dynamic and lexical scoping. The motivation behind declare-before-use rules, and their effect on mutual recursion. You should be able to: Draw the tree of nested scopes for a lexically-scoped program. Trace a program with nested function calls using lexical scoping. Roche (USNA) SI413 - Class 14 Fall 2011 10 / 10.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    12 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us