Chapter 6 Symbol tables Course “Compiler Construction” Martin Steffen Spring 2018
Section Targets Chapter 6 “Symbol tables” Course “Compiler Construction” Martin Steffen Spring 2018
Chapter 6 Learning Targets of Chapter “Symbol tables”. 1. symbol table data structure 2. design and implementation choices 3. how to deal with scopes 4. connection to attribute grammars
Chapter 6 Outline of Chapter “Symbol tables”. Targets Introduction : Symbol table design and interface Implementing symbol tables Block-structure, scoping, binding, name-space organiza- tion Symbol tables as attributes in an AG
Section Introduction Chapter 6 “Symbol tables” Course “Compiler Construction” Martin Steffen Spring 2018
Symbol tables, in general INF5110 – • central data structure Compiler Construction • “data base” or repository associating properties with “names” (identifiers, symbols) 1 Targets • declarations Targets & Outline • constants Introduction : • type declarationss Symbol table design and • variable declarations interface • procedure declarations Implementing • class declarations symbol tables • . . . Block-structure, scoping, binding, name-space • declaring occurrences vs. use occurrences of names organization (e.g. variables) Symbol tables as attributes in an AG 6-6 1 Remember the (general) notion of “attribute”.
Does my compiler need a symbol table? • goal: associate attributes (properties) to syntactic elements (names/symbols) INF5110 – Compiler • storing once calculated: (costs memory) ↔ Construction recalculating on demand (costs time) • most often: storing preferred Targets • but: can’t one store it in the nodes of the AST ? Targets & Outline Introduction : • remember: attribute grammar • however, fancy attribute grammars with many rules and Symbol table design and complex synthesized/inherited attribute (whose interface evaluation traverses up and down and across the tree): Implementing symbol tables • might be intransparent • storing info in the tree: might not be efficient Block-structure, scoping, binding, name-space ⇒ central repository (= symbol table) better organization Symbol tables as So: do I need a symbol table? attributes in an AG In theory, alternatives exists; in practice, yes, symbol tables is the way to go; most compilers do use symbol tables. 6-7
Section Symbol table design and interface Chapter 6 “Symbol tables” Course “Compiler Construction” Martin Steffen Spring 2018
Symbol table as abstract data type • separate interface from implementation • ST: “nothing else” than a lookup-table or dictionary , INF5110 – • associating “keys” with “values” Compiler Construction • here: keys = names (id’s, symbols), values the attribute(s) Targets Schematic interface: two core functions (+ more) Targets & Outline Introduction : • insert : add new binding Symbol table design and interface • lookup : retrieve Implementing symbol tables besides the core functionality: Block-structure, scoping, binding, • structure of (different?) name spaces in the name-space organization implemented language, scoping rules Symbol tables as • typically: not one single “flat” namespace ⇒ typically attributes in an AG not one big flat look-up table ⇒ influence on the design/interface of the ST (and 6-9 indirectly the choice of implementation) • necessary to “delete” or “hide” information ( delete )
Two main philosophies decls. in the AST nodes traditional table(s) INF5110 – Compiler • do look-up ⇒ tree- search Construction • central repository, • insert/delete: implicit, separate from AST Targets depending on relative • interface Targets & Outline positioning in the tree Introduction : • lookup ( name ) , • look-up: Symbol table • insert ( name , decl ) , • efficiency? design and • delete ( name ) interface • however: Implementing • last 2: update ST for optimizations exist, symbol tables declarations and when e.g. “redundant” extra Block-structure, scoping, binding, table (similar to the entering/exiting blocks name-space traditional ST) organization Symbol tables as Here, for concreteness, declarations are the attributes stored attributes in an AG in the ST. In general, it is not the only possible stored attribute. Also, there may be more than one ST. 6-10
Section Implementing symbol tables Chapter 6 “Symbol tables” Course “Compiler Construction” Martin Steffen Spring 2018
Data structures to implement a symbol table INF5110 – Compiler • different ways to implement dictionaries (or look-up Construction tables etc.) • simple (association) lists Targets • trees Targets & Outline • balanced (AVL, B, red-black, binary-search trees) Introduction : • association list Symbol table • hash tables, often method of choice design and interface • functional vs. imperative implementation Implementing symbol tables • careful choice influences efficiency Block-structure, • influenced also by the language being implemented, scoping, binding, name-space organization • in particular, by its scoping rules (or the structure of the name space in general) etc. 2 Symbol tables as attributes in an AG 2 Also the language used for implementation (and the availability of 6-12 libraries therein) may play a role (but remember “bootstrapping”)
Nested block / lexical scope INF5110 – Compiler Construction for instance: C Targets { i n t i ; . . . ; double d ; void p ( . . . ) ; Targets & Outline { Introduction : i n t i ; Symbol table . . . design and } interface i n t j ; Implementing . . . symbol tables Block-structure, scoping, binding, more later name-space organization Symbol tables as attributes in an AG 6-13
Blocks in other languages INF5110 – Compiler Construction L T EX A T EX Targets \ documentclass { a r t i c l e } \ def \x{a} \newcommand{\x}{a} Targets & Outline { \ begin {document} Introduction : \ def \x{b} \x Symbol table \x {\renewcommand{\x}{b} design and } \x interface \x } Implementing \ bye \x symbol tables \ end {document} Block-structure, scoping, binding, name-space organization But: static vs. dynamic binding (see later) Symbol tables as attributes in an AG 6-14
Hash tables • classical and common implementation for STs INF5110 – • “hash table”: Compiler Construction • generic term itself, different general forms of HTs exists • e.g. separate chaining vs. open addressing Targets Targets & Outline Code snippet Introduction : Symbol table Separate chaining { design and i n t temp ; interface i n t j ; Implementing symbol tables r e a l i ; void s i z e ( . . . . ) { Block-structure, scoping, binding, { name-space organization . . . . } Symbol tables as attributes in an } AG } 6-15
Block structures in programming languages INF5110 – • almost no language has one global namespace (at least Compiler Construction not for variables) • pretty old concept, seriously started with ALGOL60 Targets Targets & Outline Block Introduction : Symbol table • “region” in the program code design and interface • delimited often by { and } or BEGIN and END or Implementing symbol tables similar Block-structure, • organizes the scope of declarations (i.e., the name scoping, binding, name-space space) organization Symbol tables as • can be nested attributes in an AG 6-16
Block-structured scopes (in C) INF5110 – Compiler Construction i n t i , j ; i n t f ( i n t s i z e ) Targets { char i , temp ; Targets & Outline . . . Introduction : { double j ; . . Symbol table design and } interface . . . { char ∗ j ; Implementing symbol tables . . . } Block-structure, scoping, binding, } name-space organization Symbol tables as attributes in an AG 6-17
Nested procedures in Pascal program Ex ; var i , j : integer INF5110 – Compiler function f ( s i z e : integer ) : integer ; Construction var i , temp : char ; procedure g ; Targets var j : r e a l ; Targets & Outline begin . . . Introduction : end ; Symbol table design and procedure h ; interface var j : ^ char ; Implementing begin symbol tables . . . Block-structure, scoping, binding, end ; name-space organization begin (∗ f ' s body ∗) Symbol tables as . . . attributes in an AG end ; begin (∗ main program ∗) . . . 6-18 end .
Block-strucured via stack-organized separate chaining INF5110 – “Evolution” of the hash table Compiler C code snippet Construction i n t i , j ; Targets Targets & Outline i n t f ( i n t s i z e ) Introduction : { char i , temp ; Symbol table . . . design and { double j ; interface . . Implementing symbol tables } Block-structure, . . . scoping, binding, { char ∗ j ; name-space organization . . . } Symbol tables as attributes in an } AG 6-19
Recommend
More recommend