symtab.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* Symbol table manager for Bison,
  2. Copyright (C) 1984, 1989 Free Software Foundation, Inc.
  3. This file is part of Bison, the GNU Compiler Compiler.
  4. Bison is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 1, or (at your option)
  7. any later version.
  8. Bison is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with Bison; see the file COPYING. If not, write to
  14. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  15. #include <stdio.h>
  16. #ifdef USG
  17. #include <string.h>
  18. #else /* NOT USG */
  19. #include <strings.h>
  20. #endif /* NOT USG */
  21. #include "new.h"
  22. #include "symtab.h"
  23. #include "gram.h"
  24. bucket **symtab;
  25. bucket *firstsymbol;
  26. bucket *lastsymbol;
  27. int
  28. hash(key)
  29. char *key;
  30. {
  31. register char *cp;
  32. register int k;
  33. cp = key;
  34. k = 0;
  35. while (*cp)
  36. k = ((k << 1) ^ (*cp++)) & 0x3fff;
  37. return (k % TABSIZE);
  38. }
  39. char *
  40. copys(s)
  41. char *s;
  42. {
  43. register int i;
  44. register char *cp;
  45. register char *result;
  46. i = 1;
  47. for (cp = s; *cp; cp++)
  48. i++;
  49. result = mallocate((unsigned int)i);
  50. strcpy(result, s);
  51. return (result);
  52. }
  53. void
  54. tabinit()
  55. {
  56. /* register int i; JF unused */
  57. symtab = NEW2(TABSIZE, bucket *);
  58. firstsymbol = NULL;
  59. lastsymbol = NULL;
  60. }
  61. bucket *
  62. getsym(key)
  63. char *key;
  64. {
  65. register int hashval;
  66. register bucket *bp;
  67. register int found;
  68. hashval = hash(key);
  69. bp = symtab[hashval];
  70. found = 0;
  71. while (bp != NULL && found == 0)
  72. {
  73. if (strcmp(key, bp->tag) == 0)
  74. found = 1;
  75. else
  76. bp = bp->link;
  77. }
  78. if (found == 0)
  79. {
  80. nsyms++;
  81. bp = NEW(bucket);
  82. bp->link = symtab[hashval];
  83. bp->next = NULL;
  84. bp->tag = copys(key);
  85. bp->class = SUNKNOWN;
  86. if (firstsymbol == NULL)
  87. {
  88. firstsymbol = bp;
  89. lastsymbol = bp;
  90. }
  91. else
  92. {
  93. lastsymbol->next = bp;
  94. lastsymbol = bp;
  95. }
  96. symtab[hashval] = bp;
  97. }
  98. return (bp);
  99. }
  100. void
  101. free_symtab()
  102. {
  103. register int i;
  104. register bucket *bp,*bptmp;/* JF don't use ptr after free */
  105. for (i = 0; i < TABSIZE; i++)
  106. {
  107. bp = symtab[i];
  108. while (bp)
  109. {
  110. bptmp=bp->link;
  111. FREE(bp);
  112. bp = bptmp;
  113. }
  114. }
  115. }