xref: /openbsd-src/usr.bin/yacc/symtab.c (revision bbfc4b6ece250468820c0645240f8b59dd6c31e9)
1*bbfc4b6eStedu /* $OpenBSD: symtab.c,v 1.17 2014/03/13 00:56:39 tedu Exp $	 */
2f3bae140Sderaadt /* $NetBSD: symtab.c,v 1.4 1996/03/19 03:21:48 jtc Exp $	 */
3f3bae140Sderaadt 
4f3bae140Sderaadt /*
5f3bae140Sderaadt  * Copyright (c) 1989 The Regents of the University of California.
6f3bae140Sderaadt  * All rights reserved.
7f3bae140Sderaadt  *
8f3bae140Sderaadt  * This code is derived from software contributed to Berkeley by
9f3bae140Sderaadt  * Robert Paul Corbett.
10f3bae140Sderaadt  *
11f3bae140Sderaadt  * Redistribution and use in source and binary forms, with or without
12f3bae140Sderaadt  * modification, are permitted provided that the following conditions
13f3bae140Sderaadt  * are met:
14f3bae140Sderaadt  * 1. Redistributions of source code must retain the above copyright
15f3bae140Sderaadt  *    notice, this list of conditions and the following disclaimer.
16f3bae140Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
17f3bae140Sderaadt  *    notice, this list of conditions and the following disclaimer in the
18f3bae140Sderaadt  *    documentation and/or other materials provided with the distribution.
19f75387cbSmillert  * 3. Neither the name of the University nor the names of its contributors
20f3bae140Sderaadt  *    may be used to endorse or promote products derived from this software
21f3bae140Sderaadt  *    without specific prior written permission.
22f3bae140Sderaadt  *
23f3bae140Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24f3bae140Sderaadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25f3bae140Sderaadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26f3bae140Sderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27f3bae140Sderaadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28f3bae140Sderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29f3bae140Sderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30f3bae140Sderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31f3bae140Sderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32f3bae140Sderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33f3bae140Sderaadt  * SUCH DAMAGE.
34f3bae140Sderaadt  */
35f3bae140Sderaadt 
36df930be7Sderaadt #include "defs.h"
37df930be7Sderaadt 
38df930be7Sderaadt /* TABLE_SIZE is the number of entries in the symbol table. */
39df930be7Sderaadt /* TABLE_SIZE must be a power of two.			    */
40df930be7Sderaadt 
41df930be7Sderaadt #define	TABLE_SIZE 1024
42df930be7Sderaadt 
43df930be7Sderaadt 
44df930be7Sderaadt bucket **symbol_table;
45df930be7Sderaadt bucket *first_symbol;
46df930be7Sderaadt bucket *last_symbol;
47df930be7Sderaadt 
48c72b5b24Smillert int hash(char *);
491d699125Spvalchev 
50df930be7Sderaadt 
51df930be7Sderaadt int
hash(char * name)52d1e49392Spvalchev hash(char *name)
53df930be7Sderaadt {
54c0932ef1Smpech 	char *s;
55c0932ef1Smpech 	int c, k;
56df930be7Sderaadt 
57df930be7Sderaadt 	assert(name && *name);
58df930be7Sderaadt 	s = name;
59df930be7Sderaadt 	k = *s;
603ff7a0b5Spvalchev 	while ((c = *++s))
61df930be7Sderaadt 		k = (31 * k + c) & (TABLE_SIZE - 1);
62df930be7Sderaadt 
63df930be7Sderaadt 	return (k);
64df930be7Sderaadt }
65df930be7Sderaadt 
66df930be7Sderaadt 
67df930be7Sderaadt bucket *
make_bucket(char * name)68d1e49392Spvalchev make_bucket(char *name)
69df930be7Sderaadt {
70c0932ef1Smpech 	bucket *bp;
71df930be7Sderaadt 
72df930be7Sderaadt 	assert(name);
73*bbfc4b6eStedu 	bp = malloc(sizeof(bucket));
74*bbfc4b6eStedu 	if (bp == NULL)
75*bbfc4b6eStedu 		no_space();
76df930be7Sderaadt 	bp->link = 0;
77df930be7Sderaadt 	bp->next = 0;
78cf73fb34Sderaadt 	bp->name = strdup(name);
79*bbfc4b6eStedu 	if (bp->name == NULL)
80*bbfc4b6eStedu 		no_space();
81df930be7Sderaadt 	bp->tag = 0;
82df930be7Sderaadt 	bp->value = UNDEFINED;
83df930be7Sderaadt 	bp->index = 0;
84df930be7Sderaadt 	bp->prec = 0;
85df930be7Sderaadt 	bp->class = UNKNOWN;
86df930be7Sderaadt 	bp->assoc = TOKEN;
87df930be7Sderaadt 
88df930be7Sderaadt 	return (bp);
89df930be7Sderaadt }
90df930be7Sderaadt 
91df930be7Sderaadt 
92df930be7Sderaadt bucket *
lookup(char * name)93d1e49392Spvalchev lookup(char *name)
94df930be7Sderaadt {
95c0932ef1Smpech 	bucket *bp, **bpp;
96df930be7Sderaadt 
97df930be7Sderaadt 	bpp = symbol_table + hash(name);
98df930be7Sderaadt 	bp = *bpp;
99df930be7Sderaadt 
100*bbfc4b6eStedu 	while (bp) {
101*bbfc4b6eStedu 		if (strcmp(name, bp->name) == 0)
102*bbfc4b6eStedu 			return (bp);
103df930be7Sderaadt 		bpp = &bp->link;
104df930be7Sderaadt 		bp = *bpp;
105df930be7Sderaadt 	}
106df930be7Sderaadt 
107df930be7Sderaadt 	*bpp = bp = make_bucket(name);
108df930be7Sderaadt 	last_symbol->next = bp;
109df930be7Sderaadt 	last_symbol = bp;
110df930be7Sderaadt 
111df930be7Sderaadt 	return (bp);
112df930be7Sderaadt }
113df930be7Sderaadt 
114df930be7Sderaadt 
1151d699125Spvalchev void
create_symbol_table(void)116d1e49392Spvalchev create_symbol_table(void)
117df930be7Sderaadt {
118c0932ef1Smpech 	bucket *bp;
119df930be7Sderaadt 
120ef356450Smillert 	symbol_table = calloc(TABLE_SIZE, sizeof(bucket *));
121*bbfc4b6eStedu 	if (symbol_table == NULL)
122*bbfc4b6eStedu 		no_space();
123df930be7Sderaadt 
124df930be7Sderaadt 	bp = make_bucket("error");
125df930be7Sderaadt 	bp->index = 1;
126df930be7Sderaadt 	bp->class = TERM;
127df930be7Sderaadt 
128df930be7Sderaadt 	first_symbol = bp;
129df930be7Sderaadt 	last_symbol = bp;
130df930be7Sderaadt 	symbol_table[hash("error")] = bp;
131df930be7Sderaadt }
132df930be7Sderaadt 
133df930be7Sderaadt 
1341d699125Spvalchev void
free_symbol_table(void)135d1e49392Spvalchev free_symbol_table(void)
136df930be7Sderaadt {
137ef356450Smillert 	free(symbol_table);
138df930be7Sderaadt 	symbol_table = 0;
139df930be7Sderaadt }
140df930be7Sderaadt 
141df930be7Sderaadt 
1421d699125Spvalchev void
free_symbols(void)143d1e49392Spvalchev free_symbols(void)
144df930be7Sderaadt {
145c0932ef1Smpech 	bucket *p, *q;
146df930be7Sderaadt 
147*bbfc4b6eStedu 	for (p = first_symbol; p; p = q) {
148df930be7Sderaadt 		q = p->next;
149ef356450Smillert 		free(p);
150df930be7Sderaadt 	}
151df930be7Sderaadt }
152