1 / 18

SEMANTIC ANALYSIS SYMBOL TABLE

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

conor
Download Presentation

SEMANTIC ANALYSIS SYMBOL TABLE

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. SEMANTIC ANALYSISSYMBOL TABLE Shimmi Asokan

  2. Contents • Introduction • Symbol Table Implementation • Global Symbol Table Structure • Global Symbol Table • Local Symbol Table • Functions on Symbol Table • Conclusion

  3. 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

  4. 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

  5. Implementation

  6. Implementation using hash tables Key 1 Key 2 Key n Hash Keys Symbol table entries

  7. GLOBAL SYMBOL TABLE STRUCTURE

  8. 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

  9. 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

  10. 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; }

  11. Structure – Local Symbol Table struct LSymbol_table { char name[MAXLEN]; char fname[MAXLEN]; int type; // INT / BOOL int binding; struct LSymbol_table* next; }* Lhead;

  12. 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)

  13. 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; }

  14. 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; }

  15. 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; }

  16. 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

  17. 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

  18. THANK YOU

More Related