Annotation of 43BSDReno/pgrm/yacc/symtab.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * Copyright (c) 1989 The Regents of the University of California.
        !             3:  * All rights reserved.
        !             4:  *
        !             5:  * This code is derived from software contributed to Berkeley by
        !             6:  * Robert Paul Corbett.
        !             7:  *
        !             8:  * Redistribution and use in source and binary forms are permitted provided
        !             9:  * that: (1) source distributions retain this entire copyright notice and
        !            10:  * comment, and (2) distributions including binaries display the following
        !            11:  * acknowledgement:  ``This product includes software developed by the
        !            12:  * University of California, Berkeley and its contributors'' in the
        !            13:  * documentation or other materials provided with the distribution and in
        !            14:  * all advertising materials mentioning features or use of this software.
        !            15:  * Neither the name of the University nor the names of its contributors may
        !            16:  * be used to endorse or promote products derived from this software without
        !            17:  * specific prior written permission.
        !            18:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
        !            19:  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
        !            20:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
        !            21:  */
        !            22: 
        !            23: #ifndef lint
        !            24: static char sccsid[] = "@(#)symtab.c   5.3 (Berkeley) 6/1/90";
        !            25: #endif /* not lint */
        !            26: 
        !            27: #include "defs.h"
        !            28: 
        !            29: /* TABLE_SIZE is the number of entries in the symbol table. */
        !            30: /* TABLE_SIZE must be a power of two.                      */
        !            31: 
        !            32: #define        TABLE_SIZE 1024
        !            33: 
        !            34: 
        !            35: bucket **symbol_table;
        !            36: bucket *first_symbol;
        !            37: bucket *last_symbol;
        !            38: 
        !            39: 
        !            40: int
        !            41: hash(name)
        !            42: char *name;
        !            43: {
        !            44:     register char *s;
        !            45:     register int c, k;
        !            46: 
        !            47:     assert(name && *name);
        !            48:     s = name;
        !            49:     k = *s;
        !            50:     while (c = *++s)
        !            51:        k = (31*k + c) & (TABLE_SIZE - 1);
        !            52: 
        !            53:     return (k);
        !            54: }
        !            55: 
        !            56: 
        !            57: bucket *
        !            58: make_bucket(name)
        !            59: char *name;
        !            60: {
        !            61:     register bucket *bp;
        !            62: 
        !            63:     assert(name);
        !            64:     bp = (bucket *) MALLOC(sizeof(bucket));
        !            65:     if (bp == 0) no_space();
        !            66:     bp->link = 0;
        !            67:     bp->next = 0;
        !            68:     bp->name = MALLOC(strlen(name) + 1);
        !            69:     if (bp->name == 0) no_space();
        !            70:     bp->tag = 0;
        !            71:     bp->value = UNDEFINED;
        !            72:     bp->index = 0;
        !            73:     bp->prec = 0;
        !            74:     bp-> class = UNKNOWN;
        !            75:     bp->assoc = TOKEN;
        !            76: 
        !            77:     if (bp->name == 0) no_space();
        !            78:     strcpy(bp->name, name);
        !            79: 
        !            80:     return (bp);
        !            81: }
        !            82: 
        !            83: 
        !            84: bucket *
        !            85: lookup(name)
        !            86: char *name;
        !            87: {
        !            88:     register bucket *bp, **bpp;
        !            89: 
        !            90:     bpp = symbol_table + hash(name);
        !            91:     bp = *bpp;
        !            92: 
        !            93:     while (bp)
        !            94:     {
        !            95:        if (strcmp(name, bp->name) == 0) return (bp);
        !            96:        bpp = &bp->link;
        !            97:        bp = *bpp;
        !            98:     }
        !            99: 
        !           100:     *bpp = bp = make_bucket(name);
        !           101:     last_symbol->next = bp;
        !           102:     last_symbol = bp;
        !           103: 
        !           104:     return (bp);
        !           105: }
        !           106: 
        !           107: 
        !           108: create_symbol_table()
        !           109: {
        !           110:     register int i;
        !           111:     register bucket *bp;
        !           112: 
        !           113:     symbol_table = (bucket **) MALLOC(TABLE_SIZE*sizeof(bucket *));
        !           114:     if (symbol_table == 0) no_space();
        !           115:     for (i = 0; i < TABLE_SIZE; i++)
        !           116:        symbol_table[i] = 0;
        !           117: 
        !           118:     bp = make_bucket("error");
        !           119:     bp->index = 1;
        !           120:     bp->class = TERM;
        !           121: 
        !           122:     first_symbol = bp;
        !           123:     last_symbol = bp;
        !           124:     symbol_table[hash("error")] = bp;
        !           125: }
        !           126: 
        !           127: 
        !           128: free_symbol_table()
        !           129: {
        !           130:     FREE(symbol_table);
        !           131:     symbol_table = 0;
        !           132: }
        !           133: 
        !           134: 
        !           135: free_symbols()
        !           136: {
        !           137:     register bucket *p, *q;
        !           138: 
        !           139:     for (p = first_symbol; p; p = q)
        !           140:     {
        !           141:        q = p->next;
        !           142:        FREE(p);
        !           143:     }
        !           144: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.