dfa.c 25 KB

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