home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / fish / misc_utils / yacc_419 / src / rcs / symtab.c,v < prev    next >
Text File  |  1990-07-14  |  2KB  |  145 lines

  1. head     1.1;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.1
  10. date     90.07.14.18.55.03;  author loftus;  state Exp;
  11. branches ;
  12. next     ;
  13.  
  14.  
  15. desc
  16. @@
  17.  
  18.  
  19.  
  20. 1.1
  21. log
  22. @Initial revision
  23. @
  24. text
  25. @#include "defs.h"
  26.  
  27.  
  28. /* TABLE_SIZE is the number of entries in the symbol table. */
  29. /* TABLE_SIZE must be a power of two.                */
  30.  
  31. #define    TABLE_SIZE 1024
  32.  
  33.  
  34. bucket **symbol_table;
  35. bucket *first_symbol;
  36. bucket *last_symbol;
  37.  
  38.  
  39. int
  40. hash(name)
  41. char *name;
  42. {
  43.     register char *s;
  44.     register int c, k;
  45.  
  46.     assert(name && *name);
  47.     s = name;
  48.     k = *s;
  49.     while (c = *++s)
  50.     k = (31*k + c) & (TABLE_SIZE - 1);
  51.  
  52.     return (k);
  53. }
  54.  
  55.  
  56. bucket *
  57. make_bucket(name)
  58. char *name;
  59. {
  60.     register bucket *bp;
  61.  
  62.     assert(name);
  63.     bp = (bucket *) MALLOC(sizeof(bucket));
  64.     if (bp == 0) no_space();
  65.     bp->link = 0;
  66.     bp->next = 0;
  67.     bp->name = MALLOC(strlen(name) + 1);
  68.     if (bp->name == 0) no_space();
  69.     bp->tag = 0;
  70.     bp->value = UNDEFINED;
  71.     bp->index = 0;
  72.     bp->prec = 0;
  73.     bp-> class = UNKNOWN;
  74.     bp->assoc = TOKEN;
  75.  
  76.     if (bp->name == 0) no_space();
  77.     strcpy(bp->name, name);
  78.  
  79.     return (bp);
  80. }
  81.  
  82.  
  83. bucket *
  84. lookup(name)
  85. char *name;
  86. {
  87.     register bucket *bp, **bpp;
  88.  
  89.     bpp = symbol_table + hash(name);
  90.     bp = *bpp;
  91.  
  92.     while (bp)
  93.     {
  94.     if (strcmp(name, bp->name) == 0) return (bp);
  95.     bpp = &bp->link;
  96.     bp = *bpp;
  97.     }
  98.  
  99.     *bpp = bp = make_bucket(name);
  100.     last_symbol->next = bp;
  101.     last_symbol = bp;
  102.  
  103.     return (bp);
  104. }
  105.  
  106.  
  107. create_symbol_table()
  108. {
  109.     register int i;
  110.     register bucket *bp;
  111.  
  112.     symbol_table = (bucket **) MALLOC(TABLE_SIZE*sizeof(bucket *));
  113.     if (symbol_table == 0) no_space();
  114.     for (i = 0; i < TABLE_SIZE; i++)
  115.     symbol_table[i] = 0;
  116.  
  117.     bp = make_bucket("error");
  118.     bp->index = 1;
  119.     bp->class = TERM;
  120.  
  121.     first_symbol = bp;
  122.     last_symbol = bp;
  123.     symbol_table[hash("error")] = bp;
  124. }
  125.  
  126.  
  127. free_symbol_table()
  128. {
  129.     FREE(symbol_table);
  130.     symbol_table = 0;
  131. }
  132.  
  133.  
  134. free_symbols()
  135. {
  136.     register bucket *p, *q;
  137.  
  138.     for (p = first_symbol; p; p = q)
  139.     {
  140.     q = p->next;
  141.     FREE(p);
  142.     }
  143. }
  144. @
  145.