Implementing Scopes A symbol table is in essence
Implementing Scopes • A symbol table is in essence a dictionary – I. e. , every name appears in it, with the info known about it – Usually expressed as a name and an attribute list/map/etc. • Symbol table entries are added, modified, looked up – I. e. , as a name is declared, then defined, then used, etc. • A symbol table can have other operations as well – Scope entry and exit operations (may navigate within it) • Symbol tables often implemented in 1 of 2 main ways – An association list (of name/value pairs) is often simplest – A central reference table maintains an explicit mapping CSE 425: Semantics II
Symbol Tables for Nested Scopes • Scope analysis allows declarations and bindings to be processed in a stack-like manner at run-time – A symbol table is used to keep track of that information – E. g. , each identifier in a scope has a set of bindings – Structure/management may range from a single static symbol table to a dynamic hierarchy of per-scope tables pi double main 3. 141 (int, char**) argc argv int 2 char ** function scope global scope CSE 425: Semantics II
Names, Aliases, and Overloading • Multiple names may refer to same object or variable – E. g. , pointers or references (and pass-by-reference) in C++ • The ability to re-use a name is also commonplace – E. g. , using the + operator for addition or string concatenation – Since a symbol table usually stores parameter type attributes for a function identifier, can use to resolve – I. e. , look up the declaration/definition of a function or operator based on its parameter types as well as its name • Many languages allow function name overloading and a smaller number of them allow operator overloading – Syntactically introduced constraints such as precedence and associativity of operators must be respected (unless waived) – Though with tweaks like C++ prefix operator+ call syntax CSE 425: Semantics II
Coercion and Polymorphism • Types often are allowed to be converted to other types – E. g. , while (ifs >> token) // ifstream& to bool • When the complier forces this to happen its coercion – I. e. , the type conversion happens implicitly • Polymorphism lets multiple unconverted types be used – Inheritance polymorphism (E. g. , C++ classes) – Interface polymorphism (E. g. , C++ templates) – Both support subtype polymorphism (Liskov substitution) • Explicit parametric polymorphism is called “generic” – E. g. , C++ templates with specialization CSE 425: Semantics II
Binding of Reference Environments • Referencing environment depends on the order in which declarations are encountered – When is the context in which a function is executed fixed? – This matters because of nesting of scopes, hiding of names, and the ability to pass a function (pointer) as a parameter – Shallow binding is done just before function is called – Deep binding is done when the parameter is first passed • Function/subroutine closures – Bundle referencing environment with reference to subroutine – E. g. , local variable capture list in C++11 lambda expression • Object closures – Similar idea, bundle member variables and operator() CSE 425: Semantics II
Today’s Studio Exercises • We’ll code up ideas from Scott Chapter 3. 4 -3. 8 – Again looking at the semantics of different C++ constructs – Extending the symbol table from the last studio • Today’s exercises are again in C++ – Please take advantage of the on-line reference manual pages that are linked on the course web site – As always, please ask us for help as needed • When done, email your answers to the course e-mail account with “Semantics Studio II” in the subject CSE 425: Semantics II
- Slides: 6