#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create the files:
#	sym.c
#	sym.h
# This archive created: Fri Mar 23 17:49:21 1990
# By:	CARP Research Group ()
export PATH; PATH=/bin:$PATH
if test -f 'sym.c'
then
	echo shar: will not over-write existing file "'sym.c'"
else
cat << \SHAR_EOF > 'sym.c'
/*
 * 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;
 *		...
 *
 * '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:
 *
 *	aSymInit(s1,s2)	-- Create hash table with size s1, string table size s2.
 *	aSymDone(s1,s2)	-- Free hash and string table created with aSymInit().
 *	aSymAdd(key,rec)-- Add 'rec' with key 'key' to the symbol table.
 *	aSymGet(key)	-- Return pointer to last record entered under 'key'
 *			   Else return NULL
 *	aSymDel(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 the symbol table.
 *	aSymScope(sc)	-- Specifies that everything added to the symbol
 *			   table with aSymAdd() 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 aSymScope()).  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.
 *	aSymRmScope(sc)	-- Remove (aSymDel()) 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
 *			   can then manipulate the list as he/she chooses
 *			   (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.
 *	aSymStat()	-- Print out the symbol table and some relevant stats.
 *	aSymNew(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.
 *	aSymSaveStr(s)	-- Add s to the string table and return a pointer
 *			   to it.  Very fast routine allocation routine
 *			   and does not require strlen() nor calloc().
 *
 * Example:
 *
 *	#include <stdio.h>
 *	#include "sym.h"
 *
 *	main()
 *	{
 *	    Sym *scope1, *scope2, *a, *p;
 *	
 *	    aSymInit(101, 100);
 *	
 *	    a = aSymNew("Apple");	aSymAdd(a->symbol, a);	-- No scope
 *	    aSymScope( &scope1 );	-- enter scope 1
 *	    a = aSymNew("Plum");	aSymAdd(a->symbol, a);
 *	    aSymScope( &scope2 );	-- enter scope 2
 *	    a = aSymNew("Truck");	aSymAdd(a->symbol, a);
 *	
 *    	    p = aSymGet("Plum");
 *    	    if ( p == NULL ) fprintf(stderr, "Hmmm...Can't find 'Plum'\n");
 *	
 *    	    p = aSymRmScope(&scope1)
 *    	    for (; p!=NULL; p=p->scope) {printf("Scope1:  %s\n", p->symbol);}
 *    	    p = aSymRmScope(&scope2)
 *    	    for (; p!=NULL; p=p->scope) {printf("Scope2:  %s\n", p->symbol);}
 *     }
 *
 * Terence Parr
 * Purdue University
 * February 1990
 */

#include <stdio.h>
#include "sym.h"

char *calloc();

#define StrSame		0

static Sym **CurScope = NULL;
static unsigned size = 0;
static Sym **table=NULL;
static char *strings;
static char *strp;
static int strsize = 0;

aSymInit(sz, strs)
int sz, strs;
{
	if ( sz <= 0 || strs <= 0 ) return;
	table = (Sym **) calloc(sz, sizeof(Sym *));
	if ( table == NULL )
	{
		fprintf(stderr, "Cannot allocate table of size %d\n", sz);
		exit(1);
	}
	strings = (char *) calloc(strs, sizeof(char));
	if ( strings == NULL )
	{
		fprintf(stderr, "Cannot allocate string table of size %d\n", strs);
		exit(1);
	}
	size = sz;
	strsize = strs;
	strp = strings;
}

aSymDone()
{
	if ( table != NULL ) free( table );
	if ( strings != NULL ) free( strings );
}
aSymAdd(key, rec)
char *key;
register Sym *rec;
{
	register h=0;
	register char *p=key;
	
	while ( *p != '\0' ) h = (h<<1) + *p++;
	h %= size;
	
	if ( CurScope != NULL ) {rec->scope = *CurScope; *CurScope = rec;}
	rec->next = table[h];			/* Add to doubly-linked list */
	rec->prev = NULL;
	if ( rec->next != NULL ) (rec->next)->prev = rec;
	table[h] = rec;
	rec->head = &(table[h]);
}

