180 likes | 389 Views
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. INTRODUCTION. Last chance for the compiler to weed out incorrect programs
E N D
SEMANTIC ANALYSISSYMBOL TABLE Shimmi Asokan
Contents • Introduction • Symbol Table Implementation • Global Symbol Table Structure • Global Symbol Table • Local Symbol Table • Functions on Symbol Table • Conclusion
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
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
Implementation using hash tables Key 1 Key 2 Key n Hash Keys Symbol table entries
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; end; procedure j; var b, d: integer; end; end
Program e var a, b, c, d procedure f() var b procedure g() var d import a, b from P end end package P export a, b end
Structure – Global Symbol Table #define NODPTR struct node* struct node { GSymbolTable* Gstptr; LSymbolTable* Lstptr; char name[MAXLEN]; int RetType; int NodeType; int count; struct list1 *head; int value; NODPTR list; NODPTR next; NODPTR Lptr; NODPTR Mptr; NODPTR Rptr; }; struct GSymbol_table { char name[MAXLEN]; int type; int binding; int size; Argstruct* arglist; int FuncRetType; int isFuncDefined; int argcount; struct node *fbody; struct node *freturn; struct GSymbol_table* next; struct list *head; int count; }*Ghead; struct argstruct { char name[MAXLEN]; int type; struct argstruct* next; } * list_head; Fdef : func_ret_type func_name '(' FargList ')' '{' Ldecl_sec BEG stmt_list ret_stmt END '}' { $$->Gstptr->fbody=$9; $$->Gstptr->freturn=$10; }
Structure – Local Symbol Table struct LSymbol_table { char name[MAXLEN]; char fname[MAXLEN]; int type; // INT / BOOL int binding; struct LSymbol_table* next; }* Lhead;
Functions • Lookup() • Searches the given symbol table for a given symbol GSymbolTable* Glookup(char* sym_name) • $1->Gstptr=Glookup($1->name); LSymbolTable* Llookup(char* sym_name,char *fun) • Insert() • Inserts an entry for the given symbol in the given symbol table GSymbolTable* Ginsert(char* name, int type, int rtype, int size, Argstruct* list) • Ginsert($1->name, type_flag, -1, -1, NULL); • Ginsert($1->name, FUNC, type_flag, -1, list_head); LSymbolTable* Linsert(char* sym_name, int sym_type, int arg_var, char *fun)
GSymbolTable* Glookup(char* sym_name) { GSymbolTable* ptr; for(ptr = Ghead; ptr != NULL; ptr = ptr->next) { if(strcmp(ptr->name, sym_name) == 0) return ptr; } return NULL; }
GSymbolTable* 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(GSymbolTable)); strcpy(Ghead->name, name); Ghead->type = type; Ghead->FuncRetType = rtype; Ghead->size = size; Ghead->argcount = argc; Ghead->arglist = list; Ghead->isFuncDefined = 0; Ghead->next = NULL; return Ghead; }
else { GSymbolTable *ptr; ptr = malloc(sizeof(GSymbolTable)); strcpy(ptr->name, name); ptr->type = type; ptr->FuncRetType = rtype; ptr->size = size; ptr->argcount = argc; ptr->arglist = list; ptr->isFuncDefined = 0; ptr->next = Ghead; Ghead = ptr; return ptr; }
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
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