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