-2-1 CSE 3302 Programming Languages Semantics (cont.) Smbol Table Smbol Table: maintain bindings. Can be viewed as functions that map names to their attributes. Names SmbolTable Attributes Chengkai Li, Weimin He Spring Chengkai Li, Weimin He, 1 Chengkai Li, Weimin He, 2 Static vs. Dnamic Scope Static scope (leical scope): scope maintained staticall (during compilation) follow the laout of source codes used in most languages Dnamic scope: scope maintained dnamicall (during eecution) follow the eecution path few languages use it (The bindings cannot be determined staticall, ma depend on user input). Lisp: considered a bug b its inventor. Perl: can choose leical or dnamic scope Chengkai Li, Weimin He, 3 int = 1; char = a ; double =2.5; printf( %c\n,); int = 42; printf( %d\n,); char = b ; character, global Chengkai Li, Weimin He, 4 int = 1; char = a ; double =2.5; printf( %c\n,); int = 42; printf( %d\n,); The smbol table in p: the bindings available in p double, local to p char = b ; Character, global Chengkai Li, Weimin He, 5 int = 1; char = a ; double =2.5; printf( %c\n,); int = 42; printf( %d\n,); The smbol table in q: the bindings available in q integer, local to q char = b ; character, global Chengkai Li, Weimin He, 6 1
-2-1 int = 1; char = a ; double =2.5; printf( %c\n,); int = 42; printf( %d\n,); The smbol table in main: the bindings available in main character, local to main char = b ; character, global Chengkai Li, Weimin He, 7 Practice for Point 1 Point 2 Point 3 int,; void g(void) { = + 1; = + 1; void f(void) { int ; = + 1; = + 1; = 1; = 2; f(); printf("=%d,=%d\n",,); Question 1: Draw the smbol table at the given points in the program, using static scope? Question 2: What does the program print, using static scope? 8 Chengkai Li, Weimin He, What if dnamic scope is used? int = 1; char = a ; double =2.5; printf( %c\n,); int = 42; printf( %d\n,); char = b ; Chengkai Li, Weimin He, 9 What if dnamic scope is used? int = 1; char = a ; double =2.5; printf( %c\n,); int = 42; printf( %d\n,); The smbol table in main: the bindings available in main character, b, local to main char = b ; Chengkai Li, Weimin He, 10 What if dnamic scope is used? int = 1; char = a ; double =2.5; printf( %c\n,); int = 42; printf( %d\n,); 98 The smbol table in q: the bindings available in q character, b, local to main integer, 42, local to q char = b ; Chengkai Li, Weimin He, 11 What if dnamic scope is used? int = 1; char = a ; double =2.5; printf( %c\n,); int = 42; printf( %d\n,); 98 * The smbol table in p: the bindings available in p double, 2.5, local to p character, b, local to main integer, 42, local to q char = b ; Chengkai Li, Weimin He, 12 2
-2-1 Practice for Dnamic Scope Point 1 Point 2 Point 3 int,; void g(void) { = + 1; = + 1; void f(void) { int ; = + 1; = + 1; = 1; = 2; f(); printf("=%d,=%d\n",,); Question 1: Draw the smbol table at the given points in the program, using dnamic scope? Question 2: What does the program print, using dnamic scope? 13 Chengkai Li, Weimin He, Overloading What is overloading? Wh overloading? What can be overloaded? Chengkai Li, Weimin He, 14 Overload Resolution Overload Resolution: select one entit. Name isn t sufficient in resolution: need etra information (often data tpes) Function/Method Overloading C: no overloading C++/Java/Ada: resolution b number and tpes of parameters. Perfect if eact match eists; No perfect match: different conversion rules Ada: automatic conversions not allowed. Java: conversions allowed in certain directions. C++: automatic conversions more fleible. 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); Chengkai Li, Weimin He, 15 Chengkai Li, Weimin He, 16 Overload Resolution Eample (1) int sum(int, int); (2) double sum(double, int); (3) double sum(double, double); int ; double ; C++ Java Ada = sum(3,4); 1 1 1 = sum(3,4); 1 1 0 = sum(3,4.5); 0 0 0 = sum(3,4.5); 0 3 0 = sum(3.5,4); 2 0 0 = sum(3.5,4); 2 2 2 = sum(3.5,4.5); 3 0 0 = sum(3.5,4.5); 3 3 3 Environment Location: one specific attribute of names Environment: maintain bindings of names to locations Static vs. dnamic FORTRAN: completel static LISP: completel dnamic Algol-descendants (C, C++, Ada, Java) : combination global variables: static local variables: dnamic Chengkai Li, Weimin He, 17 Chengkai Li, Weimin He, 18 3
-2-1 Stack-Based Allocations Eample static(global) area stack (unallocated) heap automaticall-allocated spaces (local variables, procedures (chapter 8) under the control of runtime sstem manuall-allocated spaces under the control of programmer A: { int ; char ; B: { double ; C: { char ; D: { int ; double ; Chengkai Li, Weimin He, 19 Chapter 5 K. Louden, Programming Languages 20 Eample Eample A: { int ; char ; B: { double ; Lifetime is the duration of B C: { char ; D: { int ; double ; a A: { int ; char ; B: { double ; C: { char ; D: { int ; double ; b Chapter 5 K. Louden, Programming Languages 21 Chapter 5 K. Louden, Programming Languages 22 Eample Eample A: { int ; char ; B: { double ; C: { char ; D: { int ; double ; b A: { int ; char ; B: { double ; C: { char ; D: { int ; double ; b Chapter 5 K. Louden, Programming Languages 23 Chapter 5 K. Louden, Programming Languages 24 4
-2-1 Heap-Based Allocation Scope vs. Lifetime C int *; =(int *)malloc(sizeof(int)); free(); C++ int *; = new int; delete ; Java Integer = new Integer(2); //no delete //need garbage collection) static(global) area stack (unallocated) heap Lifetime beond scope: alive in scope hole alive outside scope Scope beond lifetime (unsafe) Chengkai Li, Weimin He, 25 Chengkai Li, Weimin He, 26 Eample: Alive in scope hole Eample: Alive outside scope A: { int ; char ; B: { double ; C: { char ; D: { int ; double ; a int func(void) { static int counter = 0; counter += 1; return counter; counter main() { int i; int ; for (i=0; i<10; i++) { =func(); printf( %d\n, ); Chengkai Li, Weimin He, 27 Chengkai Li, Weimin He, 28 Eample: Scope beond lifetime Dangling pointer: int *, *, *z; =(int *) malloc(sizeof(int)); *=2; =; free();... printf( %d\n,*); Bo-and-Circle Diagram for Variables Name l-value Location assignment Value = r-value Chengkai Li, Weimin He, 29 Chengkai Li, Weimin He, 30 5
-2-1 Assignment b sharing Assignment b cloning Java: Student = new Student( Am ); Student = new Student( John );.setage(19); = ;.setage(21); = Chengkai Li, Weimin He, 31 Chengkai Li, Weimin He, 32 (1) int *, *; (2) = (int *)malloc(sizeof(int)); (3) * = 1; (4) = ; (5) * = 2; (6) printf( %d\n,*); After line 1: (1) int *, *; (2) = (int *)malloc(sizeof(int)); (3) * = 1; (4) = ; (5) * = 2; (6) printf( %d\n,*); After line 2: Chengkai Li, Weimin He, 33 Chengkai Li, Weimin He, 34 (1) int *, *; (2) = (int *)malloc(sizeof(int)); (3) * = 1; (4) = ; (5) * = 2; (6) printf( %d\n,*); After line 3: (1) int *, *; (2) = (int *)malloc(sizeof(int)); (3) * = 1; (4) = ; (5) * = 2; (6) printf( %d\n,*); After line 4: Chengkai Li, Weimin He, 35 Chengkai Li, Weimin He, 36 6
-2-1 (1) int *, *; (2) = (int *)malloc(sizeof(int)); (3) * = 1; (4) = ; (5) * = 2; (6) printf( %d\n,*); After line 5: Practice for (1) #include <stdio.h> (2) main(){ (3) int **; (4) int *; (5) int z; (6) = (int**)malloc(sizeof(int*)); (7) = (int*)malloc(sizeof(int)); (8) z = 1; (9) * = 2; (10) * = ; (11) ** = z; (12) printf( %d\n,*); (13) z = 3; (14) printf( %d\n,*); (15) ** = 4; (16) printf( %d\n,z); (17) return 0; (18) Question 1: Draw bo-and-circle diagrams of the variables after line 11 and 15. Question 2: Which variables are aliases at each of those points? Question 3: What does the program print? Chengkai Li, Weimin He, 37 Chengkai Li, Weimin He, 38 Dangling References int *, *; = (int *)malloc(sizeof(int)); * = 2; = ; free(); /* * is now a dangling reference */ printf( %d\n,*); /*illegal reference*/ Dangling References {int *; { int ; = 2; = &; /* * is now a dangling reference */ Chengkai Li, Weimin He, 39 Chengkai Li, Weimin He, 40 Dangling References int* dangle(void) { int ; return &; = dangle(); /* * is a dangling reference */ Chengkai Li, Weimin He, 41 7