sym.c 7.3 KB

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