ack/util/topgen/symtab.c

63 lines
1.4 KiB
C
Raw Normal View History

1994-06-24 11:31:16 +00:00
/* $Id$ */
1987-03-09 19:15:41 +00:00
/*
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
1986-11-24 20:42:13 +00:00
/* s y m t a b . c
*
* Contains the routine findident, which builds the symbol table and
* searches identifiers
*/
2019-05-10 17:18:01 +00:00
#include <stdlib.h>
#include <string.h>
#include "symtab.h"
1986-11-24 20:42:13 +00:00
struct symtab *idtable, *deftable;
extern void error(char *s, char* s1);
2019-05-10 17:18:01 +00:00
struct symtab * findident(char *s, int mode, struct symtab **table)
{
1986-11-24 20:42:13 +00:00
/*
2019-05-10 17:18:01 +00:00
* Look for identifier s in the symboltable referred to by *table.
* If mode = LOOKING, no new entry's will be made.
* If mode = ENTERING, a new entry will be made if s is not in the
* table yet, otherwise an error results
1986-11-24 20:42:13 +00:00
*/
2019-05-10 17:18:01 +00:00
register struct symtab *p;
register int n;
if (!*table)
{ /* No entry for this symbol */
if (mode == LOOKING)
return (struct symtab *) 0;
/*
* Make new entry
*/
p = (struct symtab *) malloc(sizeof *p);
p->s_left = p->s_right = (struct symtab *) 0;
p->s_name = malloc((unsigned) (strlen(s) + 1));
strcpy(p->s_name, s);
*table = p;
return p;
}
else
{
p = *table;
if ((n = strcmp(p->s_name, s)) == 0)
{ /* This is it! */
if (mode == ENTERING)
{
error("Identifier %s redefined", s);
}
return p;
}
/* Binary tree ..... */
if (n < 0)
return findident(s, mode, &(p->s_left));
return findident(s, mode, &(p->s_right));
1986-11-24 20:42:13 +00:00
}
2019-05-10 17:18:01 +00:00
/* NOTREACHED */
1986-11-24 20:42:13 +00:00
}