📄 symtab.c
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "symtab.h"
/* SIZE is the size of the hash table */
#define SIZE 211
/* SHIFT is the power of two used as multiplier
in hash function */
#define SHIFT 4
/* the hash function */
static int hash ( char * key )
{ int temp = 0;
int i = 0;
while (key[i] != '\0')
{ temp = ((temp << SHIFT) + key[i]) % SIZE;
++i;
}
return temp;
}
/* the list of line numbers of the source
* code in which a variable is referenced
*/
typedef struct LineListRec
{ int lineno;
struct LineListRec * next;
} * LineList;
/* The record in the bucket lists for
* each variable, including name,
* assigned memory location, and
* the list of line numbers in which
* it appears in the source code
*/
typedef struct BucketListRec
{ char * name;
LineList lines;
Attr attr;
struct BucketListRec * next;
} * BucketList;
/* the hash table */
static BucketList hashTable[SIZE];
/* Procedure st_insert inserts line numbers and
* memory locations into the symbol table
* loc = memory location is inserted only the
* first time, otherwise ignored
*/
void st_insert( char * name, int lineno, Attr attr)
{ int h = hash(name);
BucketList l = hashTable[h];
while ((l != NULL) && (strcmp(name,l->name) != 0))
l = l->next;
if (l == NULL) /* variable not yet in table */
{ l = (BucketList) malloc(sizeof(struct BucketListRec));
l->name = name;
l->lines = (LineList) malloc(sizeof(struct LineListRec));
l->lines->lineno = lineno;
l->attr.memloc = attr.memloc;
l->attr.type = attr.type;
l->attr.isArr = attr.isArr;
l->lines->next = NULL;
l->next = hashTable[h];
hashTable[h] = l; }
else /* found in table, so just add line number */
{ LineList t = l->lines;
while (t->next != NULL) t = t->next;
t->next = (LineList) malloc(sizeof(struct LineListRec));
t->next->lineno = lineno;
t->next->next = NULL;
}
} /* st_insert */
/* Function st_lookup returns the memory
* location of a variable or -1 if not found
*/
Attr st_lookup ( char * name )
{ int h = hash(name);
Attr attr;
BucketList l = hashTable[h];
while ((l != NULL) && (strcmp(name,l->name) != 0))
l = l->next;
if (l == NULL) attr.memloc = -1;
else {
attr.memloc = l->attr.memloc;
attr.type = l->attr.type;
}
return attr;
}
/* Procedure printSymTab prints a formatted
* listing of the symbol table contents
* to the listing file
*/
void printSymTab(FILE * listing)
{ int i;
fprintf(listing,"Variable Name Type Location Line Numbers\n");
fprintf(listing,"------------- ---------------- ---------- ------------------\n");
for (i=0;i<SIZE;++i)
{ if (hashTable[i] != NULL)
{ BucketList l = hashTable[i];
while (l != NULL)
{ LineList t = l->lines;
fprintf(listing,"%-14s ",l->name);
switch(l->attr.type)
{
case Integer:
if(!l->attr.isArr)
fprintf(listing,"%-16s ","Integer");
else
fprintf(listing,"%-16s ","Integer Array");
break;
case Float:
if(!l->attr.isArr)
fprintf(listing,"%-16s ","Float");
else
fprintf(listing,"%-16s ","Float Array");
break;
case Char:
if(!l->attr.isArr)
fprintf(listing,"%-16s ","Char");
else
fprintf(listing,"%-16s ","Char Array");
break;
case Boolean:
fprintf(listing,"%-8s ","Boolean");
break;
case Void:
fprintf(listing,"%-8s ","Void");
break;
default:
fprintf(listing,"%-8s ","Unknown");
break;
}
fprintf(listing,"%-7d ",l->attr.memloc);
while (t != NULL)
{ fprintf(listing,"%4d ",t->lineno);
t = t->next;
}
fprintf(listing,"\n");
l = l->next;
}
}
}
} /* printSymTab */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -