2008-2-11CSE 3302 Programming LanguagesSemantics (cont )Chengkai 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 Scopeint x = 1;char y = ‘a’;void p(void) {double x=2.5;printf(“%c\n”,y);}xLecture 6 - Semantics, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20084void q(void) {int y = 42;printf(“%d\n”,x);p();}main() {char x = ‘b’;q();}xinteger, globalcharacter, globalyStatic Scopeint x = 1;char y = ‘a’;void p(void) {double x=2.5;printf(“%c\n”,y);}xdouble, local to pThe symbol table in p: the bindings available in pLecture 6 - Semantics, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20085void q(void) {int y = 42;printf(“%d\n”,x);p();}main() {char x = ‘b’;q();}xinteger, globalCharacter, globalyStatic Scopeint x = 1;char y = ‘a’;void p(void) {double x=2.5;printf(“%c\n”,y);}xThe symbol table in q: the bindings available in qLecture 6 - Semantics, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20086void q(void) {int y = 42;printf(“%d\n”,x);p();}main() {char x = ‘b’;q();}xinteger, globalcharacter, globalinteger, local to qy2008-2-12Static Scopeint x = 1;char y = ‘a’;void p(void) {double x=2.5;printf(“%c\n”,y);}xThe symbol table in main: the bindings available in maincharacter, local to mainLecture 6 - Semantics, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20087void q(void) {int y = 42;printf(“%d\n”,x);p();}main() {char x = ‘b’;q();}xinteger, globalcharacter, globalyPractice for Static Scopeint x,y;void g(void) {x = x + 1;y = x + 1;}void f(void) {int x;Question 1:Draw the symbol table at the given points in the program, using staticPoint 1Lecture 6 - Semantics, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20088y = y + 1;x = y + 1;g();}main() {x = 1;y = 2;f();g();printf("x=%d,y=%d\n",x,y);}program, using static scope?Question 2: What does the program print, using static scope?Point 2Point 3What if dynamic scope is used?int x = 1;char y = ‘a’;void p(void) {double x=2.5;printf(“%c\n”,y);}xLecture 6 - Semantics, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20089void 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?int x = 1;char y = ‘a’;void p(void) {double x=2.5;printf(“%c\n”,y);}xcharacter, ‘b’, local to mainThe symbol table in main: the bindings available in mainLecture 6 - Semantics, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200810void 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?int x = 1;char y = ‘a’;void p(void) {double x=2.5;printf(“%c\n”,y);}xcharacter, ‘b’, local to mainThe symbol table in q: the bindings available in qLecture 6 - Semantics, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200811void q(void) {int y = 42;printf(“%d\n”,x);p();}main() {char x = ‘b’;q();}xinteger, 1, globalcharacter, ‘a’, globalinteger, 42, local to qy98What if dynamic scope is used?int x = 1;char y = ‘a’;void p(void) {double x=2.5;printf(“%c\n”,y);}xcharacter, ‘b’, local to maindouble, 2.5, local to pThe symbol table in p: the bindings available in pLecture 6 - Semantics, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200812void q(void) {int y = 42;printf(“%d\n”,x);p();}main() {char x = ‘b’;q();}xinteger, 1, globalcharacter, ‘a’, globalinteger, 42, local to qy98*2008-2-13Practice for Dynamic Scopeint x,y;void g(void) {x = x + 1;y = x + 1;}void f(void) {int x;Question 1:Draw the symbol table at the given points in the program, using dynamicPoint 1Lecture 6 - Semantics, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200813y = y + 1;x = y + 1;g();}main() {x = 1;y = 2;f();g();printf("x=%d,y=%d\n",x,y);}program, using dynamic scope?Question 2: What does the program print, using dynamic scope?Point 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 allowedAda: automatic conversions not allowed.• Java: conversions allowed in certain directions. • C++: automatic conversions more flexible.– e.g.,• int sum(int a, int b) {…}• double sum(double a, double b) {…}• double sum(double a, int b) {…}sum(1); sum(1, 2); sum(1.0, 2.0); sum(1, 2.0);Lecture 6 - Semantics, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200816Overload Resolution Example(1) int sum(int, int);(2) double sum(double, int);(3) double sum(double, double);int x;double y;C++ Java AdaLecture 6 - Semantics, Spring 2008CSE3302 Programming Languages,
View Full Document