/24/ CSE 3302 Programming Languages Semantics Chengkai Li, Weimin He Spring Names Names: identif language entities variables, procedures, functions, constants, data tpes, Attributes: properties of names Eamples of attributes: Data tpe: int n = 5; (datatpe:integer) tpe: int itself is a name Value: ( value: 5) Location: int* ; = new int; Parameters, return value: int f(int n) {... Chengkai Li, Weimin He, Chengkai Li, Weimin He, 2 Binding Binding: associating attributes to names declarations assignments declarations (prototpe) and definition of a function The bindings can be eplicit or implicit e.g. int ; Eplicit binding: the data tpe of Implicit binding: the location of (static or dnamic, depending on where the declaration is) Binding Time Binding Time: the time when an attribute is bound to a name. Static binding (static attribute): occurs before eecution Language definition/implementation time: Therangeofdatatpeint tpe translation time (parsing/semantic analsis): The data tpe of a variable link time: The bod of eternal function load time: Location of global variable Dnamic binding (dnamic attribute): occurs during eecution entr/eit from procedure or program: the value of local variables Chengkai Li, Weimin He, 3 Chengkai Li, Weimin He, 4 Where can declarations happen? Blocks ({, begin-end, Algol descendants: C/C++, Java, Pascal, Ada, ) e.g., C Function bod Anwhere a statement can appear (compound statement) Eternal/global Structured data tpe Class Chengkai Li, Weimin He, 5 C++ Eample const int Maimum = 00; struct FullName {string Lastname, string FirstName; class Student { private: struct FullName name; int Age; public: void setvalue(const int a, struct FullName name); int TStudent(); ; void Student::setAge(const int a, string lname, string fname) { int i; Age = a; { int j; name.lastname = lname; name.firstname = fname; Chengkai Li, Weimin He, 6
/24/ Scope of Binding Scope of Binding: the region of the program where the binding is maintained (is valid, applies). Block-structured language leical scope (static scope): from the declaration to the end of the block containing the declaration. dnamic scope : introduced later. Chengkai Li, Weimin He, 7 int ; char ; { int i; double z; int w[0]; w i z Eample p q main Chengkai Li, Weimin He, 8 int ; char ; Declaration before Use Eception in OO languages: Scope of local declarations inside a class declaration includes the whole class. public class { public int getvalue() { return value; int value; Chengkai Li, Weimin He, value 9 Scope Hole Scope Hole: Declarations in nested blocks take precedence over the previous declarations. That is, binding becomes invisible/hidden. int ; char ; = a ; = 2; (bound with character data tpe) Chengkai Li, Weimin He, (bound with integer data tpe) 0 Access Hidden Declarations Hide a Declaration scope resolution operator :: (C++) int ; File : File 2: etern int ; int ; char ; = a ; (bound with ::=42; (bound with character data tpe) integer data tpe) the hidden integer variable = 2; Chengkai Li, Weimin He, File : File 2: etern int ; static int ; CSE3302 Programming Languages, UT-Arlington Chengkai Li, Weimin He, 2 2
/24/ Smbol Table Smbol Table: maintain bindings. Can be viewed as functions that map names to their attributes. Names SmbolTable Chengkai Li, Weimin He, Attributes 3 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, 4 int = ; char = a ; double =2.5; printf( %c\n,); int = 42; printf( %d\n,); int = ; 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 ; Chengkai Li, Weimin He, 5 char = b ; Character, global Chengkai Li, Weimin He, 6 int = ; char = a ; double =2.5; printf( %c\n,); int = 42; printf( %d\n,); The smbol table in q: integer, local to q char = b ; Chengkai Li, Weimin He, 7 int = ; 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 ; Chengkai Li, Weimin He, 8 3
/24/ The smbol table in previous slides are built during compilation The bindings are used in generating the machine code Result: a E.g., semantics of q int = 42; printf( %d\n,); Chengkai Li, Weimin He, The smbol table in q: integer, local to q 9 Practice for Point Point 2 Point 3 int,; void g(void) { = + ; = + ; void f(void) { int ; = + ; = + ; = ; = 2; f(); printf("=%d,=%d\n",,); Question : Draw the smbol table at the given points in the program, using static Question 2: What does the program print, using static 20 Chengkai Li, Weimin He, What if dnamic scope is used? int = ; char = a ; double =2.5; printf( %c\n,); int = 42; printf( %d\n,); char = b ; Chengkai Li, Weimin He, 2 What if dnamic scope is used? int = ; char = a ; double =2.5; printf( %c\n,); int = 42; printf( %d\n,); The smbol table in main: the bindings available in main char = b ; Chengkai Li, Weimin He, 22 What if dnamic scope is used? int = ; char = a ; double =2.5; printf( %c\n,); int = 42; printf( %d\n,); 98 The smbol table in q: integer, 42, local to q char = b ; Chengkai Li, Weimin He, 23 What if dnamic scope is used? int = ; 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 integer, 42, local to q char = b ; Chengkai Li, Weimin He, 24 4
/24/ Practice for Dnamic Scope Point Point 2 Point 3 int,; void g(void) { = + ; = + ; void f(void) { int ; = + ; = + ; = ; = 2; f(); printf("=%d,=%d\n",,); Question : Draw the smbol table at the given points in the program, using dnamic Question 2: What does the program print, using dnamic 25 Chengkai Li, Weimin He, 5