Annotation of 43BSDReno/pgrm/lex/sym.c, revision 1.1.1.1

1.1       root        1: /* sym - symbol table routines */
                      2: 
                      3: /*-
                      4:  * Copyright (c) 1990 The Regents of the University of California.
                      5:  * All rights reserved.
                      6:  *
                      7:  * This code is derived from software contributed to Berkeley by
                      8:  * Vern Paxson.
                      9:  * 
                     10:  * The United States Government has rights in this work pursuant
                     11:  * to contract no. DE-AC03-76SF00098 between the United States
                     12:  * Department of Energy and the University of California.
                     13:  *
                     14:  * Redistribution and use in source and binary forms are permitted provided
                     15:  * that: (1) source distributions retain this entire copyright notice and
                     16:  * comment, and (2) distributions including binaries display the following
                     17:  * acknowledgement:  ``This product includes software developed by the
                     18:  * University of California, Berkeley and its contributors'' in the
                     19:  * documentation or other materials provided with the distribution and in
                     20:  * all advertising materials mentioning features or use of this software.
                     21:  * Neither the name of the University nor the names of its contributors may
                     22:  * be used to endorse or promote products derived from this software without
                     23:  * specific prior written permission.
                     24:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
                     25:  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
                     26:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
                     27:  */
                     28: 
                     29: #ifndef lint
                     30: static char sccsid[] = "@(#)sym.c      5.2 (Berkeley) 6/18/90";
                     31: #endif /* not lint */
                     32: 
                     33: #include "flexdef.h"
                     34: 
                     35: /* declare functions that have forward references */
                     36: 
                     37: int hashfunct PROTO((register char[], int));
                     38: 
                     39: 
                     40: struct hash_entry *ndtbl[NAME_TABLE_HASH_SIZE];
                     41: struct hash_entry *sctbl[START_COND_HASH_SIZE];
                     42: struct hash_entry *ccltab[CCL_HASH_SIZE];
                     43: 
                     44: struct hash_entry *findsym();
                     45: 
                     46: 
                     47: /* addsym - add symbol and definitions to symbol table
                     48:  *
                     49:  * synopsis
                     50:  *    char sym[], *str_def;
                     51:  *    int int_def;
                     52:  *    hash_table table;
                     53:  *    int table_size;
                     54:  *    0 / -1 = addsym( sym, def, int_def, table, table_size );
                     55:  *
                     56:  * -1 is returned if the symbol already exists, and the change not made.
                     57:  */
                     58: 
                     59: int addsym( sym, str_def, int_def, table, table_size )
                     60: register char sym[];
                     61: char *str_def;
                     62: int int_def;
                     63: hash_table table;
                     64: int table_size;
                     65: 
                     66:     {
                     67:     int hash_val = hashfunct( sym, table_size );
                     68:     register struct hash_entry *sym_entry = table[hash_val];
                     69:     register struct hash_entry *new_entry;
                     70:     register struct hash_entry *successor;
                     71: 
                     72:     while ( sym_entry )
                     73:        {
                     74:        if ( ! strcmp( sym, sym_entry->name ) )
                     75:            { /* entry already exists */
                     76:            return ( -1 );
                     77:            }
                     78:        
                     79:        sym_entry = sym_entry->next;
                     80:        }
                     81: 
                     82:     /* create new entry */
                     83:     new_entry = (struct hash_entry *) malloc( sizeof( struct hash_entry ) );
                     84: 
                     85:     if ( new_entry == NULL )
                     86:        flexfatal( "symbol table memory allocation failed" );
                     87: 
                     88:     if ( (successor = table[hash_val]) )
                     89:        {
                     90:        new_entry->next = successor;
                     91:        successor->prev = new_entry;
                     92:        }
                     93:     else
                     94:        new_entry->next = NULL;
                     95: 
                     96:     new_entry->prev = NULL;
                     97:     new_entry->name = sym;
                     98:     new_entry->str_val = str_def;
                     99:     new_entry->int_val = int_def;
                    100: 
                    101:     table[hash_val] = new_entry;
                    102: 
                    103:     return ( 0 );
                    104:     }
                    105: 
                    106: 
                    107: /* cclinstal - save the text of a character class
                    108:  *
                    109:  * synopsis
                    110:  *    Char ccltxt[];
                    111:  *    int cclnum;
                    112:  *    cclinstal( ccltxt, cclnum );
                    113:  */
                    114: 
                    115: void cclinstal( ccltxt, cclnum )
                    116: Char ccltxt[];
                    117: int cclnum;
                    118: 
                    119:     {
                    120:     /* we don't bother checking the return status because we are not called
                    121:      * unless the symbol is new
                    122:      */
                    123:     Char *copy_unsigned_string();
                    124: 
                    125:     (void) addsym( (char *) copy_unsigned_string( ccltxt ), (char *) 0, cclnum,
                    126:                   ccltab, CCL_HASH_SIZE );
                    127:     }
                    128: 
                    129: 
                    130: /* ccllookup - lookup the number associated with character class text
                    131:  *
                    132:  * synopsis
                    133:  *    Char ccltxt[];
                    134:  *    int ccllookup, cclval;
                    135:  *    cclval/0 = ccllookup( ccltxt );
                    136:  */
                    137: 
                    138: int ccllookup( ccltxt )
                    139: Char ccltxt[];
                    140: 
                    141:     {
                    142:     return ( findsym( (char *) ccltxt, ccltab, CCL_HASH_SIZE )->int_val );
                    143:     }
                    144: 
                    145: 
                    146: /* findsym - find symbol in symbol table
                    147:  *
                    148:  * synopsis
                    149:  *    char sym[];
                    150:  *    hash_table table;
                    151:  *    int table_size;
                    152:  *    struct hash_entry *sym_entry, *findsym();
                    153:  *    sym_entry = findsym( sym, table, table_size );
                    154:  */
                    155: 
                    156: struct hash_entry *findsym( sym, table, table_size )
                    157: register char sym[];
                    158: hash_table table;
                    159: int table_size;
                    160: 
                    161:     {
                    162:     register struct hash_entry *sym_entry = table[hashfunct( sym, table_size )];
                    163:     static struct hash_entry empty_entry =
                    164:        {
                    165:        (struct hash_entry *) 0, (struct hash_entry *) 0, NULL, NULL, 0,
                    166:        } ;
                    167: 
                    168:     while ( sym_entry )
                    169:        {
                    170:        if ( ! strcmp( sym, sym_entry->name ) )
                    171:            return ( sym_entry );
                    172:        sym_entry = sym_entry->next;
                    173:        }
                    174: 
                    175:     return ( &empty_entry );
                    176:     }
                    177: 
                    178:     
                    179: /* hashfunct - compute the hash value for "str" and hash size "hash_size"
                    180:  *
                    181:  * synopsis
                    182:  *    char str[];
                    183:  *    int hash_size, hash_val;
                    184:  *    hash_val = hashfunct( str, hash_size );
                    185:  */
                    186: 
                    187: int hashfunct( str, hash_size )
                    188: register char str[];
                    189: int hash_size;
                    190: 
                    191:     {
                    192:     register int hashval;
                    193:     register int locstr;
                    194: 
                    195:     hashval = 0;
                    196:     locstr = 0;
                    197: 
                    198:     while ( str[locstr] )
                    199:        hashval = ((hashval << 1) + str[locstr++]) % hash_size;
                    200: 
                    201:     return ( hashval );
                    202:     }
                    203: 
                    204: 
                    205: /* ndinstal - install a name definition
                    206:  *
                    207:  * synopsis
                    208:  *    char nd[];
                    209:  *    Char def[];
                    210:  *    ndinstal( nd, def );
                    211:  */
                    212: 
                    213: void ndinstal( nd, def )
                    214: char nd[];
                    215: Char def[];
                    216: 
                    217:     {
                    218:     char *copy_string();
                    219:     Char *copy_unsigned_string();
                    220: 
                    221:     if ( addsym( copy_string( nd ), (char *) copy_unsigned_string( def ), 0,
                    222:                 ndtbl, NAME_TABLE_HASH_SIZE ) )
                    223:        synerr( "name defined twice" );
                    224:     }
                    225: 
                    226: 
                    227: /* ndlookup - lookup a name definition
                    228:  *
                    229:  * synopsis
                    230:  *    char nd[], *def;
                    231:  *    char *ndlookup();
                    232:  *    def/NULL = ndlookup( nd );
                    233:  */
                    234: 
                    235: Char *ndlookup( nd )
                    236: char nd[];
                    237: 
                    238:     {
                    239:     return ( (Char *) findsym( nd, ndtbl, NAME_TABLE_HASH_SIZE )->str_val );
                    240:     }
                    241: 
                    242: 
                    243: /* scinstal - make a start condition
                    244:  *
                    245:  * synopsis
                    246:  *    char str[];
                    247:  *    int xcluflg;
                    248:  *    scinstal( str, xcluflg );
                    249:  *
                    250:  * NOTE
                    251:  *    the start condition is Exclusive if xcluflg is true
                    252:  */
                    253: 
                    254: void scinstal( str, xcluflg )
                    255: char str[];
                    256: int xcluflg;
                    257: 
                    258:     {
                    259:     char *copy_string();
                    260: 
                    261:     /* bit of a hack.  We know how the default start-condition is
                    262:      * declared, and don't put out a define for it, because it
                    263:      * would come out as "#define 0 1"
                    264:      */
                    265:     /* actually, this is no longer the case.  The default start-condition
                    266:      * is now called "INITIAL".  But we keep the following for the sake
                    267:      * of future robustness.
                    268:      */
                    269: 
                    270:     if ( strcmp( str, "0" ) )
                    271:        printf( "#define %s %d\n", str, lastsc );
                    272: 
                    273:     if ( ++lastsc >= current_max_scs )
                    274:        {
                    275:        current_max_scs += MAX_SCS_INCREMENT;
                    276: 
                    277:        ++num_reallocs;
                    278: 
                    279:        scset = reallocate_integer_array( scset, current_max_scs );
                    280:        scbol = reallocate_integer_array( scbol, current_max_scs );
                    281:        scxclu = reallocate_integer_array( scxclu, current_max_scs );
                    282:        sceof = reallocate_integer_array( sceof, current_max_scs );
                    283:        scname = reallocate_char_ptr_array( scname, current_max_scs );
                    284:        actvsc = reallocate_integer_array( actvsc, current_max_scs );
                    285:        }
                    286: 
                    287:     scname[lastsc] = copy_string( str );
                    288: 
                    289:     if ( addsym( scname[lastsc], (char *) 0, lastsc,
                    290:                 sctbl, START_COND_HASH_SIZE ) )
                    291:        format_pinpoint_message( "start condition %s declared twice", str );
                    292: 
                    293:     scset[lastsc] = mkstate( SYM_EPSILON );
                    294:     scbol[lastsc] = mkstate( SYM_EPSILON );
                    295:     scxclu[lastsc] = xcluflg;
                    296:     sceof[lastsc] = false;
                    297:     }
                    298: 
                    299: 
                    300: /* sclookup - lookup the number associated with a start condition
                    301:  *
                    302:  * synopsis
                    303:  *    char str[], scnum;
                    304:  *    int sclookup;
                    305:  *    scnum/0 = sclookup( str );
                    306:  */
                    307: 
                    308: int sclookup( str )
                    309: char str[];
                    310: 
                    311:     {
                    312:     return ( findsym( str, sctbl, START_COND_HASH_SIZE )->int_val );
                    313:     }

unix.superglobalmegacorp.com

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