main.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  1. /* flex - tool to generate fast lexical analyzers */
  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. #ifndef lint
  28. char copyright[] =
  29. "@(#) Copyright (c) 1990 The Regents of the University of California.\n\
  30. All rights reserved.\n";
  31. #endif /* not lint */
  32. /* $Header: /opt/vlysenkov/CVSROOT/arcadia/contrib/tools/flex-old/main.c,v 1.2 2007-11-30 02:28:15 pg Exp $ */
  33. #include "flexdef.h"
  34. #include "version.h"
  35. static char flex_version[] = FLEX_VERSION;
  36. /* declare functions that have forward references */
  37. void flexinit PROTO((int, char**));
  38. void readin PROTO((void));
  39. void set_up_initial_allocations PROTO((void));
  40. #ifdef NEED_ARGV_FIXUP
  41. extern void argv_fixup PROTO((int *, char ***));
  42. #endif
  43. /* these globals are all defined and commented in flexdef.h */
  44. int printstats, syntaxerror, eofseen, ddebug, trace, nowarn, spprdflt;
  45. int interactive, caseins, lex_compat, do_yylineno, useecs, fulltbl, usemecs;
  46. int fullspd, gen_line_dirs, performance_report, backing_up_report;
  47. int C_plus_plus, long_align, use_read, yytext_is_array, do_yywrap, csize;
  48. int yymore_used, reject, real_reject, continued_action, in_rule;
  49. int yymore_really_used, reject_really_used;
  50. int datapos, dataline, linenum, out_linenum;
  51. FILE *skelfile = NULL;
  52. int skel_ind = 0;
  53. char *action_array;
  54. int action_size, defs1_offset, prolog_offset, action_offset, action_index;
  55. char *infilename = NULL, *outfilename = NULL;
  56. int did_outfilename;
  57. char *prefix, *yyclass;
  58. int do_stdinit, use_stdout;
  59. int onestate[ONE_STACK_SIZE], onesym[ONE_STACK_SIZE];
  60. int onenext[ONE_STACK_SIZE], onedef[ONE_STACK_SIZE], onesp;
  61. int maximum_mns, current_mns, current_max_rules;
  62. int num_rules, num_eof_rules, default_rule, lastnfa;
  63. int *firstst, *lastst, *finalst, *transchar, *trans1, *trans2;
  64. int *accptnum, *assoc_rule, *state_type;
  65. int *rule_type, *rule_linenum, *rule_useful;
  66. int current_state_type;
  67. int variable_trailing_context_rules;
  68. int numtemps, numprots, protprev[MSP], protnext[MSP], prottbl[MSP];
  69. int protcomst[MSP], firstprot, lastprot, protsave[PROT_SAVE_SIZE];
  70. int numecs, nextecm[CSIZE + 1], ecgroup[CSIZE + 1], nummecs, tecfwd[CSIZE + 1];
  71. int tecbck[CSIZE + 1];
  72. int lastsc, *scset, *scbol, *scxclu, *sceof;
  73. int current_max_scs;
  74. char **scname;
  75. int current_max_dfa_size, current_max_xpairs;
  76. int current_max_template_xpairs, current_max_dfas;
  77. int lastdfa, *nxt, *chk, *tnxt;
  78. int *base, *def, *nultrans, NUL_ec, tblend, firstfree, **dss, *dfasiz;
  79. union dfaacc_union *dfaacc;
  80. int *accsiz, *dhash, numas;
  81. int numsnpairs, jambase, jamstate;
  82. int lastccl, *cclmap, *ccllen, *cclng, cclreuse;
  83. int current_maxccls, current_max_ccl_tbl_size;
  84. Char *ccltbl;
  85. char nmstr[MAXLINE];
  86. int sectnum, nummt, hshcol, dfaeql, numeps, eps2, num_reallocs;
  87. int tmpuses, totnst, peakpairs, numuniq, numdup, hshsave;
  88. int num_backing_up, bol_needed;
  89. FILE *backing_up_file;
  90. int end_of_buffer_state;
  91. char **input_files;
  92. int num_input_files;
  93. /* Make sure program_name is initialized so we don't crash if writing
  94. * out an error message before getting the program name from argv[0].
  95. */
  96. char *program_name = "flex";
  97. #ifndef SHORT_FILE_NAMES
  98. static char *outfile_template = "lex.%s.%s";
  99. static char *backing_name = "lex.backup";
  100. #else
  101. static char *outfile_template = "lex%s.%s";
  102. static char *backing_name = "lex.bck";
  103. #endif
  104. #ifdef THINK_C
  105. #include <console.h>
  106. #endif
  107. #ifdef MS_DOS
  108. extern unsigned _stklen = 16384;
  109. #endif
  110. static char outfile_path[MAXLINE];
  111. static int outfile_created = 0;
  112. static char *skelname = NULL;
  113. int main( argc, argv )
  114. int argc;
  115. char **argv;
  116. {
  117. int i;
  118. //setlocale(LC_ALL, "");
  119. //textdomain("flex");
  120. //bindtextdomain("flex", "/usr/share/locale");
  121. #ifdef THINK_C
  122. argc = ccommand( &argv );
  123. #endif
  124. #ifdef NEED_ARGV_FIXUP
  125. argv_fixup( &argc, &argv );
  126. #endif
  127. flexinit( argc, argv );
  128. readin();
  129. ntod();
  130. for ( i = 1; i <= num_rules; ++i )
  131. if ( ! rule_useful[i] && i != default_rule )
  132. line_warning( _( "rule cannot be matched" ),
  133. rule_linenum[i] );
  134. if ( spprdflt && ! reject && rule_useful[default_rule] )
  135. line_warning(
  136. _( "-s option given but default rule can be matched" ),
  137. rule_linenum[default_rule] );
  138. /* Generate the C state transition tables from the DFA. */
  139. make_tables();
  140. /* Note, flexend does not return. It exits with its argument
  141. * as status.
  142. */
  143. flexend( 0 );
  144. return 0; /* keep compilers/lint happy */
  145. }
  146. /* check_options - check user-specified options */
  147. void check_options()
  148. {
  149. int i;
  150. if ( lex_compat )
  151. {
  152. if ( C_plus_plus )
  153. flexerror( _( "Can't use -+ with -l option" ) );
  154. if ( fulltbl || fullspd )
  155. flexerror( _( "Can't use -f or -F with -l option" ) );
  156. /* Don't rely on detecting use of yymore() and REJECT,
  157. * just assume they'll be used.
  158. */
  159. yymore_really_used = reject_really_used = true;
  160. yytext_is_array = true;
  161. do_yylineno = true;
  162. use_read = false;
  163. }
  164. if ( do_yylineno )
  165. /* This should really be "maintain_backup_tables = true" */
  166. reject_really_used = true;
  167. if ( csize == unspecified )
  168. {
  169. if ( (fulltbl || fullspd) && ! useecs )
  170. csize = DEFAULT_CSIZE;
  171. else
  172. csize = CSIZE;
  173. }
  174. if ( interactive == unspecified )
  175. {
  176. if ( fulltbl || fullspd )
  177. interactive = false;
  178. else
  179. interactive = true;
  180. }
  181. if ( fulltbl || fullspd )
  182. {
  183. if ( usemecs )
  184. flexerror(
  185. _( "-Cf/-CF and -Cm don't make sense together" ) );
  186. if ( interactive )
  187. flexerror( _( "-Cf/-CF and -I are incompatible" ) );
  188. if ( lex_compat )
  189. flexerror(
  190. _( "-Cf/-CF are incompatible with lex-compatibility mode" ) );
  191. if ( do_yylineno )
  192. flexerror(
  193. _( "-Cf/-CF and %option yylineno are incompatible" ) );
  194. if ( fulltbl && fullspd )
  195. flexerror( _( "-Cf and -CF are mutually exclusive" ) );
  196. }
  197. if ( C_plus_plus && fullspd )
  198. flexerror( _( "Can't use -+ with -CF option" ) );
  199. if ( C_plus_plus && yytext_is_array )
  200. {
  201. warn( _( "%array incompatible with -+ option" ) );
  202. yytext_is_array = false;
  203. }
  204. if ( useecs )
  205. { /* Set up doubly-linked equivalence classes. */
  206. /* We loop all the way up to csize, since ecgroup[csize] is
  207. * the position used for NUL characters.
  208. */
  209. ecgroup[1] = NIL;
  210. for ( i = 2; i <= csize; ++i )
  211. {
  212. ecgroup[i] = i - 1;
  213. nextecm[i - 1] = i;
  214. }
  215. nextecm[csize] = NIL;
  216. }
  217. else
  218. {
  219. /* Put everything in its own equivalence class. */
  220. for ( i = 1; i <= csize; ++i )
  221. {
  222. ecgroup[i] = i;
  223. nextecm[i] = BAD_SUBSCRIPT; /* to catch errors */
  224. }
  225. }
  226. if ( ! use_stdout )
  227. {
  228. FILE *prev_stdout;
  229. if ( ! did_outfilename )
  230. {
  231. char *suffix;
  232. if ( C_plus_plus )
  233. suffix = "cc";
  234. else
  235. suffix = "c";
  236. sprintf( outfile_path, outfile_template,
  237. prefix, suffix );
  238. outfilename = outfile_path;
  239. }
  240. prev_stdout = freopen( outfilename, "w", stdout );
  241. if ( prev_stdout == NULL )
  242. lerrsf( _( "could not create %s" ), outfilename );
  243. outfile_created = 1;
  244. }
  245. if ( skelname && (skelfile = fopen( skelname, "r" )) == NULL )
  246. lerrsf( _( "can't open skeleton file %s" ), skelname );
  247. if ( strcmp( prefix, "yy" ) )
  248. {
  249. #define GEN_PREFIX(name) out_str3( "#define yy%s %s%s\n", name, prefix, name )
  250. if ( C_plus_plus )
  251. GEN_PREFIX( "FlexLexer" );
  252. else
  253. {
  254. GEN_PREFIX( "_create_buffer" );
  255. GEN_PREFIX( "_delete_buffer" );
  256. GEN_PREFIX( "_scan_buffer" );
  257. GEN_PREFIX( "_scan_string" );
  258. GEN_PREFIX( "_scan_bytes" );
  259. GEN_PREFIX( "_flex_debug" );
  260. GEN_PREFIX( "_init_buffer" );
  261. GEN_PREFIX( "_flush_buffer" );
  262. GEN_PREFIX( "_load_buffer_state" );
  263. GEN_PREFIX( "_switch_to_buffer" );
  264. GEN_PREFIX( "in" );
  265. GEN_PREFIX( "leng" );
  266. GEN_PREFIX( "lex" );
  267. GEN_PREFIX( "out" );
  268. GEN_PREFIX( "restart" );
  269. GEN_PREFIX( "text" );
  270. if ( do_yylineno )
  271. GEN_PREFIX( "lineno" );
  272. }
  273. if ( do_yywrap )
  274. GEN_PREFIX( "wrap" );
  275. outn( "" );
  276. }
  277. if ( did_outfilename )
  278. line_directive_out( stdout, 0 );
  279. skelout();
  280. }
  281. /* flexend - terminate flex
  282. *
  283. * note
  284. * This routine does not return.
  285. */
  286. void flexend( exit_status )
  287. int exit_status;
  288. {
  289. int tblsiz;
  290. int unlink();
  291. if ( skelfile != NULL )
  292. {
  293. if ( ferror( skelfile ) )
  294. lerrsf( _( "input error reading skeleton file %s" ),
  295. skelname );
  296. else if ( fclose( skelfile ) )
  297. lerrsf( _( "error closing skeleton file %s" ),
  298. skelname );
  299. }
  300. if ( exit_status != 0 && outfile_created )
  301. {
  302. if ( ferror( stdout ) )
  303. lerrsf( _( "error writing output file %s" ),
  304. outfilename );
  305. else if ( fclose( stdout ) )
  306. lerrsf( _( "error closing output file %s" ),
  307. outfilename );
  308. else if ( unlink( outfilename ) )
  309. lerrsf( _( "error deleting output file %s" ),
  310. outfilename );
  311. }
  312. if ( backing_up_report && backing_up_file )
  313. {
  314. if ( num_backing_up == 0 )
  315. fprintf( backing_up_file, _( "No backing up.\n" ) );
  316. else if ( fullspd || fulltbl )
  317. fprintf( backing_up_file,
  318. _( "%d backing up (non-accepting) states.\n" ),
  319. num_backing_up );
  320. else
  321. fprintf( backing_up_file,
  322. _( "Compressed tables always back up.\n" ) );
  323. if ( ferror( backing_up_file ) )
  324. lerrsf( _( "error writing backup file %s" ),
  325. backing_name );
  326. else if ( fclose( backing_up_file ) )
  327. lerrsf( _( "error closing backup file %s" ),
  328. backing_name );
  329. }
  330. if ( printstats )
  331. {
  332. fprintf( stderr, _( "%s version %s usage statistics:\n" ),
  333. program_name, flex_version );
  334. fprintf( stderr, _( " scanner options: -" ) );
  335. if ( C_plus_plus )
  336. putc( '+', stderr );
  337. if ( backing_up_report )
  338. putc( 'b', stderr );
  339. if ( ddebug )
  340. putc( 'd', stderr );
  341. if ( caseins )
  342. putc( 'i', stderr );
  343. if ( lex_compat )
  344. putc( 'l', stderr );
  345. if ( performance_report > 0 )
  346. putc( 'p', stderr );
  347. if ( performance_report > 1 )
  348. putc( 'p', stderr );
  349. if ( spprdflt )
  350. putc( 's', stderr );
  351. if ( use_stdout )
  352. putc( 't', stderr );
  353. if ( printstats )
  354. putc( 'v', stderr ); /* always true! */
  355. if ( nowarn )
  356. putc( 'w', stderr );
  357. if ( interactive == false )
  358. putc( 'B', stderr );
  359. if ( interactive == true )
  360. putc( 'I', stderr );
  361. if ( ! gen_line_dirs )
  362. putc( 'L', stderr );
  363. if ( trace )
  364. putc( 'T', stderr );
  365. if ( csize == unspecified )
  366. /* We encountered an error fairly early on, so csize
  367. * never got specified. Define it now, to prevent
  368. * bogus table sizes being written out below.
  369. */
  370. csize = 256;
  371. if ( csize == 128 )
  372. putc( '7', stderr );
  373. else
  374. putc( '8', stderr );
  375. fprintf( stderr, " -C" );
  376. if ( long_align )
  377. putc( 'a', stderr );
  378. if ( fulltbl )
  379. putc( 'f', stderr );
  380. if ( fullspd )
  381. putc( 'F', stderr );
  382. if ( useecs )
  383. putc( 'e', stderr );
  384. if ( usemecs )
  385. putc( 'm', stderr );
  386. if ( use_read )
  387. putc( 'r', stderr );
  388. if ( did_outfilename )
  389. fprintf( stderr, " -o%s", outfilename );
  390. if ( skelname )
  391. fprintf( stderr, " -S%s", skelname );
  392. if ( strcmp( prefix, "yy" ) )
  393. fprintf( stderr, " -P%s", prefix );
  394. putc( '\n', stderr );
  395. fprintf( stderr, _( " %d/%d NFA states\n" ),
  396. lastnfa, current_mns );
  397. fprintf( stderr, _( " %d/%d DFA states (%d words)\n" ),
  398. lastdfa, current_max_dfas, totnst );
  399. fprintf( stderr, _( " %d rules\n" ),
  400. num_rules + num_eof_rules - 1 /* - 1 for def. rule */ );
  401. if ( num_backing_up == 0 )
  402. fprintf( stderr, _( " No backing up\n" ) );
  403. else if ( fullspd || fulltbl )
  404. fprintf( stderr,
  405. _( " %d backing-up (non-accepting) states\n" ),
  406. num_backing_up );
  407. else
  408. fprintf( stderr,
  409. _( " Compressed tables always back-up\n" ) );
  410. if ( bol_needed )
  411. fprintf( stderr,
  412. _( " Beginning-of-line patterns used\n" ) );
  413. fprintf( stderr, _( " %d/%d start conditions\n" ), lastsc,
  414. current_max_scs );
  415. fprintf( stderr,
  416. _( " %d epsilon states, %d double epsilon states\n" ),
  417. numeps, eps2 );
  418. if ( lastccl == 0 )
  419. fprintf( stderr, _( " no character classes\n" ) );
  420. else
  421. fprintf( stderr,
  422. _( " %d/%d character classes needed %d/%d words of storage, %d reused\n" ),
  423. lastccl, current_maxccls,
  424. cclmap[lastccl] + ccllen[lastccl],
  425. current_max_ccl_tbl_size, cclreuse );
  426. fprintf( stderr, _( " %d state/nextstate pairs created\n" ),
  427. numsnpairs );
  428. fprintf( stderr, _( " %d/%d unique/duplicate transitions\n" ),
  429. numuniq, numdup );
  430. if ( fulltbl )
  431. {
  432. tblsiz = lastdfa * numecs;
  433. fprintf( stderr, _( " %d table entries\n" ), tblsiz );
  434. }
  435. else
  436. {
  437. tblsiz = 2 * (lastdfa + numtemps) + 2 * tblend;
  438. fprintf( stderr,
  439. _( " %d/%d base-def entries created\n" ),
  440. lastdfa + numtemps, current_max_dfas );
  441. fprintf( stderr,
  442. _( " %d/%d (peak %d) nxt-chk entries created\n" ),
  443. tblend, current_max_xpairs, peakpairs );
  444. fprintf( stderr,
  445. _( " %d/%d (peak %d) template nxt-chk entries created\n" ),
  446. numtemps * nummecs,
  447. current_max_template_xpairs,
  448. numtemps * numecs );
  449. fprintf( stderr, _( " %d empty table entries\n" ),
  450. nummt );
  451. fprintf( stderr, _( " %d protos created\n" ),
  452. numprots );
  453. fprintf( stderr,
  454. _( " %d templates created, %d uses\n" ),
  455. numtemps, tmpuses );
  456. }
  457. if ( useecs )
  458. {
  459. tblsiz = tblsiz + csize;
  460. fprintf( stderr,
  461. _( " %d/%d equivalence classes created\n" ),
  462. numecs, csize );
  463. }
  464. if ( usemecs )
  465. {
  466. tblsiz = tblsiz + numecs;
  467. fprintf( stderr,
  468. _( " %d/%d meta-equivalence classes created\n" ),
  469. nummecs, csize );
  470. }
  471. fprintf( stderr,
  472. _( " %d (%d saved) hash collisions, %d DFAs equal\n" ),
  473. hshcol, hshsave, dfaeql );
  474. fprintf( stderr, _( " %d sets of reallocations needed\n" ),
  475. num_reallocs );
  476. fprintf( stderr, _( " %d total table entries needed\n" ),
  477. tblsiz );
  478. }
  479. exit( exit_status );
  480. }
  481. /* flexinit - initialize flex */
  482. void flexinit( argc, argv )
  483. int argc;
  484. char **argv;
  485. {
  486. int i, sawcmpflag;
  487. char *arg;
  488. printstats = syntaxerror = trace = spprdflt = caseins = false;
  489. lex_compat = C_plus_plus = backing_up_report = ddebug = fulltbl = false;
  490. fullspd = long_align = nowarn = yymore_used = continued_action = false;
  491. do_yylineno = yytext_is_array = in_rule = reject = do_stdinit = false;
  492. yymore_really_used = reject_really_used = unspecified;
  493. interactive = csize = unspecified;
  494. do_yywrap = gen_line_dirs = usemecs = useecs = true;
  495. performance_report = 0;
  496. did_outfilename = 0;
  497. prefix = "yy";
  498. yyclass = 0;
  499. use_read = use_stdout = false;
  500. sawcmpflag = false;
  501. /* Initialize dynamic array for holding the rule actions. */
  502. action_size = 2048; /* default size of action array in bytes */
  503. action_array = allocate_character_array( action_size );
  504. defs1_offset = prolog_offset = action_offset = action_index = 0;
  505. action_array[0] = '\0';
  506. program_name = argv[0];
  507. if ( program_name[0] != '\0' &&
  508. program_name[strlen( program_name ) - 1] == '+' )
  509. C_plus_plus = true;
  510. /* read flags */
  511. for ( --argc, ++argv; argc ; --argc, ++argv )
  512. {
  513. arg = argv[0];
  514. if ( arg[0] != '-' || arg[1] == '\0' )
  515. break;
  516. if ( arg[1] == '-' )
  517. { /* --option */
  518. if ( ! strcmp( arg, "--help" ) )
  519. arg = "-h";
  520. else if ( ! strcmp( arg, "--version" ) )
  521. arg = "-V";
  522. else if ( ! strcmp( arg, "--" ) )
  523. { /* end of options */
  524. --argc;
  525. ++argv;
  526. break;
  527. }
  528. }
  529. for ( i = 1; arg[i] != '\0'; ++i )
  530. switch ( arg[i] )
  531. {
  532. case '+':
  533. C_plus_plus = true;
  534. break;
  535. case 'B':
  536. interactive = false;
  537. break;
  538. case 'b':
  539. backing_up_report = true;
  540. break;
  541. case 'c':
  542. break;
  543. case 'C':
  544. if ( i != 1 )
  545. flexerror(
  546. _( "-C flag must be given separately" ) );
  547. if ( ! sawcmpflag )
  548. {
  549. useecs = false;
  550. usemecs = false;
  551. fulltbl = false;
  552. sawcmpflag = true;
  553. }
  554. for ( ++i; arg[i] != '\0'; ++i )
  555. switch ( arg[i] )
  556. {
  557. case 'a':
  558. long_align =
  559. true;
  560. break;
  561. case 'e':
  562. useecs = true;
  563. break;
  564. case 'F':
  565. fullspd = true;
  566. break;
  567. case 'f':
  568. fulltbl = true;
  569. break;
  570. case 'm':
  571. usemecs = true;
  572. break;
  573. case 'r':
  574. use_read = true;
  575. break;
  576. default:
  577. lerrif(
  578. _( "unknown -C option '%c'" ),
  579. (int) arg[i] );
  580. break;
  581. }
  582. goto get_next_arg;
  583. case 'd':
  584. ddebug = true;
  585. break;
  586. case 'f':
  587. useecs = usemecs = false;
  588. use_read = fulltbl = true;
  589. break;
  590. case 'F':
  591. useecs = usemecs = false;
  592. use_read = fullspd = true;
  593. break;
  594. case '?':
  595. case 'h':
  596. usage();
  597. exit( 0 );
  598. case 'I':
  599. interactive = true;
  600. break;
  601. case 'i':
  602. caseins = true;
  603. break;
  604. case 'l':
  605. lex_compat = true;
  606. break;
  607. case 'L':
  608. gen_line_dirs = false;
  609. break;
  610. case 'n':
  611. /* Stupid do-nothing deprecated
  612. * option.
  613. */
  614. break;
  615. case 'o':
  616. if ( i != 1 )
  617. flexerror(
  618. _( "-o flag must be given separately" ) );
  619. if(arg[i+1] || !argv[1] ||
  620. argv[1][0] == '-')
  621. {
  622. outfilename = arg + i + 1;
  623. } /* end of if(arg[i+1]) */
  624. else
  625. {
  626. --argc;
  627. ++argv;
  628. outfilename = *argv;
  629. } /* end of else */
  630. did_outfilename = 1;
  631. goto get_next_arg;
  632. case 'P':
  633. if ( i != 1 )
  634. flexerror(
  635. _( "-P flag must be given separately" ) );
  636. prefix = arg + i + 1;
  637. goto get_next_arg;
  638. case 'p':
  639. ++performance_report;
  640. break;
  641. case 'S':
  642. if ( i != 1 )
  643. flexerror(
  644. _( "-S flag must be given separately" ) );
  645. skelname = arg + i + 1;
  646. goto get_next_arg;
  647. case 's':
  648. spprdflt = true;
  649. break;
  650. case 't':
  651. use_stdout = true;
  652. break;
  653. case 'T':
  654. trace = true;
  655. break;
  656. case 'v':
  657. printstats = true;
  658. break;
  659. case 'V':
  660. printf( _( "%s version %s\n" ),
  661. program_name, flex_version );
  662. exit( 0 );
  663. case 'w':
  664. nowarn = true;
  665. break;
  666. case '7':
  667. csize = 128;
  668. break;
  669. case '8':
  670. csize = CSIZE;
  671. break;
  672. default:
  673. fprintf( stderr,
  674. _( "%s: unknown flag '%c'. For usage, try\n\t%s --help\n" ),
  675. program_name, (int) arg[i],
  676. program_name );
  677. exit( 1 );
  678. }
  679. /* Used by -C, -S, -o, and -P flags in lieu of a "continue 2"
  680. * control.
  681. */
  682. get_next_arg: ;
  683. }
  684. num_input_files = argc;
  685. input_files = argv;
  686. set_input_file( num_input_files > 0 ? input_files[0] : NULL );
  687. lastccl = lastsc = lastdfa = lastnfa = 0;
  688. num_rules = num_eof_rules = default_rule = 0;
  689. numas = numsnpairs = tmpuses = 0;
  690. numecs = numeps = eps2 = num_reallocs = hshcol = dfaeql = totnst = 0;
  691. numuniq = numdup = hshsave = eofseen = datapos = dataline = 0;
  692. num_backing_up = onesp = numprots = 0;
  693. variable_trailing_context_rules = bol_needed = false;
  694. out_linenum = linenum = sectnum = 1;
  695. firstprot = NIL;
  696. /* Used in mkprot() so that the first proto goes in slot 1
  697. * of the proto queue.
  698. */
  699. lastprot = 1;
  700. set_up_initial_allocations();
  701. }
  702. /* readin - read in the rules section of the input file(s) */
  703. void readin()
  704. {
  705. static char yy_stdinit[] = "FILE *yyin = stdin, *yyout = stdout;";
  706. static char yy_nostdinit[] =
  707. "FILE *yyin = (FILE *) 0, *yyout = (FILE *) 0;";
  708. line_directive_out( (FILE *) 0, 1 );
  709. if ( yyparse() )
  710. {
  711. pinpoint_message( _( "fatal parse error" ) );
  712. flexend( 1 );
  713. }
  714. if ( syntaxerror )
  715. flexend( 1 );
  716. if ( backing_up_report )
  717. {
  718. backing_up_file = fopen( backing_name, "w" );
  719. if ( backing_up_file == NULL )
  720. lerrsf(
  721. _( "could not create backing-up info file %s" ),
  722. backing_name );
  723. }
  724. else
  725. backing_up_file = NULL;
  726. if ( yymore_really_used == true )
  727. yymore_used = true;
  728. else if ( yymore_really_used == false )
  729. yymore_used = false;
  730. if ( reject_really_used == true )
  731. reject = true;
  732. else if ( reject_really_used == false )
  733. reject = false;
  734. if ( performance_report > 0 )
  735. {
  736. if ( lex_compat )
  737. {
  738. fprintf( stderr,
  739. _( "-l AT&T lex compatibility option entails a large performance penalty\n" ) );
  740. fprintf( stderr,
  741. _( " and may be the actual source of other reported performance penalties\n" ) );
  742. }
  743. else if ( do_yylineno )
  744. {
  745. fprintf( stderr,
  746. _( "%%option yylineno entails a large performance penalty\n" ) );
  747. }
  748. if ( performance_report > 1 )
  749. {
  750. if ( interactive )
  751. fprintf( stderr,
  752. _( "-I (interactive) entails a minor performance penalty\n" ) );
  753. if ( yymore_used )
  754. fprintf( stderr,
  755. _( "yymore() entails a minor performance penalty\n" ) );
  756. }
  757. if ( reject )
  758. fprintf( stderr,
  759. _( "REJECT entails a large performance penalty\n" ) );
  760. if ( variable_trailing_context_rules )
  761. fprintf( stderr,
  762. _( "Variable trailing context rules entail a large performance penalty\n" ) );
  763. }
  764. if ( reject )
  765. real_reject = true;
  766. if ( variable_trailing_context_rules )
  767. reject = true;
  768. if ( (fulltbl || fullspd) && reject )
  769. {
  770. if ( real_reject )
  771. flexerror(
  772. _( "REJECT cannot be used with -f or -F" ) );
  773. else if ( do_yylineno )
  774. flexerror(
  775. _( "%option yylineno cannot be used with -f or -F" ) );
  776. else
  777. flexerror(
  778. _( "variable trailing context rules cannot be used with -f or -F" ) );
  779. }
  780. if ( reject )
  781. outn( "\n#define YY_USES_REJECT" );
  782. if ( ! do_yywrap )
  783. {
  784. outn( "\n#define yywrap() 1" );
  785. outn( "#define YY_SKIP_YYWRAP" );
  786. }
  787. if ( ddebug )
  788. outn( "\n#define FLEX_DEBUG" );
  789. if ( csize == 256 )
  790. outn( "typedef unsigned char YY_CHAR;" );
  791. else
  792. outn( "typedef char YY_CHAR;" );
  793. if ( C_plus_plus )
  794. {
  795. outn( "#define yytext_ptr yytext" );
  796. if ( interactive )
  797. outn( "#define YY_INTERACTIVE" );
  798. }
  799. else
  800. {
  801. if ( do_stdinit )
  802. {
  803. outn( "#ifdef VMS" );
  804. outn( "#ifndef __VMS_POSIX" );
  805. outn( yy_nostdinit );
  806. outn( "#else" );
  807. outn( yy_stdinit );
  808. outn( "#endif" );
  809. outn( "#else" );
  810. outn( yy_stdinit );
  811. outn( "#endif" );
  812. }
  813. else
  814. outn( yy_nostdinit );
  815. }
  816. if ( fullspd )
  817. outn( "typedef yyconst struct yy_trans_info *yy_state_type;" );
  818. else if ( ! C_plus_plus )
  819. outn( "typedef int yy_state_type;" );
  820. if ( ddebug )
  821. outn( "\n#define FLEX_DEBUG" );
  822. if ( lex_compat )
  823. outn( "#define YY_FLEX_LEX_COMPAT" );
  824. if ( do_yylineno && ! C_plus_plus )
  825. {
  826. outn( "extern int yylineno;" );
  827. outn( "int yylineno = 1;" );
  828. }
  829. if ( C_plus_plus )
  830. {
  831. outn( "\n#include <contrib/tools/flex-old/FlexLexer.h>" );
  832. if ( yyclass )
  833. {
  834. outn( "int yyFlexLexer::yylex()" );
  835. outn( "\t{" );
  836. outn(
  837. "\tLexerError( \"yyFlexLexer::yylex invoked but %option yyclass used\" );" );
  838. outn( "\treturn 0;" );
  839. outn( "\t}" );
  840. out_str( "\n#define YY_DECL int %s::yylex()\n",
  841. yyclass );
  842. }
  843. }
  844. else
  845. {
  846. if ( yytext_is_array )
  847. outn( "extern char yytext[];\n" );
  848. else
  849. {
  850. outn( "extern char *yytext;" );
  851. outn( "#define yytext_ptr yytext" );
  852. }
  853. if ( yyclass )
  854. flexerror(
  855. _( "%option yyclass only meaningful for C++ scanners" ) );
  856. }
  857. if ( useecs )
  858. numecs = cre8ecs( nextecm, ecgroup, csize );
  859. else
  860. numecs = csize;
  861. /* Now map the equivalence class for NUL to its expected place. */
  862. ecgroup[0] = ecgroup[csize];
  863. NUL_ec = ABS( ecgroup[0] );
  864. if ( useecs )
  865. ccl2ecl();
  866. }
  867. /* set_up_initial_allocations - allocate memory for internal tables */
  868. void set_up_initial_allocations()
  869. {
  870. maximum_mns = (long_align ? MAXIMUM_MNS_LONG : MAXIMUM_MNS);
  871. current_mns = INITIAL_MNS;
  872. firstst = allocate_integer_array( current_mns );
  873. lastst = allocate_integer_array( current_mns );
  874. finalst = allocate_integer_array( current_mns );
  875. transchar = allocate_integer_array( current_mns );
  876. trans1 = allocate_integer_array( current_mns );
  877. trans2 = allocate_integer_array( current_mns );
  878. accptnum = allocate_integer_array( current_mns );
  879. assoc_rule = allocate_integer_array( current_mns );
  880. state_type = allocate_integer_array( current_mns );
  881. current_max_rules = INITIAL_MAX_RULES;
  882. rule_type = allocate_integer_array( current_max_rules );
  883. rule_linenum = allocate_integer_array( current_max_rules );
  884. rule_useful = allocate_integer_array( current_max_rules );
  885. current_max_scs = INITIAL_MAX_SCS;
  886. scset = allocate_integer_array( current_max_scs );
  887. scbol = allocate_integer_array( current_max_scs );
  888. scxclu = allocate_integer_array( current_max_scs );
  889. sceof = allocate_integer_array( current_max_scs );
  890. scname = allocate_char_ptr_array( current_max_scs );
  891. current_maxccls = INITIAL_MAX_CCLS;
  892. cclmap = allocate_integer_array( current_maxccls );
  893. ccllen = allocate_integer_array( current_maxccls );
  894. cclng = allocate_integer_array( current_maxccls );
  895. current_max_ccl_tbl_size = INITIAL_MAX_CCL_TBL_SIZE;
  896. ccltbl = allocate_Character_array( current_max_ccl_tbl_size );
  897. current_max_dfa_size = INITIAL_MAX_DFA_SIZE;
  898. current_max_xpairs = INITIAL_MAX_XPAIRS;
  899. nxt = allocate_integer_array( current_max_xpairs );
  900. chk = allocate_integer_array( current_max_xpairs );
  901. current_max_template_xpairs = INITIAL_MAX_TEMPLATE_XPAIRS;
  902. tnxt = allocate_integer_array( current_max_template_xpairs );
  903. current_max_dfas = INITIAL_MAX_DFAS;
  904. base = allocate_integer_array( current_max_dfas );
  905. def = allocate_integer_array( current_max_dfas );
  906. dfasiz = allocate_integer_array( current_max_dfas );
  907. accsiz = allocate_integer_array( current_max_dfas );
  908. dhash = allocate_integer_array( current_max_dfas );
  909. dss = allocate_int_ptr_array( current_max_dfas );
  910. dfaacc = allocate_dfaacc_union( current_max_dfas );
  911. nultrans = (int *) 0;
  912. }
  913. void usage()
  914. {
  915. FILE *f = stdout;
  916. fprintf( f,
  917. _( "%s [-bcdfhilnpstvwBFILTV78+? -C[aefFmr] -ooutput -Pprefix -Sskeleton]\n" ),
  918. program_name );
  919. fprintf( f, _( "\t[--help --version] [file ...]\n" ) );
  920. fprintf( f, _( "\t-b generate backing-up information to %s\n" ),
  921. backing_name );
  922. fprintf( f, _( "\t-c do-nothing POSIX option\n" ) );
  923. fprintf( f, _( "\t-d turn on debug mode in generated scanner\n" ) );
  924. fprintf( f, _( "\t-f generate fast, large scanner\n" ) );
  925. fprintf( f, _( "\t-h produce this help message\n" ) );
  926. fprintf( f, _( "\t-i generate case-insensitive scanner\n" ) );
  927. fprintf( f, _( "\t-l maximal compatibility with original lex\n" ) );
  928. fprintf( f, _( "\t-n do-nothing POSIX option\n" ) );
  929. fprintf( f, _( "\t-p generate performance report to stderr\n" ) );
  930. fprintf( f,
  931. _( "\t-s suppress default rule to ECHO unmatched text\n" ) );
  932. if ( ! did_outfilename )
  933. {
  934. sprintf( outfile_path, outfile_template,
  935. prefix, C_plus_plus ? "cc" : "c" );
  936. outfilename = outfile_path;
  937. }
  938. fprintf( f,
  939. _( "\t-t write generated scanner on stdout instead of %s\n" ),
  940. outfilename );
  941. fprintf( f,
  942. _( "\t-v write summary of scanner statistics to f\n" ) );
  943. fprintf( f, _( "\t-w do not generate warnings\n" ) );
  944. fprintf( f, _( "\t-B generate batch scanner (opposite of -I)\n" ) );
  945. fprintf( f,
  946. _( "\t-F use alternative fast scanner representation\n" ) );
  947. fprintf( f,
  948. _( "\t-I generate interactive scanner (opposite of -B)\n" ) );
  949. fprintf( f, _( "\t-L suppress #line directives in scanner\n" ) );
  950. fprintf( f, _( "\t-T %s should run in trace mode\n" ), program_name );
  951. fprintf( f, _( "\t-V report %s version\n" ), program_name );
  952. fprintf( f, _( "\t-7 generate 7-bit scanner\n" ) );
  953. fprintf( f, _( "\t-8 generate 8-bit scanner\n" ) );
  954. fprintf( f, _( "\t-+ generate C++ scanner class\n" ) );
  955. fprintf( f, _( "\t-? produce this help message\n" ) );
  956. fprintf( f,
  957. _( "\t-C specify degree of table compression (default is -Cem):\n" ) );
  958. fprintf( f,
  959. _( "\t\t-Ca trade off larger tables for better memory alignment\n" ) );
  960. fprintf( f, _( "\t\t-Ce construct equivalence classes\n" ) );
  961. fprintf( f,
  962. _( "\t\t-Cf do not compress scanner tables; use -f representation\n" ) );
  963. fprintf( f,
  964. _( "\t\t-CF do not compress scanner tables; use -F representation\n" ) );
  965. fprintf( f, _( "\t\t-Cm construct meta-equivalence classes\n" ) );
  966. fprintf( f,
  967. _( "\t\t-Cr use read() instead of stdio for scanner input\n" ) );
  968. fprintf( f, _( "\t-o specify output filename\n" ) );
  969. fprintf( f, _( "\t-P specify scanner prefix other than \"yy\"\n" ) );
  970. fprintf( f, _( "\t-S specify skeleton file\n" ) );
  971. fprintf( f, _( "\t--help produce this help message\n" ) );
  972. fprintf( f, _( "\t--version report %s version\n" ), program_name );
  973. }