SEMANTIC ANALYSIS SYMBOL TABLE Shimmi Asokan Contents Introduction
SEMANTIC ANALYSIS SYMBOL TABLE Shimmi Asokan
Contents Introduction Symbol Table Implementation Global Symbol Table Structure Global Symbol Table Local Symbol Table Functions on Symbol Table Conclusion Page 2
INTRODUCTION Last chance for the compiler to weed out incorrect programs It consists of • Identifiers are defined • no multiple definition of same identifier • local variables are defined before used Major Tasks Involved • Symbol Table Construction • Type Checking Page 3
SYMBOL TABLE Used to keep track of scope and binding information about names Declarations can be • Global • Local Each symbol has a set of attributes associated with it • Name • Type • Size • Binding information Page 4
Implementation Page 5
Implementation using hash tables Entry 1. 1 Key 1 Entry 2. 3 Key 2 Entry n. 2 Entry 2. 2 Entry n. 1 …. Entry 2. 1 Key n Hash Keys Page 6 Entry n. 3 Symbol table entries
GLOBAL SYMBOL TABLE STRUCTURE Page 7
Program e; var a, b, c: integer; procedure f; var a, b, c: integer; end; procedure g; var a, b: integer; procedure h; var c, d: integer; end; procedure i; var b, d: integer; end; procedure j; var b, d: integer; end Page 8
Program e var a, b, c, d procedure f() var b procedure g() var d import a, b from P end package P export a, b end Page 9
Structure – Global Symbol Table struct GSymbol_table #define NODPTR struct node* { struct node char name[MAXLEN]; struct argstruct { int type; Fdef : func_ret_type func_name '(' Farg. List ')' '{' Ldecl_sec BEG stmt_list ret_stmt END '}' GSymbol. Table* Gstptr; { { $$->Gstptr->fbody=$9; int binding; LSymbol. Table* Lstptr; $$->Gstptr->freturn=$10; } int size; char name[MAXLEN]; Argstruct* arglist; int type; Ret. Type; int Func. Ret. Type; int Node. Type; struct argstruct* next; int count; int is. Func. Defined; struct list 1 *head; } * list_head; int argcount; struct node *fbody; struct node *freturn; struct GSymbol_table* next; struct list *head; int count; }*Ghead; Page 10 int value; NODPTR list; NODPTR next; NODPTR Lptr; NODPTR Mptr; NODPTR Rptr; };
Structure – Local Symbol Table struct LSymbol_table { char name[MAXLEN]; char fname[MAXLEN]; int type; // INT / BOOL int binding; struct LSymbol_table* next; }* Lhead; Page 11
Functions Lookup() • Searches the given symbol table for a given symbol GSymbol. Table* Glookup(char* sym_name) • $1 ->Gstptr=Glookup($1 ->name); LSymbol. Table* Llookup(char* sym_name, char *fun) Insert() • Inserts an entry for the given symbol in the given symbol table GSymbol. Table* Ginsert(char* name, int type, int rtype, int size, Argstruct* list) • Ginsert($1 ->name, type_flag, -1, NULL); • Ginsert($1 ->name, FUNC, type_flag, -1, list_head); LSymbol. Table* Linsert(char* sym_name, int sym_type, int arg_var, char *fun) Page 12
GSymbol. Table* Glookup(char* sym_name) { GSymbol. Table* ptr; for(ptr = Ghead; ptr != NULL; ptr = ptr->next) { if(strcmp(ptr->name, sym_name) == 0) return ptr; } return NULL; } Page 13
GSymbol. Table* Ginsert(char* name, int type, int rtype, int size, Argstruct* list) { int argc = 0; Argstruct* tmp = list; while(tmp) { argc++; tmp = tmp->next; } if(Ghead == NULL) { Ghead = malloc(sizeof(GSymbol. Table)); strcpy(Ghead->name, name); Ghead->type = type; Ghead->Func. Ret. Type = rtype; Ghead->size = size; Ghead->argcount = argc; Ghead->arglist = list; Ghead->is. Func. Defined = 0; Ghead->next = NULL; Page 14 return Ghead; }
else { GSymbol. Table *ptr; ptr = malloc(sizeof(GSymbol. Table)); strcpy(ptr->name, name); ptr->type = type; ptr->Func. Ret. Type = rtype; ptr->size = size; ptr->argcount = argc; ptr->arglist = list; ptr->is. Func. Defined = 0; ptr->next = Ghead; Ghead = ptr; return ptr; } Page 15
CONCLUSION Semantic Analysis ensures the combination of tokens forms a sensible set of instructions in the programming language Symbol table collects together the attributes of a particular symbol in a way that allows them to be easily set and retrieved Global symbol table can be build as a stack of local symbol tables Page 16
References [1] A. Aho, R. Sethi, J. Ullman, Compilers: Principles, Techniques, and Tools, Pearson Education. [2] Steven S. Muchnick, Advanced Compiler Design and Implementation, Morgan Kaufmann Publishers Page 17
THANK YOU Page 18
- Slides: 18