source: vendor/flex/2.5.33/dfa.c@ 3682

Last change on this file since 3682 was 3031, checked in by bird, 18 years ago

flex 2.5.33.

File size: 27.1 KB
Line 
1/* dfa - DFA construction 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 the following conditions */
15/* are met: */
16
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. */
22
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. */
26
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. */
31
32#include "flexdef.h"
33#include "tables.h"
34
35/* declare functions that have forward references */
36
37void dump_associated_rules PROTO ((FILE *, int));
38void dump_transitions PROTO ((FILE *, int[]));
39void sympartition PROTO ((int[], int, int[], int[]));
40int symfollowset PROTO ((int[], int, int, int[]));
41
42
43/* check_for_backing_up - check a DFA state for backing up
44 *
45 * synopsis
46 * void check_for_backing_up( int ds, int state[numecs] );
47 *
48 * ds is the number of the state to check and state[] is its out-transitions,
49 * indexed by equivalence class.
50 */
51
52void check_for_backing_up (ds, state)
53 int ds;
54 int state[];
55{
56 if ((reject && !dfaacc[ds].dfaacc_set) || (!reject && !dfaacc[ds].dfaacc_state)) { /* state is non-accepting */
57 ++num_backing_up;
58
59 if (backing_up_report) {
60 fprintf (backing_up_file,
61 _("State #%d is non-accepting -\n"), ds);
62
63 /* identify the state */
64 dump_associated_rules (backing_up_file, ds);
65
66 /* Now identify it further using the out- and
67 * jam-transitions.
68 */
69 dump_transitions (backing_up_file, state);
70
71 putc ('\n', backing_up_file);
72 }
73 }
74}
75
76
77/* check_trailing_context - check to see if NFA state set constitutes
78 * "dangerous" trailing context
79 *
80 * synopsis
81 * void check_trailing_context( int nfa_states[num_states+1], int num_states,
82 * int accset[nacc+1], int nacc );
83 *
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.
89 *
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.
94 *
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.
97 */
98
99void check_trailing_context (nfa_states, num_states, accset, nacc)
100 int *nfa_states, num_states;
101 int *accset;
102 int nacc;
103{
104 register int i, j;
105
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];
110
111 if (type == STATE_NORMAL || rule_type[ar] != RULE_VARIABLE) { /* do nothing */
112 }
113
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.
120 */
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;
127 }
128 }
129 }
130}
131
132
133/* dump_associated_rules - list the rules associated with a DFA state
134 *
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.
138 */
139
140void dump_associated_rules (file, ds)
141 FILE *file;
142 int ds;
143{
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];
149
150 for (i = 1; i <= size; ++i) {
151 register int rule_num = rule_linenum[assoc_rule[dset[i]]];
152
153 for (j = 1; j <= num_associated_rules; ++j)
154 if (rule_num == rule_set[j])
155 break;
156
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;
161 }
162 }
163
164 bubble (rule_set, num_associated_rules);
165
166 fprintf (file, _(" associated rule line numbers:"));
167
168 for (i = 1; i <= num_associated_rules; ++i) {
169 if (i % 8 == 1)
170 putc ('\n', file);
171
172 fprintf (file, "\t%d", rule_set[i]);
173 }
174
175 putc ('\n', file);
176}
177
178
179/* dump_transitions - list the transitions associated with a DFA state
180 *
181 * synopsis
182 * dump_transitions( FILE *file, int state[numecs] );
183 *
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.
188 */
189
190void dump_transitions (file, state)
191 FILE *file;
192 int state[];
193{
194 register int i, ec;
195 int out_char_set[CSIZE];
196
197 for (i = 0; i < csize; ++i) {
198 ec = ABS (ecgroup[i]);
199 out_char_set[i] = state[ec];
200 }
201
202 fprintf (file, _(" out-transitions: "));
203
204 list_character_set (file, out_char_set);
205
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];
209
210 fprintf (file, _("\n jam-transitions: EOF "));
211
212 list_character_set (file, out_char_set);
213
214 putc ('\n', file);
215}
216
217
218/* epsclosure - construct the epsilon closure of a set of ndfa states
219 *
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 );
224 *
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.
234 *
235 * hashval is the hash value for the dfa corresponding to the state set.
236 */
237
238int *epsclosure (t, ns_addr, accset, nacc_addr, hv_addr)
239 int *t, *ns_addr, accset[], *nacc_addr, *hv_addr;
240{
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;
245
246#define MARK_STATE(state) \
247do{ trans1[state] = trans1[state] - MARKER_DIFFERENCE;} while(0)
248
249#define IS_MARKED(state) (trans1[state] < 0)
250
251#define UNMARK_STATE(state) \
252do{ trans1[state] = trans1[state] + MARKER_DIFFERENCE;} while(0)
253
254#define CHECK_ACCEPT(state) \
255do{ \
256nfaccnum = accptnum[state]; \
257if ( nfaccnum != NIL ) \
258accset[++nacc] = nfaccnum; \
259}while(0)
260
261#define DO_REALLOCATION() \
262do { \
263current_max_dfa_size += MAX_DFA_SIZE_INCREMENT; \
264++num_reallocs; \
265t = reallocate_integer_array( t, current_max_dfa_size ); \
266stk = reallocate_integer_array( stk, current_max_dfa_size ); \
267}while(0) \
268
269#define PUT_ON_STACK(state) \
270do { \
271if ( ++stkend >= current_max_dfa_size ) \
272DO_REALLOCATION(); \
273stk[stkend] = state; \
274MARK_STATE(state); \
275}while(0)
276
277#define ADD_STATE(state) \
278do { \
279if ( ++numstates >= current_max_dfa_size ) \
280DO_REALLOCATION(); \
281t[numstates] = state; \
282hashval += state; \
283}while(0)
284
285#define STACK_STATE(state) \
286do { \
287PUT_ON_STACK(state); \
288CHECK_ACCEPT(state); \
289if ( nfaccnum != NIL || transchar[state] != SYM_EPSILON ) \
290ADD_STATE(state); \
291}while(0)
292
293
294 if (!did_stk_init) {
295 stk = allocate_integer_array (current_max_dfa_size);
296 did_stk_init = true;
297 }
298
299 nacc = stkend = hashval = 0;
300
301 for (nstate = 1; nstate <= numstates; ++nstate) {
302 ns = t[nstate];
303
304 /* The state could be marked if we've already pushed it onto
305 * the stack.
306 */
307 if (!IS_MARKED (ns)) {
308 PUT_ON_STACK (ns);
309 CHECK_ACCEPT (ns);
310 hashval += ns;
311 }
312 }
313
314 for (stkpos = 1; stkpos <= stkend; ++stkpos) {
315 ns = stk[stkpos];
316 transsym = transchar[ns];
317
318 if (transsym == SYM_EPSILON) {
319 tsp = trans1[ns] + MARKER_DIFFERENCE;
320
321 if (tsp != NO_TRANSITION) {
322 if (!IS_MARKED (tsp))
323 STACK_STATE (tsp);
324
325 tsp = trans2[ns];
326
327 if (tsp != NO_TRANSITION
328 && !IS_MARKED (tsp))
329 STACK_STATE (tsp);
330 }
331 }
332 }
333
334 /* Clear out "visit" markers. */
335
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()"));
342 }
343
344 *ns_addr = numstates;
345 *hv_addr = hashval;
346 *nacc_addr = nacc;
347
348 return t;
349}
350
351
352/* increase_max_dfas - increase the maximum number of DFAs */
353
354void increase_max_dfas ()
355{
356 current_max_dfas += MAX_DFAS_INCREMENT;
357
358 ++num_reallocs;
359
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);
367
368 if (nultrans)
369 nultrans =
370 reallocate_integer_array (nultrans,
371 current_max_dfas);
372}
373
374
375/* ntod - convert an ndfa to a dfa
376 *
377 * Creates the dfa corresponding to the ndfa we've constructed. The
378 * dfa starts out in state #1.
379 */
380
381void ntod ()
382{
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;
391
392 struct yytbl_data *yynxt_tbl = 0;
393 flex_int32_t *yynxt_data = 0, yynxt_curr = 0;
394
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.
401 */
402 int duplist[CSIZE + 1], state[CSIZE + 1];
403 int targfreq[CSIZE + 1], targstate[CSIZE + 1];
404
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.
407 */
408 accset = allocate_integer_array ((num_rules + 1) * 2);
409 nset = allocate_integer_array (current_max_dfa_size);
410
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.
416 */
417 todo_head = todo_next = 0;
418
419 for (i = 0; i <= csize; ++i) {
420 duplist[i] = NIL;
421 symlist[i] = false;
422 }
423
424 for (i = 0; i <= num_rules; ++i)
425 accset[i] = NIL;
426
427 if (trace) {
428 dumpnfa (scset[1]);
429 fputs (_("\n\nDFA Dump:\n\n"), stderr);
430 }
431
432 inittbl ();
433
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 * ###).
454 *
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).
461 */
462
463 /* Note that the test for ecgroup[0] == numecs below accomplishes
464 * both (1) and (2) above
465 */
466 if (!fullspd && ecgroup[0] == numecs) {
467 /* NUL is alone in its equivalence class, which is the
468 * last one.
469 */
470 int use_NUL_table = (numecs == csize);
471
472 if (fulltbl && !use_NUL_table) {
473 /* We still may want to use the table if numecs
474 * is a power of 2.
475 */
476 int power_of_two;
477
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;
483 }
484 }
485
486 if (use_NUL_table)
487 nultrans =
488 allocate_integer_array (current_max_dfas);
489
490 /* From now on, nultrans != nil indicates that we're
491 * saving null transitions for later, separate encoding.
492 */
493 }
494
495
496 if (fullspd) {
497 for (i = 0; i <= numecs; ++i)
498 state[i] = 0;
499
500 place_state (state, 0, 0);
501 dfaacc[0].dfaacc_state = 0;
502 }
503
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.
508 */
509 num_full_table_rows = numecs;
510
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.
515 */
516 num_full_table_rows = numecs + 1;
517
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.
523 */
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;
536
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");
540
541 /* Unless -Ca, declare it "short" because it's a real
542 * long-shot that that won't be large enough.
543 */
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");
553 }
554
555
556 if (gentables)
557 outn (" {");
558
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;
563 }
564
565 dataflush ();
566 if (gentables)
567 outn (" },\n");
568 }
569
570 /* Create the first states. */
571
572 num_start_states = lastsc * 2;
573
574 for (i = 1; i <= num_start_states; ++i) {
575 numstates = 1;
576
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.
580 */
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]);
586
587 nset = epsclosure (nset, &numstates, accset, &nacc,
588 &hashval);
589
590 if (snstods (nset, numstates, accset, nacc, hashval, &ds)) {
591 numas += nacc;
592 totnst += numstates;
593 ++todo_next;
594
595 if (variable_trailing_context_rules && nacc > 0)
596 check_trailing_context (nset, numstates,
597 accset, nacc);
598 }
599 }
600
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"));
605
606 ++numas;
607 ++num_start_states;
608 ++todo_next;
609 }
610
611
612 while (todo_head < todo_next) {
613 targptr = 0;
614 totaltrans = 0;
615
616 for (i = 1; i <= numecs; ++i)
617 state[i] = 0;
618
619 ds = ++todo_head;
620
621 dset = dss[ds];
622 dsize = dfasiz[ds];
623
624 if (trace)
625 fprintf (stderr, _("state # %d:\n"), ds);
626
627 sympartition (dset, dsize, symlist, duplist);
628
629 for (sym = 1; sym <= numecs; ++sym) {
630 if (symlist[sym]) {
631 symlist[sym] = 0;
632
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);
642
643 if (snstods
644 (nset, numstates, accset, nacc,
645 hashval, &newds)) {
646 totnst = totnst +
647 numstates;
648 ++todo_next;
649 numas += nacc;
650
651 if (variable_trailing_context_rules && nacc > 0)
652 check_trailing_context
653 (nset,
654 numstates,
655 accset,
656 nacc);
657 }
658
659 state[sym] = newds;
660
661 if (trace)
662 fprintf (stderr,
663 "\t%d\t%d\n", sym,
664 newds);
665
666 targfreq[++targptr] = 1;
667 targstate[targptr] = newds;
668 ++numuniq;
669 }
670
671 else {
672 /* sym's equivalence class has the same
673 * transitions as duplist(sym)'s
674 * equivalence class.
675 */
676 targ = state[duplist[sym]];
677 state[sym] = targ;
678
679 if (trace)
680 fprintf (stderr,
681 "\t%d\t%d\n", sym,
682 targ);
683
684 /* Update frequency count for
685 * destination state.
686 */
687
688 i = 0;
689 while (targstate[++i] != targ) ;
690
691 ++targfreq[i];
692 ++numdup;
693 }
694
695 ++totaltrans;
696 duplist[sym] = NIL;
697 }
698 }
699
700 if (caseins && !useecs) {
701 register int j;
702
703 for (i = 'A', j = 'a'; i <= 'Z'; ++i, ++j) {
704 if (state[i] == 0 && state[j] != 0)
705 /* We're adding a transition. */
706 ++totaltrans;
707
708 else if (state[i] != 0 && state[j] == 0)
709 /* We're taking away a transition. */
710 --totaltrans;
711
712 state[i] = state[j];
713 }
714 }
715
716 numsnpairs += totaltrans;
717
718 if (ds > num_start_states)
719 check_for_backing_up (ds, state);
720
721 if (nultrans) {
722 nultrans[ds] = state[NUL_ec];
723 state[NUL_ec] = 0; /* remove transition */
724 }
725
726 if (fulltbl) {
727
728 /* Each time we hit here, it's another td_hilen, so we realloc. */
729 yynxt_tbl->td_hilen++;
730 yynxt_tbl->td_data = yynxt_data =
731 (flex_int32_t *) realloc (yynxt_data,
732 yynxt_tbl->td_hilen *
733 yynxt_tbl->td_lolen *
734 sizeof (flex_int32_t));
735
736
737 if (gentables)
738 outn (" {");
739
740 /* Supply array's 0-element. */
741 if (ds == end_of_buffer_state) {
742 mk2data (-end_of_buffer_state);
743 yynxt_data[yynxt_curr++] =
744 -end_of_buffer_state;
745 }
746 else {
747 mk2data (end_of_buffer_state);
748 yynxt_data[yynxt_curr++] =
749 end_of_buffer_state;
750 }
751
752 for (i = 1; i < num_full_table_rows; ++i) {
753 /* Jams are marked by negative of state
754 * number.
755 */
756 mk2data (state[i] ? state[i] : -ds);
757 yynxt_data[yynxt_curr++] =
758 state[i] ? state[i] : -ds;
759 }
760
761 dataflush ();
762 if (gentables)
763 outn (" },\n");
764 }
765
766 else if (fullspd)
767 place_state (state, ds, totaltrans);
768
769 else if (ds == end_of_buffer_state)
770 /* Special case this state to make sure it does what
771 * it's supposed to, i.e., jam on end-of-buffer.
772 */
773 stack1 (ds, 0, 0, JAMSTATE);
774
775 else { /* normal, compressed state */
776
777 /* Determine which destination state is the most
778 * common, and how many transitions to it there are.
779 */
780
781 comfreq = 0;
782 comstate = 0;
783
784 for (i = 1; i <= targptr; ++i)
785 if (targfreq[i] > comfreq) {
786 comfreq = targfreq[i];
787 comstate = targstate[i];
788 }
789
790 bldtbl (state, ds, totaltrans, comstate, comfreq);
791 }
792 }
793
794 if (fulltbl) {
795 dataend ();
796 if (tablesext) {
797 yytbl_data_compress (yynxt_tbl);
798 if (yytbl_data_fwrite (&tableswr, yynxt_tbl) < 0)
799 flexerror (_
800 ("Could not write yynxt_tbl[][]"));
801 }
802 if (yynxt_tbl) {
803 yytbl_data_destroy (yynxt_tbl);
804 yynxt_tbl = 0;
805 }
806 }
807
808 else if (!fullspd) {
809 cmptmps (); /* create compressed template entries */
810
811 /* Create tables for all the states with only one
812 * out-transition.
813 */
814 while (onesp > 0) {
815 mk1tbl (onestate[onesp], onesym[onesp],
816 onenext[onesp], onedef[onesp]);
817 --onesp;
818 }
819
820 mkdeftbl ();
821 }
822
823 flex_free ((void *) accset);
824 flex_free ((void *) nset);
825}
826
827
828/* snstods - converts a set of ndfa states into a dfa state
829 *
830 * synopsis
831 * is_new_state = snstods( int sns[numstates], int numstates,
832 * int accset[num_rules+1], int nacc,
833 * int hashval, int *newds_addr );
834 *
835 * On return, the dfa state number is in newds.
836 */
837
838int snstods (sns, numstates, accset, nacc, hashval, newds_addr)
839 int sns[], numstates, accset[], nacc, hashval, *newds_addr;
840{
841 int didsort = 0;
842 register int i, j;
843 int newds, *oldsns;
844
845 for (i = 1; i <= lastdfa; ++i)
846 if (hashval == dhash[i]) {
847 if (numstates == dfasiz[i]) {
848 oldsns = dss[i];
849
850 if (!didsort) {
851 /* We sort the states in sns so we
852 * can compare it to oldsns quickly.
853 * We use bubble because there probably
854 * aren't very many states.
855 */
856 bubble (sns, numstates);
857 didsort = 1;
858 }
859
860 for (j = 1; j <= numstates; ++j)
861 if (sns[j] != oldsns[j])
862 break;
863
864 if (j > numstates) {
865 ++dfaeql;
866 *newds_addr = i;
867 return 0;
868 }
869
870 ++hshcol;
871 }
872
873 else
874 ++hshsave;
875 }
876
877 /* Make a new dfa. */
878
879 if (++lastdfa >= current_max_dfas)
880 increase_max_dfas ();
881
882 newds = lastdfa;
883
884 dss[newds] = allocate_integer_array (numstates + 1);
885
886 /* If we haven't already sorted the states in sns, we do so now,
887 * so that future comparisons with it can be made quickly.
888 */
889
890 if (!didsort)
891 bubble (sns, numstates);
892
893 for (i = 1; i <= numstates; ++i)
894 dss[newds][i] = sns[i];
895
896 dfasiz[newds] = numstates;
897 dhash[newds] = hashval;
898
899 if (nacc == 0) {
900 if (reject)
901 dfaacc[newds].dfaacc_set = (int *) 0;
902 else
903 dfaacc[newds].dfaacc_state = 0;
904
905 accsiz[newds] = 0;
906 }
907
908 else if (reject) {
909 /* We sort the accepting set in increasing order so the
910 * disambiguating rule that the first rule listed is considered
911 * match in the event of ties will work. We use a bubble
912 * sort since the list is probably quite small.
913 */
914
915 bubble (accset, nacc);
916
917 dfaacc[newds].dfaacc_set =
918 allocate_integer_array (nacc + 1);
919
920 /* Save the accepting set for later */
921 for (i = 1; i <= nacc; ++i) {
922 dfaacc[newds].dfaacc_set[i] = accset[i];
923
924 if (accset[i] <= num_rules)
925 /* Who knows, perhaps a REJECT can yield
926 * this rule.
927 */
928 rule_useful[accset[i]] = true;
929 }
930
931 accsiz[newds] = nacc;
932 }
933
934 else {
935 /* Find lowest numbered rule so the disambiguating rule
936 * will work.
937 */
938 j = num_rules + 1;
939
940 for (i = 1; i <= nacc; ++i)
941 if (accset[i] < j)
942 j = accset[i];
943
944 dfaacc[newds].dfaacc_state = j;
945
946 if (j <= num_rules)
947 rule_useful[j] = true;
948 }
949
950 *newds_addr = newds;
951
952 return 1;
953}
954
955
956/* symfollowset - follow the symbol transitions one step
957 *
958 * synopsis
959 * numstates = symfollowset( int ds[current_max_dfa_size], int dsize,
960 * int transsym, int nset[current_max_dfa_size] );
961 */
962
963int symfollowset (ds, dsize, transsym, nset)
964 int ds[], dsize, transsym, nset[];
965{
966 int ns, tsp, sym, i, j, lenccl, ch, numstates, ccllist;
967
968 numstates = 0;
969
970 for (i = 1; i <= dsize; ++i) { /* for each nfa state ns in the state set of ds */
971 ns = ds[i];
972 sym = transchar[ns];
973 tsp = trans1[ns];
974
975 if (sym < 0) { /* it's a character class */
976 sym = -sym;
977 ccllist = cclmap[sym];
978 lenccl = ccllen[sym];
979
980 if (cclng[sym]) {
981 for (j = 0; j < lenccl; ++j) {
982 /* Loop through negated character
983 * class.
984 */
985 ch = ccltbl[ccllist + j];
986
987 if (ch == 0)
988 ch = NUL_ec;
989
990 if (ch > transsym)
991 /* Transsym isn't in negated
992 * ccl.
993 */
994 break;
995
996 else if (ch == transsym)
997 /* next 2 */
998 goto bottom;
999 }
1000
1001 /* Didn't find transsym in ccl. */
1002 nset[++numstates] = tsp;
1003 }
1004
1005 else
1006 for (j = 0; j < lenccl; ++j) {
1007 ch = ccltbl[ccllist + j];
1008
1009 if (ch == 0)
1010 ch = NUL_ec;
1011
1012 if (ch > transsym)
1013 break;
1014 else if (ch == transsym) {
1015 nset[++numstates] = tsp;
1016 break;
1017 }
1018 }
1019 }
1020
1021 else if (sym >= 'A' && sym <= 'Z' && caseins)
1022 flexfatal (_
1023 ("consistency check failed in symfollowset"));
1024
1025 else if (sym == SYM_EPSILON) { /* do nothing */
1026 }
1027
1028 else if (ABS (ecgroup[sym]) == transsym)
1029 nset[++numstates] = tsp;
1030
1031 bottom:;
1032 }
1033
1034 return numstates;
1035}
1036
1037
1038/* sympartition - partition characters with same out-transitions
1039 *
1040 * synopsis
1041 * sympartition( int ds[current_max_dfa_size], int numstates,
1042 * int symlist[numecs], int duplist[numecs] );
1043 */
1044
1045void sympartition (ds, numstates, symlist, duplist)
1046 int ds[], numstates;
1047 int symlist[], duplist[];
1048{
1049 int tch, i, j, k, ns, dupfwd[CSIZE + 1], lenccl, cclp, ich;
1050
1051 /* Partitioning is done by creating equivalence classes for those
1052 * characters which have out-transitions from the given state. Thus
1053 * we are really creating equivalence classes of equivalence classes.
1054 */
1055
1056 for (i = 1; i <= numecs; ++i) { /* initialize equivalence class list */
1057 duplist[i] = i - 1;
1058 dupfwd[i] = i + 1;
1059 }
1060
1061 duplist[1] = NIL;
1062 dupfwd[numecs] = NIL;
1063
1064 for (i = 1; i <= numstates; ++i) {
1065 ns = ds[i];
1066 tch = transchar[ns];
1067
1068 if (tch != SYM_EPSILON) {
1069 if (tch < -lastccl || tch >= csize) {
1070 flexfatal (_
1071 ("bad transition character detected in sympartition()"));
1072 }
1073
1074 if (tch >= 0) { /* character transition */
1075 int ec = ecgroup[tch];
1076
1077 mkechar (ec, dupfwd, duplist);
1078 symlist[ec] = 1;
1079 }
1080
1081 else { /* character class */
1082 tch = -tch;
1083
1084 lenccl = ccllen[tch];
1085 cclp = cclmap[tch];
1086 mkeccl (ccltbl + cclp, lenccl, dupfwd,
1087 duplist, numecs, NUL_ec);
1088
1089 if (cclng[tch]) {
1090 j = 0;
1091
1092 for (k = 0; k < lenccl; ++k) {
1093 ich = ccltbl[cclp + k];
1094
1095 if (ich == 0)
1096 ich = NUL_ec;
1097
1098 for (++j; j < ich; ++j)
1099 symlist[j] = 1;
1100 }
1101
1102 for (++j; j <= numecs; ++j)
1103 symlist[j] = 1;
1104 }
1105
1106 else
1107 for (k = 0; k < lenccl; ++k) {
1108 ich = ccltbl[cclp + k];
1109
1110 if (ich == 0)
1111 ich = NUL_ec;
1112
1113 symlist[ich] = 1;
1114 }
1115 }
1116 }
1117 }
1118}
Note: See TracBrowser for help on using the repository browser.