c99(1): Staticize.
[dragonfly.git] / contrib / flex / dfa.c
blobb8b68ebad8eb0b39c7923beae63c74e6bdec11d7
1 /* dfa - DFA construction routines */
3 /* Copyright (c) 1990 The Regents of the University of California. */
4 /* All rights reserved. */
6 /* This code is derived from software contributed to Berkeley by */
7 /* Vern Paxson. */
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. */
13 /* Redistribution and use in source and binary forms, with or without */
14 /* modification, are permitted provided that the following conditions */
15 /* are met: */
17 /* 1. Redistributions of source code must retain the above copyright */
18 /* notice, this list of conditions and the following disclaimer. */
19 /* 2. Redistributions in binary form must reproduce the above copyright */
20 /* notice, this list of conditions and the following disclaimer in the */
21 /* documentation and/or other materials provided with the distribution. */
23 /* Neither the name of the University nor the names of its contributors */
24 /* may be used to endorse or promote products derived from this software */
25 /* without specific prior written permission. */
27 /* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */
28 /* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */
29 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
30 /* PURPOSE. */
32 #include "flexdef.h"
33 #include "tables.h"
35 /* declare functions that have forward references */
37 void dump_associated_rules PROTO ((FILE *, int));
38 void dump_transitions PROTO ((FILE *, int[]));
39 void sympartition PROTO ((int[], int, int[], int[]));
40 int symfollowset PROTO ((int[], int, int, int[]));
43 /* check_for_backing_up - check a DFA state for backing up
45 * synopsis
46 * void check_for_backing_up( int ds, int state[numecs] );
48 * ds is the number of the state to check and state[] is its out-transitions,
49 * indexed by equivalence class.
52 void check_for_backing_up (ds, state)
53 int ds;
54 int state[];
56 if ((reject && !dfaacc[ds].dfaacc_set) || (!reject && !dfaacc[ds].dfaacc_state)) { /* state is non-accepting */
57 ++num_backing_up;
59 if (backing_up_report) {
60 fprintf (backing_up_file,
61 _("State #%d is non-accepting -\n"), ds);
63 /* identify the state */
64 dump_associated_rules (backing_up_file, ds);
66 /* Now identify it further using the out- and
67 * jam-transitions.
69 dump_transitions (backing_up_file, state);
71 putc ('\n', backing_up_file);
77 /* check_trailing_context - check to see if NFA state set constitutes
78 * "dangerous" trailing context
80 * synopsis
81 * void check_trailing_context( int nfa_states[num_states+1], int num_states,
82 * int accset[nacc+1], int nacc );
84 * NOTES
85 * Trailing context is "dangerous" if both the head and the trailing
86 * part are of variable size \and/ there's a DFA state which contains
87 * both an accepting state for the head part of the rule and NFA states
88 * which occur after the beginning of the trailing context.
90 * When such a rule is matched, it's impossible to tell if having been
91 * in the DFA state indicates the beginning of the trailing context or
92 * further-along scanning of the pattern. In these cases, a warning
93 * message is issued.
95 * nfa_states[1 .. num_states] is the list of NFA states in the DFA.
96 * accset[1 .. nacc] is the list of accepting numbers for the DFA state.
99 void check_trailing_context (nfa_states, num_states, accset, nacc)
100 int *nfa_states, num_states;
101 int *accset;
102 int nacc;
104 register int i, j;
106 for (i = 1; i <= num_states; ++i) {
107 int ns = nfa_states[i];
108 register int type = state_type[ns];
109 register int ar = assoc_rule[ns];
111 if (type == STATE_NORMAL || rule_type[ar] != RULE_VARIABLE) { /* do nothing */
114 else if (type == STATE_TRAILING_CONTEXT) {
115 /* Potential trouble. Scan set of accepting numbers
116 * for the one marking the end of the "head". We
117 * assume that this looping will be fairly cheap
118 * since it's rare that an accepting number set
119 * is large.
121 for (j = 1; j <= nacc; ++j)
122 if (accset[j] & YY_TRAILING_HEAD_MASK) {
123 line_warning (_
124 ("dangerous trailing context"),
125 rule_linenum[ar]);
126 return;
133 /* dump_associated_rules - list the rules associated with a DFA state
135 * Goes through the set of NFA states associated with the DFA and
136 * extracts the first MAX_ASSOC_RULES unique rules, sorts them,
137 * and writes a report to the given file.
140 void dump_associated_rules (file, ds)
141 FILE *file;
142 int ds;
144 register int i, j;
145 register int num_associated_rules = 0;
146 int rule_set[MAX_ASSOC_RULES + 1];
147 int *dset = dss[ds];
148 int size = dfasiz[ds];
150 for (i = 1; i <= size; ++i) {
151 register int rule_num = rule_linenum[assoc_rule[dset[i]]];
153 for (j = 1; j <= num_associated_rules; ++j)
154 if (rule_num == rule_set[j])
155 break;
157 if (j > num_associated_rules) { /* new rule */
158 if (num_associated_rules < MAX_ASSOC_RULES)
159 rule_set[++num_associated_rules] =
160 rule_num;
164 qsort (&rule_set [1], num_associated_rules, sizeof (rule_set [1]), intcmp);
166 fprintf (file, _(" associated rule line numbers:"));
168 for (i = 1; i <= num_associated_rules; ++i) {
169 if (i % 8 == 1)
170 putc ('\n', file);
172 fprintf (file, "\t%d", rule_set[i]);
175 putc ('\n', file);
179 /* dump_transitions - list the transitions associated with a DFA state
181 * synopsis
182 * dump_transitions( FILE *file, int state[numecs] );
184 * Goes through the set of out-transitions and lists them in human-readable
185 * form (i.e., not as equivalence classes); also lists jam transitions
186 * (i.e., all those which are not out-transitions, plus EOF). The dump
187 * is done to the given file.
190 void dump_transitions (file, state)
191 FILE *file;
192 int state[];
194 register int i, ec;
195 int out_char_set[CSIZE];
197 for (i = 0; i < csize; ++i) {
198 ec = ABS (ecgroup[i]);
199 out_char_set[i] = state[ec];
202 fprintf (file, _(" out-transitions: "));
204 list_character_set (file, out_char_set);
206 /* now invert the members of the set to get the jam transitions */
207 for (i = 0; i < csize; ++i)
208 out_char_set[i] = !out_char_set[i];
210 fprintf (file, _("\n jam-transitions: EOF "));
212 list_character_set (file, out_char_set);
214 putc ('\n', file);
218 /* epsclosure - construct the epsilon closure of a set of ndfa states
220 * synopsis
221 * int *epsclosure( int t[num_states], int *numstates_addr,
222 * int accset[num_rules+1], int *nacc_addr,
223 * int *hashval_addr );
225 * NOTES
226 * The epsilon closure is the set of all states reachable by an arbitrary
227 * number of epsilon transitions, which themselves do not have epsilon
228 * transitions going out, unioned with the set of states which have non-null
229 * accepting numbers. t is an array of size numstates of nfa state numbers.
230 * Upon return, t holds the epsilon closure and *numstates_addr is updated.
231 * accset holds a list of the accepting numbers, and the size of accset is
232 * given by *nacc_addr. t may be subjected to reallocation if it is not
233 * large enough to hold the epsilon closure.
235 * hashval is the hash value for the dfa corresponding to the state set.
238 int *epsclosure (t, ns_addr, accset, nacc_addr, hv_addr)
239 int *t, *ns_addr, accset[], *nacc_addr, *hv_addr;
241 register int stkpos, ns, tsp;
242 int numstates = *ns_addr, nacc, hashval, transsym, nfaccnum;
243 int stkend, nstate;
244 static int did_stk_init = false, *stk;
246 #define MARK_STATE(state) \
247 do{ trans1[state] = trans1[state] - MARKER_DIFFERENCE;} while(0)
249 #define IS_MARKED(state) (trans1[state] < 0)
251 #define UNMARK_STATE(state) \
252 do{ trans1[state] = trans1[state] + MARKER_DIFFERENCE;} while(0)
254 #define CHECK_ACCEPT(state) \
255 do{ \
256 nfaccnum = accptnum[state]; \
257 if ( nfaccnum != NIL ) \
258 accset[++nacc] = nfaccnum; \
259 }while(0)
261 #define DO_REALLOCATION() \
262 do { \
263 current_max_dfa_size += MAX_DFA_SIZE_INCREMENT; \
264 ++num_reallocs; \
265 t = reallocate_integer_array( t, current_max_dfa_size ); \
266 stk = reallocate_integer_array( stk, current_max_dfa_size ); \
267 }while(0) \
269 #define PUT_ON_STACK(state) \
270 do { \
271 if ( ++stkend >= current_max_dfa_size ) \
272 DO_REALLOCATION(); \
273 stk[stkend] = state; \
274 MARK_STATE(state); \
275 }while(0)
277 #define ADD_STATE(state) \
278 do { \
279 if ( ++numstates >= current_max_dfa_size ) \
280 DO_REALLOCATION(); \
281 t[numstates] = state; \
282 hashval += state; \
283 }while(0)
285 #define STACK_STATE(state) \
286 do { \
287 PUT_ON_STACK(state); \
288 CHECK_ACCEPT(state); \
289 if ( nfaccnum != NIL || transchar[state] != SYM_EPSILON ) \
290 ADD_STATE(state); \
291 }while(0)
294 if (!did_stk_init) {
295 stk = allocate_integer_array (current_max_dfa_size);
296 did_stk_init = true;
299 nacc = stkend = hashval = 0;
301 for (nstate = 1; nstate <= numstates; ++nstate) {
302 ns = t[nstate];
304 /* The state could be marked if we've already pushed it onto
305 * the stack.
307 if (!IS_MARKED (ns)) {
308 PUT_ON_STACK (ns);
309 CHECK_ACCEPT (ns);
310 hashval += ns;
314 for (stkpos = 1; stkpos <= stkend; ++stkpos) {
315 ns = stk[stkpos];
316 transsym = transchar[ns];
318 if (transsym == SYM_EPSILON) {
319 tsp = trans1[ns] + MARKER_DIFFERENCE;
321 if (tsp != NO_TRANSITION) {
322 if (!IS_MARKED (tsp))
323 STACK_STATE (tsp);
325 tsp = trans2[ns];
327 if (tsp != NO_TRANSITION
328 && !IS_MARKED (tsp))
329 STACK_STATE (tsp);
334 /* Clear out "visit" markers. */
336 for (stkpos = 1; stkpos <= stkend; ++stkpos) {
337 if (IS_MARKED (stk[stkpos]))
338 UNMARK_STATE (stk[stkpos]);
339 else
340 flexfatal (_
341 ("consistency check failed in epsclosure()"));
344 *ns_addr = numstates;
345 *hv_addr = hashval;
346 *nacc_addr = nacc;
348 return t;
352 /* increase_max_dfas - increase the maximum number of DFAs */
354 void increase_max_dfas ()
356 current_max_dfas += MAX_DFAS_INCREMENT;
358 ++num_reallocs;
360 base = reallocate_integer_array (base, current_max_dfas);
361 def = reallocate_integer_array (def, current_max_dfas);
362 dfasiz = reallocate_integer_array (dfasiz, current_max_dfas);
363 accsiz = reallocate_integer_array (accsiz, current_max_dfas);
364 dhash = reallocate_integer_array (dhash, current_max_dfas);
365 dss = reallocate_int_ptr_array (dss, current_max_dfas);
366 dfaacc = reallocate_dfaacc_union (dfaacc, current_max_dfas);
368 if (nultrans)
369 nultrans =
370 reallocate_integer_array (nultrans,
371 current_max_dfas);
375 /* ntod - convert an ndfa to a dfa
377 * Creates the dfa corresponding to the ndfa we've constructed. The
378 * dfa starts out in state #1.
381 void ntod ()
383 int *accset, ds, nacc, newds;
384 int sym, hashval, numstates, dsize;
385 int num_full_table_rows=0; /* used only for -f */
386 int *nset, *dset;
387 int targptr, totaltrans, i, comstate, comfreq, targ;
388 int symlist[CSIZE + 1];
389 int num_start_states;
390 int todo_head, todo_next;
392 struct yytbl_data *yynxt_tbl = 0;
393 flex_int32_t *yynxt_data = 0, yynxt_curr = 0;
395 /* Note that the following are indexed by *equivalence classes*
396 * and not by characters. Since equivalence classes are indexed
397 * beginning with 1, even if the scanner accepts NUL's, this
398 * means that (since every character is potentially in its own
399 * equivalence class) these arrays must have room for indices
400 * from 1 to CSIZE, so their size must be CSIZE + 1.
402 int duplist[CSIZE + 1], state[CSIZE + 1];
403 int targfreq[CSIZE + 1], targstate[CSIZE + 1];
405 /* accset needs to be large enough to hold all of the rules present
406 * in the input, *plus* their YY_TRAILING_HEAD_MASK variants.
408 accset = allocate_integer_array ((num_rules + 1) * 2);
409 nset = allocate_integer_array (current_max_dfa_size);
411 /* The "todo" queue is represented by the head, which is the DFA
412 * state currently being processed, and the "next", which is the
413 * next DFA state number available (not in use). We depend on the
414 * fact that snstods() returns DFA's \in increasing order/, and thus
415 * need only know the bounds of the dfas to be processed.
417 todo_head = todo_next = 0;
419 for (i = 0; i <= csize; ++i) {
420 duplist[i] = NIL;
421 symlist[i] = false;
424 for (i = 0; i <= num_rules; ++i)
425 accset[i] = NIL;
427 if (trace) {
428 dumpnfa (scset[1]);
429 fputs (_("\n\nDFA Dump:\n\n"), stderr);
432 inittbl ();
434 /* Check to see whether we should build a separate table for
435 * transitions on NUL characters. We don't do this for full-speed
436 * (-F) scanners, since for them we don't have a simple state
437 * number lying around with which to index the table. We also
438 * don't bother doing it for scanners unless (1) NUL is in its own
439 * equivalence class (indicated by a positive value of
440 * ecgroup[NUL]), (2) NUL's equivalence class is the last
441 * equivalence class, and (3) the number of equivalence classes is
442 * the same as the number of characters. This latter case comes
443 * about when useecs is false or when it's true but every character
444 * still manages to land in its own class (unlikely, but it's
445 * cheap to check for). If all these things are true then the
446 * character code needed to represent NUL's equivalence class for
447 * indexing the tables is going to take one more bit than the
448 * number of characters, and therefore we won't be assured of
449 * being able to fit it into a YY_CHAR variable. This rules out
450 * storing the transitions in a compressed table, since the code
451 * for interpreting them uses a YY_CHAR variable (perhaps it
452 * should just use an integer, though; this is worth pondering ...
453 * ###).
455 * Finally, for full tables, we want the number of entries in the
456 * table to be a power of two so the array references go fast (it
457 * will just take a shift to compute the major index). If
458 * encoding NUL's transitions in the table will spoil this, we
459 * give it its own table (note that this will be the case if we're
460 * not using equivalence classes).
463 /* Note that the test for ecgroup[0] == numecs below accomplishes
464 * both (1) and (2) above
466 if (!fullspd && ecgroup[0] == numecs) {
467 /* NUL is alone in its equivalence class, which is the
468 * last one.
470 int use_NUL_table = (numecs == csize);
472 if (fulltbl && !use_NUL_table) {
473 /* We still may want to use the table if numecs
474 * is a power of 2.
476 int power_of_two;
478 for (power_of_two = 1; power_of_two <= csize;
479 power_of_two *= 2)
480 if (numecs == power_of_two) {
481 use_NUL_table = true;
482 break;
486 if (use_NUL_table)
487 nultrans =
488 allocate_integer_array (current_max_dfas);
490 /* From now on, nultrans != nil indicates that we're
491 * saving null transitions for later, separate encoding.
496 if (fullspd) {
497 for (i = 0; i <= numecs; ++i)
498 state[i] = 0;
500 place_state (state, 0, 0);
501 dfaacc[0].dfaacc_state = 0;
504 else if (fulltbl) {
505 if (nultrans)
506 /* We won't be including NUL's transitions in the
507 * table, so build it for entries from 0 .. numecs - 1.
509 num_full_table_rows = numecs;
511 else
512 /* Take into account the fact that we'll be including
513 * the NUL entries in the transition table. Build it
514 * from 0 .. numecs.
516 num_full_table_rows = numecs + 1;
518 /* Begin generating yy_nxt[][]
519 * This spans the entire LONG function.
520 * This table is tricky because we don't know how big it will be.
521 * So we'll have to realloc() on the way...
522 * we'll wait until we can calculate yynxt_tbl->td_hilen.
524 yynxt_tbl =
525 (struct yytbl_data *) calloc (1,
526 sizeof (struct
527 yytbl_data));
528 yytbl_data_init (yynxt_tbl, YYTD_ID_NXT);
529 yynxt_tbl->td_hilen = 1;
530 yynxt_tbl->td_lolen = num_full_table_rows;
531 yynxt_tbl->td_data = yynxt_data =
532 (flex_int32_t *) calloc (yynxt_tbl->td_lolen *
533 yynxt_tbl->td_hilen,
534 sizeof (flex_int32_t));
535 yynxt_curr = 0;
537 buf_prints (&yydmap_buf,
538 "\t{YYTD_ID_NXT, (void**)&yy_nxt, sizeof(%s)},\n",
539 long_align ? "flex_int32_t" : "flex_int16_t");
541 /* Unless -Ca, declare it "short" because it's a real
542 * long-shot that that won't be large enough.
544 if (gentables)
545 out_str_dec
546 ("static yyconst %s yy_nxt[][%d] =\n {\n",
547 long_align ? "flex_int32_t" : "flex_int16_t",
548 num_full_table_rows);
549 else {
550 out_dec ("#undef YY_NXT_LOLEN\n#define YY_NXT_LOLEN (%d)\n", num_full_table_rows);
551 out_str ("static yyconst %s *yy_nxt =0;\n",
552 long_align ? "flex_int32_t" : "flex_int16_t");
556 if (gentables)
557 outn (" {");
559 /* Generate 0 entries for state #0. */
560 for (i = 0; i < num_full_table_rows; ++i) {
561 mk2data (0);
562 yynxt_data[yynxt_curr++] = 0;
565 dataflush ();
566 if (gentables)
567 outn (" },\n");
570 /* Create the first states. */
572 num_start_states = lastsc * 2;
574 for (i = 1; i <= num_start_states; ++i) {
575 numstates = 1;
577 /* For each start condition, make one state for the case when
578 * we're at the beginning of the line (the '^' operator) and
579 * one for the case when we're not.
581 if (i % 2 == 1)
582 nset[numstates] = scset[(i / 2) + 1];
583 else
584 nset[numstates] =
585 mkbranch (scbol[i / 2], scset[i / 2]);
587 nset = epsclosure (nset, &numstates, accset, &nacc,
588 &hashval);
590 if (snstods (nset, numstates, accset, nacc, hashval, &ds)) {
591 numas += nacc;
592 totnst += numstates;
593 ++todo_next;
595 if (variable_trailing_context_rules && nacc > 0)
596 check_trailing_context (nset, numstates,
597 accset, nacc);
601 if (!fullspd) {
602 if (!snstods (nset, 0, accset, 0, 0, &end_of_buffer_state))
603 flexfatal (_
604 ("could not create unique end-of-buffer state"));
606 ++numas;
607 ++num_start_states;
608 ++todo_next;
612 while (todo_head < todo_next) {
613 targptr = 0;
614 totaltrans = 0;
616 for (i = 1; i <= numecs; ++i)
617 state[i] = 0;
619 ds = ++todo_head;
621 dset = dss[ds];
622 dsize = dfasiz[ds];
624 if (trace)
625 fprintf (stderr, _("state # %d:\n"), ds);
627 sympartition (dset, dsize, symlist, duplist);
629 for (sym = 1; sym <= numecs; ++sym) {
630 if (symlist[sym]) {
631 symlist[sym] = 0;
633 if (duplist[sym] == NIL) {
634 /* Symbol has unique out-transitions. */
635 numstates =
636 symfollowset (dset, dsize,
637 sym, nset);
638 nset = epsclosure (nset,
639 &numstates,
640 accset, &nacc,
641 &hashval);
643 if (snstods
644 (nset, numstates, accset, nacc,
645 hashval, &newds)) {
646 totnst = totnst +
647 numstates;
648 ++todo_next;
649 numas += nacc;
651 if (variable_trailing_context_rules && nacc > 0)
652 check_trailing_context
653 (nset,
654 numstates,
655 accset,
656 nacc);
659 state[sym] = newds;
661 if (trace)
662 fprintf (stderr,
663 "\t%d\t%d\n", sym,
664 newds);
666 targfreq[++targptr] = 1;
667 targstate[targptr] = newds;
668 ++numuniq;
671 else {
672 /* sym's equivalence class has the same
673 * transitions as duplist(sym)'s
674 * equivalence class.
676 targ = state[duplist[sym]];
677 state[sym] = targ;
679 if (trace)
680 fprintf (stderr,
681 "\t%d\t%d\n", sym,
682 targ);
684 /* Update frequency count for
685 * destination state.
688 i = 0;
689 while (targstate[++i] != targ) ;
691 ++targfreq[i];
692 ++numdup;
695 ++totaltrans;
696 duplist[sym] = NIL;
701 numsnpairs += totaltrans;
703 if (ds > num_start_states)
704 check_for_backing_up (ds, state);
706 if (nultrans) {
707 nultrans[ds] = state[NUL_ec];
708 state[NUL_ec] = 0; /* remove transition */
711 if (fulltbl) {
713 /* Each time we hit here, it's another td_hilen, so we realloc. */
714 yynxt_tbl->td_hilen++;
715 yynxt_tbl->td_data = yynxt_data =
716 (flex_int32_t *) realloc (yynxt_data,
717 yynxt_tbl->td_hilen *
718 yynxt_tbl->td_lolen *
719 sizeof (flex_int32_t));
722 if (gentables)
723 outn (" {");
725 /* Supply array's 0-element. */
726 if (ds == end_of_buffer_state) {
727 mk2data (-end_of_buffer_state);
728 yynxt_data[yynxt_curr++] =
729 -end_of_buffer_state;
731 else {
732 mk2data (end_of_buffer_state);
733 yynxt_data[yynxt_curr++] =
734 end_of_buffer_state;
737 for (i = 1; i < num_full_table_rows; ++i) {
738 /* Jams are marked by negative of state
739 * number.
741 mk2data (state[i] ? state[i] : -ds);
742 yynxt_data[yynxt_curr++] =
743 state[i] ? state[i] : -ds;
746 dataflush ();
747 if (gentables)
748 outn (" },\n");
751 else if (fullspd)
752 place_state (state, ds, totaltrans);
754 else if (ds == end_of_buffer_state)
755 /* Special case this state to make sure it does what
756 * it's supposed to, i.e., jam on end-of-buffer.
758 stack1 (ds, 0, 0, JAMSTATE);
760 else { /* normal, compressed state */
762 /* Determine which destination state is the most
763 * common, and how many transitions to it there are.
766 comfreq = 0;
767 comstate = 0;
769 for (i = 1; i <= targptr; ++i)
770 if (targfreq[i] > comfreq) {
771 comfreq = targfreq[i];
772 comstate = targstate[i];
775 bldtbl (state, ds, totaltrans, comstate, comfreq);
779 if (fulltbl) {
780 dataend ();
781 if (tablesext) {
782 yytbl_data_compress (yynxt_tbl);
783 if (yytbl_data_fwrite (&tableswr, yynxt_tbl) < 0)
784 flexerror (_
785 ("Could not write yynxt_tbl[][]"));
787 if (yynxt_tbl) {
788 yytbl_data_destroy (yynxt_tbl);
789 yynxt_tbl = 0;
793 else if (!fullspd) {
794 cmptmps (); /* create compressed template entries */
796 /* Create tables for all the states with only one
797 * out-transition.
799 while (onesp > 0) {
800 mk1tbl (onestate[onesp], onesym[onesp],
801 onenext[onesp], onedef[onesp]);
802 --onesp;
805 mkdeftbl ();
808 flex_free ((void *) accset);
809 flex_free ((void *) nset);
813 /* snstods - converts a set of ndfa states into a dfa state
815 * synopsis
816 * is_new_state = snstods( int sns[numstates], int numstates,
817 * int accset[num_rules+1], int nacc,
818 * int hashval, int *newds_addr );
820 * On return, the dfa state number is in newds.
823 int snstods (sns, numstates, accset, nacc, hashval, newds_addr)
824 int sns[], numstates, accset[], nacc, hashval, *newds_addr;
826 int didsort = 0;
827 register int i, j;
828 int newds, *oldsns;
830 for (i = 1; i <= lastdfa; ++i)
831 if (hashval == dhash[i]) {
832 if (numstates == dfasiz[i]) {
833 oldsns = dss[i];
835 if (!didsort) {
836 /* We sort the states in sns so we
837 * can compare it to oldsns quickly.
839 qsort (&sns [1], numstates, sizeof (sns [1]), intcmp);
840 didsort = 1;
843 for (j = 1; j <= numstates; ++j)
844 if (sns[j] != oldsns[j])
845 break;
847 if (j > numstates) {
848 ++dfaeql;
849 *newds_addr = i;
850 return 0;
853 ++hshcol;
856 else
857 ++hshsave;
860 /* Make a new dfa. */
862 if (++lastdfa >= current_max_dfas)
863 increase_max_dfas ();
865 newds = lastdfa;
867 dss[newds] = allocate_integer_array (numstates + 1);
869 /* If we haven't already sorted the states in sns, we do so now,
870 * so that future comparisons with it can be made quickly.
873 if (!didsort)
874 qsort (&sns [1], numstates, sizeof (sns [1]), intcmp);
876 for (i = 1; i <= numstates; ++i)
877 dss[newds][i] = sns[i];
879 dfasiz[newds] = numstates;
880 dhash[newds] = hashval;
882 if (nacc == 0) {
883 if (reject)
884 dfaacc[newds].dfaacc_set = (int *) 0;
885 else
886 dfaacc[newds].dfaacc_state = 0;
888 accsiz[newds] = 0;
891 else if (reject) {
892 /* We sort the accepting set in increasing order so the
893 * disambiguating rule that the first rule listed is considered
894 * match in the event of ties will work.
897 qsort (&accset [1], nacc, sizeof (accset [1]), intcmp);
899 dfaacc[newds].dfaacc_set =
900 allocate_integer_array (nacc + 1);
902 /* Save the accepting set for later */
903 for (i = 1; i <= nacc; ++i) {
904 dfaacc[newds].dfaacc_set[i] = accset[i];
906 if (accset[i] <= num_rules)
907 /* Who knows, perhaps a REJECT can yield
908 * this rule.
910 rule_useful[accset[i]] = true;
913 accsiz[newds] = nacc;
916 else {
917 /* Find lowest numbered rule so the disambiguating rule
918 * will work.
920 j = num_rules + 1;
922 for (i = 1; i <= nacc; ++i)
923 if (accset[i] < j)
924 j = accset[i];
926 dfaacc[newds].dfaacc_state = j;
928 if (j <= num_rules)
929 rule_useful[j] = true;
932 *newds_addr = newds;
934 return 1;
938 /* symfollowset - follow the symbol transitions one step
940 * synopsis
941 * numstates = symfollowset( int ds[current_max_dfa_size], int dsize,
942 * int transsym, int nset[current_max_dfa_size] );
945 int symfollowset (ds, dsize, transsym, nset)
946 int ds[], dsize, transsym, nset[];
948 int ns, tsp, sym, i, j, lenccl, ch, numstates, ccllist;
950 numstates = 0;
952 for (i = 1; i <= dsize; ++i) { /* for each nfa state ns in the state set of ds */
953 ns = ds[i];
954 sym = transchar[ns];
955 tsp = trans1[ns];
957 if (sym < 0) { /* it's a character class */
958 sym = -sym;
959 ccllist = cclmap[sym];
960 lenccl = ccllen[sym];
962 if (cclng[sym]) {
963 for (j = 0; j < lenccl; ++j) {
964 /* Loop through negated character
965 * class.
967 ch = ccltbl[ccllist + j];
969 if (ch == 0)
970 ch = NUL_ec;
972 if (ch > transsym)
973 /* Transsym isn't in negated
974 * ccl.
976 break;
978 else if (ch == transsym)
979 /* next 2 */
980 goto bottom;
983 /* Didn't find transsym in ccl. */
984 nset[++numstates] = tsp;
987 else
988 for (j = 0; j < lenccl; ++j) {
989 ch = ccltbl[ccllist + j];
991 if (ch == 0)
992 ch = NUL_ec;
994 if (ch > transsym)
995 break;
996 else if (ch == transsym) {
997 nset[++numstates] = tsp;
998 break;
1003 else if (sym == SYM_EPSILON) { /* do nothing */
1006 else if (ABS (ecgroup[sym]) == transsym)
1007 nset[++numstates] = tsp;
1009 bottom:;
1012 return numstates;
1016 /* sympartition - partition characters with same out-transitions
1018 * synopsis
1019 * sympartition( int ds[current_max_dfa_size], int numstates,
1020 * int symlist[numecs], int duplist[numecs] );
1023 void sympartition (ds, numstates, symlist, duplist)
1024 int ds[], numstates;
1025 int symlist[], duplist[];
1027 int tch, i, j, k, ns, dupfwd[CSIZE + 1], lenccl, cclp, ich;
1029 /* Partitioning is done by creating equivalence classes for those
1030 * characters which have out-transitions from the given state. Thus
1031 * we are really creating equivalence classes of equivalence classes.
1034 for (i = 1; i <= numecs; ++i) { /* initialize equivalence class list */
1035 duplist[i] = i - 1;
1036 dupfwd[i] = i + 1;
1039 duplist[1] = NIL;
1040 dupfwd[numecs] = NIL;
1042 for (i = 1; i <= numstates; ++i) {
1043 ns = ds[i];
1044 tch = transchar[ns];
1046 if (tch != SYM_EPSILON) {
1047 if (tch < -lastccl || tch >= csize) {
1048 flexfatal (_
1049 ("bad transition character detected in sympartition()"));
1052 if (tch >= 0) { /* character transition */
1053 int ec = ecgroup[tch];
1055 mkechar (ec, dupfwd, duplist);
1056 symlist[ec] = 1;
1059 else { /* character class */
1060 tch = -tch;
1062 lenccl = ccllen[tch];
1063 cclp = cclmap[tch];
1064 mkeccl (ccltbl + cclp, lenccl, dupfwd,
1065 duplist, numecs, NUL_ec);
1067 if (cclng[tch]) {
1068 j = 0;
1070 for (k = 0; k < lenccl; ++k) {
1071 ich = ccltbl[cclp + k];
1073 if (ich == 0)
1074 ich = NUL_ec;
1076 for (++j; j < ich; ++j)
1077 symlist[j] = 1;
1080 for (++j; j <= numecs; ++j)
1081 symlist[j] = 1;
1084 else
1085 for (k = 0; k < lenccl; ++k) {
1086 ich = ccltbl[cclp + k];
1088 if (ich == 0)
1089 ich = NUL_ec;
1091 symlist[ich] = 1;