sym.c revision 4fd606d1f5abe38e1f42c38de1d2e895166bd0f4
/*
* Simple symbol table manager using coalesced chaining to resolve collisions
*
* Doubly-linked lists are used for fast removal of entries.
*
* 'sym.h' must have a definition for typedef "Sym". Sym must include at
* minimum the following fields:
*
* ...
* char *symbol;
* struct ... *next, *prev, **head, *scope;
* unsigned int hash;
* ...
*
* 'template.h' can be used as a template to create a 'sym.h'.
*
* 'head' is &(table[hash(itself)]).
* The hash table is not resizable at run-time.
* The scope field is used to link all symbols of a current scope together.
* Scope() sets the current scope (linked list) to add symbols to.
* Any number of scopes can be handled. The user passes the address of
* a pointer to a symbol table
* entry (INITIALIZED TO NULL first time).
*
* Available Functions:
*
* zzs_init(s1,s2) -- Create hash table with size s1, string table size s2.
* zzs_done() -- Free hash and string table created with zzs_init().
* zzs_add(key,rec)-- Add 'rec' with key 'key' to the symbol table.
* zzs_newadd(key) -- create entry; add using 'key' to the symbol table.
* zzs_get(key) -- Return pointer to last record entered under 'key'
* Else return NULL
* zzs_del(p) -- Unlink the entry associated with p. This does
* NOT free 'p' and DOES NOT remove it from a scope
* list. If it was a part of your intermediate code
* tree or another structure. It will still be there.
* It is only removed from further consideration
* by the symbol table.
* zzs_keydel(s) -- Unlink the entry associated with key s.
* Calls zzs_del(p) to unlink.
* zzs_scope(sc) -- Specifies that everything added to the symbol
* table with zzs_add() is added to the list (scope)
* 'sc'. 'sc' is of 'Sym **sc' type and must be
* initialized to NULL before trying to add anything
* to it (passing it to zzs_scope()). Scopes can be
* switched at any time and merely links a set of
* symbol table entries. If a NULL pointer is
* passed, the current scope is returned.
* zzs_rmscope(sc) -- Remove (zzs_del()) all elements of scope 'sc'
* from the symbol table. The entries are NOT
* free()'d. A pointer to the first
* element in the "scope" is returned. The user
* (such as freeing them all). NOTE that this
* function sets your scope pointer to NULL,
* but returns a pointer to the list for you to use.
* zzs_stat() -- Print out the symbol table and some relevant stats.
* zzs_new(key) -- Create a new record with calloc() of type Sym.
* Add 'key' to the string table and make the new
* records 'symbol' pointer point to it.
* zzs_strdup(s) -- Add s to the string table and return a pointer
* to it. Very fast allocation routine
* and does not require strlen() nor calloc().
*
* Example:
*
* #include <stdio.h>
* #include "sym.h"
*
* main()
* {
* Sym *scope1=NULL, *scope2=NULL, *a, *p;
*
* zzs_init(101, 100);
*
* a = zzs_new("Apple"); zzs_add(a->symbol, a); -- No scope
* zzs_scope( &scope1 ); -- enter scope 1
* a = zzs_new("Plum"); zzs_add(a->symbol, a);
* zzs_scope( &scope2 ); -- enter scope 2
* a = zzs_new("Truck"); zzs_add(a->symbol, a);
*
* p = zzs_get("Plum");
* if ( p == NULL ) fprintf(stderr, "Hmmm...Can't find 'Plum'\n");
*
* p = zzs_rmscope(&scope1)
* for (; p!=NULL; p=p->scope) {printf("Scope1: %s\n", p->symbol);}
* p = zzs_rmscope(&scope2)
* for (; p!=NULL; p=p->scope) {printf("Scope2: %s\n", p->symbol);}
* }
*
* Terence Parr
* Purdue University
* February 1990
*
* CHANGES
*
* Terence Parr
* May 1991
* Renamed functions to be consistent with ANTLR
* Made HASH macro
* Added zzs_keydel()
* Added zzs_newadd()
* Fixed up zzs_stat()
*
* July 1991
* Made symbol table entry save its hash code for fast comparison
* during searching etc...
*/
#include <stdio.h>
#if defined(__STDC__) || defined(__USE_PROTOS)
#include <string.h>
#include <stdlib.h>
#else
#include <malloc.h>
#endif
#include "sym.h"
#define StrSame 0
static unsigned size = 0;
static char *strings;
static char *strp;
static int strsize = 0;
#ifdef __USE_PROTOS
#else
#endif
{
{
exit(1);
}
{
exit(1);
}
}
#ifdef __USE_PROTOS
void zzs_done(void)
#else
void zzs_done()
#endif
{
}
#ifdef __USE_PROTOS
#else
char *key;
#endif
{
register unsigned int h=0;
register char *p=key;
HASH(p, h);
h %= size;
}
#ifdef __USE_PROTOS
#else
char *key;
#endif
{
register unsigned int h=0;
register char *p=key;
register Sym *q;
HASH(p, h);
{
if ( q->hash == h ) /* do we even have a chance of matching? */
}
return( NULL );
}
/*
* Unlink p from the symbol table. Hopefully, it's actually in the
* symbol table.
*
* If p is not part of a bucket chain of the symbol table, bad things
* will happen.
*
* Will do nothing if all list pointers are NULL
*/
#ifdef __USE_PROTOS
#else
void zzs_del(p)
register Sym *p;
#endif
{
{
if ( t == NULL ) return; /* not part of symbol table */
(*t) = p->next;
}
else
{
}
}
#ifdef __USE_PROTOS
void zzs_keydel(char *key)
#else
void zzs_keydel(key)
char *key;
#endif
{
}
/* S c o p e S t u f f */
/* Set current scope to 'scope'; return current scope if 'scope' == NULL */
#ifdef __USE_PROTOS
#else
#endif
{
return( scope );
}
/* Remove a scope described by 'scope'. Return pointer to 1st element in scope */
#ifdef __USE_PROTOS
#else
#endif
{
register Sym *p;
return( start );
}
#ifdef __USE_PROTOS
void zzs_stat(void)
#else
void zzs_stat()
#endif
{
static unsigned short count[20];
register Sym **p;
float avg=0.0;
for (i=0; i<20; i++) count[i] = 0;
{
register Sym *q = *p;
unsigned int len;
len = 0;
while ( q != NULL )
{
len++;
n++;
q = q->next;
}
}
printf("Storing %d recs used %d hash positions out of %d\n",
printf("%f %% utilization\n",
for (i=0; i<20; i++)
{
if ( count[i] != 0 )
{
printf("Buckets of len %d == %d (%f %% of recs)\n",
}
}
}
/*
* Given a string, this function allocates and returns a pointer to a
* symbol table record whose "symbol" pointer is reset to a position
* in the string table.
*/
#ifdef __USE_PROTOS
#else
char *text;
#endif
{
Sym *p;
{
exit(1);
}
return p;
}
/* create a new symbol table entry and add it to the symbol table */
#ifdef __USE_PROTOS
#else
char *text;
#endif
{
return p;
}
/* Add a string to the string table and return a pointer to it.
* Bump the pointer into the string table to next avail position.
*/
#ifdef __USE_PROTOS
char * zzs_strdup(char *s)
#else
char * zzs_strdup(s)
register char *s;
#endif
{
while ( *s != '\0' )
{
{
exit(-1);
}
*strp++ = *s++;
}
*strp++ = '\0';
return( start );
}