This is Unofficial EPICS BASE Doxygen Site
ecs.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 /* ecs - equivalence class 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 /* ccl2ecl - convert character classes to set of equivalence classes
40  *
41  * synopsis
42  * ccl2ecl();
43  */
44 
45 void ccl2ecl(void)
46 {
47  int i, ich, newlen, cclp, ccls, cclmec;
48 
49  for ( i = 1; i <= lastccl; ++i )
50  {
51  /* we loop through each character class, and for each character
52  * in the class, add the character's equivalence class to the
53  * new "character" class we are creating. Thus when we are all
54  * done, character classes will really consist of collections
55  * of equivalence classes
56  */
57 
58  newlen = 0;
59  cclp = cclmap[i];
60 
61  for ( ccls = 0; ccls < ccllen[i]; ++ccls )
62  {
63  ich = ccltbl[cclp + ccls];
64  cclmec = ecgroup[ich];
65 
66  if ( xlation && cclmec < 0 )
67  {
68  /* special hack--if we're doing %t tables then it's
69  * possible that no representative of this character's
70  * equivalence class is in the ccl. So waiting till
71  * we see the representative would be disastrous. Instead,
72  * we add this character's equivalence class anyway, if it's
73  * not already present.
74  */
75  int j;
76 
77  /* this loop makes this whole process n^2; but we don't
78  * really care about %t performance anyway
79  */
80  for ( j = 0; j < newlen; ++j )
81  if ( ccltbl[cclp + j] == -cclmec )
82  break;
83 
84  if ( j >= newlen )
85  { /* no representative yet, add this one in */
86  ccltbl[cclp + newlen] = -cclmec;
87  ++newlen;
88  }
89  }
90 
91  else if ( cclmec > 0 )
92  {
93  ccltbl[cclp + newlen] = cclmec;
94  ++newlen;
95  }
96  }
97 
98  ccllen[i] = newlen;
99  }
100  }
101 
102 
103 /* cre8ecs - associate equivalence class numbers with class members
104  *
105  * synopsis
106  * int cre8ecs();
107  * number of classes = cre8ecs( fwd, bck, num );
108  *
109  * fwd is the forward linked-list of equivalence class members. bck
110  * is the backward linked-list, and num is the number of class members.
111  *
112  * Returned is the number of classes.
113  */
114 
115 int cre8ecs(int fwd[], int bck[], int num)
116 {
117  int i, j, numcl;
118 
119  numcl = 0;
120 
121  /* create equivalence class numbers. From now on, abs( bck(x) )
122  * is the equivalence class number for object x. If bck(x)
123  * is positive, then x is the representative of its equivalence
124  * class.
125  */
126  for ( i = 1; i <= num; ++i )
127  if ( bck[i] == NIL )
128  {
129  bck[i] = ++numcl;
130  for ( j = fwd[i]; j != NIL; j = fwd[j] )
131  bck[j] = -numcl;
132  }
133 
134  return ( numcl );
135  }
136 
137 
138 /* ecs_from_xlation - associate equivalence class numbers using %t table
139  *
140  * synopsis
141  * numecs = ecs_from_xlation( ecmap );
142  *
143  * Upon return, ecmap will map each character code to its equivalence
144  * class. The mapping will be positive if the character is the representative
145  * of its class, negative otherwise.
146  *
147  * Returns the number of equivalence classes used.
148  */
149 
150 int ecs_from_xlation(int ecmap[])
151 {
152  int i;
153  int nul_is_alone = false;
154  int did_default_xlation_class = false;
155 
156  if ( xlation[0] != 0 )
157  {
158  /* if NUL shares its translation with other characters, choose one
159  * of the other characters as the representative for the equivalence
160  * class. This allows a cheap test later to see whether we can
161  * do away with NUL's equivalence class.
162  */
163  for ( i = 1; i < csize; ++i )
164  if ( xlation[i] == -xlation[0] )
165  {
166  xlation[i] = xlation[0];
167  ecmap[0] = -xlation[0];
168  break;
169  }
170 
171  if ( i >= csize )
172  /* didn't find a companion character--remember this fact */
173  nul_is_alone = true;
174  }
175 
176  for ( i = 1; i < csize; ++i )
177  if ( xlation[i] == 0 )
178  {
179  if ( did_default_xlation_class )
180  ecmap[i] = -num_xlations;
181 
182  else
183  {
184  /* make an equivalence class for those characters not
185  * specified in the %t table
186  */
187  ++num_xlations;
188  ecmap[i] = num_xlations;
189  did_default_xlation_class = true;
190  }
191  }
192 
193  else
194  ecmap[i] = xlation[i];
195 
196  if ( nul_is_alone )
197  /* force NUL's equivalence class to be the last one */
198  {
199  ++num_xlations;
200  ecmap[0] = num_xlations;
201 
202  /* there's actually a bug here: if someone is fanatic enough to
203  * put every character in its own translation class, then right
204  * now we just promoted NUL's equivalence class to be csize + 1;
205  * we can handle NUL's class number being == csize (by instead
206  * putting it in its own table), but we can't handle some *other*
207  * character having to be put in its own table, too. So in
208  * this case we bail out.
209  */
210  if ( num_xlations > csize )
211  flexfatal( "too many %t classes!" );
212  }
213 
214  return num_xlations;
215  }
216 
217 
218 /* mkeccl - update equivalence classes based on character class xtions
219  *
220  * synopsis
221  * Char ccls[];
222  * int lenccl, fwd[llsiz], bck[llsiz], llsiz, NUL_mapping;
223  * mkeccl( ccls, lenccl, fwd, bck, llsiz, NUL_mapping );
224  *
225  * where ccls contains the elements of the character class, lenccl is the
226  * number of elements in the ccl, fwd is the forward link-list of equivalent
227  * characters, bck is the backward link-list, and llsiz size of the link-list
228  *
229  * NUL_mapping is the value which NUL (0) should be mapped to.
230  */
231 
232 void mkeccl(unsigned char ccls[], int lenccl, int fwd[], int bck[], int llsiz, int NUL_mapping)
233 {
234  int cclp, oldec, newec;
235  int cclm, i, j;
236  static unsigned char cclflags[CSIZE]; /* initialized to all '\0' */
237 
238  /* note that it doesn't matter whether or not the character class is
239  * negated. The same results will be obtained in either case.
240  */
241 
242  cclp = 0;
243 
244  while ( cclp < lenccl )
245  {
246  cclm = ccls[cclp];
247 
248  if ( NUL_mapping && cclm == 0 )
249  cclm = NUL_mapping;
250 
251  oldec = bck[cclm];
252  newec = cclm;
253 
254  j = cclp + 1;
255 
256  for ( i = fwd[cclm]; i != NIL && i <= llsiz; i = fwd[i] )
257  { /* look for the symbol in the character class */
258  for ( ; j < lenccl; ++j )
259  {
260  int ccl_char;
261 
262  if ( NUL_mapping && ccls[j] == 0 )
263  ccl_char = NUL_mapping;
264  else
265  ccl_char = ccls[j];
266 
267  if ( ccl_char > i )
268  break;
269 
270  if ( ccl_char == i && ! cclflags[j] )
271  {
272  /* we found an old companion of cclm in the ccl.
273  * link it into the new equivalence class and flag it as
274  * having been processed
275  */
276 
277  bck[i] = newec;
278  fwd[newec] = i;
279  newec = i;
280  cclflags[j] = 1; /* set flag so we don't reprocess */
281 
282  /* get next equivalence class member */
283  /* continue 2 */
284  goto next_pt;
285  }
286  }
287 
288  /* symbol isn't in character class. Put it in the old equivalence
289  * class
290  */
291 
292  bck[i] = oldec;
293 
294  if ( oldec != NIL )
295  fwd[oldec] = i;
296 
297  oldec = i;
298 next_pt:
299  ;
300  }
301 
302  if ( bck[cclm] != NIL || oldec != bck[cclm] )
303  {
304  bck[cclm] = NIL;
305  fwd[oldec] = NIL;
306  }
307 
308  fwd[newec] = NIL;
309 
310  /* find next ccl member to process */
311 
312  for ( ++cclp; cclflags[cclp] && cclp < lenccl; ++cclp )
313  {
314  /* reset "doesn't need processing" flag */
315  cclflags[cclp] = 0;
316  }
317  }
318  }
319 
320 
321 /* mkechar - create equivalence class for single character
322  *
323  * synopsis
324  * int tch, fwd[], bck[];
325  * mkechar( tch, fwd, bck );
326  */
327 
328 void mkechar(int tch, int fwd[], int bck[])
329 {
330  /* if until now the character has been a proper subset of
331  * an equivalence class, break it away to create a new ec
332  */
333 
334  if ( fwd[tch] != NIL )
335  bck[fwd[tch]] = bck[tch];
336 
337  if ( bck[tch] != NIL )
338  fwd[bck[tch]] = fwd[tch];
339 
340  fwd[tch] = NIL;
341  bck[tch] = NIL;
342  }
int cre8ecs(int fwd[], int bck[], int num)
Definition: ecs.c:115
int ecgroup[CSIZE+1]
Definition: flex.c:83
int i
Definition: scan.c:967
int * xlation
Definition: flex.c:85
#define NIL
Definition: flexdef.h:153
int ecs_from_xlation(int ecmap[])
Definition: ecs.c:150
int csize
Definition: flex.c:68
void flexfatal(char[])
int * ccllen
Definition: flex.c:96
int * cclmap
Definition: flex.c:96
void ccl2ecl(void)
Definition: ecs.c:45
Char * ccltbl
Definition: flex.c:98
int num_xlations
Definition: flex.c:86
#define CSIZE
Definition: flexdef.h:58
void mkeccl(unsigned char ccls[], int lenccl, int fwd[], int bck[], int llsiz, int NUL_mapping)
Definition: ecs.c:232
int lastccl
Definition: flex.c:96
void mkechar(int tch, int fwd[], int bck[])
Definition: ecs.c:328