xref: /openbsd/usr.bin/lex/dfa.c (revision 20c29e2b)
1 /*	$OpenBSD: dfa.c,v 1.9 2024/11/09 18:03:44 op Exp $	*/
2 
3 /* dfa - DFA construction routines */
4 
5 /*  Copyright (c) 1990 The Regents of the University of California. */
6 /*  All rights reserved. */
7 
8 /*  This code is derived from software contributed to Berkeley by */
9 /*  Vern Paxson. */
10 
11 /*  The United States Government has rights in this work pursuant */
12 /*  to contract no. DE-AC03-76SF00098 between the United States */
13 /*  Department of Energy and the University of California. */
14 
15 /*  Redistribution and use in source and binary forms, with or without */
16 /*  modification, are permitted provided that the following conditions */
17 /*  are met: */
18 
19 /*  1. Redistributions of source code must retain the above copyright */
20 /*     notice, this list of conditions and the following disclaimer. */
21 /*  2. Redistributions in binary form must reproduce the above copyright */
22 /*     notice, this list of conditions and the following disclaimer in the */
23 /*     documentation and/or other materials provided with the distribution. */
24 
25 /*  Neither the name of the University nor the names of its contributors */
26 /*  may be used to endorse or promote products derived from this software */
27 /*  without specific prior written permission. */
28 
29 /*  THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */
30 /*  IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */
31 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
32 /*  PURPOSE. */
33 
34 #include "flexdef.h"
35 #include "tables.h"
36 
37 /* declare functions that have forward references */
38 
39 void dump_associated_rules PROTO ((FILE *, int));
40 void dump_transitions PROTO ((FILE *, int[]));
41 void sympartition PROTO ((int[], int, int[], int[]));
42 int symfollowset PROTO ((int[], int, int, int[]));
43 
44 
45 /* check_for_backing_up - check a DFA state for backing up
46  *
47  * synopsis
48  *     void check_for_backing_up( int ds, int state[numecs] );
49  *
50  * ds is the number of the state to check and state[] is its out-transitions,
51  * indexed by equivalence class.
52  */
53 
check_for_backing_up(int ds,int state[])54 void check_for_backing_up (int ds, 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 
check_trailing_context(int * nfa_states,int num_states,int * accset,int nacc)99 void check_trailing_context (int *nfa_states, int num_states,
100     int *accset, int nacc)
101 {
102 	int i, j;
103 
104 	for (i = 1; i <= num_states; ++i) {
105 		int     ns = nfa_states[i];
106 		int type = state_type[ns];
107 		int ar = assoc_rule[ns];
108 
109 		if (type == STATE_NORMAL || rule_type[ar] != RULE_VARIABLE) {	/* do nothing */
110 		}
111 
112 		else if (type == STATE_TRAILING_CONTEXT) {
113 			/* Potential trouble.  Scan set of accepting numbers
114 			 * for the one marking the end of the "head".  We
115 			 * assume that this looping will be fairly cheap
116 			 * since it's rare that an accepting number set
117 			 * is large.
118 			 */
119 			for (j = 1; j <= nacc; ++j)
120 				if (accset[j] & YY_TRAILING_HEAD_MASK) {
121 					line_warning (_
122 						      ("dangerous trailing context"),
123 						      rule_linenum[ar]);
124 					return;
125 				}
126 		}
127 	}
128 }
129 
130 
131 /* dump_associated_rules - list the rules associated with a DFA state
132  *
133  * Goes through the set of NFA states associated with the DFA and
134  * extracts the first MAX_ASSOC_RULES unique rules, sorts them,
135  * and writes a report to the given file.
136  */
137 
dump_associated_rules(FILE * file,int ds)138 void dump_associated_rules (FILE *file, int ds)
139 {
140 	int i, j;
141 	int num_associated_rules = 0;
142 	int     rule_set[MAX_ASSOC_RULES + 1];
143 	int    *dset = dss[ds];
144 	int     size = dfasiz[ds];
145 
146 	for (i = 1; i <= size; ++i) {
147 		int rule_num = rule_linenum[assoc_rule[dset[i]]];
148 
149 		for (j = 1; j <= num_associated_rules; ++j)
150 			if (rule_num == rule_set[j])
151 				break;
152 
153 		if (j > num_associated_rules) {	/* new rule */
154 			if (num_associated_rules < MAX_ASSOC_RULES)
155 				rule_set[++num_associated_rules] =
156 					rule_num;
157 		}
158 	}
159 
160 	qsort (&rule_set [1], num_associated_rules, sizeof (rule_set [1]), intcmp);
161 
162 	fprintf (file, _(" associated rule line numbers:"));
163 
164 	for (i = 1; i <= num_associated_rules; ++i) {
165 		if (i % 8 == 1)
166 			putc ('\n', file);
167 
168 		fprintf (file, "\t%d", rule_set[i]);
169 	}
170 
171 	putc ('\n', file);
172 }
173 
174 
175 /* dump_transitions - list the transitions associated with a DFA state
176  *
177  * synopsis
178  *     dump_transitions( FILE *file, int state[numecs] );
179  *
180  * Goes through the set of out-transitions and lists them in human-readable
181  * form (i.e., not as equivalence classes); also lists jam transitions
182  * (i.e., all those which are not out-transitions, plus EOF).  The dump
183  * is done to the given file.
184  */
185 
dump_transitions(FILE * file,int state[])186 void dump_transitions (FILE *file, int state[])
187 {
188 	int i, ec;
189 	int     out_char_set[CSIZE];
190 
191 	for (i = 0; i < csize; ++i) {
192 		ec = ABS (ecgroup[i]);
193 		out_char_set[i] = state[ec];
194 	}
195 
196 	fprintf (file, _(" out-transitions: "));
197 
198 	list_character_set (file, out_char_set);
199 
200 	/* now invert the members of the set to get the jam transitions */
201 	for (i = 0; i < csize; ++i)
202 		out_char_set[i] = !out_char_set[i];
203 
204 	fprintf (file, _("\n jam-transitions: EOF "));
205 
206 	list_character_set (file, out_char_set);
207 
208 	putc ('\n', file);
209 }
210 
211 
212 /* epsclosure - construct the epsilon closure of a set of ndfa states
213  *
214  * synopsis
215  *    int *epsclosure( int t[num_states], int *numstates_addr,
216  *			int accset[num_rules+1], int *nacc_addr,
217  *			int *hashval_addr );
218  *
219  * NOTES
220  *  The epsilon closure is the set of all states reachable by an arbitrary
221  *  number of epsilon transitions, which themselves do not have epsilon
222  *  transitions going out, unioned with the set of states which have non-null
223  *  accepting numbers.  t is an array of size numstates of nfa state numbers.
224  *  Upon return, t holds the epsilon closure and *numstates_addr is updated.
225  *  accset holds a list of the accepting numbers, and the size of accset is
226  *  given by *nacc_addr.  t may be subjected to reallocation if it is not
227  *  large enough to hold the epsilon closure.
228  *
229  *  hashval is the hash value for the dfa corresponding to the state set.
230  */
231 
epsclosure(int * t,int * ns_addr,int accset[],int * nacc_addr,int * hv_addr)232 int    *epsclosure (int *t, int *ns_addr, int accset[], int *nacc_addr,
233     int *hv_addr)
234 {
235 	int stkpos, ns, tsp;
236 	int     numstates = *ns_addr, nacc, hashval, transsym, nfaccnum;
237 	int     stkend, nstate;
238 	static int did_stk_init = false, *stk;
239 
240 #define MARK_STATE(state) \
241 do{ trans1[state] = trans1[state] - MARKER_DIFFERENCE;} while(0)
242 
243 #define IS_MARKED(state) (trans1[state] < 0)
244 
245 #define UNMARK_STATE(state) \
246 do{ trans1[state] = trans1[state] + MARKER_DIFFERENCE;} while(0)
247 
248 #define CHECK_ACCEPT(state) \
249 do{ \
250 nfaccnum = accptnum[state]; \
251 if ( nfaccnum != NIL ) \
252 accset[++nacc] = nfaccnum; \
253 }while(0)
254 
255 #define DO_REALLOCATION() \
256 do { \
257 current_max_dfa_size += MAX_DFA_SIZE_INCREMENT; \
258 ++num_reallocs; \
259 t = reallocate_integer_array( t, current_max_dfa_size ); \
260 stk = reallocate_integer_array( stk, current_max_dfa_size ); \
261 }while(0) \
262 
263 #define PUT_ON_STACK(state) \
264 do { \
265 if ( ++stkend >= current_max_dfa_size ) \
266 DO_REALLOCATION(); \
267 stk[stkend] = state; \
268 MARK_STATE(state); \
269 }while(0)
270 
271 #define ADD_STATE(state) \
272 do { \
273 if ( ++numstates >= current_max_dfa_size ) \
274 DO_REALLOCATION(); \
275 t[numstates] = state; \
276 hashval += state; \
277 }while(0)
278 
279 #define STACK_STATE(state) \
280 do { \
281 PUT_ON_STACK(state); \
282 CHECK_ACCEPT(state); \
283 if ( nfaccnum != NIL || transchar[state] != SYM_EPSILON ) \
284 ADD_STATE(state); \
285 }while(0)
286 
287 
288 	if (!did_stk_init) {
289 		stk = allocate_integer_array (current_max_dfa_size);
290 		did_stk_init = true;
291 	}
292 
293 	nacc = stkend = hashval = 0;
294 
295 	for (nstate = 1; nstate <= numstates; ++nstate) {
296 		ns = t[nstate];
297 
298 		/* The state could be marked if we've already pushed it onto
299 		 * the stack.
300 		 */
301 		if (!IS_MARKED (ns)) {
302 			PUT_ON_STACK (ns);
303 			CHECK_ACCEPT (ns);
304 			hashval += ns;
305 		}
306 	}
307 
308 	for (stkpos = 1; stkpos <= stkend; ++stkpos) {
309 		ns = stk[stkpos];
310 		transsym = transchar[ns];
311 
312 		if (transsym == SYM_EPSILON) {
313 			tsp = trans1[ns] + MARKER_DIFFERENCE;
314 
315 			if (tsp != NO_TRANSITION) {
316 				if (!IS_MARKED (tsp))
317 					STACK_STATE (tsp);
318 
319 				tsp = trans2[ns];
320 
321 				if (tsp != NO_TRANSITION
322 				    && !IS_MARKED (tsp))
323 					STACK_STATE (tsp);
324 			}
325 		}
326 	}
327 
328 	/* Clear out "visit" markers. */
329 
330 	for (stkpos = 1; stkpos <= stkend; ++stkpos) {
331 		if (IS_MARKED (stk[stkpos]))
332 			UNMARK_STATE (stk[stkpos]);
333 		else
334 			flexfatal (_
335 				   ("consistency check failed in epsclosure()"));
336 	}
337 
338 	*ns_addr = numstates;
339 	*hv_addr = hashval;
340 	*nacc_addr = nacc;
341 
342 	return t;
343 }
344 
345 
346 /* increase_max_dfas - increase the maximum number of DFAs */
347 
increase_max_dfas(void)348 void increase_max_dfas (void)
349 {
350 	current_max_dfas += MAX_DFAS_INCREMENT;
351 
352 	++num_reallocs;
353 
354 	base = reallocate_integer_array (base, current_max_dfas);
355 	def = reallocate_integer_array (def, current_max_dfas);
356 	dfasiz = reallocate_integer_array (dfasiz, current_max_dfas);
357 	accsiz = reallocate_integer_array (accsiz, current_max_dfas);
358 	dhash = reallocate_integer_array (dhash, current_max_dfas);
359 	dss = reallocate_int_ptr_array (dss, current_max_dfas);
360 	dfaacc = reallocate_dfaacc_union (dfaacc, current_max_dfas);
361 
362 	if (nultrans)
363 		nultrans =
364 			reallocate_integer_array (nultrans,
365 						  current_max_dfas);
366 }
367 
368 
369 /* ntod - convert an ndfa to a dfa
370  *
371  * Creates the dfa corresponding to the ndfa we've constructed.  The
372  * dfa starts out in state #1.
373  */
374 
ntod(void)375 void ntod (void)
376 {
377 	int    *accset, ds, nacc, newds;
378 	int     sym, hashval, numstates, dsize;
379 	int     num_full_table_rows=0;	/* used only for -f */
380 	int    *nset, *dset;
381 	int     targptr, totaltrans, i, comstate, comfreq, targ;
382 	int     symlist[CSIZE + 1];
383 	int     num_start_states;
384 	int     todo_head, todo_next;
385 
386 	struct yytbl_data *yynxt_tbl = 0;
387 	flex_int32_t *yynxt_data = 0, yynxt_curr = 0;
388 
389 	/* Note that the following are indexed by *equivalence classes*
390 	 * and not by characters.  Since equivalence classes are indexed
391 	 * beginning with 1, even if the scanner accepts NUL's, this
392 	 * means that (since every character is potentially in its own
393 	 * equivalence class) these arrays must have room for indices
394 	 * from 1 to CSIZE, so their size must be CSIZE + 1.
395 	 */
396 	int     duplist[CSIZE + 1], state[CSIZE + 1];
397 	int     targfreq[CSIZE + 1], targstate[CSIZE + 1];
398 
399 	/* accset needs to be large enough to hold all of the rules present
400 	 * in the input, *plus* their YY_TRAILING_HEAD_MASK variants.
401 	 */
402 	accset = allocate_integer_array ((num_rules + 1) * 2);
403 	nset = allocate_integer_array (current_max_dfa_size);
404 
405 	/* The "todo" queue is represented by the head, which is the DFA
406 	 * state currently being processed, and the "next", which is the
407 	 * next DFA state number available (not in use).  We depend on the
408 	 * fact that snstods() returns DFA's \in increasing order/, and thus
409 	 * need only know the bounds of the dfas to be processed.
410 	 */
411 	todo_head = todo_next = 0;
412 
413 	for (i = 0; i <= csize; ++i) {
414 		duplist[i] = NIL;
415 		symlist[i] = false;
416 	}
417 
418 	for (i = 0; i <= num_rules; ++i)
419 		accset[i] = NIL;
420 
421 	if (trace) {
422 		dumpnfa (scset[1]);
423 		fputs (_("\n\nDFA Dump:\n\n"), stderr);
424 	}
425 
426 	inittbl ();
427 
428 	/* Check to see whether we should build a separate table for
429 	 * transitions on NUL characters.  We don't do this for full-speed
430 	 * (-F) scanners, since for them we don't have a simple state
431 	 * number lying around with which to index the table.  We also
432 	 * don't bother doing it for scanners unless (1) NUL is in its own
433 	 * equivalence class (indicated by a positive value of
434 	 * ecgroup[NUL]), (2) NUL's equivalence class is the last
435 	 * equivalence class, and (3) the number of equivalence classes is
436 	 * the same as the number of characters.  This latter case comes
437 	 * about when useecs is false or when it's true but every character
438 	 * still manages to land in its own class (unlikely, but it's
439 	 * cheap to check for).  If all these things are true then the
440 	 * character code needed to represent NUL's equivalence class for
441 	 * indexing the tables is going to take one more bit than the
442 	 * number of characters, and therefore we won't be assured of
443 	 * being able to fit it into a YY_CHAR variable.  This rules out
444 	 * storing the transitions in a compressed table, since the code
445 	 * for interpreting them uses a YY_CHAR variable (perhaps it
446 	 * should just use an integer, though; this is worth pondering ...
447 	 * ###).
448 	 *
449 	 * Finally, for full tables, we want the number of entries in the
450 	 * table to be a power of two so the array references go fast (it
451 	 * will just take a shift to compute the major index).  If
452 	 * encoding NUL's transitions in the table will spoil this, we
453 	 * give it its own table (note that this will be the case if we're
454 	 * not using equivalence classes).
455 	 */
456 
457 	/* Note that the test for ecgroup[0] == numecs below accomplishes
458 	 * both (1) and (2) above
459 	 */
460 	if (!fullspd && ecgroup[0] == numecs) {
461 		/* NUL is alone in its equivalence class, which is the
462 		 * last one.
463 		 */
464 		int     use_NUL_table = (numecs == csize);
465 
466 		if (fulltbl && !use_NUL_table) {
467 			/* We still may want to use the table if numecs
468 			 * is a power of 2.
469 			 */
470 			int     power_of_two;
471 
472 			for (power_of_two = 1; power_of_two <= csize;
473 			     power_of_two *= 2)
474 				if (numecs == power_of_two) {
475 					use_NUL_table = true;
476 					break;
477 				}
478 		}
479 
480 		if (use_NUL_table)
481 			nultrans =
482 				allocate_integer_array (current_max_dfas);
483 
484 		/* From now on, nultrans != nil indicates that we're
485 		 * saving null transitions for later, separate encoding.
486 		 */
487 	}
488 
489 
490 	if (fullspd) {
491 		for (i = 0; i <= numecs; ++i)
492 			state[i] = 0;
493 
494 		place_state (state, 0, 0);
495 		dfaacc[0].dfaacc_state = 0;
496 	}
497 
498 	else if (fulltbl) {
499 		if (nultrans)
500 			/* We won't be including NUL's transitions in the
501 			 * table, so build it for entries from 0 .. numecs - 1.
502 			 */
503 			num_full_table_rows = numecs;
504 
505 		else
506 			/* Take into account the fact that we'll be including
507 			 * the NUL entries in the transition table.  Build it
508 			 * from 0 .. numecs.
509 			 */
510 			num_full_table_rows = numecs + 1;
511 
512 		/* Begin generating yy_nxt[][]
513 		 * This spans the entire LONG function.
514 		 * This table is tricky because we don't know how big it will be.
515 		 * So we'll have to realloc() on the way...
516 		 * we'll wait until we can calculate yynxt_tbl->td_hilen.
517 		 */
518 		yynxt_tbl =
519 			(struct yytbl_data *) calloc (1,
520 						      sizeof (struct
521 							      yytbl_data));
522 		yytbl_data_init (yynxt_tbl, YYTD_ID_NXT);
523 		yynxt_tbl->td_hilen = 1;
524 		yynxt_tbl->td_lolen = num_full_table_rows;
525 		yynxt_tbl->td_data = yynxt_data =
526 			(flex_int32_t *) calloc (yynxt_tbl->td_lolen *
527 					    yynxt_tbl->td_hilen,
528 					    sizeof (flex_int32_t));
529 		yynxt_curr = 0;
530 
531 		buf_prints (&yydmap_buf,
532 			    "\t{YYTD_ID_NXT, (void**)&yy_nxt, sizeof(%s)},\n",
533 			    long_align ? "flex_int32_t" : "flex_int16_t");
534 
535 		/* Unless -Ca, declare it "short" because it's a real
536 		 * long-shot that that won't be large enough.
537 		 */
538 		if (gentables)
539 			out_str_dec
540 				("static yyconst %s yy_nxt[][%d] =\n    {\n",
541 				 long_align ? "flex_int32_t" : "flex_int16_t",
542 				 num_full_table_rows);
543 		else {
544 			out_dec ("#undef YY_NXT_LOLEN\n#define YY_NXT_LOLEN (%d)\n", num_full_table_rows);
545 			out_str ("static yyconst %s *yy_nxt =0;\n",
546 				 long_align ? "flex_int32_t" : "flex_int16_t");
547 		}
548 
549 
550 		if (gentables)
551 			outn ("    {");
552 
553 		/* Generate 0 entries for state #0. */
554 		for (i = 0; i < num_full_table_rows; ++i) {
555 			mk2data (0);
556 			yynxt_data[yynxt_curr++] = 0;
557 		}
558 
559 		dataflush ();
560 		if (gentables)
561 			outn ("    },\n");
562 	}
563 
564 	/* Create the first states. */
565 
566 	num_start_states = lastsc * 2;
567 
568 	for (i = 1; i <= num_start_states; ++i) {
569 		numstates = 1;
570 
571 		/* For each start condition, make one state for the case when
572 		 * we're at the beginning of the line (the '^' operator) and
573 		 * one for the case when we're not.
574 		 */
575 		if (i % 2 == 1)
576 			nset[numstates] = scset[(i / 2) + 1];
577 		else
578 			nset[numstates] =
579 				mkbranch (scbol[i / 2], scset[i / 2]);
580 
581 		nset = epsclosure (nset, &numstates, accset, &nacc,
582 				   &hashval);
583 
584 		if (snstods (nset, numstates, accset, nacc, hashval, &ds)) {
585 			numas += nacc;
586 			totnst += numstates;
587 			++todo_next;
588 
589 			if (variable_trailing_context_rules && nacc > 0)
590 				check_trailing_context (nset, numstates,
591 							accset, nacc);
592 		}
593 	}
594 
595 	if (!fullspd) {
596 		if (!snstods (nset, 0, accset, 0, 0, &end_of_buffer_state))
597 			flexfatal (_
598 				   ("could not create unique end-of-buffer state"));
599 
600 		++numas;
601 		++num_start_states;
602 		++todo_next;
603 	}
604 
605 
606 	while (todo_head < todo_next) {
607 		targptr = 0;
608 		totaltrans = 0;
609 
610 		for (i = 1; i <= numecs; ++i)
611 			state[i] = 0;
612 
613 		ds = ++todo_head;
614 
615 		dset = dss[ds];
616 		dsize = dfasiz[ds];
617 
618 		if (trace)
619 			fprintf (stderr, _("state # %d:\n"), ds);
620 
621 		sympartition (dset, dsize, symlist, duplist);
622 
623 		for (sym = 1; sym <= numecs; ++sym) {
624 			if (symlist[sym]) {
625 				symlist[sym] = 0;
626 
627 				if (duplist[sym] == NIL) {
628 					/* Symbol has unique out-transitions. */
629 					numstates =
630 						symfollowset (dset, dsize,
631 							      sym, nset);
632 					nset = epsclosure (nset,
633 							   &numstates,
634 							   accset, &nacc,
635 							   &hashval);
636 
637 					if (snstods
638 					    (nset, numstates, accset, nacc,
639 					     hashval, &newds)) {
640 						totnst = totnst +
641 							numstates;
642 						++todo_next;
643 						numas += nacc;
644 
645 						if (variable_trailing_context_rules && nacc > 0)
646 							check_trailing_context
647 								(nset,
648 								 numstates,
649 								 accset,
650 								 nacc);
651 					}
652 
653 					state[sym] = newds;
654 
655 					if (trace)
656 						fprintf (stderr,
657 							 "\t%d\t%d\n", sym,
658 							 newds);
659 
660 					targfreq[++targptr] = 1;
661 					targstate[targptr] = newds;
662 					++numuniq;
663 				}
664 
665 				else {
666 					/* sym's equivalence class has the same
667 					 * transitions as duplist(sym)'s
668 					 * equivalence class.
669 					 */
670 					targ = state[duplist[sym]];
671 					state[sym] = targ;
672 
673 					if (trace)
674 						fprintf (stderr,
675 							 "\t%d\t%d\n", sym,
676 							 targ);
677 
678 					/* Update frequency count for
679 					 * destination state.
680 					 */
681 
682 					i = 0;
683 					while (targstate[++i] != targ) ;
684 
685 					++targfreq[i];
686 					++numdup;
687 				}
688 
689 				++totaltrans;
690 				duplist[sym] = NIL;
691 			}
692 		}
693 
694 
695 		numsnpairs += totaltrans;
696 
697 		if (ds > num_start_states)
698 			check_for_backing_up (ds, state);
699 
700 		if (nultrans) {
701 			nultrans[ds] = state[NUL_ec];
702 			state[NUL_ec] = 0;	/* remove transition */
703 		}
704 
705 		if (fulltbl) {
706 
707 			/* Each time we hit here, it's another td_hilen, so we realloc. */
708 			yynxt_tbl->td_hilen++;
709 			yynxt_tbl->td_data = yynxt_data =
710 				(flex_int32_t *) realloc (yynxt_data,
711 						     yynxt_tbl->td_hilen *
712 						     yynxt_tbl->td_lolen *
713 						     sizeof (flex_int32_t));
714 
715 
716 			if (gentables)
717 				outn ("    {");
718 
719 			/* Supply array's 0-element. */
720 			if (ds == end_of_buffer_state) {
721 				mk2data (-end_of_buffer_state);
722 				yynxt_data[yynxt_curr++] =
723 					-end_of_buffer_state;
724 			}
725 			else {
726 				mk2data (end_of_buffer_state);
727 				yynxt_data[yynxt_curr++] =
728 					end_of_buffer_state;
729 			}
730 
731 			for (i = 1; i < num_full_table_rows; ++i) {
732 				/* Jams are marked by negative of state
733 				 * number.
734 				 */
735 				mk2data (state[i] ? state[i] : -ds);
736 				yynxt_data[yynxt_curr++] =
737 					state[i] ? state[i] : -ds;
738 			}
739 
740 			dataflush ();
741 			if (gentables)
742 				outn ("    },\n");
743 		}
744 
745 		else if (fullspd)
746 			place_state (state, ds, totaltrans);
747 
748 		else if (ds == end_of_buffer_state)
749 			/* Special case this state to make sure it does what
750 			 * it's supposed to, i.e., jam on end-of-buffer.
751 			 */
752 			stack1 (ds, 0, 0, JAMSTATE);
753 
754 		else {		/* normal, compressed state */
755 
756 			/* Determine which destination state is the most
757 			 * common, and how many transitions to it there are.
758 			 */
759 
760 			comfreq = 0;
761 			comstate = 0;
762 
763 			for (i = 1; i <= targptr; ++i)
764 				if (targfreq[i] > comfreq) {
765 					comfreq = targfreq[i];
766 					comstate = targstate[i];
767 				}
768 
769 			bldtbl (state, ds, totaltrans, comstate, comfreq);
770 		}
771 	}
772 
773 	if (fulltbl) {
774 		dataend ();
775 		if (tablesext) {
776 			yytbl_data_compress (yynxt_tbl);
777 			if (yytbl_data_fwrite (&tableswr, yynxt_tbl) < 0)
778 				flexerror (_
779 					   ("Could not write yynxt_tbl[][]"));
780 		}
781 		if (yynxt_tbl) {
782 			yytbl_data_destroy (yynxt_tbl);
783 			yynxt_tbl = 0;
784 		}
785 	}
786 
787 	else if (!fullspd) {
788 		cmptmps ();	/* create compressed template entries */
789 
790 		/* Create tables for all the states with only one
791 		 * out-transition.
792 		 */
793 		while (onesp > 0) {
794 			mk1tbl (onestate[onesp], onesym[onesp],
795 				onenext[onesp], onedef[onesp]);
796 			--onesp;
797 		}
798 
799 		mkdeftbl ();
800 	}
801 
802 	free ((void *) accset);
803 	free ((void *) nset);
804 }
805 
806 
807 /* snstods - converts a set of ndfa states into a dfa state
808  *
809  * synopsis
810  *    is_new_state = snstods( int sns[numstates], int numstates,
811  *				int accset[num_rules+1], int nacc,
812  *				int hashval, int *newds_addr );
813  *
814  * On return, the dfa state number is in newds.
815  */
816 
snstods(int sns[],int numstates,int accset[],int nacc,int hashval,int * newds_addr)817 int snstods (int sns[], int numstates, int accset[], int nacc, int hashval,
818     int *newds_addr)
819 {
820 	int     didsort = 0;
821 	int i, j;
822 	int     newds, *oldsns;
823 
824 	for (i = 1; i <= lastdfa; ++i)
825 		if (hashval == dhash[i]) {
826 			if (numstates == dfasiz[i]) {
827 				oldsns = dss[i];
828 
829 				if (!didsort) {
830 					/* We sort the states in sns so we
831 					 * can compare it to oldsns quickly.
832 					 */
833 					qsort (&sns [1], numstates, sizeof (sns [1]), intcmp);
834 					didsort = 1;
835 				}
836 
837 				for (j = 1; j <= numstates; ++j)
838 					if (sns[j] != oldsns[j])
839 						break;
840 
841 				if (j > numstates) {
842 					++dfaeql;
843 					*newds_addr = i;
844 					return 0;
845 				}
846 
847 				++hshcol;
848 			}
849 
850 			else
851 				++hshsave;
852 		}
853 
854 	/* Make a new dfa. */
855 
856 	if (++lastdfa >= current_max_dfas)
857 		increase_max_dfas ();
858 
859 	newds = lastdfa;
860 
861 	dss[newds] = allocate_integer_array (numstates + 1);
862 
863 	/* If we haven't already sorted the states in sns, we do so now,
864 	 * so that future comparisons with it can be made quickly.
865 	 */
866 
867 	if (!didsort)
868 		qsort (&sns [1], numstates, sizeof (sns [1]), intcmp);
869 
870 	for (i = 1; i <= numstates; ++i)
871 		dss[newds][i] = sns[i];
872 
873 	dfasiz[newds] = numstates;
874 	dhash[newds] = hashval;
875 
876 	if (nacc == 0) {
877 		if (reject)
878 			dfaacc[newds].dfaacc_set = (int *) 0;
879 		else
880 			dfaacc[newds].dfaacc_state = 0;
881 
882 		accsiz[newds] = 0;
883 	}
884 
885 	else if (reject) {
886 		/* We sort the accepting set in increasing order so the
887 		 * disambiguating rule that the first rule listed is considered
888 		 * match in the event of ties will work.
889 		 */
890 
891 		qsort (&accset [1], nacc, sizeof (accset [1]), intcmp);
892 
893 		dfaacc[newds].dfaacc_set =
894 			allocate_integer_array (nacc + 1);
895 
896 		/* Save the accepting set for later */
897 		for (i = 1; i <= nacc; ++i) {
898 			dfaacc[newds].dfaacc_set[i] = accset[i];
899 
900 			if (accset[i] <= num_rules)
901 				/* Who knows, perhaps a REJECT can yield
902 				 * this rule.
903 				 */
904 				rule_useful[accset[i]] = true;
905 		}
906 
907 		accsiz[newds] = nacc;
908 	}
909 
910 	else {
911 		/* Find lowest numbered rule so the disambiguating rule
912 		 * will work.
913 		 */
914 		j = num_rules + 1;
915 
916 		for (i = 1; i <= nacc; ++i)
917 			if (accset[i] < j)
918 				j = accset[i];
919 
920 		dfaacc[newds].dfaacc_state = j;
921 
922 		if (j <= num_rules)
923 			rule_useful[j] = true;
924 	}
925 
926 	*newds_addr = newds;
927 
928 	return 1;
929 }
930 
931 
932 /* symfollowset - follow the symbol transitions one step
933  *
934  * synopsis
935  *    numstates = symfollowset( int ds[current_max_dfa_size], int dsize,
936  *				int transsym, int nset[current_max_dfa_size] );
937  */
938 
symfollowset(int ds[],int dsize,int transsym,int nset[])939 int symfollowset (int ds[], int dsize, int transsym, int nset[])
940 {
941 	int     ns, tsp, sym, i, j, lenccl, ch, numstates, ccllist;
942 
943 	numstates = 0;
944 
945 	for (i = 1; i <= dsize; ++i) {	/* for each nfa state ns in the state set of ds */
946 		ns = ds[i];
947 		sym = transchar[ns];
948 		tsp = trans1[ns];
949 
950 		if (sym < 0) {	/* it's a character class */
951 			sym = -sym;
952 			ccllist = cclmap[sym];
953 			lenccl = ccllen[sym];
954 
955 			if (cclng[sym]) {
956 				for (j = 0; j < lenccl; ++j) {
957 					/* Loop through negated character
958 					 * class.
959 					 */
960 					ch = ccltbl[ccllist + j];
961 
962 					if (ch == 0)
963 						ch = NUL_ec;
964 
965 					if (ch > transsym)
966 						/* Transsym isn't in negated
967 						 * ccl.
968 						 */
969 						break;
970 
971 					else if (ch == transsym)
972 						/* next 2 */
973 						goto bottom;
974 				}
975 
976 				/* Didn't find transsym in ccl. */
977 				nset[++numstates] = tsp;
978 			}
979 
980 			else
981 				for (j = 0; j < lenccl; ++j) {
982 					ch = ccltbl[ccllist + j];
983 
984 					if (ch == 0)
985 						ch = NUL_ec;
986 
987 					if (ch > transsym)
988 						break;
989 					else if (ch == transsym) {
990 						nset[++numstates] = tsp;
991 						break;
992 					}
993 				}
994 		}
995 
996 		else if (sym == SYM_EPSILON) {	/* do nothing */
997 		}
998 
999 		else if (ABS (ecgroup[sym]) == transsym)
1000 			nset[++numstates] = tsp;
1001 
1002 	      bottom:;
1003 	}
1004 
1005 	return numstates;
1006 }
1007 
1008 
1009 /* sympartition - partition characters with same out-transitions
1010  *
1011  * synopsis
1012  *    sympartition( int ds[current_max_dfa_size], int numstates,
1013  *			int symlist[numecs], int duplist[numecs] );
1014  */
1015 
sympartition(int ds[],int numstates,int symlist[],int duplist[])1016 void sympartition (int ds[], int numstates, int symlist[], int duplist[])
1017 {
1018 	int     tch, i, j, k, ns, dupfwd[CSIZE + 1], lenccl, cclp, ich;
1019 
1020 	/* Partitioning is done by creating equivalence classes for those
1021 	 * characters which have out-transitions from the given state.  Thus
1022 	 * we are really creating equivalence classes of equivalence classes.
1023 	 */
1024 
1025 	for (i = 1; i <= numecs; ++i) {	/* initialize equivalence class list */
1026 		duplist[i] = i - 1;
1027 		dupfwd[i] = i + 1;
1028 	}
1029 
1030 	duplist[1] = NIL;
1031 	dupfwd[numecs] = NIL;
1032 
1033 	for (i = 1; i <= numstates; ++i) {
1034 		ns = ds[i];
1035 		tch = transchar[ns];
1036 
1037 		if (tch != SYM_EPSILON) {
1038 			if (tch < -lastccl || tch >= csize) {
1039 				flexfatal (_
1040 					   ("bad transition character detected in sympartition()"));
1041 			}
1042 
1043 			if (tch >= 0) {	/* character transition */
1044 				int     ec = ecgroup[tch];
1045 
1046 				mkechar (ec, dupfwd, duplist);
1047 				symlist[ec] = 1;
1048 			}
1049 
1050 			else {	/* character class */
1051 				tch = -tch;
1052 
1053 				lenccl = ccllen[tch];
1054 				cclp = cclmap[tch];
1055 				mkeccl (ccltbl + cclp, lenccl, dupfwd,
1056 					duplist, numecs, NUL_ec);
1057 
1058 				if (cclng[tch]) {
1059 					j = 0;
1060 
1061 					for (k = 0; k < lenccl; ++k) {
1062 						ich = ccltbl[cclp + k];
1063 
1064 						if (ich == 0)
1065 							ich = NUL_ec;
1066 
1067 						for (++j; j < ich; ++j)
1068 							symlist[j] = 1;
1069 					}
1070 
1071 					for (++j; j <= numecs; ++j)
1072 						symlist[j] = 1;
1073 				}
1074 
1075 				else
1076 					for (k = 0; k < lenccl; ++k) {
1077 						ich = ccltbl[cclp + k];
1078 
1079 						if (ich == 0)
1080 							ich = NUL_ec;
1081 
1082 						symlist[ich] = 1;
1083 					}
1084 			}
1085 		}
1086 	}
1087 }
1088