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