Top Banner
SEMANTIC ANALYSIS SYMBOL TABLE Shimmi Asokan
18

SEMANTIC ANALYSIS SYMBOL TABLE Shimmi Asokan. Page 2 Contents Introduction Symbol Table Implementation Global Symbol Table Structure Global.

Dec 18, 2015

Download

Documents

Melvyn Jordan
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: SEMANTIC ANALYSIS SYMBOL TABLE Shimmi Asokan. Page  2 Contents  Introduction  Symbol Table Implementation  Global Symbol Table Structure  Global.

SEMANTIC ANALYSISSYMBOL TABLE

Shimmi Asokan

Page 2: SEMANTIC ANALYSIS SYMBOL TABLE Shimmi Asokan. Page  2 Contents  Introduction  Symbol Table Implementation  Global Symbol Table Structure  Global.

Page 2

Contents

Introduction

Symbol Table Implementation

Global Symbol Table Structure

Global Symbol Table

Local Symbol Table

Functions on Symbol Table

Conclusion

Page 3: SEMANTIC ANALYSIS SYMBOL TABLE Shimmi Asokan. Page  2 Contents  Introduction  Symbol Table Implementation  Global Symbol Table Structure  Global.

Page 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

Page 4: SEMANTIC ANALYSIS SYMBOL TABLE Shimmi Asokan. Page  2 Contents  Introduction  Symbol Table Implementation  Global Symbol Table Structure  Global.

Page 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

Page 5: SEMANTIC ANALYSIS SYMBOL TABLE Shimmi Asokan. Page  2 Contents  Introduction  Symbol Table Implementation  Global Symbol Table Structure  Global.

Page 5

Implementation

Page 6: SEMANTIC ANALYSIS SYMBOL TABLE Shimmi Asokan. Page  2 Contents  Introduction  Symbol Table Implementation  Global Symbol Table Structure  Global.

Page 6

Implementation using hash tables

Entry 1.1

Entry 2.3

Entry n.2

Entry 2.2

Entry n.1

….

Entry 2.1

Entry n.3

Key 1

Key 2

Key n

Hash Keys

Symbol table entries

Page 7: SEMANTIC ANALYSIS SYMBOL TABLE Shimmi Asokan. Page  2 Contents  Introduction  Symbol Table Implementation  Global Symbol Table Structure  Global.

Page 7

GLOBAL SYMBOL TABLE STRUCTURE

Page 8: SEMANTIC ANALYSIS SYMBOL TABLE Shimmi Asokan. Page  2 Contents  Introduction  Symbol Table Implementation  Global Symbol Table Structure  Global.

Page 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

Page 9: SEMANTIC ANALYSIS SYMBOL TABLE Shimmi Asokan. Page  2 Contents  Introduction  Symbol Table Implementation  Global Symbol Table Structure  Global.

Page 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

Page 10: SEMANTIC ANALYSIS SYMBOL TABLE Shimmi Asokan. Page  2 Contents  Introduction  Symbol Table Implementation  Global Symbol Table Structure  Global.

Page 10

Structure – Global Symbol Table

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;

#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;

};

Fdef : func_ret_type func_name '(' FargList ')' '{' Ldecl_sec BEG stmt_list ret_stmt END '}'{ $$->Gstptr->fbody=$9;

$$->Gstptr->freturn=$10; }

Page 11: SEMANTIC ANALYSIS SYMBOL TABLE Shimmi Asokan. Page  2 Contents  Introduction  Symbol Table Implementation  Global Symbol Table Structure  Global.

Page 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;

Page 12: SEMANTIC ANALYSIS SYMBOL TABLE Shimmi Asokan. Page  2 Contents  Introduction  Symbol Table Implementation  Global Symbol Table Structure  Global.

Page 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)

Page 13: SEMANTIC ANALYSIS SYMBOL TABLE Shimmi Asokan. Page  2 Contents  Introduction  Symbol Table Implementation  Global Symbol Table Structure  Global.

Page 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;

}

Page 14: SEMANTIC ANALYSIS SYMBOL TABLE Shimmi Asokan. Page  2 Contents  Introduction  Symbol Table Implementation  Global Symbol Table Structure  Global.

Page 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; }

Page 15: SEMANTIC ANALYSIS SYMBOL TABLE Shimmi Asokan. Page  2 Contents  Introduction  Symbol Table Implementation  Global Symbol Table Structure  Global.

Page 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;

}

Page 16: SEMANTIC ANALYSIS SYMBOL TABLE Shimmi Asokan. Page  2 Contents  Introduction  Symbol Table Implementation  Global Symbol Table Structure  Global.

Page 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

Page 17: SEMANTIC ANALYSIS SYMBOL TABLE Shimmi Asokan. Page  2 Contents  Introduction  Symbol Table Implementation  Global Symbol Table Structure  Global.

Page 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

Page 18: SEMANTIC ANALYSIS SYMBOL TABLE Shimmi Asokan. Page  2 Contents  Introduction  Symbol Table Implementation  Global Symbol Table Structure  Global.

Page 18

THANK YOU