sym.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 with or without
  14. * modification are permitted provided that: (1) source distributions retain
  15. * this entire copyright notice and comment, and (2) distributions including
  16. * binaries display the following acknowledgement: ``This product includes
  17. * software developed by the University of California, Berkeley and its
  18. * contributors'' in the documentation or other materials provided with the
  19. * distribution and in all advertising materials mentioning features or use
  20. * of this software. Neither the name of the University nor the names of
  21. * its contributors may be used to endorse or promote products derived from
  22. * this software without 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. /* $Header: /opt/vlysenkov/CVSROOT/arcadia/contrib/tools/flex-old/sym.c,v 1.2 2007-11-30 02:28:15 pg Exp $ */
  28. #include "flexdef.h"
  29. /* declare functions that have forward references */
  30. int hashfunct PROTO((char[], int));
  31. struct hash_entry *ndtbl[NAME_TABLE_HASH_SIZE];
  32. struct hash_entry *sctbl[START_COND_HASH_SIZE];
  33. struct hash_entry *ccltab[CCL_HASH_SIZE];
  34. struct hash_entry *findsym();
  35. /* addsym - add symbol and definitions to symbol table
  36. *
  37. * -1 is returned if the symbol already exists, and the change not made.
  38. */
  39. int addsym( sym, str_def, int_def, table, table_size )
  40. char sym[];
  41. char *str_def;
  42. int int_def;
  43. hash_table table;
  44. int table_size;
  45. {
  46. int hash_val = hashfunct( sym, table_size );
  47. struct hash_entry *sym_entry = table[hash_val];
  48. struct hash_entry *new_entry;
  49. struct hash_entry *successor;
  50. while ( sym_entry )
  51. {
  52. if ( ! strcmp( sym, sym_entry->name ) )
  53. { /* entry already exists */
  54. return -1;
  55. }
  56. sym_entry = sym_entry->next;
  57. }
  58. /* create new entry */
  59. new_entry = (struct hash_entry *)
  60. flex_alloc( sizeof( struct hash_entry ) );
  61. if ( new_entry == NULL )
  62. flexfatal( _( "symbol table memory allocation failed" ) );
  63. if ( (successor = table[hash_val]) != 0 )
  64. {
  65. new_entry->next = successor;
  66. successor->prev = new_entry;
  67. }
  68. else
  69. new_entry->next = NULL;
  70. new_entry->prev = NULL;
  71. new_entry->name = sym;
  72. new_entry->str_val = str_def;
  73. new_entry->int_val = int_def;
  74. table[hash_val] = new_entry;
  75. return 0;
  76. }
  77. /* cclinstal - save the text of a character class */
  78. void cclinstal( ccltxt, cclnum )
  79. Char ccltxt[];
  80. int cclnum;
  81. {
  82. /* We don't bother checking the return status because we are not
  83. * called unless the symbol is new.
  84. */
  85. Char *copy_unsigned_string();
  86. (void) addsym( (char *) copy_unsigned_string( ccltxt ),
  87. (char *) 0, cclnum,
  88. ccltab, CCL_HASH_SIZE );
  89. }
  90. /* ccllookup - lookup the number associated with character class text
  91. *
  92. * Returns 0 if there's no CCL associated with the text.
  93. */
  94. int ccllookup( ccltxt )
  95. Char ccltxt[];
  96. {
  97. return findsym( (char *) ccltxt, ccltab, CCL_HASH_SIZE )->int_val;
  98. }
  99. /* findsym - find symbol in symbol table */
  100. struct hash_entry *findsym( sym, table, table_size )
  101. char sym[];
  102. hash_table table;
  103. int table_size;
  104. {
  105. static struct hash_entry empty_entry =
  106. {
  107. (struct hash_entry *) 0, (struct hash_entry *) 0,
  108. (char *) 0, (char *) 0, 0,
  109. } ;
  110. struct hash_entry *sym_entry =
  111. table[hashfunct( sym, table_size )];
  112. while ( sym_entry )
  113. {
  114. if ( ! strcmp( sym, sym_entry->name ) )
  115. return sym_entry;
  116. sym_entry = sym_entry->next;
  117. }
  118. return &empty_entry;
  119. }
  120. /* hashfunct - compute the hash value for "str" and hash size "hash_size" */
  121. int hashfunct( str, hash_size )
  122. char str[];
  123. int hash_size;
  124. {
  125. int hashval;
  126. int locstr;
  127. hashval = 0;
  128. locstr = 0;
  129. while ( str[locstr] )
  130. {
  131. hashval = (hashval << 1) + (unsigned char) str[locstr++];
  132. hashval %= hash_size;
  133. }
  134. return hashval;
  135. }
  136. /* ndinstal - install a name definition */
  137. void ndinstal( name, definition )
  138. char name[];
  139. Char definition[];
  140. {
  141. char *copy_string();
  142. Char *copy_unsigned_string();
  143. if ( addsym( copy_string( name ),
  144. (char *) copy_unsigned_string( definition ), 0,
  145. ndtbl, NAME_TABLE_HASH_SIZE ) )
  146. synerr( _( "name defined twice" ) );
  147. }
  148. /* ndlookup - lookup a name definition
  149. *
  150. * Returns a nil pointer if the name definition does not exist.
  151. */
  152. Char *ndlookup( nd )
  153. char nd[];
  154. {
  155. return (Char *) findsym( nd, ndtbl, NAME_TABLE_HASH_SIZE )->str_val;
  156. }
  157. /* scextend - increase the maximum number of start conditions */
  158. void scextend()
  159. {
  160. current_max_scs += MAX_SCS_INCREMENT;
  161. ++num_reallocs;
  162. scset = reallocate_integer_array( scset, current_max_scs );
  163. scbol = reallocate_integer_array( scbol, current_max_scs );
  164. scxclu = reallocate_integer_array( scxclu, current_max_scs );
  165. sceof = reallocate_integer_array( sceof, current_max_scs );
  166. scname = reallocate_char_ptr_array( scname, current_max_scs );
  167. }
  168. /* scinstal - make a start condition
  169. *
  170. * NOTE
  171. * The start condition is "exclusive" if xcluflg is true.
  172. */
  173. void scinstal( str, xcluflg )
  174. char str[];
  175. int xcluflg;
  176. {
  177. char *copy_string();
  178. /* Generate start condition definition, for use in BEGIN et al. */
  179. action_define( str, lastsc );
  180. if ( ++lastsc >= current_max_scs )
  181. scextend();
  182. scname[lastsc] = copy_string( str );
  183. if ( addsym( scname[lastsc], (char *) 0, lastsc,
  184. sctbl, START_COND_HASH_SIZE ) )
  185. format_pinpoint_message(
  186. _( "start condition %s declared twice" ),
  187. str );
  188. scset[lastsc] = mkstate( SYM_EPSILON );
  189. scbol[lastsc] = mkstate( SYM_EPSILON );
  190. scxclu[lastsc] = xcluflg;
  191. sceof[lastsc] = false;
  192. }
  193. /* sclookup - lookup the number associated with a start condition
  194. *
  195. * Returns 0 if no such start condition.
  196. */
  197. int sclookup( str )
  198. char str[];
  199. {
  200. return findsym( str, sctbl, START_COND_HASH_SIZE )->int_val;
  201. }