Sym *
aSymGet(key)
char *key;
{
	register h=0;
	register char *p=key;
	register Sym *q;
	
	while ( *p != '\0' ) h = (h<<1) + *p++;
	h %= size;
	
	for (q = table[h]; q != NULL; q = q->next)
	{
		if ( strcmp(key, q->symbol) == StrSame ) return( q );
	}
	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
 */
aSymDel(p)
register Sym *p;
{
	if ( p == NULL ) {fprintf(stderr, "aSymDel(NULL)\n"); exit(1);}
	if ( p->prev == NULL )	/* Head of list */
	{
		register Sym **t = p->head;
		
		if ( t == NULL ) return;	/* not part of symbol table */
		(*t) = p->next;
		if ( (*t) != NULL ) (*t)->prev = NULL;
	}
	else
	{
		(p->prev)->next = p->next;
		if ( p->next != NULL ) (p->next)->prev = p->prev;
	}
	p->next = p->prev = NULL;	/* not part of symbol table anymore */
	p->head = NULL;
}

/* S c o p e  S t u f f */

/* Set current scope to 'scope'; return current scope if 'scope' == NULL */
Sym **
aSymScope(scope)
Sym **scope;
{
	if ( scope == NULL ) return( CurScope );
	CurScope = scope;
	return( scope );
}

/* Remove a scope described by 'scope'.  Return pointer to 1st element in scope */
Sym *
aSymRmScope(scope)
register Sym **scope;
{
	register Sym *p;
	Sym *start;

	if ( scope == NULL ) return(NULL);
	start = p = *scope;
	for (; p != NULL; p=p->scope) {aSymDel( p );}
	*scope = NULL;
	return( start );
}

aSymStat()
{
	static unsigned short count[20];
	int i,n=0,low=0, hi=0;
	register Sym **p;
	float avg=0.0;
	
	for (i=0; i<20; i++) count[i] = 0;
	for (p=table; p<&(table[size]); p++)
	{
		register Sym *q = *p;
		int len;
		
		if ( q != NULL && low==0 ) low = p-table;
		len = 0;
		if ( q != NULL ) fprintf(stderr, "[%d]", p-table);
		while ( q != NULL )
		{
			len++;
			n++;
			fprintf(stderr, " %s", q->symbol);
			q = q->next;
			if ( q == NULL ) fprintf(stderr, "\n");
		}
		count[len]++;
		if ( *p != NULL ) hi = p-table;
	}

	fprintf(stderr, "Storing %d recs used %d hash positions out of %d\n",
					n, size-count[0], size);
	fprintf(stderr, "%f %% utilization\n",
					((float)(size-count[0]))/((float)size));
	for (i=0; i<20; i++)
	{
		if ( count[i] != 0 )
		{
			avg += (((float)(i*count[i]))/((float)n)) * i;
			fprintf(stderr, "Bucket len %d == %d (%f %% of recs)\n",
							i, count[i], ((float)(i*count[i]))/((float)n));
		}
	}
	fprintf(stderr, "Avg bucket length %f\n", avg);
	fprintf(stderr, "Range of hash function: %d..%d\n", low, hi);
}

/*
 * 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.
 */
Sym *
aSymNew(text)
char *text;
{
	Sym *p;
	char *aSymSaveStr();
	
	if ( (p = (Sym *) calloc(1,sizeof(Sym))) == 0 )
	{
		fprintf(stderr,"Out of memory\n");
		exit(1);
	}
	p->symbol = aSymSaveStr(text);
	
	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.
 */
char *
aSymSaveStr(s)
register char *s;
{
	register char *start=strp;

	while ( *s != '\0' ) { *strp++ = *s++; }
	*strp++ = '\0';

	return( start );
}
SHAR_EOF
fi # end of overwriting check
if test -f 'sym.h'
then
	echo shar: will not over-write existing file "'sym.h'"
else
cat << \SHAR_EOF > 'sym.h'

/* minimum ANTLR symbol table record */

typedef struct symrec {
			char *symbol;
			struct symrec *next, *prev, **head, *scope;
		} Sym, *SymPtr;

Sym *aSymGet();
Sym *aSymNew();
Sym *aSymRmScope();
Sym **aSymScope();
char *aSymSaveStr();
SHAR_EOF
fi # end of overwriting check
#	End of shell archive
exit 0
