90 likes | 244 Views
SYMBOL TABLE. Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan University Taipei, TAIWAN. semantic meaning. name. attribute. name. Introduction (1/2). Introduction (1/2). function dictionary interface (operations)
E N D
SYMBOL TABLE Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan University Taipei, TAIWAN
semantic meaning name attribute name Introduction (1/2) Introduction (1/2) • function • dictionary • interface (operations) typedef char string[MAXSTRING]; typedef struct symtab { ... } *symbol_table; /* a pointer */ typedef struct id_entry { ... } id_entry; /* Create a new (empty) symbol table . */ extern symbol_table create(void); /* Remove all entries in table and destroy it. */ extern void destory(symbol_table table);
Introduction (2/2) /* Enter name in table; return a reference to the entry corresponding to name * and a flag to indicate whether the name was already present. */ extern void enter( symbol_table table, const string name, id_entry *entry, boolean *present ); /* Search for name is table; return a reference to the entry corresponding to name * (if there is one) and a flag to indicate whether the name was present. */ extern void find( const symbol_table table, const string name, id_entry *entry, boolean *present ); /* Associate the attrs record with entry. */ extern void set_attributes( id_entry *entry, const attributes *attrs ); /* Get the attributes record associated with entry. */ extern void get_attributes( const id_entry entry, attributes *attrs );
Basic implementation techniques • considerations • insert time u search time • storage utilization (especially, “name” field) • QUIZ: how to handle “name” field efficiently? • and so on • unordered list • array u linked list • ordered list • array • binary search tree • with balancing technique • hash table • with chaining (linked list, binary tree) • QUIZ: comparison
Block-structured symbol tables • nested name scopes declare H, A, L : Integer; begin declare X, Y : Real; begin ... H, A(integer), L, X, Y are visible end; declare A, C, M : Character; begin ... H, L, A(character), C, M are visible end; end; • problem -- a variable may be visible in one place and invisible in another place • approaches1. an integrated table2.individual table per scope
A(3) L(1) A(1) C(3) H(1) . . . M(3) hash table chained entries for names (with scope numbers) Integrated table for all scopes • name + scope number • global hash table implementation of nested scopes • hash key : name only • QUIZ: how about binary tree implementation? • QUIZ: what to do, when a scope is closed? drawbacks?
A,C,M individual symbol tables H,A,L Individual table per scope • scope stack • scopes are opened and closed in LIFO manner • interface extern void sts_push(const symbol_table table); extern symbol_table sts_pop(void); extern symbol_table sts_current_scope(void); /* for insertion */ /* Search stack of tables for name; return a reference to the entry corresponding to * name (if there is one) and a flag to indicate whether the name was present. */ extern void sts_find( const string name, id_entry *entry, boolean *present ); • QUIZ: drawbacks? • QUIZ: comparison of two approaches
A A A int real . . . . . . . . . R X C bool . . . Symbol table for fields/records Symbol table for fields/records • example record declarationlapproach 2 A, R : record A : Integer; X : record A : real; C : boolean; end; end; • example field references -- A.X.A R.X.C • approach 1 (A,0) 1 (R,0) 2 (A,1) - (X,1) 3 (A,3) - (C,3) - (A,2) - (X,2) 4 (A,4) - (C,4) -
Advanced features • export rules • import rules • altered search rules • Pascal’s with • Ada’s use • implicit declaration • overloading • forwarding reference • QUIZ: how?