CSE 3302 Programming LanguagesSymbol TableStatic vs. Dynamic ScopeStatic ScopeSlide 5Slide 6Slide 7Practice for Static ScopeWhat if dynamic scope is used?Slide 10Slide 11Slide 12Practice for Dynamic ScopeOverloadingOverload ResolutionFunction/Method OverloadingOverload Resolution ExampleEnvironmentStack-Based AllocationsExampleSlide 21Slide 22Slide 23Slide 24Heap-Based AllocationScope vs. LifetimeExample: Alive in scope holeExample: Alive outside scopeExample: Scope beyond lifetimeBox-and-Circle Diagram for VariablesAssignment by sharingAssignment by cloningAliasesSlide 34Slide 35Slide 36Slide 37Practice for AliasesDangling ReferencesSlide 40Slide 41CSE 3302 Programming LanguagesChengkai Li, Weimin HeSpring 2008Semantics (cont.)Lecture 6 - Semantics, Spring 20081CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008Symbol Table•Symbol Table: maintain bindings. Can be viewed as functions that map names to their attributes.Lecture 6 - Semantics, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20082Names AttributesSymbolTableStatic vs. Dynamic Scope•Static scope (lexical scope): –scope maintained statically (during compilation) –follow the layout of source codes–used in most languages•Dynamic scope: –scope maintained dynamically (during execution) –follow the execution path–few languages use it (The bindings cannot be determined statically, may depend on user input).•Lisp: considered a bug by its inventor.•Perl: can choose lexical or dynamic scopeLecture 6 - Semantics, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20083Static ScopeLecture 6 - Semantics, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20084int x = 1;char y = ‘a’;void p(void) {double x=2.5;printf(“%c\n”,y);}void q(void) {int y = 42;printf(“%d\n”,x);p();}main() {char x = ‘b’;q();}xinteger, globalcharacter, globalyStatic ScopeLecture 6 - Semantics, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20085int x = 1;char y = ‘a’;void p(void) {double x=2.5;printf(“%c\n”,y);}void q(void) {int y = 42;printf(“%d\n”,x);p();}main() {char x = ‘b’;q();}xinteger, globalCharacter, globalydouble, local to pThe symbol table in p: the bindings available in pStatic ScopeLecture 6 - Semantics, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20086int x = 1;char y = ‘a’;void p(void) {double x=2.5;printf(“%c\n”,y);}void q(void) {int y = 42;printf(“%d\n”,x);p();}main() {char x = ‘b’;q();}xinteger, globalcharacter, globalinteger, local to qyThe symbol table in q: the bindings available in qStatic ScopeLecture 6 - Semantics, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20087int x = 1;char y = ‘a’;void p(void) {double x=2.5;printf(“%c\n”,y);}void q(void) {int y = 42;printf(“%d\n”,x);p();}main() {char x = ‘b’;q();}xinteger, globalcharacter, globalyThe symbol table in main: the bindings available in maincharacter, local to mainPractice for Static ScopeLecture 6 - Semantics, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20088int x,y;void g(void) { x = x + 1; y = x + 1;}void f(void) { int x; y = y + 1; x = y + 1; g();}main() { x = 1; y = 2; f(); g(); printf("x=%d,y=%d\n",x,y);}Question 1: Draw the symbol table at the given points in the program, using static scope?Question 2: What does the program print, using static scope?Point 1Point 2Point 3What if dynamic scope is used?Lecture 6 - Semantics, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20089int x = 1;char y = ‘a’;void p(void) {double x=2.5;printf(“%c\n”,y);}void q(void) {int y = 42;printf(“%d\n”,x);p();}main() {char x = ‘b’;q();}xinteger, 1, globalcharacter, ‘a’, globalyWhat if dynamic scope is used?Lecture 6 - Semantics, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200810int x = 1;char y = ‘a’;void p(void) {double x=2.5;printf(“%c\n”,y);}void q(void) {int y = 42;printf(“%d\n”,x);p();}main() {char x = ‘b’;q();}xinteger, 1, globalcharacter, ‘b’, local to maincharacter, ‘a’, globalyThe symbol table in main: the bindings available in mainWhat if dynamic scope is used?Lecture 6 - Semantics, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200811int x = 1;char y = ‘a’;void p(void) {double x=2.5;printf(“%c\n”,y);}void q(void) {int y = 42;printf(“%d\n”,x);p();}main() {char x = ‘b’;q();}xinteger, 1, globalcharacter, ‘b’, local to maincharacter, ‘a’, globalinteger, 42, local to qyThe symbol table in q: the bindings available in q98What if dynamic scope is used?Lecture 6 - Semantics, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200812int x = 1;char y = ‘a’;void p(void) {double x=2.5;printf(“%c\n”,y);}void q(void) {int y = 42;printf(“%d\n”,x);p();}main() {char x = ‘b’;q();}xinteger, 1, globalcharacter, ‘b’, local to maincharacter, ‘a’, globalinteger, 42, local to qydouble, 2.5, local to pThe symbol table in p: the bindings available in p98*Practice for Dynamic ScopeLecture 6 - Semantics, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200813int x,y;void g(void) { x = x + 1; y = x + 1;}void f(void) { int x; y = y + 1; x = y + 1; g();}main() { x = 1; y = 2; f(); g(); printf("x=%d,y=%d\n",x,y);}Question 1: Draw the symbol table at the given points in the program, using dynamic scope?Question 2: What does the program print, using dynamic scope?Point 1Point 2Point 3Overloading•What is overloading?•Why overloading?•What can be overloaded?Lecture 6 - Semantics, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200814Overload Resolution•Overload Resolution: select one entity.•Name isn’t sufficient in resolution: need extra information (often data types)Lecture 6 - Semantics, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200815Function/Method Overloading•C: no overloading •C++/Java/Ada: resolution by number and types of parameters.–Perfect if exact match exists;–No perfect match: different conversion rules•Ada: automatic conversions not allowed.•Java:
View Full Document