gen.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336
  1. /* gen - actual generation (writing) of flex scanners */
  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 are permitted provided
  14. * that: (1) source distributions retain this entire copyright notice and
  15. * comment, and (2) distributions including binaries display the following
  16. * acknowledgement: ``This product includes software developed by the
  17. * University of California, Berkeley and its contributors'' in the
  18. * documentation or other materials provided with the distribution and in
  19. * all advertising materials mentioning features or use of this software.
  20. * Neither the name of the University nor the names of its contributors may
  21. * be used to endorse or promote products derived from this software without
  22. * 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. #ifndef lint
  28. static char rcsid[] =
  29. "@(#) $Id$ (LBL)";
  30. #endif
  31. #include "flexdef.h"
  32. /* declare functions that have forward references */
  33. void gen_next_state PROTO((int));
  34. void genecs PROTO(());
  35. void indent_put2s PROTO((char [], char []));
  36. void indent_puts PROTO((char []));
  37. static int indent_level = 0; /* each level is 4 spaces */
  38. #define indent_up() (++indent_level)
  39. #define indent_down() (--indent_level)
  40. #define set_indent(indent_val) indent_level = indent_val
  41. /* *everything* is done in terms of arrays starting at 1, so provide
  42. * a null entry for the zero element of all C arrays
  43. */
  44. static char C_short_decl[] = "static const short int %s[%d] =\n { 0,\n";
  45. static char C_long_decl[] = "static const long int %s[%d] =\n { 0,\n";
  46. static char C_state_decl[] =
  47. "static const yy_state_type %s[%d] =\n { 0,\n";
  48. /* indent to the current level */
  49. void do_indent()
  50. {
  51. register int i = indent_level * 4;
  52. while ( i >= 8 )
  53. {
  54. putchar( '\t' );
  55. i -= 8;
  56. }
  57. while ( i > 0 )
  58. {
  59. putchar( ' ' );
  60. --i;
  61. }
  62. }
  63. /* generate the code to keep backtracking information */
  64. void gen_backtracking()
  65. {
  66. if ( reject || num_backtracking == 0 )
  67. return;
  68. if ( fullspd )
  69. indent_puts( "if ( yy_current_state[-1].yy_nxt )" );
  70. else
  71. indent_puts( "if ( yy_accept[yy_current_state] )" );
  72. indent_up();
  73. indent_puts( "{" );
  74. indent_puts( "yy_last_accepting_state = yy_current_state;" );
  75. indent_puts( "yy_last_accepting_cpos = yy_cp;" );
  76. indent_puts( "}" );
  77. indent_down();
  78. }
  79. /* generate the code to perform the backtrack */
  80. void gen_bt_action()
  81. {
  82. if ( reject || num_backtracking == 0 )
  83. return;
  84. set_indent( 3 );
  85. indent_puts( "case 0: /* must backtrack */" );
  86. indent_puts( "/* undo the effects of YY_DO_BEFORE_ACTION */" );
  87. indent_puts( "*yy_cp = yy_hold_char;" );
  88. if ( fullspd || fulltbl )
  89. indent_puts( "yy_cp = yy_last_accepting_cpos + 1;" );
  90. else
  91. /* backtracking info for compressed tables is taken \after/
  92. * yy_cp has been incremented for the next state
  93. */
  94. indent_puts( "yy_cp = yy_last_accepting_cpos;" );
  95. indent_puts( "yy_current_state = yy_last_accepting_state;" );
  96. indent_puts( "goto yy_find_action;" );
  97. putchar( '\n' );
  98. set_indent( 0 );
  99. }
  100. /* genctbl - generates full speed compressed transition table
  101. *
  102. * synopsis
  103. * genctbl();
  104. */
  105. void genctbl()
  106. {
  107. register int i;
  108. int end_of_buffer_action = num_rules + 1;
  109. /* table of verify for transition and offset to next state */
  110. printf( "static const struct yy_trans_info yy_transition[%d] =\n",
  111. tblend + numecs + 1 );
  112. printf( " {\n" );
  113. /* We want the transition to be represented as the offset to the
  114. * next state, not the actual state number, which is what it currently is.
  115. * The offset is base[nxt[i]] - base[chk[i]]. That's just the
  116. * difference between the starting points of the two involved states
  117. * (to - from).
  118. *
  119. * first, though, we need to find some way to put in our end-of-buffer
  120. * flags and states. We do this by making a state with absolutely no
  121. * transitions. We put it at the end of the table.
  122. */
  123. /* at this point, we're guaranteed that there's enough room in nxt[]
  124. * and chk[] to hold tblend + numecs entries. We need just two slots.
  125. * One for the action and one for the end-of-buffer transition. We
  126. * now *assume* that we're guaranteed the only character we'll try to
  127. * index this nxt/chk pair with is EOB, i.e., 0, so we don't have to
  128. * make sure there's room for jam entries for other characters.
  129. */
  130. base[lastdfa + 1] = tblend + 2;
  131. nxt[tblend + 1] = end_of_buffer_action;
  132. chk[tblend + 1] = numecs + 1;
  133. chk[tblend + 2] = 1; /* anything but EOB */
  134. nxt[tblend + 2] = 0; /* so that "make test" won't show arb. differences */
  135. /* make sure every state has a end-of-buffer transition and an action # */
  136. for ( i = 0; i <= lastdfa; ++i )
  137. {
  138. register int anum = dfaacc[i].dfaacc_state;
  139. chk[base[i]] = EOB_POSITION;
  140. chk[base[i] - 1] = ACTION_POSITION;
  141. nxt[base[i] - 1] = anum; /* action number */
  142. }
  143. for ( i = 0; i <= tblend; ++i )
  144. {
  145. if ( chk[i] == EOB_POSITION )
  146. transition_struct_out( 0, base[lastdfa + 1] - i );
  147. else if ( chk[i] == ACTION_POSITION )
  148. transition_struct_out( 0, nxt[i] );
  149. else if ( chk[i] > numecs || chk[i] == 0 )
  150. transition_struct_out( 0, 0 ); /* unused slot */
  151. else /* verify, transition */
  152. transition_struct_out( chk[i], base[nxt[i]] - (i - chk[i]) );
  153. }
  154. /* here's the final, end-of-buffer state */
  155. transition_struct_out( chk[tblend + 1], nxt[tblend + 1] );
  156. transition_struct_out( chk[tblend + 2], nxt[tblend + 2] );
  157. printf( " };\n" );
  158. printf( "\n" );
  159. /* table of pointers to start states */
  160. printf( "static const struct yy_trans_info *yy_start_state_list[%d] =\n",
  161. lastsc * 2 + 1 );
  162. printf( " {\n" );
  163. for ( i = 0; i <= lastsc * 2; ++i )
  164. printf( " &yy_transition[%d],\n", base[i] );
  165. dataend();
  166. if ( useecs )
  167. genecs();
  168. }
  169. /* generate equivalence-class tables */
  170. void genecs()
  171. {
  172. register int i, j;
  173. static char C_char_decl[] = "static const %s %s[%d] =\n { 0,\n";
  174. int numrows;
  175. Char clower();
  176. if ( numecs < csize )
  177. printf( C_char_decl, "YY_CHAR", "yy_ec", csize );
  178. else
  179. printf( C_char_decl, "short", "yy_ec", csize );
  180. for ( i = 1; i < csize; ++i )
  181. {
  182. if ( caseins && (i >= 'A') && (i <= 'Z') )
  183. ecgroup[i] = ecgroup[clower( i )];
  184. ecgroup[i] = abs( ecgroup[i] );
  185. mkdata( ecgroup[i] );
  186. }
  187. dataend();
  188. if ( trace )
  189. {
  190. char *readable_form();
  191. fputs( "\n\nEquivalence Classes:\n\n", stderr );
  192. numrows = csize / 8;
  193. for ( j = 0; j < numrows; ++j )
  194. {
  195. for ( i = j; i < csize; i = i + numrows )
  196. {
  197. fprintf( stderr, "%4s = %-2d", readable_form( i ), ecgroup[i] );
  198. putc( ' ', stderr );
  199. }
  200. putc( '\n', stderr );
  201. }
  202. }
  203. }
  204. /* generate the code to find the action number */
  205. void gen_find_action()
  206. {
  207. if ( fullspd )
  208. indent_puts( "yy_act = yy_current_state[-1].yy_nxt;" );
  209. else if ( fulltbl )
  210. indent_puts( "yy_act = yy_accept[yy_current_state];" );
  211. else if ( reject )
  212. {
  213. indent_puts( "yy_current_state = *--yy_state_ptr;" );
  214. indent_puts( "yy_lp = yy_accept[yy_current_state];" );
  215. puts( "find_rule: /* we branch to this label when backtracking */" );
  216. indent_puts( "for ( ; ; ) /* until we find what rule we matched */" );
  217. indent_up();
  218. indent_puts( "{" );
  219. indent_puts( "if ( yy_lp && yy_lp < yy_accept[yy_current_state + 1] )" );
  220. indent_up();
  221. indent_puts( "{" );
  222. indent_puts( "yy_act = yy_acclist[yy_lp];" );
  223. if ( variable_trailing_context_rules )
  224. {
  225. indent_puts( "if ( yy_act & YY_TRAILING_HEAD_MASK ||" );
  226. indent_puts( " yy_looking_for_trail_begin )" );
  227. indent_up();
  228. indent_puts( "{" );
  229. indent_puts( "if ( yy_act == yy_looking_for_trail_begin )" );
  230. indent_up();
  231. indent_puts( "{" );
  232. indent_puts( "yy_looking_for_trail_begin = 0;" );
  233. indent_puts( "yy_act &= ~YY_TRAILING_HEAD_MASK;" );
  234. indent_puts( "break;" );
  235. indent_puts( "}" );
  236. indent_down();
  237. indent_puts( "}" );
  238. indent_down();
  239. indent_puts( "else if ( yy_act & YY_TRAILING_MASK )" );
  240. indent_up();
  241. indent_puts( "{" );
  242. indent_puts(
  243. "yy_looking_for_trail_begin = yy_act & ~YY_TRAILING_MASK;" );
  244. indent_puts(
  245. "yy_looking_for_trail_begin |= YY_TRAILING_HEAD_MASK;" );
  246. if ( real_reject )
  247. {
  248. /* remember matched text in case we back up due to REJECT */
  249. indent_puts( "yy_full_match = yy_cp;" );
  250. indent_puts( "yy_full_state = yy_state_ptr;" );
  251. indent_puts( "yy_full_lp = yy_lp;" );
  252. }
  253. indent_puts( "}" );
  254. indent_down();
  255. indent_puts( "else" );
  256. indent_up();
  257. indent_puts( "{" );
  258. indent_puts( "yy_full_match = yy_cp;" );
  259. indent_puts( "yy_full_state = yy_state_ptr;" );
  260. indent_puts( "yy_full_lp = yy_lp;" );
  261. indent_puts( "break;" );
  262. indent_puts( "}" );
  263. indent_down();
  264. indent_puts( "++yy_lp;" );
  265. indent_puts( "goto find_rule;" );
  266. }
  267. else
  268. {
  269. /* remember matched text in case we back up due to trailing context
  270. * plus REJECT
  271. */
  272. indent_up();
  273. indent_puts( "{" );
  274. indent_puts( "yy_full_match = yy_cp;" );
  275. indent_puts( "break;" );
  276. indent_puts( "}" );
  277. indent_down();
  278. }
  279. indent_puts( "}" );
  280. indent_down();
  281. indent_puts( "--yy_cp;" );
  282. /* we could consolidate the following two lines with those at
  283. * the beginning, but at the cost of complaints that we're
  284. * branching inside a loop
  285. */
  286. indent_puts( "yy_current_state = *--yy_state_ptr;" );
  287. indent_puts( "yy_lp = yy_accept[yy_current_state];" );
  288. indent_puts( "}" );
  289. indent_down();
  290. }
  291. else
  292. /* compressed */
  293. indent_puts( "yy_act = yy_accept[yy_current_state];" );
  294. }
  295. /* genftbl - generates full transition table
  296. *
  297. * synopsis
  298. * genftbl();
  299. */
  300. void genftbl()
  301. {
  302. register int i;
  303. int end_of_buffer_action = num_rules + 1;
  304. printf( C_short_decl, "yy_accept", lastdfa + 1 );
  305. dfaacc[end_of_buffer_state].dfaacc_state = end_of_buffer_action;
  306. for ( i = 1; i <= lastdfa; ++i )
  307. {
  308. register int anum = dfaacc[i].dfaacc_state;
  309. mkdata( anum );
  310. if ( trace && anum )
  311. fprintf( stderr, "state # %d accepts: [%d]\n", i, anum );
  312. }
  313. dataend();
  314. if ( useecs )
  315. genecs();
  316. /* don't have to dump the actual full table entries - they were created
  317. * on-the-fly
  318. */
  319. }
  320. /* generate the code to find the next compressed-table state */
  321. void gen_next_compressed_state( char_map )
  322. char *char_map;
  323. {
  324. indent_put2s( "register YY_CHAR yy_c = %s;", char_map );
  325. /* save the backtracking info \before/ computing the next state
  326. * because we always compute one more state than needed - we
  327. * always proceed until we reach a jam state
  328. */
  329. gen_backtracking();
  330. indent_puts(
  331. "while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )" );
  332. indent_up();
  333. indent_puts( "{" );
  334. indent_puts( "yy_current_state = yy_def[yy_current_state];" );
  335. if ( usemecs )
  336. {
  337. /* we've arrange it so that templates are never chained
  338. * to one another. This means we can afford make a
  339. * very simple test to see if we need to convert to
  340. * yy_c's meta-equivalence class without worrying
  341. * about erroneously looking up the meta-equivalence
  342. * class twice
  343. */
  344. do_indent();
  345. /* lastdfa + 2 is the beginning of the templates */
  346. printf( "if ( yy_current_state >= %d )\n", lastdfa + 2 );
  347. indent_up();
  348. indent_puts( "yy_c = yy_meta[yy_c];" );
  349. indent_down();
  350. }
  351. indent_puts( "}" );
  352. indent_down();
  353. indent_puts(
  354. "yy_current_state = yy_nxt[yy_base[yy_current_state] + yy_c];" );
  355. }
  356. /* generate the code to find the next match */
  357. void gen_next_match()
  358. {
  359. /* NOTE - changes in here should be reflected in gen_next_state() and
  360. * gen_NUL_trans()
  361. */
  362. char *char_map = useecs ? "yy_ec[*yy_cp]" : "*yy_cp";
  363. char *char_map_2 = useecs ? "yy_ec[*++yy_cp]" : "*++yy_cp";
  364. if ( fulltbl )
  365. {
  366. indent_put2s(
  367. "while ( (yy_current_state = yy_nxt[yy_current_state][%s]) > 0 )",
  368. char_map );
  369. indent_up();
  370. if ( num_backtracking > 0 )
  371. {
  372. indent_puts( "{" );
  373. gen_backtracking();
  374. putchar( '\n' );
  375. }
  376. indent_puts( "++yy_cp;" );
  377. if ( num_backtracking > 0 )
  378. indent_puts( "}" );
  379. indent_down();
  380. putchar( '\n' );
  381. indent_puts( "yy_current_state = -yy_current_state;" );
  382. }
  383. else if ( fullspd )
  384. {
  385. indent_puts( "{" );
  386. indent_puts( "register const struct yy_trans_info *yy_trans_info;\n" );
  387. indent_puts( "register YY_CHAR yy_c;\n" );
  388. indent_put2s( "for ( yy_c = %s;", char_map );
  389. indent_puts(
  390. " (yy_trans_info = &yy_current_state[yy_c])->yy_verify == yy_c;" );
  391. indent_put2s( " yy_c = %s )", char_map_2 );
  392. indent_up();
  393. if ( num_backtracking > 0 )
  394. indent_puts( "{" );
  395. indent_puts( "yy_current_state += yy_trans_info->yy_nxt;" );
  396. if ( num_backtracking > 0 )
  397. {
  398. putchar( '\n' );
  399. gen_backtracking();
  400. indent_puts( "}" );
  401. }
  402. indent_down();
  403. indent_puts( "}" );
  404. }
  405. else
  406. { /* compressed */
  407. indent_puts( "do" );
  408. indent_up();
  409. indent_puts( "{" );
  410. gen_next_state( false );
  411. indent_puts( "++yy_cp;" );
  412. indent_puts( "}" );
  413. indent_down();
  414. do_indent();
  415. if ( interactive )
  416. printf( "while ( yy_base[yy_current_state] != %d );\n", jambase );
  417. else
  418. printf( "while ( yy_current_state != %d );\n", jamstate );
  419. if ( ! reject && ! interactive )
  420. {
  421. /* do the guaranteed-needed backtrack to figure out the match */
  422. indent_puts( "yy_cp = yy_last_accepting_cpos;" );
  423. indent_puts( "yy_current_state = yy_last_accepting_state;" );
  424. }
  425. }
  426. }
  427. /* generate the code to find the next state */
  428. void gen_next_state( worry_about_NULs )
  429. int worry_about_NULs;
  430. { /* NOTE - changes in here should be reflected in get_next_match() */
  431. char char_map[256];
  432. if ( worry_about_NULs && ! nultrans )
  433. {
  434. if ( useecs )
  435. (void) sprintf( char_map, "(*yy_cp ? yy_ec[*yy_cp] : %d)", NUL_ec );
  436. else
  437. (void) sprintf( char_map, "(*yy_cp ? *yy_cp : %d)", NUL_ec );
  438. }
  439. else
  440. (void) strcpy( char_map, useecs ? "yy_ec[*yy_cp]" : "*yy_cp" );
  441. if ( worry_about_NULs && nultrans )
  442. {
  443. if ( ! fulltbl && ! fullspd )
  444. /* compressed tables backtrack *before* they match */
  445. gen_backtracking();
  446. indent_puts( "if ( *yy_cp )" );
  447. indent_up();
  448. indent_puts( "{" );
  449. }
  450. if ( fulltbl )
  451. indent_put2s( "yy_current_state = yy_nxt[yy_current_state][%s];",
  452. char_map );
  453. else if ( fullspd )
  454. indent_put2s( "yy_current_state += yy_current_state[%s].yy_nxt;",
  455. char_map );
  456. else
  457. gen_next_compressed_state( char_map );
  458. if ( worry_about_NULs && nultrans )
  459. {
  460. indent_puts( "}" );
  461. indent_down();
  462. indent_puts( "else" );
  463. indent_up();
  464. indent_puts( "yy_current_state = yy_NUL_trans[yy_current_state];" );
  465. indent_down();
  466. }
  467. if ( fullspd || fulltbl )
  468. gen_backtracking();
  469. if ( reject )
  470. indent_puts( "*yy_state_ptr++ = yy_current_state;" );
  471. }
  472. /* generate the code to make a NUL transition */
  473. void gen_NUL_trans()
  474. { /* NOTE - changes in here should be reflected in get_next_match() */
  475. int need_backtracking = (num_backtracking > 0 && ! reject);
  476. if ( need_backtracking )
  477. /* we'll need yy_cp lying around for the gen_backtracking() */
  478. indent_puts( "register YY_CHAR *yy_cp = yy_c_buf_p;" );
  479. putchar( '\n' );
  480. if ( nultrans )
  481. {
  482. indent_puts( "yy_current_state = yy_NUL_trans[yy_current_state];" );
  483. indent_puts( "yy_is_jam = (yy_current_state == 0);" );
  484. }
  485. else if ( fulltbl )
  486. {
  487. do_indent();
  488. printf( "yy_current_state = yy_nxt[yy_current_state][%d];\n",
  489. NUL_ec );
  490. indent_puts( "yy_is_jam = (yy_current_state <= 0);" );
  491. }
  492. else if ( fullspd )
  493. {
  494. do_indent();
  495. printf( "register int yy_c = %d;\n", NUL_ec );
  496. indent_puts(
  497. "register const struct yy_trans_info *yy_trans_info;\n" );
  498. indent_puts( "yy_trans_info = &yy_current_state[yy_c];" );
  499. indent_puts( "yy_current_state += yy_trans_info->yy_nxt;" );
  500. indent_puts( "yy_is_jam = (yy_trans_info->yy_verify != yy_c);" );
  501. }
  502. else
  503. {
  504. char NUL_ec_str[20];
  505. (void) sprintf( NUL_ec_str, "%d", NUL_ec );
  506. gen_next_compressed_state( NUL_ec_str );
  507. if ( reject )
  508. indent_puts( "*yy_state_ptr++ = yy_current_state;" );
  509. do_indent();
  510. if ( interactive )
  511. printf( "yy_is_jam = (yy_base[yy_current_state] == %d);\n",
  512. jambase );
  513. else
  514. printf( "yy_is_jam = (yy_current_state == %d);\n", jamstate );
  515. }
  516. /* if we've entered an accepting state, backtrack; note that
  517. * compressed tables have *already* done such backtracking, so
  518. * we needn't bother with it again
  519. */
  520. if ( need_backtracking && (fullspd || fulltbl) )
  521. {
  522. putchar( '\n' );
  523. indent_puts( "if ( ! yy_is_jam )" );
  524. indent_up();
  525. indent_puts( "{" );
  526. gen_backtracking();
  527. indent_puts( "}" );
  528. indent_down();
  529. }
  530. }
  531. /* generate the code to find the start state */
  532. void gen_start_state()
  533. {
  534. if ( fullspd )
  535. indent_put2s( "yy_current_state = yy_start_state_list[yy_start%s];",
  536. bol_needed ? " + (yy_bp[-1] == '\\n' ? 1 : 0)" : "" );
  537. else
  538. {
  539. indent_puts( "yy_current_state = yy_start;" );
  540. if ( bol_needed )
  541. {
  542. indent_puts( "if ( yy_bp[-1] == '\\n' )" );
  543. indent_up();
  544. indent_puts( "++yy_current_state;" );
  545. indent_down();
  546. }
  547. if ( reject )
  548. {
  549. /* set up for storing up states */
  550. indent_puts( "yy_state_ptr = yy_state_buf;" );
  551. indent_puts( "*yy_state_ptr++ = yy_current_state;" );
  552. }
  553. }
  554. }
  555. /* gentabs - generate data statements for the transition tables
  556. *
  557. * synopsis
  558. * gentabs();
  559. */
  560. void gentabs()
  561. {
  562. int i, j, k, *accset, nacc, *acc_array, total_states;
  563. int end_of_buffer_action = num_rules + 1;
  564. /* *everything* is done in terms of arrays starting at 1, so provide
  565. * a null entry for the zero element of all C arrays
  566. */
  567. static char C_char_decl[] =
  568. "static const YY_CHAR %s[%d] =\n { 0,\n";
  569. acc_array = allocate_integer_array( current_max_dfas );
  570. nummt = 0;
  571. /* the compressed table format jams by entering the "jam state",
  572. * losing information about the previous state in the process.
  573. * In order to recover the previous state, we effectively need
  574. * to keep backtracking information.
  575. */
  576. ++num_backtracking;
  577. if ( reject )
  578. {
  579. /* write out accepting list and pointer list
  580. *
  581. * first we generate the "yy_acclist" array. In the process, we compute
  582. * the indices that will go into the "yy_accept" array, and save the
  583. * indices in the dfaacc array
  584. */
  585. int EOB_accepting_list[2];
  586. /* set up accepting structures for the End Of Buffer state */
  587. EOB_accepting_list[0] = 0;
  588. EOB_accepting_list[1] = end_of_buffer_action;
  589. accsiz[end_of_buffer_state] = 1;
  590. dfaacc[end_of_buffer_state].dfaacc_set = EOB_accepting_list;
  591. printf( C_short_decl, "yy_acclist", max( numas, 1 ) + 1 );
  592. j = 1; /* index into "yy_acclist" array */
  593. for ( i = 1; i <= lastdfa; ++i )
  594. {
  595. acc_array[i] = j;
  596. if ( accsiz[i] != 0 )
  597. {
  598. accset = dfaacc[i].dfaacc_set;
  599. nacc = accsiz[i];
  600. if ( trace )
  601. fprintf( stderr, "state # %d accepts: ", i );
  602. for ( k = 1; k <= nacc; ++k )
  603. {
  604. int accnum = accset[k];
  605. ++j;
  606. if ( variable_trailing_context_rules &&
  607. ! (accnum & YY_TRAILING_HEAD_MASK) &&
  608. accnum > 0 && accnum <= num_rules &&
  609. rule_type[accnum] == RULE_VARIABLE )
  610. {
  611. /* special hack to flag accepting number as part
  612. * of trailing context rule
  613. */
  614. accnum |= YY_TRAILING_MASK;
  615. }
  616. mkdata( accnum );
  617. if ( trace )
  618. {
  619. fprintf( stderr, "[%d]", accset[k] );
  620. if ( k < nacc )
  621. fputs( ", ", stderr );
  622. else
  623. putc( '\n', stderr );
  624. }
  625. }
  626. }
  627. }
  628. /* add accepting number for the "jam" state */
  629. acc_array[i] = j;
  630. dataend();
  631. }
  632. else
  633. {
  634. dfaacc[end_of_buffer_state].dfaacc_state = end_of_buffer_action;
  635. for ( i = 1; i <= lastdfa; ++i )
  636. acc_array[i] = dfaacc[i].dfaacc_state;
  637. /* add accepting number for jam state */
  638. acc_array[i] = 0;
  639. }
  640. /* spit out "yy_accept" array. If we're doing "reject", it'll be pointers
  641. * into the "yy_acclist" array. Otherwise it's actual accepting numbers.
  642. * In either case, we just dump the numbers.
  643. */
  644. /* "lastdfa + 2" is the size of "yy_accept"; includes room for C arrays
  645. * beginning at 0 and for "jam" state
  646. */
  647. k = lastdfa + 2;
  648. if ( reject )
  649. /* we put a "cap" on the table associating lists of accepting
  650. * numbers with state numbers. This is needed because we tell
  651. * where the end of an accepting list is by looking at where
  652. * the list for the next state starts.
  653. */
  654. ++k;
  655. printf( C_short_decl, "yy_accept", k );
  656. for ( i = 1; i <= lastdfa; ++i )
  657. {
  658. mkdata( acc_array[i] );
  659. if ( ! reject && trace && acc_array[i] )
  660. fprintf( stderr, "state # %d accepts: [%d]\n", i, acc_array[i] );
  661. }
  662. /* add entry for "jam" state */
  663. mkdata( acc_array[i] );
  664. if ( reject )
  665. /* add "cap" for the list */
  666. mkdata( acc_array[i] );
  667. dataend();
  668. if ( useecs )
  669. genecs();
  670. if ( usemecs )
  671. {
  672. /* write out meta-equivalence classes (used to index templates with) */
  673. if ( trace )
  674. fputs( "\n\nMeta-Equivalence Classes:\n", stderr );
  675. printf( C_char_decl, "yy_meta", numecs + 1 );
  676. for ( i = 1; i <= numecs; ++i )
  677. {
  678. if ( trace )
  679. fprintf( stderr, "%d = %d\n", i, abs( tecbck[i] ) );
  680. mkdata( abs( tecbck[i] ) );
  681. }
  682. dataend();
  683. }
  684. total_states = lastdfa + numtemps;
  685. printf( tblend > MAX_SHORT ? C_long_decl : C_short_decl,
  686. "yy_base", total_states + 1 );
  687. for ( i = 1; i <= lastdfa; ++i )
  688. {
  689. register int d = def[i];
  690. if ( base[i] == JAMSTATE )
  691. base[i] = jambase;
  692. if ( d == JAMSTATE )
  693. def[i] = jamstate;
  694. else if ( d < 0 )
  695. {
  696. /* template reference */
  697. ++tmpuses;
  698. def[i] = lastdfa - d + 1;
  699. }
  700. mkdata( base[i] );
  701. }
  702. /* generate jam state's base index */
  703. mkdata( base[i] );
  704. for ( ++i /* skip jam state */; i <= total_states; ++i )
  705. {
  706. mkdata( base[i] );
  707. def[i] = jamstate;
  708. }
  709. dataend();
  710. printf( tblend > MAX_SHORT ? C_long_decl : C_short_decl,
  711. "yy_def", total_states + 1 );
  712. for ( i = 1; i <= total_states; ++i )
  713. mkdata( def[i] );
  714. dataend();
  715. printf( lastdfa > MAX_SHORT ? C_long_decl : C_short_decl,
  716. "yy_nxt", tblend + 1 );
  717. for ( i = 1; i <= tblend; ++i )
  718. {
  719. if ( nxt[i] == 0 || chk[i] == 0 )
  720. nxt[i] = jamstate; /* new state is the JAM state */
  721. mkdata( nxt[i] );
  722. }
  723. dataend();
  724. printf( lastdfa > MAX_SHORT ? C_long_decl : C_short_decl,
  725. "yy_chk", tblend + 1 );
  726. for ( i = 1; i <= tblend; ++i )
  727. {
  728. if ( chk[i] == 0 )
  729. ++nummt;
  730. mkdata( chk[i] );
  731. }
  732. dataend();
  733. }
  734. /* write out a formatted string (with a secondary string argument) at the
  735. * current indentation level, adding a final newline
  736. */
  737. void indent_put2s( fmt, arg )
  738. char fmt[], arg[];
  739. {
  740. do_indent();
  741. printf( fmt, arg );
  742. putchar( '\n' );
  743. }
  744. /* write out a string at the current indentation level, adding a final
  745. * newline
  746. */
  747. void indent_puts( str )
  748. char str[];
  749. {
  750. do_indent();
  751. puts( str );
  752. }
  753. /* make_tables - generate transition tables
  754. *
  755. * synopsis
  756. * make_tables();
  757. *
  758. * Generates transition tables and finishes generating output file
  759. */
  760. void make_tables()
  761. {
  762. register int i;
  763. int did_eof_rule = false;
  764. skelout();
  765. /* first, take care of YY_DO_BEFORE_ACTION depending on yymore being used */
  766. set_indent( 2 );
  767. if ( yymore_used )
  768. {
  769. indent_puts( "yytext -= yy_more_len; \\" );
  770. indent_puts( "yyleng = yy_cp - yytext; \\" );
  771. }
  772. else
  773. indent_puts( "yyleng = yy_cp - yy_bp; \\" );
  774. set_indent( 0 );
  775. skelout();
  776. printf( "#define YY_END_OF_BUFFER %d\n", num_rules + 1 );
  777. if ( fullspd )
  778. { /* need to define the transet type as a size large
  779. * enough to hold the biggest offset
  780. */
  781. int total_table_size = tblend + numecs + 1;
  782. char *trans_offset_type =
  783. total_table_size > MAX_SHORT ? "long" : "short";
  784. set_indent( 0 );
  785. indent_puts( "struct yy_trans_info" );
  786. indent_up();
  787. indent_puts( "{" );
  788. indent_puts( "short yy_verify;" );
  789. /* in cases where its sister yy_verify *is* a "yes, there is a
  790. * transition", yy_nxt is the offset (in records) to the next state.
  791. * In most cases where there is no transition, the value of yy_nxt
  792. * is irrelevant. If yy_nxt is the -1th record of a state, though,
  793. * then yy_nxt is the action number for that state
  794. */
  795. indent_put2s( "%s yy_nxt;", trans_offset_type );
  796. indent_puts( "};" );
  797. indent_down();
  798. indent_puts( "typedef const struct yy_trans_info *yy_state_type;" );
  799. }
  800. else
  801. indent_puts( "typedef int yy_state_type;" );
  802. if ( fullspd )
  803. genctbl();
  804. else if ( fulltbl )
  805. genftbl();
  806. else
  807. gentabs();
  808. if ( num_backtracking > 0 )
  809. {
  810. indent_puts( "static yy_state_type yy_last_accepting_state;" );
  811. indent_puts( "static YY_CHAR *yy_last_accepting_cpos;\n" );
  812. }
  813. if ( nultrans )
  814. {
  815. printf( C_state_decl, "yy_NUL_trans", lastdfa + 1 );
  816. for ( i = 1; i <= lastdfa; ++i )
  817. {
  818. if ( fullspd )
  819. {
  820. if ( nultrans )
  821. printf( " &yy_transition[%d],\n", base[i] );
  822. else
  823. printf( " 0,\n" );
  824. }
  825. else
  826. mkdata( nultrans[i] );
  827. }
  828. dataend();
  829. }
  830. if ( ddebug )
  831. { /* spit out table mapping rules to line numbers */
  832. indent_puts( "extern int yy_flex_debug;" );
  833. indent_puts( "int yy_flex_debug = 1;\n" );
  834. printf( C_short_decl, "yy_rule_linenum", num_rules );
  835. for ( i = 1; i < num_rules; ++i )
  836. mkdata( rule_linenum[i] );
  837. dataend();
  838. }
  839. if ( reject )
  840. {
  841. /* declare state buffer variables */
  842. puts(
  843. "static yy_state_type yy_state_buf[YY_BUF_SIZE + 2], *yy_state_ptr;" );
  844. puts( "static YY_CHAR *yy_full_match;" );
  845. puts( "static int yy_lp;" );
  846. if ( variable_trailing_context_rules )
  847. {
  848. puts( "static int yy_looking_for_trail_begin = 0;" );
  849. puts( "static int yy_full_lp;" );
  850. puts( "static int *yy_full_state;" );
  851. printf( "#define YY_TRAILING_MASK 0x%x\n", YY_TRAILING_MASK );
  852. printf( "#define YY_TRAILING_HEAD_MASK 0x%x\n",
  853. YY_TRAILING_HEAD_MASK );
  854. }
  855. puts( "#define REJECT \\" );
  856. puts( "{ \\" );
  857. puts(
  858. "*yy_cp = yy_hold_char; /* undo effects of setting up yytext */ \\" );
  859. puts(
  860. "yy_cp = yy_full_match; /* restore poss. backed-over text */ \\" );
  861. if ( variable_trailing_context_rules )
  862. {
  863. puts( "yy_lp = yy_full_lp; /* restore orig. accepting pos. */ \\" );
  864. puts(
  865. "yy_state_ptr = yy_full_state; /* restore orig. state */ \\" );
  866. puts(
  867. "yy_current_state = *yy_state_ptr; /* restore curr. state */ \\" );
  868. }
  869. puts( "++yy_lp; \\" );
  870. puts( "goto find_rule; \\" );
  871. puts( "}" );
  872. }
  873. else
  874. {
  875. puts( "/* the intent behind this definition is that it'll catch" );
  876. puts( " * any uses of REJECT which flex missed" );
  877. puts( " */" );
  878. puts( "#define REJECT reject_used_but_not_detected" );
  879. }
  880. if ( yymore_used )
  881. {
  882. indent_puts( "static int yy_more_flag = 0;" );
  883. indent_puts( "static int yy_doing_yy_more = 0;" );
  884. indent_puts( "static int yy_more_len = 0;" );
  885. indent_puts(
  886. "#define yymore() { yy_more_flag = 1; }" );
  887. indent_puts(
  888. "#define YY_MORE_ADJ (yy_doing_yy_more ? yy_more_len : 0)" );
  889. }
  890. else
  891. {
  892. indent_puts( "#define yymore() yymore_used_but_not_detected" );
  893. indent_puts( "#define YY_MORE_ADJ 0" );
  894. }
  895. skelout();
  896. if ( ferror( temp_action_file ) )
  897. flexfatal( "error occurred when writing temporary action file" );
  898. else if ( fclose( temp_action_file ) )
  899. flexfatal( "error occurred when closing temporary action file" );
  900. temp_action_file = fopen( action_file_name, "r" );
  901. if ( temp_action_file == NULL )
  902. flexfatal( "could not re-open temporary action file" );
  903. /* copy prolog from action_file to output file */
  904. action_out();
  905. skelout();
  906. set_indent( 2 );
  907. if ( yymore_used )
  908. {
  909. indent_puts( "yy_more_len = 0;" );
  910. indent_puts( "yy_doing_yy_more = yy_more_flag;" );
  911. indent_puts( "if ( yy_doing_yy_more )" );
  912. indent_up();
  913. indent_puts( "{" );
  914. indent_puts( "yy_more_len = yyleng;" );
  915. indent_puts( "yy_more_flag = 0;" );
  916. indent_puts( "}" );
  917. indent_down();
  918. }
  919. skelout();
  920. gen_start_state();
  921. /* note, don't use any indentation */
  922. puts( "yy_match:" );
  923. gen_next_match();
  924. skelout();
  925. set_indent( 2 );
  926. gen_find_action();
  927. skelout();
  928. if ( ddebug )
  929. {
  930. indent_puts( "if ( yy_flex_debug )" );
  931. indent_up();
  932. indent_puts( "{" );
  933. indent_puts( "if ( yy_act == 0 )" );
  934. indent_up();
  935. indent_puts( "fprintf( stderr, \"--scanner backtracking\\n\" );" );
  936. indent_down();
  937. do_indent();
  938. printf( "else if ( yy_act < %d )\n", num_rules );
  939. indent_up();
  940. indent_puts(
  941. "fprintf( stderr, \"--accepting rule at line %d (\\\"%s\\\")\\n\"," );
  942. indent_puts( " yy_rule_linenum[yy_act], yytext );" );
  943. indent_down();
  944. do_indent();
  945. printf( "else if ( yy_act == %d )\n", num_rules );
  946. indent_up();
  947. indent_puts(
  948. "fprintf( stderr, \"--accepting default rule (\\\"%s\\\")\\n\"," );
  949. indent_puts( " yytext );" );
  950. indent_down();
  951. do_indent();
  952. printf( "else if ( yy_act == %d )\n", num_rules + 1 );
  953. indent_up();
  954. indent_puts( "fprintf( stderr, \"--(end of buffer or a NUL)\\n\" );" );
  955. indent_down();
  956. do_indent();
  957. printf( "else\n" );
  958. indent_up();
  959. indent_puts( "fprintf( stderr, \"--EOF\\n\" );" );
  960. indent_down();
  961. indent_puts( "}" );
  962. indent_down();
  963. }
  964. /* copy actions from action_file to output file */
  965. skelout();
  966. indent_up();
  967. gen_bt_action();
  968. action_out();
  969. /* generate cases for any missing EOF rules */
  970. for ( i = 1; i <= lastsc; ++i )
  971. if ( ! sceof[i] )
  972. {
  973. do_indent();
  974. printf( "case YY_STATE_EOF(%s):\n", scname[i] );
  975. did_eof_rule = true;
  976. }
  977. if ( did_eof_rule )
  978. {
  979. indent_up();
  980. indent_puts( "yyterminate();" );
  981. indent_down();
  982. }
  983. /* generate code for handling NUL's, if needed */
  984. /* first, deal with backtracking and setting up yy_cp if the scanner
  985. * finds that it should JAM on the NUL
  986. */
  987. skelout();
  988. set_indent( 7 );
  989. if ( fullspd || fulltbl )
  990. indent_puts( "yy_cp = yy_c_buf_p;" );
  991. else
  992. { /* compressed table */
  993. if ( ! reject && ! interactive )
  994. {
  995. /* do the guaranteed-needed backtrack to figure out the match */
  996. indent_puts( "yy_cp = yy_last_accepting_cpos;" );
  997. indent_puts( "yy_current_state = yy_last_accepting_state;" );
  998. }
  999. }
  1000. /* generate code for yy_get_previous_state() */
  1001. set_indent( 1 );
  1002. skelout();
  1003. if ( bol_needed )
  1004. indent_puts( "register YY_CHAR *yy_bp = yytext;\n" );
  1005. gen_start_state();
  1006. set_indent( 2 );
  1007. skelout();
  1008. gen_next_state( true );
  1009. set_indent( 1 );
  1010. skelout();
  1011. gen_NUL_trans();
  1012. skelout();
  1013. /* copy remainder of input to output */
  1014. line_directive_out( stdout );
  1015. (void) flexscan(); /* copy remainder of input to output */
  1016. }