No empty .Rs/.Re
[netbsd-mini2440.git] / usr.bin / yacc / symtab.c
bloba340cff68cbb3def2787599f909cdd60a19c1226
1 /* $NetBSD: symtab.c,v 1.11 2006/05/24 18:06:58 christos Exp $ */
3 /*
4 * Copyright (c) 1989 The Regents of the University of California.
5 * All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Robert Paul Corbett.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
35 #include <sys/cdefs.h>
36 #if defined(__RCSID) && !defined(lint)
37 #if 0
38 static char sccsid[] = "@(#)symtab.c 5.3 (Berkeley) 6/1/90";
39 #else
40 __RCSID("$NetBSD: symtab.c,v 1.11 2006/05/24 18:06:58 christos Exp $");
41 #endif
42 #endif /* not lint */
44 #include "defs.h"
46 /* TABLE_SIZE is the number of entries in the symbol table. */
47 /* TABLE_SIZE must be a power of two. */
49 #define TABLE_SIZE 1024
52 bucket *first_symbol;
53 bucket *last_symbol;
55 static bucket **symbol_table;
57 static int hash(const char *);
60 static int
61 hash(const char *name)
63 const char *s;
64 int c, k;
66 assert(name && *name);
67 s = name;
68 k = *s;
69 while ((c = *++s) != '\0')
70 k = (31*k + c) & (TABLE_SIZE - 1);
72 return (k);
76 bucket *
77 make_bucket(const char *name)
79 bucket *bp;
81 assert(name);
82 bp = (bucket *) MALLOC(sizeof(bucket));
83 if (bp == 0) no_space();
84 bp->link = 0;
85 bp->next = 0;
86 bp->name = strdup(name);
87 if (bp->name == 0) no_space();
88 bp->tag = 0;
89 bp->value = UNDEFINED;
90 bp->index = 0;
91 bp->prec = 0;
92 bp-> class = UNKNOWN;
93 bp->assoc = TOKEN;
95 return (bp);
99 bucket *
100 lookup(char *name)
102 bucket *bp, **bpp;
104 bpp = symbol_table + hash(name);
105 bp = *bpp;
107 while (bp)
109 if (strcmp(name, bp->name) == 0) return (bp);
110 bpp = &bp->link;
111 bp = *bpp;
114 *bpp = bp = make_bucket(name);
115 last_symbol->next = bp;
116 last_symbol = bp;
118 return (bp);
122 void
123 create_symbol_table(void)
125 int i;
126 bucket *bp;
128 symbol_table = (bucket **) MALLOC(TABLE_SIZE*sizeof(bucket *));
129 if (symbol_table == 0) no_space();
130 for (i = 0; i < TABLE_SIZE; i++)
131 symbol_table[i] = 0;
133 bp = make_bucket("error");
134 bp->index = 1;
135 bp->class = TERM;
137 first_symbol = bp;
138 last_symbol = bp;
139 symbol_table[hash("error")] = bp;
143 void
144 free_symbol_table(void)
146 FREE(symbol_table);
147 symbol_table = 0;
151 void
152 free_symbols(void)
154 bucket *p, *q;
156 for (p = first_symbol; p; p = q)
158 q = p->next;
159 FREE(p);