[BACK]Return to symtab.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / yacc

Annotation of src/usr.bin/yacc/symtab.c, Revision 1.6

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