Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Dynamic vs Static Scoping problem?
Yes, this is a class problem. However there was no lecture on this and there are no examples in the book so I don't understand. The professor specifically instructed us to "google it". So:
Consider the following program:
proc main
int a, x, z;
proc A:
proc B;
int x, z;
{ x := 4; call C; print x; } (end B)
proc C;
int a, z; { x := 6; z := 9; a := 2; call D; print x; print a;} (end C)
{ x := 5; z := 7; call B; print x; } (end A)
proc D;
int x, b;
proc E;
int x; { x := 7 ; call F; print x; } (end E)
proc F;
proc G;
{print z; x:= 10 ; a:= 8 ;} (end G)
{print x; call G; x:=9 ; } (end F)
{ x := 8 ; call E; print x; } (end D)
{ a := 4; x:= 3; z := 11 ; call A; print x; print a;} (end main)
(a) What is the program output assuming static scoping rules are employed?
(b) What is the corresponding output when dynamic scoping rules are employed?
(c) List the units that proc G can call (assuming static scoping ).
1 Answer
- Anonymous8 years agoFavorite Answer
Scoping defines the rules by which the variable identifiers in your code (i.e. names) are mapped to the actual variables.
Static (or lexical) scoping determines the mapping, by which variable names are mapped based on the program code structure. First the immediate scope (i.e. the block between proc..end in your pseudo-language), that is the scope where the variable is referenced, is searched for the variable declaration. If it's there, the name is bound to this variable. If it's not, the name is looked up in the parent scope (based on the code structure - i.e. on the program text), in the grandparent, etc.