K&R style function removal. Update functions to ANSI style.
[dragonfly.git] / usr.bin / lex / tblcmp.c
1 /* tblcmp - table compression routines */
2
3 /*-
4  * Copyright (c) 1990 The Regents of the University of California.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Vern Paxson.
9  * 
10  * The United States Government has rights in this work pursuant
11  * to contract no. DE-AC03-76SF00098 between the United States
12  * Department of Energy and the University of California.
13  *
14  * Redistribution and use in source and binary forms are permitted provided
15  * that: (1) source distributions retain this entire copyright notice and
16  * comment, and (2) distributions including binaries display the following
17  * acknowledgement:  ``This product includes software developed by the
18  * University of California, Berkeley and its contributors'' in the
19  * documentation or other materials provided with the distribution and in
20  * all advertising materials mentioning features or use of this software.
21  * Neither the name of the University nor the names of its contributors may
22  * be used to endorse or promote products derived from this software without
23  * specific prior written permission.
24  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
25  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
26  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27  */
28
29 /* $Header: /home/daffy/u0/vern/flex/RCS/tblcmp.c,v 2.11 94/11/05 17:08:28 vern Exp $ */
30 /* $FreeBSD: src/usr.bin/lex/tblcmp.c,v 1.5 1999/10/27 07:56:47 obrien Exp $ */
31 /* $DragonFly: src/usr.bin/lex/tblcmp.c,v 1.3 2003/10/04 20:36:47 hmp Exp $ */
32
33 #include "flexdef.h"
34
35
36 /* declarations for functions that have forward references */
37
38 void mkentry PROTO((register int*, int, int, int, int));
39 void mkprot PROTO((int[], int, int));
40 void mktemplate PROTO((int[], int, int));
41 void mv2front PROTO((int));
42 int tbldiff PROTO((int[], int, int[]));
43
44
45 /* bldtbl - build table entries for dfa state
46  *
47  * synopsis
48  *   int state[numecs], statenum, totaltrans, comstate, comfreq;
49  *   bldtbl( state, statenum, totaltrans, comstate, comfreq );
50  *
51  * State is the statenum'th dfa state.  It is indexed by equivalence class and
52  * gives the number of the state to enter for a given equivalence class.
53  * totaltrans is the total number of transitions out of the state.  Comstate
54  * is that state which is the destination of the most transitions out of State.
55  * Comfreq is how many transitions there are out of State to Comstate.
56  *
57  * A note on terminology:
58  *    "protos" are transition tables which have a high probability of
59  * either being redundant (a state processed later will have an identical
60  * transition table) or nearly redundant (a state processed later will have
61  * many of the same out-transitions).  A "most recently used" queue of
62  * protos is kept around with the hope that most states will find a proto
63  * which is similar enough to be usable, and therefore compacting the
64  * output tables.
65  *    "templates" are a special type of proto.  If a transition table is
66  * homogeneous or nearly homogeneous (all transitions go to the same
67  * destination) then the odds are good that future states will also go
68  * to the same destination state on basically the same character set.
69  * These homogeneous states are so common when dealing with large rule
70  * sets that they merit special attention.  If the transition table were
71  * simply made into a proto, then (typically) each subsequent, similar
72  * state will differ from the proto for two out-transitions.  One of these
73  * out-transitions will be that character on which the proto does not go
74  * to the common destination, and one will be that character on which the
75  * state does not go to the common destination.  Templates, on the other
76  * hand, go to the common state on EVERY transition character, and therefore
77  * cost only one difference.
78  */
79
80 void bldtbl(int *state, int statenum, int totaltrans, int comstate, int comfreq)
81         {
82         int extptr, extrct[2][CSIZE + 1];
83         int mindiff, minprot, i, d;
84
85         /* If extptr is 0 then the first array of extrct holds the result
86          * of the "best difference" to date, which is those transitions
87          * which occur in "state" but not in the proto which, to date,
88          * has the fewest differences between itself and "state".  If
89          * extptr is 1 then the second array of extrct hold the best
90          * difference.  The two arrays are toggled between so that the
91          * best difference to date can be kept around and also a difference
92          * just created by checking against a candidate "best" proto.
93          */
94
95         extptr = 0;
96
97         /* If the state has too few out-transitions, don't bother trying to
98          * compact its tables.
99          */
100
101         if ( (totaltrans * 100) < (numecs * PROTO_SIZE_PERCENTAGE) )
102                 mkentry( state, numecs, statenum, JAMSTATE, totaltrans );
103
104         else
105                 {
106                 /* "checkcom" is true if we should only check "state" against
107                  * protos which have the same "comstate" value.
108                  */
109                 int checkcom =
110                         comfreq * 100 > totaltrans * CHECK_COM_PERCENTAGE;
111
112                 minprot = firstprot;
113                 mindiff = totaltrans;
114
115                 if ( checkcom )
116                         {
117                         /* Find first proto which has the same "comstate". */
118                         for ( i = firstprot; i != NIL; i = protnext[i] )
119                                 if ( protcomst[i] == comstate )
120                                         {
121                                         minprot = i;
122                                         mindiff = tbldiff( state, minprot,
123                                                         extrct[extptr] );
124                                         break;
125                                         }
126                         }
127
128                 else
129                         {
130                         /* Since we've decided that the most common destination
131                          * out of "state" does not occur with a high enough
132                          * frequency, we set the "comstate" to zero, assuring
133                          * that if this state is entered into the proto list,
134                          * it will not be considered a template.
135                          */
136                         comstate = 0;
137
138                         if ( firstprot != NIL )
139                                 {
140                                 minprot = firstprot;
141                                 mindiff = tbldiff( state, minprot,
142                                                 extrct[extptr] );
143                                 }
144                         }
145
146                 /* We now have the first interesting proto in "minprot".  If
147                  * it matches within the tolerances set for the first proto,
148                  * we don't want to bother scanning the rest of the proto list
149                  * to see if we have any other reasonable matches.
150                  */
151
152                 if ( mindiff * 100 > totaltrans * FIRST_MATCH_DIFF_PERCENTAGE )
153                         {
154                         /* Not a good enough match.  Scan the rest of the
155                          * protos.
156                          */
157                         for ( i = minprot; i != NIL; i = protnext[i] )
158                                 {
159                                 d = tbldiff( state, i, extrct[1 - extptr] );
160                                 if ( d < mindiff )
161                                         {
162                                         extptr = 1 - extptr;
163                                         mindiff = d;
164                                         minprot = i;
165                                         }
166                                 }
167                         }
168
169                 /* Check if the proto we've decided on as our best bet is close
170                  * enough to the state we want to match to be usable.
171                  */
172
173                 if ( mindiff * 100 > totaltrans * ACCEPTABLE_DIFF_PERCENTAGE )
174                         {
175                         /* No good.  If the state is homogeneous enough,
176                          * we make a template out of it.  Otherwise, we
177                          * make a proto.
178                          */
179
180                         if ( comfreq * 100 >=
181                              totaltrans * TEMPLATE_SAME_PERCENTAGE )
182                                 mktemplate( state, statenum, comstate );
183
184                         else
185                                 {
186                                 mkprot( state, statenum, comstate );
187                                 mkentry( state, numecs, statenum,
188                                         JAMSTATE, totaltrans );
189                                 }
190                         }
191
192                 else
193                         { /* use the proto */
194                         mkentry( extrct[extptr], numecs, statenum,
195                                 prottbl[minprot], mindiff );
196
197                         /* If this state was sufficiently different from the
198                          * proto we built it from, make it, too, a proto.
199                          */
200
201                         if ( mindiff * 100 >=
202                              totaltrans * NEW_PROTO_DIFF_PERCENTAGE )
203                                 mkprot( state, statenum, comstate );
204
205                         /* Since mkprot added a new proto to the proto queue,
206                          * it's possible that "minprot" is no longer on the
207                          * proto queue (if it happened to have been the last
208                          * entry, it would have been bumped off).  If it's
209                          * not there, then the new proto took its physical
210                          * place (though logically the new proto is at the
211                          * beginning of the queue), so in that case the
212                          * following call will do nothing.
213                          */
214
215                         mv2front( minprot );
216                         }
217                 }
218         }
219
220
221 /* cmptmps - compress template table entries
222  *
223  * Template tables are compressed by using the 'template equivalence
224  * classes', which are collections of transition character equivalence
225  * classes which always appear together in templates - really meta-equivalence
226  * classes.
227  */
228
229 void cmptmps(void)
230         {
231         int tmpstorage[CSIZE + 1];
232         register int *tmp = tmpstorage, i, j;
233         int totaltrans, trans;
234
235         peakpairs = numtemps * numecs + tblend;
236
237         if ( usemecs )
238                 {
239                 /* Create equivalence classes based on data gathered on
240                  * template transitions.
241                  */
242                 nummecs = cre8ecs( tecfwd, tecbck, numecs );
243                 }
244
245         else
246                 nummecs = numecs;
247
248         while ( lastdfa + numtemps + 1 >= current_max_dfas )
249                 increase_max_dfas();
250
251         /* Loop through each template. */
252
253         for ( i = 1; i <= numtemps; ++i )
254                 {
255                 /* Number of non-jam transitions out of this template. */
256                 totaltrans = 0;
257
258                 for ( j = 1; j <= numecs; ++j )
259                         {
260                         trans = tnxt[numecs * i + j];
261
262                         if ( usemecs )
263                                 {
264                                 /* The absolute value of tecbck is the
265                                  * meta-equivalence class of a given
266                                  * equivalence class, as set up by cre8ecs().
267                                  */
268                                 if ( tecbck[j] > 0 )
269                                         {
270                                         tmp[tecbck[j]] = trans;
271
272                                         if ( trans > 0 )
273                                                 ++totaltrans;
274                                         }
275                                 }
276
277                         else
278                                 {
279                                 tmp[j] = trans;
280
281                                 if ( trans > 0 )
282                                         ++totaltrans;
283                                 }
284                         }
285
286                 /* It is assumed (in a rather subtle way) in the skeleton
287                  * that if we're using meta-equivalence classes, the def[]
288                  * entry for all templates is the jam template, i.e.,
289                  * templates never default to other non-jam table entries
290                  * (e.g., another template)
291                  */
292
293                 /* Leave room for the jam-state after the last real state. */
294                 mkentry( tmp, nummecs, lastdfa + i + 1, JAMSTATE, totaltrans );
295                 }
296         }
297
298
299
300 /* expand_nxt_chk - expand the next check arrays */
301
302 void expand_nxt_chk(void)
303         {
304         register int old_max = current_max_xpairs;
305
306         current_max_xpairs += MAX_XPAIRS_INCREMENT;
307
308         ++num_reallocs;
309
310         nxt = reallocate_integer_array( nxt, current_max_xpairs );
311         chk = reallocate_integer_array( chk, current_max_xpairs );
312
313         zero_out( (char *) (chk + old_max),
314                 (size_t) (MAX_XPAIRS_INCREMENT * sizeof( int )) );
315         }
316
317
318 /* find_table_space - finds a space in the table for a state to be placed
319  *
320  * synopsis
321  *     int *state, numtrans, block_start;
322  *     int find_table_space();
323  *
324  *     block_start = find_table_space( state, numtrans );
325  *
326  * State is the state to be added to the full speed transition table.
327  * Numtrans is the number of out-transitions for the state.
328  *
329  * find_table_space() returns the position of the start of the first block (in
330  * chk) able to accommodate the state
331  *
332  * In determining if a state will or will not fit, find_table_space() must take
333  * into account the fact that an end-of-buffer state will be added at [0],
334  * and an action number will be added in [-1].
335  */
336
337 int find_table_space(int *state, int numtrans)
338         {
339         /* Firstfree is the position of the first possible occurrence of two
340          * consecutive unused records in the chk and nxt arrays.
341          */
342         register int i;
343         register int *state_ptr, *chk_ptr;
344         register int *ptr_to_last_entry_in_state;
345
346         /* If there are too many out-transitions, put the state at the end of
347          * nxt and chk.
348          */
349         if ( numtrans > MAX_XTIONS_FULL_INTERIOR_FIT )
350                 {
351                 /* If table is empty, return the first available spot in
352                  * chk/nxt, which should be 1.
353                  */
354                 if ( tblend < 2 )
355                         return 1;
356
357                 /* Start searching for table space near the end of
358                  * chk/nxt arrays.
359                  */
360                 i = tblend - numecs;
361                 }
362
363         else
364                 /* Start searching for table space from the beginning
365                  * (skipping only the elements which will definitely not
366                  * hold the new state).
367                  */
368                 i = firstfree;
369
370         while ( 1 )     /* loops until a space is found */
371                 {
372                 while ( i + numecs >= current_max_xpairs )
373                         expand_nxt_chk();
374
375                 /* Loops until space for end-of-buffer and action number
376                  * are found.
377                  */
378                 while ( 1 )
379                         {
380                         /* Check for action number space. */
381                         if ( chk[i - 1] == 0 )
382                                 {
383                                 /* Check for end-of-buffer space. */
384                                 if ( chk[i] == 0 )
385                                         break;
386
387                                 else
388                                         /* Since i != 0, there is no use
389                                          * checking to see if (++i) - 1 == 0,
390                                          * because that's the same as i == 0,
391                                          * so we skip a space.
392                                          */
393                                         i += 2;
394                                 }
395
396                         else
397                                 ++i;
398
399                         while ( i + numecs >= current_max_xpairs )
400                                 expand_nxt_chk();
401                         }
402
403                 /* If we started search from the beginning, store the new
404                  * firstfree for the next call of find_table_space().
405                  */
406                 if ( numtrans <= MAX_XTIONS_FULL_INTERIOR_FIT )
407                         firstfree = i + 1;
408
409                 /* Check to see if all elements in chk (and therefore nxt)
410                  * that are needed for the new state have not yet been taken.
411                  */
412
413                 state_ptr = &state[1];
414                 ptr_to_last_entry_in_state = &chk[i + numecs + 1];
415
416                 for ( chk_ptr = &chk[i + 1];
417                       chk_ptr != ptr_to_last_entry_in_state; ++chk_ptr )
418                         if ( *(state_ptr++) != 0 && *chk_ptr != 0 )
419                                 break;
420
421                 if ( chk_ptr == ptr_to_last_entry_in_state )
422                         return i;
423
424                 else
425                 ++i;
426                 }
427         }
428
429
430 /* inittbl - initialize transition tables
431  *
432  * Initializes "firstfree" to be one beyond the end of the table.  Initializes
433  * all "chk" entries to be zero.
434  */
435 void inittbl(void)
436         {
437         register int i;
438
439         zero_out( (char *) chk, (size_t) (current_max_xpairs * sizeof( int )) );
440
441         tblend = 0;
442         firstfree = tblend + 1;
443         numtemps = 0;
444
445         if ( usemecs )
446                 {
447                 /* Set up doubly-linked meta-equivalence classes; these
448                  * are sets of equivalence classes which all have identical
449                  * transitions out of TEMPLATES.
450                  */
451
452                 tecbck[1] = NIL;
453
454                 for ( i = 2; i <= numecs; ++i )
455                         {
456                         tecbck[i] = i - 1;
457                         tecfwd[i - 1] = i;
458                         }
459
460                 tecfwd[numecs] = NIL;
461                 }
462         }
463
464
465 /* mkdeftbl - make the default, "jam" table entries */
466
467 void mkdeftbl(void)
468         {
469         int i;
470
471         jamstate = lastdfa + 1;
472
473         ++tblend; /* room for transition on end-of-buffer character */
474
475         while ( tblend + numecs >= current_max_xpairs )
476                 expand_nxt_chk();
477
478         /* Add in default end-of-buffer transition. */
479         nxt[tblend] = end_of_buffer_state;
480         chk[tblend] = jamstate;
481
482         for ( i = 1; i <= numecs; ++i )
483                 {
484                 nxt[tblend + i] = 0;
485                 chk[tblend + i] = jamstate;
486                 }
487
488         jambase = tblend;
489
490         base[jamstate] = jambase;
491         def[jamstate] = 0;
492
493         tblend += numecs;
494         ++numtemps;
495         }
496
497
498 /* mkentry - create base/def and nxt/chk entries for transition array
499  *
500  * synopsis
501  *   int state[numchars + 1], numchars, statenum, deflink, totaltrans;
502  *   mkentry( state, numchars, statenum, deflink, totaltrans );
503  *
504  * "state" is a transition array "numchars" characters in size, "statenum"
505  * is the offset to be used into the base/def tables, and "deflink" is the
506  * entry to put in the "def" table entry.  If "deflink" is equal to
507  * "JAMSTATE", then no attempt will be made to fit zero entries of "state"
508  * (i.e., jam entries) into the table.  It is assumed that by linking to
509  * "JAMSTATE" they will be taken care of.  In any case, entries in "state"
510  * marking transitions to "SAME_TRANS" are treated as though they will be
511  * taken care of by whereever "deflink" points.  "totaltrans" is the total
512  * number of transitions out of the state.  If it is below a certain threshold,
513  * the tables are searched for an interior spot that will accommodate the
514  * state array.
515  */
516
517 void mkentry(register int *state, int numchars, int statenum, int deflink, 
518              int totaltrans)
519         {
520         register int minec, maxec, i, baseaddr;
521         int tblbase, tbllast;
522
523         if ( totaltrans == 0 )
524                 { /* there are no out-transitions */
525                 if ( deflink == JAMSTATE )
526                         base[statenum] = JAMSTATE;
527                 else
528                         base[statenum] = 0;
529
530                 def[statenum] = deflink;
531                 return;
532                 }
533
534         for ( minec = 1; minec <= numchars; ++minec )
535                 {
536                 if ( state[minec] != SAME_TRANS )
537                         if ( state[minec] != 0 || deflink != JAMSTATE )
538                                 break;
539                 }
540
541         if ( totaltrans == 1 )
542                 {
543                 /* There's only one out-transition.  Save it for later to fill
544                  * in holes in the tables.
545                  */
546                 stack1( statenum, minec, state[minec], deflink );
547                 return;
548                 }
549
550         for ( maxec = numchars; maxec > 0; --maxec )
551                 {
552                 if ( state[maxec] != SAME_TRANS )
553                         if ( state[maxec] != 0 || deflink != JAMSTATE )
554                                 break;
555                 }
556
557         /* Whether we try to fit the state table in the middle of the table
558          * entries we have already generated, or if we just take the state
559          * table at the end of the nxt/chk tables, we must make sure that we
560          * have a valid base address (i.e., non-negative).  Note that
561          * negative base addresses dangerous at run-time (because indexing
562          * the nxt array with one and a low-valued character will access
563          * memory before the start of the array.
564          */
565
566         /* Find the first transition of state that we need to worry about. */
567         if ( totaltrans * 100 <= numchars * INTERIOR_FIT_PERCENTAGE )
568                 {
569                 /* Attempt to squeeze it into the middle of the tables. */
570                 baseaddr = firstfree;
571
572                 while ( baseaddr < minec )
573                         {
574                         /* Using baseaddr would result in a negative base
575                          * address below; find the next free slot.
576                          */
577                         for ( ++baseaddr; chk[baseaddr] != 0; ++baseaddr )
578                                 ;
579                         }
580
581                 while ( baseaddr + maxec - minec + 1 >= current_max_xpairs )
582                         expand_nxt_chk();
583
584                 for ( i = minec; i <= maxec; ++i )
585                         if ( state[i] != SAME_TRANS &&
586                              (state[i] != 0 || deflink != JAMSTATE) &&
587                              chk[baseaddr + i - minec] != 0 )
588                                 { /* baseaddr unsuitable - find another */
589                                 for ( ++baseaddr;
590                                       baseaddr < current_max_xpairs &&
591                                       chk[baseaddr] != 0; ++baseaddr )
592                                         ;
593
594                                 while ( baseaddr + maxec - minec + 1 >=
595                                         current_max_xpairs )
596                                         expand_nxt_chk();
597
598                                 /* Reset the loop counter so we'll start all
599                                  * over again next time it's incremented.
600                                  */
601
602                                 i = minec - 1;
603                                 }
604                 }
605
606         else
607                 {
608                 /* Ensure that the base address we eventually generate is
609                  * non-negative.
610                  */
611                 baseaddr = MAX( tblend + 1, minec );
612                 }
613
614         tblbase = baseaddr - minec;
615         tbllast = tblbase + maxec;
616
617         while ( tbllast + 1 >= current_max_xpairs )
618                 expand_nxt_chk();
619
620         base[statenum] = tblbase;
621         def[statenum] = deflink;
622
623         for ( i = minec; i <= maxec; ++i )
624                 if ( state[i] != SAME_TRANS )
625                         if ( state[i] != 0 || deflink != JAMSTATE )
626                                 {
627                                 nxt[tblbase + i] = state[i];
628                                 chk[tblbase + i] = statenum;
629                                 }
630
631         if ( baseaddr == firstfree )
632                 /* Find next free slot in tables. */
633                 for ( ++firstfree; chk[firstfree] != 0; ++firstfree )
634                         ;
635
636         tblend = MAX( tblend, tbllast );
637         }
638
639
640 /* mk1tbl - create table entries for a state (or state fragment) which
641  *            has only one out-transition
642  */
643
644 void mk1tbl(int state, int sym, int onenxt, int onedef)
645         {
646         if ( firstfree < sym )
647                 firstfree = sym;
648
649         while ( chk[firstfree] != 0 )
650                 if ( ++firstfree >= current_max_xpairs )
651                         expand_nxt_chk();
652
653         base[state] = firstfree - sym;
654         def[state] = onedef;
655         chk[firstfree] = state;
656         nxt[firstfree] = onenxt;
657
658         if ( firstfree > tblend )
659                 {
660                 tblend = firstfree++;
661
662                 if ( firstfree >= current_max_xpairs )
663                         expand_nxt_chk();
664                 }
665         }
666
667
668 /* mkprot - create new proto entry */
669
670 void mkprot(int *state, int statenum, int comstate )
671         {
672         int i, slot, tblbase;
673
674         if ( ++numprots >= MSP || numecs * numprots >= PROT_SAVE_SIZE )
675                 {
676                 /* Gotta make room for the new proto by dropping last entry in
677                  * the queue.
678                  */
679                 slot = lastprot;
680                 lastprot = protprev[lastprot];
681                 protnext[lastprot] = NIL;
682                 }
683
684         else
685                 slot = numprots;
686
687         protnext[slot] = firstprot;
688
689         if ( firstprot != NIL )
690                 protprev[firstprot] = slot;
691
692         firstprot = slot;
693         prottbl[slot] = statenum;
694         protcomst[slot] = comstate;
695
696         /* Copy state into save area so it can be compared with rapidly. */
697         tblbase = numecs * (slot - 1);
698
699         for ( i = 1; i <= numecs; ++i )
700                 protsave[tblbase + i] = state[i];
701         }
702
703
704 /* mktemplate - create a template entry based on a state, and connect the state
705  *              to it
706  */
707
708 void mktemplate(int *state, int statenum, int comstate)
709         {
710         int i, numdiff, tmpbase, tmp[CSIZE + 1];
711         Char transset[CSIZE + 1];
712         int tsptr;
713
714         ++numtemps;
715
716         tsptr = 0;
717
718         /* Calculate where we will temporarily store the transition table
719          * of the template in the tnxt[] array.  The final transition table
720          * gets created by cmptmps().
721          */
722
723         tmpbase = numtemps * numecs;
724
725         if ( tmpbase + numecs >= current_max_template_xpairs )
726                 {
727                 current_max_template_xpairs += MAX_TEMPLATE_XPAIRS_INCREMENT;
728
729                 ++num_reallocs;
730
731                 tnxt = reallocate_integer_array( tnxt,
732                         current_max_template_xpairs );
733                 }
734
735         for ( i = 1; i <= numecs; ++i )
736                 if ( state[i] == 0 )
737                         tnxt[tmpbase + i] = 0;
738                 else
739                         {
740                         transset[tsptr++] = i;
741                         tnxt[tmpbase + i] = comstate;
742                         }
743
744         if ( usemecs )
745                 mkeccl( transset, tsptr, tecfwd, tecbck, numecs, 0 );
746
747         mkprot( tnxt + tmpbase, -numtemps, comstate );
748
749         /* We rely on the fact that mkprot adds things to the beginning
750          * of the proto queue.
751          */
752
753         numdiff = tbldiff( state, firstprot, tmp );
754         mkentry( tmp, numecs, statenum, -numtemps, numdiff );
755         }
756
757
758 /* mv2front - move proto queue element to front of queue */
759
760 void mv2front(int qelm)
761         {
762         if ( firstprot != qelm )
763                 {
764                 if ( qelm == lastprot )
765                         lastprot = protprev[lastprot];
766
767                 protnext[protprev[qelm]] = protnext[qelm];
768
769                 if ( protnext[qelm] != NIL )
770                         protprev[protnext[qelm]] = protprev[qelm];
771
772                 protprev[qelm] = NIL;
773                 protnext[qelm] = firstprot;
774                 protprev[firstprot] = qelm;
775                 firstprot = qelm;
776                 }
777         }
778
779
780 /* place_state - place a state into full speed transition table
781  *
782  * State is the statenum'th state.  It is indexed by equivalence class and
783  * gives the number of the state to enter for a given equivalence class.
784  * Transnum is the number of out-transitions for the state.
785  */
786
787 void place_state(int *state, int statenum, int transnum )
788         {
789         register int i;
790         register int *state_ptr;
791         int position = find_table_space( state, transnum );
792
793         /* "base" is the table of start positions. */
794         base[statenum] = position;
795
796         /* Put in action number marker; this non-zero number makes sure that
797          * find_table_space() knows that this position in chk/nxt is taken
798          * and should not be used for another accepting number in another
799          * state.
800          */
801         chk[position - 1] = 1;
802
803         /* Put in end-of-buffer marker; this is for the same purposes as
804          * above.
805          */
806         chk[position] = 1;
807
808         /* Place the state into chk and nxt. */
809         state_ptr = &state[1];
810
811         for ( i = 1; i <= numecs; ++i, ++state_ptr )
812                 if ( *state_ptr != 0 )
813                         {
814                         chk[position + i] = i;
815                         nxt[position + i] = *state_ptr;
816                         }
817
818         if ( position + numecs > tblend )
819                 tblend = position + numecs;
820         }
821
822
823 /* stack1 - save states with only one out-transition to be processed later
824  *
825  * If there's room for another state on the "one-transition" stack, the
826  * state is pushed onto it, to be processed later by mk1tbl.  If there's
827  * no room, we process the sucker right now.
828  */
829
830 void stack1(int statenum, int sym, int nextstate, int deflink)
831         {
832         if ( onesp >= ONE_STACK_SIZE - 1 )
833                 mk1tbl( statenum, sym, nextstate, deflink );
834
835         else
836                 {
837                 ++onesp;
838                 onestate[onesp] = statenum;
839                 onesym[onesp] = sym;
840                 onenext[onesp] = nextstate;
841                 onedef[onesp] = deflink;
842                 }
843         }
844
845
846 /* tbldiff - compute differences between two state tables
847  *
848  * "state" is the state array which is to be extracted from the pr'th
849  * proto.  "pr" is both the number of the proto we are extracting from
850  * and an index into the save area where we can find the proto's complete
851  * state table.  Each entry in "state" which differs from the corresponding
852  * entry of "pr" will appear in "ext".
853  *
854  * Entries which are the same in both "state" and "pr" will be marked
855  * as transitions to "SAME_TRANS" in "ext".  The total number of differences
856  * between "state" and "pr" is returned as function value.  Note that this
857  * number is "numecs" minus the number of "SAME_TRANS" entries in "ext".
858  */
859
860 int tbldiff(int *state, int pr, int *ext)
861         {
862         register int i, *sp = state, *ep = ext, *protp;
863         register int numdiff = 0;
864
865         protp = &protsave[numecs * (pr - 1)];
866
867         for ( i = numecs; i > 0; --i )
868                 {
869                 if ( *++protp == *++sp )
870                         *++ep = SAME_TRANS;
871                 else
872                         {
873                         *++ep = *sp;
874                         ++numdiff;
875                         }
876                 }
877
878         return numdiff;
879         }