1 /* $NetBSD: flexdef.h,v 1.3 2017/01/02 17:45:27 christos Exp $ */ 2 3 /* flexdef - definitions file for flex */ 4 5 /* Copyright (c) 1990 The Regents of the University of California. */ 6 /* All rights reserved. */ 7 8 /* This code is derived from software contributed to Berkeley by */ 9 /* Vern Paxson. */ 10 11 /* The United States Government has rights in this work pursuant */ 12 /* to contract no. DE-AC03-76SF00098 between the United States */ 13 /* Department of Energy and the University of California. */ 14 15 /* This file is part of flex. */ 16 17 /* Redistribution and use in source and binary forms, with or without */ 18 /* modification, are permitted provided that the following conditions */ 19 /* are met: */ 20 21 /* 1. Redistributions of source code must retain the above copyright */ 22 /* notice, this list of conditions and the following disclaimer. */ 23 /* 2. Redistributions in binary form must reproduce the above copyright */ 24 /* notice, this list of conditions and the following disclaimer in the */ 25 /* documentation and/or other materials provided with the distribution. */ 26 27 /* Neither the name of the University nor the names of its contributors */ 28 /* may be used to endorse or promote products derived from this software */ 29 /* without specific prior written permission. */ 30 31 /* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */ 32 /* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */ 33 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */ 34 /* PURPOSE. */ 35 36 #ifndef FLEXDEF_H 37 #define FLEXDEF_H 1 38 39 #ifdef HAVE_CONFIG_H 40 #include <config.h> 41 #endif 42 43 #include <stdio.h> 44 #include <stdlib.h> 45 #include <stdarg.h> 46 #include <setjmp.h> 47 #include <ctype.h> 48 #include <libgen.h> /* for XPG version of basename(3) */ 49 #include <string.h> 50 #include <math.h> 51 52 #ifdef HAVE_ASSERT_H 53 #include <assert.h> 54 #else 55 #define assert(Pred) 56 #endif 57 58 #ifdef HAVE_LIMITS_H 59 #include <limits.h> 60 #endif 61 #ifdef HAVE_UNISTD_H 62 #include <unistd.h> 63 #endif 64 #ifdef HAVE_NETINET_IN_H 65 #include <netinet/in.h> 66 #endif 67 #ifdef HAVE_SYS_PARAMS_H 68 #include <sys/params.h> 69 #endif 70 #ifdef HAVE_SYS_STAT_H 71 #include <sys/stat.h> 72 #endif 73 #include <sys/wait.h> 74 #include <stdbool.h> 75 #ifdef HAVE_REGEX_H 76 #include <regex.h> 77 #endif 78 #include "flexint.h" 79 80 /* We use gettext. So, when we write strings which should be translated, we mark them with _() */ 81 #ifdef ENABLE_NLS 82 #ifdef HAVE_LOCALE_H 83 #include <locale.h> 84 #endif /* HAVE_LOCALE_H */ 85 #include "gettext.h" 86 #define _(String) gettext (String) 87 #else 88 #define _(STRING) STRING 89 #endif /* ENABLE_NLS */ 90 91 /* Always be prepared to generate an 8-bit scanner. */ 92 #define CSIZE 256 93 94 /* Size of input alphabet - should be size of ASCII set. */ 95 #ifndef DEFAULT_CSIZE 96 #define DEFAULT_CSIZE 128 97 #endif 98 99 /* Maximum line length we'll have to deal with. */ 100 #define MAXLINE 2048 101 102 #ifndef MIN 103 #define MIN(x,y) ((x) < (y) ? (x) : (y)) 104 #endif 105 #ifndef MAX 106 #define MAX(x,y) ((x) > (y) ? (x) : (y)) 107 #endif 108 #ifndef ABS 109 #define ABS(x) ((x) < 0 ? -(x) : (x)) 110 #endif 111 112 113 #define unspecified -1 114 115 /* Special chk[] values marking the slots taking by end-of-buffer and action 116 * numbers. 117 */ 118 #define EOB_POSITION -1 119 #define ACTION_POSITION -2 120 121 /* Number of data items per line for -f output. */ 122 #define NUMDATAITEMS 10 123 124 /* Number of lines of data in -f output before inserting a blank line for 125 * readability. 126 */ 127 #define NUMDATALINES 10 128 129 /* transition_struct_out() definitions. */ 130 #define TRANS_STRUCT_PRINT_LENGTH 14 131 132 /* Returns true if an nfa state has an epsilon out-transition slot 133 * that can be used. This definition is currently not used. 134 */ 135 #define FREE_EPSILON(state) \ 136 (transchar[state] == SYM_EPSILON && \ 137 trans2[state] == NO_TRANSITION && \ 138 finalst[state] != state) 139 140 /* Returns true if an nfa state has an epsilon out-transition character 141 * and both slots are free 142 */ 143 #define SUPER_FREE_EPSILON(state) \ 144 (transchar[state] == SYM_EPSILON && \ 145 trans1[state] == NO_TRANSITION) \ 146 147 /* Maximum number of NFA states that can comprise a DFA state. It's real 148 * big because if there's a lot of rules, the initial state will have a 149 * huge epsilon closure. 150 */ 151 #define INITIAL_MAX_DFA_SIZE 750 152 #define MAX_DFA_SIZE_INCREMENT 750 153 154 155 /* A note on the following masks. They are used to mark accepting numbers 156 * as being special. As such, they implicitly limit the number of accepting 157 * numbers (i.e., rules) because if there are too many rules the rule numbers 158 * will overload the mask bits. Fortunately, this limit is \large/ (0x2000 == 159 * 8192) so unlikely to actually cause any problems. A check is made in 160 * new_rule() to ensure that this limit is not reached. 161 */ 162 163 /* Mask to mark a trailing context accepting number. */ 164 #define YY_TRAILING_MASK 0x2000 165 166 /* Mask to mark the accepting number of the "head" of a trailing context 167 * rule. 168 */ 169 #define YY_TRAILING_HEAD_MASK 0x4000 170 171 /* Maximum number of rules, as outlined in the above note. */ 172 #define MAX_RULE (YY_TRAILING_MASK - 1) 173 174 175 /* NIL must be 0. If not, its special meaning when making equivalence classes 176 * (it marks the representative of a given e.c.) will be unidentifiable. 177 */ 178 #define NIL 0 179 180 #define JAM -1 /* to mark a missing DFA transition */ 181 #define NO_TRANSITION NIL 182 #define UNIQUE -1 /* marks a symbol as an e.c. representative */ 183 #define INFINITE_REPEAT -1 /* for x{5,} constructions */ 184 185 #define INITIAL_MAX_CCLS 100 /* max number of unique character classes */ 186 #define MAX_CCLS_INCREMENT 100 187 188 /* Size of table holding members of character classes. */ 189 #define INITIAL_MAX_CCL_TBL_SIZE 500 190 #define MAX_CCL_TBL_SIZE_INCREMENT 250 191 192 #define INITIAL_MAX_RULES 100 /* default maximum number of rules */ 193 #define MAX_RULES_INCREMENT 100 194 195 #define INITIAL_MNS 2000 /* default maximum number of nfa states */ 196 #define MNS_INCREMENT 1000 /* amount to bump above by if it's not enough */ 197 198 #define INITIAL_MAX_DFAS 1000 /* default maximum number of dfa states */ 199 #define MAX_DFAS_INCREMENT 1000 200 201 #define JAMSTATE -32766 /* marks a reference to the state that always jams */ 202 203 /* Maximum number of NFA states. */ 204 #define MAXIMUM_MNS 31999 205 #define MAXIMUM_MNS_LONG 1999999999 206 207 /* Enough so that if it's subtracted from an NFA state number, the result 208 * is guaranteed to be negative. 209 */ 210 #define MARKER_DIFFERENCE (maximum_mns+2) 211 212 /* Maximum number of nxt/chk pairs for non-templates. */ 213 #define INITIAL_MAX_XPAIRS 2000 214 #define MAX_XPAIRS_INCREMENT 2000 215 216 /* Maximum number of nxt/chk pairs needed for templates. */ 217 #define INITIAL_MAX_TEMPLATE_XPAIRS 2500 218 #define MAX_TEMPLATE_XPAIRS_INCREMENT 2500 219 220 #define SYM_EPSILON (CSIZE + 1) /* to mark transitions on the symbol epsilon */ 221 222 #define INITIAL_MAX_SCS 40 /* maximum number of start conditions */ 223 #define MAX_SCS_INCREMENT 40 /* amount to bump by if it's not enough */ 224 225 #define ONE_STACK_SIZE 500 /* stack of states with only one out-transition */ 226 #define SAME_TRANS -1 /* transition is the same as "default" entry for state */ 227 228 /* The following percentages are used to tune table compression: 229 230 * The percentage the number of out-transitions a state must be of the 231 * number of equivalence classes in order to be considered for table 232 * compaction by using protos. 233 */ 234 #define PROTO_SIZE_PERCENTAGE 15 235 236 /* The percentage the number of homogeneous out-transitions of a state 237 * must be of the number of total out-transitions of the state in order 238 * that the state's transition table is first compared with a potential 239 * template of the most common out-transition instead of with the first 240 * proto in the proto queue. 241 */ 242 #define CHECK_COM_PERCENTAGE 50 243 244 /* The percentage the number of differences between a state's transition 245 * table and the proto it was first compared with must be of the total 246 * number of out-transitions of the state in order to keep the first 247 * proto as a good match and not search any further. 248 */ 249 #define FIRST_MATCH_DIFF_PERCENTAGE 10 250 251 /* The percentage the number of differences between a state's transition 252 * table and the most similar proto must be of the state's total number 253 * of out-transitions to use the proto as an acceptable close match. 254 */ 255 #define ACCEPTABLE_DIFF_PERCENTAGE 50 256 257 /* The percentage the number of homogeneous out-transitions of a state 258 * must be of the number of total out-transitions of the state in order 259 * to consider making a template from the state. 260 */ 261 #define TEMPLATE_SAME_PERCENTAGE 60 262 263 /* The percentage the number of differences between a state's transition 264 * table and the most similar proto must be of the state's total number 265 * of out-transitions to create a new proto from the state. 266 */ 267 #define NEW_PROTO_DIFF_PERCENTAGE 20 268 269 /* The percentage the total number of out-transitions of a state must be 270 * of the number of equivalence classes in order to consider trying to 271 * fit the transition table into "holes" inside the nxt/chk table. 272 */ 273 #define INTERIOR_FIT_PERCENTAGE 15 274 275 /* Size of region set aside to cache the complete transition table of 276 * protos on the proto queue to enable quick comparisons. 277 */ 278 #define PROT_SAVE_SIZE 2000 279 280 #define MSP 50 /* maximum number of saved protos (protos on the proto queue) */ 281 282 /* Maximum number of out-transitions a state can have that we'll rummage 283 * around through the interior of the internal fast table looking for a 284 * spot for it. 285 */ 286 #define MAX_XTIONS_FULL_INTERIOR_FIT 4 287 288 /* Maximum number of rules which will be reported as being associated 289 * with a DFA state. 290 */ 291 #define MAX_ASSOC_RULES 100 292 293 /* Number that, if used to subscript an array, has a good chance of producing 294 * an error; should be small enough to fit into a short. 295 */ 296 #define BAD_SUBSCRIPT -32767 297 298 /* Absolute value of largest number that can be stored in a short, with a 299 * bit of slop thrown in for general paranoia. 300 */ 301 #define MAX_SHORT 32700 302 303 304 /* Declarations for global variables. */ 305 306 307 /* Variables for flags: 308 * printstats - if true (-v), dump statistics 309 * syntaxerror - true if a syntax error has been found 310 * eofseen - true if we've seen an eof in the input file 311 * ddebug - if true (-d), make a "debug" scanner 312 * trace - if true (-T), trace processing 313 * nowarn - if true (-w), do not generate warnings 314 * spprdflt - if true (-s), suppress the default rule 315 * interactive - if true (-I), generate an interactive scanner 316 * lex_compat - if true (-l), maximize compatibility with AT&T lex 317 * posix_compat - if true (-X), maximize compatibility with POSIX lex 318 * do_yylineno - if true, generate code to maintain yylineno 319 * useecs - if true (-Ce flag), use equivalence classes 320 * fulltbl - if true (-Cf flag), don't compress the DFA state table 321 * usemecs - if true (-Cm flag), use meta-equivalence classes 322 * fullspd - if true (-F flag), use Jacobson method of table representation 323 * gen_line_dirs - if true (i.e., no -L flag), generate #line directives 324 * performance_report - if > 0 (i.e., -p flag), generate a report relating 325 * to scanner performance; if > 1 (-p -p), report on minor performance 326 * problems, too 327 * backing_up_report - if true (i.e., -b flag), generate "lex.backup" file 328 * listing backing-up states 329 * C_plus_plus - if true (i.e., -+ flag), generate a C++ scanner class; 330 * otherwise, a standard C scanner 331 * reentrant - if true (-R), generate a reentrant C scanner. 332 * bison_bridge_lval - if true (--bison-bridge), bison pure calling convention. 333 * bison_bridge_lloc - if true (--bison-locations), bison yylloc. 334 * long_align - if true (-Ca flag), favor long-word alignment. 335 * use_read - if true (-f, -F, or -Cr) then use read() for scanner input; 336 * otherwise, use fread(). 337 * yytext_is_array - if true (i.e., %array directive), then declare 338 * yytext as a array instead of a character pointer. Nice and inefficient. 339 * do_yywrap - do yywrap() processing on EOF. If false, EOF treated as 340 * "no more files". 341 * csize - size of character set for the scanner we're generating; 342 * 128 for 7-bit chars and 256 for 8-bit 343 * yymore_used - if true, yymore() is used in input rules 344 * reject - if true, generate back-up tables for REJECT macro 345 * real_reject - if true, scanner really uses REJECT (as opposed to just 346 * having "reject" set for variable trailing context) 347 * continued_action - true if this rule's action is to "fall through" to 348 * the next rule's action (i.e., the '|' action) 349 * in_rule - true if we're inside an individual rule, false if not. 350 * yymore_really_used - whether to treat yymore() as really used, regardless 351 * of what we think based on references to it in the user's actions. 352 * reject_really_used - same for REJECT 353 * trace_hex - use hexadecimal numbers in trace/debug outputs instead of octals 354 */ 355 356 extern int printstats, syntaxerror, eofseen, ddebug, trace, nowarn, 357 spprdflt; 358 extern int interactive, lex_compat, posix_compat, do_yylineno; 359 extern int useecs, fulltbl, usemecs, fullspd; 360 extern int gen_line_dirs, performance_report, backing_up_report; 361 extern int reentrant, bison_bridge_lval, bison_bridge_lloc; 362 extern int C_plus_plus, long_align, use_read, yytext_is_array, do_yywrap; 363 extern int csize; 364 extern int yymore_used, reject, real_reject, continued_action, in_rule; 365 366 extern int yymore_really_used, reject_really_used; 367 extern int trace_hex; 368 369 /* Variables used in the flex input routines: 370 * datapos - characters on current output line 371 * dataline - number of contiguous lines of data in current data 372 * statement. Used to generate readable -f output 373 * linenum - current input line number 374 * skelfile - the skeleton file 375 * skel - compiled-in skeleton array 376 * skel_ind - index into "skel" array, if skelfile is nil 377 * yyin - input file 378 * backing_up_file - file to summarize backing-up states to 379 * infilename - name of input file 380 * outfilename - name of output file 381 * headerfilename - name of the .h file to generate 382 * did_outfilename - whether outfilename was explicitly set 383 * prefix - the prefix used for externally visible names ("yy" by default) 384 * yyclass - yyFlexLexer subclass to use for YY_DECL 385 * do_stdinit - whether to initialize yyin/yyout to stdin/stdout 386 * use_stdout - the -t flag 387 * input_files - array holding names of input files 388 * num_input_files - size of input_files array 389 * program_name - name with which program was invoked 390 * 391 * action_array - array to hold the rule actions 392 * action_size - size of action_array 393 * defs1_offset - index where the user's section 1 definitions start 394 * in action_array 395 * prolog_offset - index where the prolog starts in action_array 396 * action_offset - index where the non-prolog starts in action_array 397 * action_index - index where the next action should go, with respect 398 * to "action_array" 399 */ 400 401 extern int datapos, dataline, linenum; 402 extern FILE *skelfile, *backing_up_file; 403 extern const char *skel[]; 404 extern int skel_ind; 405 extern char *infilename, *outfilename, *headerfilename; 406 extern int did_outfilename; 407 extern char *prefix, *yyclass, *extra_type; 408 extern int do_stdinit, use_stdout; 409 extern char **input_files; 410 extern int num_input_files; 411 extern char *program_name; 412 413 extern char *action_array; 414 extern int action_size; 415 extern int defs1_offset, prolog_offset, action_offset, action_index; 416 417 418 /* Variables for stack of states having only one out-transition: 419 * onestate - state number 420 * onesym - transition symbol 421 * onenext - target state 422 * onedef - default base entry 423 * onesp - stack pointer 424 */ 425 426 extern int onestate[ONE_STACK_SIZE], onesym[ONE_STACK_SIZE]; 427 extern int onenext[ONE_STACK_SIZE], onedef[ONE_STACK_SIZE], onesp; 428 429 430 /* Variables for nfa machine data: 431 * maximum_mns - maximal number of NFA states supported by tables 432 * current_mns - current maximum on number of NFA states 433 * num_rules - number of the last accepting state; also is number of 434 * rules created so far 435 * num_eof_rules - number of <<EOF>> rules 436 * default_rule - number of the default rule 437 * current_max_rules - current maximum number of rules 438 * lastnfa - last nfa state number created 439 * firstst - physically the first state of a fragment 440 * lastst - last physical state of fragment 441 * finalst - last logical state of fragment 442 * transchar - transition character 443 * trans1 - transition state 444 * trans2 - 2nd transition state for epsilons 445 * accptnum - accepting number 446 * assoc_rule - rule associated with this NFA state (or 0 if none) 447 * state_type - a STATE_xxx type identifying whether the state is part 448 * of a normal rule, the leading state in a trailing context 449 * rule (i.e., the state which marks the transition from 450 * recognizing the text-to-be-matched to the beginning of 451 * the trailing context), or a subsequent state in a trailing 452 * context rule 453 * rule_type - a RULE_xxx type identifying whether this a ho-hum 454 * normal rule or one which has variable head & trailing 455 * context 456 * rule_linenum - line number associated with rule 457 * rule_useful - true if we've determined that the rule can be matched 458 * rule_has_nl - true if rule could possibly match a newline 459 * ccl_has_nl - true if current ccl could match a newline 460 * nlch - default eol char 461 */ 462 463 extern int maximum_mns, current_mns, current_max_rules; 464 extern int num_rules, num_eof_rules, default_rule, lastnfa; 465 extern int *firstst, *lastst, *finalst, *transchar, *trans1, *trans2; 466 extern int *accptnum, *assoc_rule, *state_type; 467 extern int *rule_type, *rule_linenum, *rule_useful; 468 extern bool *rule_has_nl, *ccl_has_nl; 469 extern int nlch; 470 471 /* Different types of states; values are useful as masks, as well, for 472 * routines like check_trailing_context(). 473 */ 474 #define STATE_NORMAL 0x1 475 #define STATE_TRAILING_CONTEXT 0x2 476 477 /* Global holding current type of state we're making. */ 478 479 extern int current_state_type; 480 481 /* Different types of rules. */ 482 #define RULE_NORMAL 0 483 #define RULE_VARIABLE 1 484 485 /* True if the input rules include a rule with both variable-length head 486 * and trailing context, false otherwise. 487 */ 488 extern int variable_trailing_context_rules; 489 490 491 /* Variables for protos: 492 * numtemps - number of templates created 493 * numprots - number of protos created 494 * protprev - backlink to a more-recently used proto 495 * protnext - forward link to a less-recently used proto 496 * prottbl - base/def table entry for proto 497 * protcomst - common state of proto 498 * firstprot - number of the most recently used proto 499 * lastprot - number of the least recently used proto 500 * protsave contains the entire state array for protos 501 */ 502 503 extern int numtemps, numprots, protprev[MSP], protnext[MSP], prottbl[MSP]; 504 extern int protcomst[MSP], firstprot, lastprot, protsave[PROT_SAVE_SIZE]; 505 506 507 /* Variables for managing equivalence classes: 508 * numecs - number of equivalence classes 509 * nextecm - forward link of Equivalence Class members 510 * ecgroup - class number or backward link of EC members 511 * nummecs - number of meta-equivalence classes (used to compress 512 * templates) 513 * tecfwd - forward link of meta-equivalence classes members 514 * tecbck - backward link of MEC's 515 */ 516 517 /* Reserve enough room in the equivalence class arrays so that we 518 * can use the CSIZE'th element to hold equivalence class information 519 * for the NUL character. Later we'll move this information into 520 * the 0th element. 521 */ 522 extern int numecs, nextecm[CSIZE + 1], ecgroup[CSIZE + 1], nummecs; 523 524 /* Meta-equivalence classes are indexed starting at 1, so it's possible 525 * that they will require positions from 1 .. CSIZE, i.e., CSIZE + 1 526 * slots total (since the arrays are 0-based). nextecm[] and ecgroup[] 527 * don't require the extra position since they're indexed from 1 .. CSIZE - 1. 528 */ 529 extern int tecfwd[CSIZE + 1], tecbck[CSIZE + 1]; 530 531 532 /* Variables for start conditions: 533 * lastsc - last start condition created 534 * current_max_scs - current limit on number of start conditions 535 * scset - set of rules active in start condition 536 * scbol - set of rules active only at the beginning of line in a s.c. 537 * scxclu - true if start condition is exclusive 538 * sceof - true if start condition has EOF rule 539 * scname - start condition name 540 */ 541 542 extern int lastsc, *scset, *scbol, *scxclu, *sceof; 543 extern int current_max_scs; 544 extern char **scname; 545 546 547 /* Variables for dfa machine data: 548 * current_max_dfa_size - current maximum number of NFA states in DFA 549 * current_max_xpairs - current maximum number of non-template xtion pairs 550 * current_max_template_xpairs - current maximum number of template pairs 551 * current_max_dfas - current maximum number DFA states 552 * lastdfa - last dfa state number created 553 * nxt - state to enter upon reading character 554 * chk - check value to see if "nxt" applies 555 * tnxt - internal nxt table for templates 556 * base - offset into "nxt" for given state 557 * def - where to go if "chk" disallows "nxt" entry 558 * nultrans - NUL transition for each state 559 * NUL_ec - equivalence class of the NUL character 560 * tblend - last "nxt/chk" table entry being used 561 * firstfree - first empty entry in "nxt/chk" table 562 * dss - nfa state set for each dfa 563 * dfasiz - size of nfa state set for each dfa 564 * dfaacc - accepting set for each dfa state (if using REJECT), or accepting 565 * number, if not 566 * accsiz - size of accepting set for each dfa state 567 * dhash - dfa state hash value 568 * numas - number of DFA accepting states created; note that this 569 * is not necessarily the same value as num_rules, which is the analogous 570 * value for the NFA 571 * numsnpairs - number of state/nextstate transition pairs 572 * jambase - position in base/def where the default jam table starts 573 * jamstate - state number corresponding to "jam" state 574 * end_of_buffer_state - end-of-buffer dfa state number 575 */ 576 577 extern int current_max_dfa_size, current_max_xpairs; 578 extern int current_max_template_xpairs, current_max_dfas; 579 extern int lastdfa, *nxt, *chk, *tnxt; 580 extern int *base, *def, *nultrans, NUL_ec, tblend, firstfree, **dss, 581 *dfasiz; 582 extern union dfaacc_union { 583 int *dfaacc_set; 584 int dfaacc_state; 585 } *dfaacc; 586 extern int *accsiz, *dhash, numas; 587 extern int numsnpairs, jambase, jamstate; 588 extern int end_of_buffer_state; 589 590 /* Variables for ccl information: 591 * lastccl - ccl index of the last created ccl 592 * current_maxccls - current limit on the maximum number of unique ccl's 593 * cclmap - maps a ccl index to its set pointer 594 * ccllen - gives the length of a ccl 595 * cclng - true for a given ccl if the ccl is negated 596 * cclreuse - counts how many times a ccl is re-used 597 * current_max_ccl_tbl_size - current limit on number of characters needed 598 * to represent the unique ccl's 599 * ccltbl - holds the characters in each ccl - indexed by cclmap 600 */ 601 602 extern int lastccl, *cclmap, *ccllen, *cclng, cclreuse; 603 extern int current_maxccls, current_max_ccl_tbl_size; 604 extern unsigned char *ccltbl; 605 606 607 /* Variables for miscellaneous information: 608 * nmstr - last NAME scanned by the scanner 609 * sectnum - section number currently being parsed 610 * nummt - number of empty nxt/chk table entries 611 * hshcol - number of hash collisions detected by snstods 612 * dfaeql - number of times a newly created dfa was equal to an old one 613 * numeps - number of epsilon NFA states created 614 * eps2 - number of epsilon states which have 2 out-transitions 615 * num_reallocs - number of times it was necessary to realloc() a group 616 * of arrays 617 * tmpuses - number of DFA states that chain to templates 618 * totnst - total number of NFA states used to make DFA states 619 * peakpairs - peak number of transition pairs we had to store internally 620 * numuniq - number of unique transitions 621 * numdup - number of duplicate transitions 622 * hshsave - number of hash collisions saved by checking number of states 623 * num_backing_up - number of DFA states requiring backing up 624 * bol_needed - whether scanner needs beginning-of-line recognition 625 */ 626 627 extern char nmstr[MAXLINE]; 628 extern int sectnum, nummt, hshcol, dfaeql, numeps, eps2, num_reallocs; 629 extern int tmpuses, totnst, peakpairs, numuniq, numdup, hshsave; 630 extern int num_backing_up, bol_needed; 631 632 #ifndef HAVE_REALLOCARRAY 633 void *reallocarray(void *, size_t, size_t); 634 #endif 635 636 void *allocate_array(int, size_t); 637 void *reallocate_array(void *, int, size_t); 638 639 #define allocate_integer_array(size) \ 640 allocate_array(size, sizeof(int)) 641 642 #define reallocate_integer_array(array,size) \ 643 reallocate_array((void *) array, size, sizeof(int)) 644 645 #define allocate_bool_array(size) \ 646 allocate_array(size, sizeof(bool)) 647 648 #define reallocate_bool_array(array,size) \ 649 reallocate_array((void *) array, size, sizeof(bool)) 650 651 #define allocate_int_ptr_array(size) \ 652 allocate_array(size, sizeof(int *)) 653 654 #define allocate_char_ptr_array(size) \ 655 allocate_array(size, sizeof(char *)) 656 657 #define allocate_dfaacc_union(size) \ 658 allocate_array(size, sizeof(union dfaacc_union)) 659 660 #define reallocate_int_ptr_array(array,size) \ 661 reallocate_array((void *) array, size, sizeof(int *)) 662 663 #define reallocate_char_ptr_array(array,size) \ 664 reallocate_array((void *) array, size, sizeof(char *)) 665 666 #define reallocate_dfaacc_union(array, size) \ 667 reallocate_array((void *) array, size, sizeof(union dfaacc_union)) 668 669 #define allocate_character_array(size) \ 670 allocate_array( size, sizeof(char)) 671 672 #define reallocate_character_array(array,size) \ 673 reallocate_array((void *) array, size, sizeof(char)) 674 675 #define allocate_Character_array(size) \ 676 allocate_array(size, sizeof(unsigned char)) 677 678 #define reallocate_Character_array(array,size) \ 679 reallocate_array((void *) array, size, sizeof(unsigned char)) 680 681 682 /* External functions that are cross-referenced among the flex source files. */ 683 684 685 /* from file ccl.c */ 686 687 extern void ccladd(int, int); /* add a single character to a ccl */ 688 extern int cclinit(void); /* make an empty ccl */ 689 extern void cclnegate(int); /* negate a ccl */ 690 extern int ccl_set_diff (int a, int b); /* set difference of two ccls. */ 691 extern int ccl_set_union (int a, int b); /* set union of two ccls. */ 692 693 /* List the members of a set of characters in CCL form. */ 694 extern void list_character_set(FILE *, int[]); 695 696 697 /* from file dfa.c */ 698 699 /* Check a DFA state for backing up. */ 700 extern void check_for_backing_up(int, int[]); 701 702 /* Check to see if NFA state set constitutes "dangerous" trailing context. */ 703 extern void check_trailing_context(int *, int, int *, int); 704 705 /* Construct the epsilon closure of a set of ndfa states. */ 706 extern int *epsclosure(int *, int *, int[], int *, int *); 707 708 /* Increase the maximum number of dfas. */ 709 extern void increase_max_dfas(void); 710 711 extern void ntod(void); /* convert a ndfa to a dfa */ 712 713 /* Converts a set of ndfa states into a dfa state. */ 714 extern int snstods(int[], int, int[], int, int, int *); 715 716 717 /* from file ecs.c */ 718 719 /* Convert character classes to set of equivalence classes. */ 720 extern void ccl2ecl(void); 721 722 /* Associate equivalence class numbers with class members. */ 723 extern int cre8ecs(int[], int[], int); 724 725 /* Update equivalence classes based on character class transitions. */ 726 extern void mkeccl(unsigned char[], int, int[], int[], int, int); 727 728 /* Create equivalence class for single character. */ 729 extern void mkechar(int, int[], int[]); 730 731 732 /* from file gen.c */ 733 734 extern void do_indent(void); /* indent to the current level */ 735 736 /* Generate the code to keep backing-up information. */ 737 extern void gen_backing_up(void); 738 739 /* Generate the code to perform the backing up. */ 740 extern void gen_bu_action(void); 741 742 /* Generate full speed compressed transition table. */ 743 extern void genctbl(void); 744 745 /* Generate the code to find the action number. */ 746 extern void gen_find_action(void); 747 748 extern void genftbl(void); /* generate full transition table */ 749 750 /* Generate the code to find the next compressed-table state. */ 751 extern void gen_next_compressed_state(char *); 752 753 /* Generate the code to find the next match. */ 754 extern void gen_next_match(void); 755 756 /* Generate the code to find the next state. */ 757 extern void gen_next_state(int); 758 759 /* Generate the code to make a NUL transition. */ 760 extern void gen_NUL_trans(void); 761 762 /* Generate the code to find the start state. */ 763 extern void gen_start_state(void); 764 765 /* Generate data statements for the transition tables. */ 766 extern void gentabs(void); 767 768 /* Write out a formatted string at the current indentation level. */ 769 extern void indent_put2s(const char *, const char *); 770 771 /* Write out a string + newline at the current indentation level. */ 772 extern void indent_puts(const char *); 773 774 extern void make_tables(void); /* generate transition tables */ 775 776 777 /* from file main.c */ 778 779 extern void check_options(void); 780 extern void flexend(int); 781 extern void usage(void); 782 783 784 /* from file misc.c */ 785 786 /* Add a #define to the action file. */ 787 extern void action_define(const char *defname, int value); 788 789 /* Add the given text to the stored actions. */ 790 extern void add_action(const char *new_text); 791 792 /* True if a string is all lower case. */ 793 extern int all_lower(char *); 794 795 /* True if a string is all upper case. */ 796 extern int all_upper(char *); 797 798 /* Compare two integers for use by qsort. */ 799 extern int intcmp(const void *, const void *); 800 801 /* Check a character to make sure it's in the expected range. */ 802 extern void check_char(int c); 803 804 /* Replace upper-case letter to lower-case. */ 805 extern unsigned char clower(int); 806 807 /* strdup() that fails fatally on allocation failures. */ 808 extern char *xstrdup(const char *); 809 810 /* Compare two characters for use by qsort with '\0' sorting last. */ 811 extern int cclcmp(const void *, const void *); 812 813 /* Finish up a block of data declarations. */ 814 extern void dataend(void); 815 816 /* Flush generated data statements. */ 817 extern void dataflush(void); 818 819 /* Report an error message and terminate. */ 820 extern void flexerror(const char *); 821 822 /* Report a fatal error message and terminate. */ 823 extern void flexfatal(const char *); 824 825 /* Report a fatal error with a pinpoint, and terminate */ 826 #if HAVE_DECL___FUNC__ 827 #define flex_die(msg) \ 828 do{ \ 829 fprintf (stderr,\ 830 _("%s: fatal internal error at %s:%d (%s): %s\n"),\ 831 program_name, __FILE__, (int)__LINE__,\ 832 __func__,msg);\ 833 FLEX_EXIT(1);\ 834 }while(0) 835 #else /* ! HAVE_DECL___FUNC__ */ 836 #define flex_die(msg) \ 837 do{ \ 838 fprintf (stderr,\ 839 _("%s: fatal internal error at %s:%d %s\n"),\ 840 program_name, __FILE__, (int)__LINE__,\ 841 msg);\ 842 FLEX_EXIT(1);\ 843 }while(0) 844 #endif /* ! HAVE_DECL___func__ */ 845 846 /* Convert a hexadecimal digit string to an integer value. */ 847 extern unsigned int htoui(unsigned char[]); 848 849 /* Report an error message formatted */ 850 extern void lerr(const char *, ...) 851 #if defined(__GNUC__) && __GNUC__ >= 3 852 __attribute__((__format__(__printf__, 1, 2))) 853 #endif 854 ; 855 856 /* Like lerr, but also exit after displaying message. */ 857 extern void lerr_fatal(const char *, ...) 858 #if defined(__GNUC__) && __GNUC__ >= 3 859 __attribute__((__format__(__printf__, 1, 2))) 860 #endif 861 ; 862 863 /* Spit out a "#line" statement. */ 864 extern void line_directive_out(FILE *, int); 865 866 /* Mark the current position in the action array as the end of the section 1 867 * user defs. 868 */ 869 extern void mark_defs1(void); 870 871 /* Mark the current position in the action array as the end of the prolog. */ 872 extern void mark_prolog(void); 873 874 /* Generate a data statment for a two-dimensional array. */ 875 extern void mk2data(int); 876 877 extern void mkdata(int); /* generate a data statement */ 878 879 /* Return the integer represented by a string of digits. */ 880 extern int myctoi(const char *); 881 882 /* Return character corresponding to escape sequence. */ 883 extern unsigned char myesc(unsigned char[]); 884 885 /* Convert an octal digit string to an integer value. */ 886 extern unsigned int otoui(unsigned char[]); 887 888 /* Output a (possibly-formatted) string to the generated scanner. */ 889 extern void out(const char *); 890 extern void out_dec(const char *, int); 891 extern void out_dec2(const char *, int, int); 892 extern void out_hex(const char *, unsigned int); 893 extern void out_str(const char *, const char *); 894 extern void out_str3(const char *, const char *, const char *, const char *); 895 extern void out_str_dec(const char *, const char *, int); 896 extern void outc(int); 897 extern void outn(const char *); 898 extern void out_m4_define(const char* def, const char* val); 899 900 /* Return a printable version of the given character, which might be 901 * 8-bit. 902 */ 903 extern char *readable_form(int); 904 905 /* Write out one section of the skeleton file. */ 906 extern void skelout(void); 907 908 /* Output a yy_trans_info structure. */ 909 extern void transition_struct_out(int, int); 910 911 /* Only needed when using certain broken versions of bison to build parse.c. */ 912 extern void *yy_flex_xmalloc(int); 913 914 915 /* from file nfa.c */ 916 917 /* Add an accepting state to a machine. */ 918 extern void add_accept(int, int); 919 920 /* Make a given number of copies of a singleton machine. */ 921 extern int copysingl(int, int); 922 923 /* Debugging routine to write out an nfa. */ 924 extern void dumpnfa(int); 925 926 /* Finish up the processing for a rule. */ 927 extern void finish_rule(int, int, int, int, int); 928 929 /* Connect two machines together. */ 930 extern int link_machines(int, int); 931 932 /* Mark each "beginning" state in a machine as being a "normal" (i.e., 933 * not trailing context associated) state. 934 */ 935 extern void mark_beginning_as_normal(int); 936 937 /* Make a machine that branches to two machines. */ 938 extern int mkbranch(int, int); 939 940 extern int mkclos(int); /* convert a machine into a closure */ 941 extern int mkopt(int); /* make a machine optional */ 942 943 /* Make a machine that matches either one of two machines. */ 944 extern int mkor(int, int); 945 946 /* Convert a machine into a positive closure. */ 947 extern int mkposcl(int); 948 949 extern int mkrep(int, int, int); /* make a replicated machine */ 950 951 /* Create a state with a transition on a given symbol. */ 952 extern int mkstate(int); 953 954 extern void new_rule(void); /* initialize for a new rule */ 955 956 957 /* from file parse.y */ 958 959 /* Build the "<<EOF>>" action for the active start conditions. */ 960 extern void build_eof_action(void); 961 962 /* Write out a message formatted with one string, pinpointing its location. */ 963 extern void format_pinpoint_message(const char *, const char *); 964 965 /* Write out a message, pinpointing its location. */ 966 extern void pinpoint_message(const char *); 967 968 /* Write out a warning, pinpointing it at the given line. */ 969 extern void line_warning(const char *, int); 970 971 /* Write out a message, pinpointing it at the given line. */ 972 extern void line_pinpoint(const char *, int); 973 974 /* Report a formatted syntax error. */ 975 extern void format_synerr(const char *, const char *); 976 extern void synerr(const char *); /* report a syntax error */ 977 extern void format_warn(const char *, const char *); 978 extern void lwarn(const char *); /* report a warning */ 979 extern void yyerror(const char *); /* report a parse error */ 980 extern int yyparse(void); /* the YACC parser */ 981 982 983 /* from file scan.l */ 984 985 /* The Flex-generated scanner for flex. */ 986 extern int flexscan(void); 987 988 /* Open the given file (if NULL, stdin) for scanning. */ 989 extern void set_input_file(char *); 990 991 992 /* from file sym.c */ 993 994 /* Save the text of a character class. */ 995 extern void cclinstal(char[], int); 996 997 /* Lookup the number associated with character class. */ 998 extern int ccllookup(char[]); 999 1000 extern void ndinstal(const char *, char[]); /* install a name definition */ 1001 extern char *ndlookup(const char *); /* lookup a name definition */ 1002 1003 /* Increase maximum number of SC's. */ 1004 extern void scextend(void); 1005 extern void scinstal(const char *, int); /* make a start condition */ 1006 1007 /* Lookup the number associated with a start condition. */ 1008 extern int sclookup(const char *); 1009 1010 1011 /* from file tblcmp.c */ 1012 1013 /* Build table entries for dfa state. */ 1014 extern void bldtbl(int[], int, int, int, int); 1015 1016 extern void cmptmps(void); /* compress template table entries */ 1017 extern void expand_nxt_chk(void); /* increase nxt/chk arrays */ 1018 1019 /* Finds a space in the table for a state to be placed. */ 1020 extern int find_table_space(int *, int); 1021 extern void inittbl(void); /* initialize transition tables */ 1022 1023 /* Make the default, "jam" table entries. */ 1024 extern void mkdeftbl(void); 1025 1026 /* Create table entries for a state (or state fragment) which has 1027 * only one out-transition. 1028 */ 1029 extern void mk1tbl(int, int, int, int); 1030 1031 /* Place a state into full speed transition table. */ 1032 extern void place_state(int *, int, int); 1033 1034 /* Save states with only one out-transition to be processed later. */ 1035 extern void stack1(int, int, int, int); 1036 1037 1038 /* from file yylex.c */ 1039 1040 extern int yylex(void); 1041 1042 /* A growable array. See buf.c. */ 1043 struct Buf { 1044 void *elts; /* elements. */ 1045 int nelts; /* number of elements. */ 1046 size_t elt_size; /* in bytes. */ 1047 int nmax; /* max capacity of elements. */ 1048 }; 1049 1050 extern void buf_init(struct Buf * buf, size_t elem_size); 1051 extern void buf_destroy(struct Buf * buf); 1052 extern struct Buf *buf_append(struct Buf * buf, const void *ptr, int n_elem); 1053 extern struct Buf *buf_concat(struct Buf* dest, const struct Buf* src); 1054 extern struct Buf *buf_strappend(struct Buf *, const char *str); 1055 extern struct Buf *buf_strnappend(struct Buf *, const char *str, int nchars); 1056 extern struct Buf *buf_strdefine(struct Buf * buf, const char *str, const char *def); 1057 extern struct Buf *buf_prints(struct Buf *buf, const char *fmt, const char* s); 1058 extern struct Buf *buf_m4_define(struct Buf *buf, const char* def, const char* val); 1059 extern struct Buf *buf_m4_undefine(struct Buf *buf, const char* def); 1060 extern struct Buf *buf_print_strings(struct Buf * buf, FILE* out); 1061 extern struct Buf *buf_linedir(struct Buf *buf, const char* filename, int lineno); 1062 1063 extern struct Buf userdef_buf; /* a string buffer for #define's generated by user-options on cmd line. */ 1064 extern struct Buf defs_buf; /* a char* buffer to save #define'd some symbols generated by flex. */ 1065 extern struct Buf yydmap_buf; /* a string buffer to hold yydmap elements */ 1066 extern struct Buf m4defs_buf; /* Holds m4 definitions. */ 1067 extern struct Buf top_buf; /* contains %top code. String buffer. */ 1068 extern bool no_section3_escape; /* True if the undocumented option --unsafe-no-m4-sect3-escape was passed */ 1069 1070 /* For blocking out code from the header file. */ 1071 #define OUT_BEGIN_CODE() outn("m4_ifdef( [[M4_YY_IN_HEADER]],,[[m4_dnl") 1072 #define OUT_END_CODE() outn("]])") 1073 1074 /* For setjmp/longjmp (instead of calling exit(2)). Linkage in main.c */ 1075 extern jmp_buf flex_main_jmp_buf; 1076 1077 #define FLEX_EXIT(status) longjmp(flex_main_jmp_buf,(status)+1) 1078 1079 /* Removes all \n and \r chars from tail of str. returns str. */ 1080 extern char *chomp (char *str); 1081 1082 /* ctype functions forced to return boolean */ 1083 #define b_isalnum(c) (isalnum(c)?true:false) 1084 #define b_isalpha(c) (isalpha(c)?true:false) 1085 #define b_isascii(c) (isascii(c)?true:false) 1086 #define b_isblank(c) (isblank(c)?true:false) 1087 #define b_iscntrl(c) (iscntrl(c)?true:false) 1088 #define b_isdigit(c) (isdigit(c)?true:false) 1089 #define b_isgraph(c) (isgraph(c)?true:false) 1090 #define b_islower(c) (islower(c)?true:false) 1091 #define b_isprint(c) (isprint(c)?true:false) 1092 #define b_ispunct(c) (ispunct(c)?true:false) 1093 #define b_isspace(c) (isspace(c)?true:false) 1094 #define b_isupper(c) (isupper(c)?true:false) 1095 #define b_isxdigit(c) (isxdigit(c)?true:false) 1096 1097 /* return true if char is uppercase or lowercase. */ 1098 bool has_case(int c); 1099 1100 /* Change case of character if possible. */ 1101 int reverse_case(int c); 1102 1103 /* return false if [c1-c2] is ambiguous for a caseless scanner. */ 1104 bool range_covers_case (int c1, int c2); 1105 1106 /* 1107 * From "filter.c" 1108 */ 1109 1110 /** A single stdio filter to execute. 1111 * The filter may be external, such as "sed", or it 1112 * may be internal, as a function call. 1113 */ 1114 struct filter { 1115 int (*filter_func)(struct filter*); /**< internal filter function */ 1116 void * extra; /**< extra data passed to filter_func */ 1117 int argc; /**< arg count */ 1118 const char ** argv; /**< arg vector, \0-terminated */ 1119 struct filter * next; /**< next filter or NULL */ 1120 }; 1121 1122 /* output filter chain */ 1123 extern struct filter * output_chain; 1124 extern struct filter *filter_create_ext (struct filter * chain, const char *cmd, ...); 1125 struct filter *filter_create_int(struct filter *chain, 1126 int (*filter_func) (struct filter *), 1127 void *extra); 1128 extern bool filter_apply_chain(struct filter * chain); 1129 extern int filter_truncate(struct filter * chain, int max_len); 1130 extern int filter_tee_header(struct filter *chain); 1131 extern int filter_fix_linedirs(struct filter *chain); 1132 1133 1134 /* 1135 * From "regex.c" 1136 */ 1137 1138 extern regex_t regex_linedir, regex_blank_line; 1139 bool flex_init_regex(void); 1140 void flex_regcomp(regex_t *preg, const char *regex, int cflags); 1141 char *regmatch_dup (regmatch_t * m, const char *src); 1142 char *regmatch_cpy (regmatch_t * m, char *dest, const char *src); 1143 int regmatch_len (regmatch_t * m); 1144 int regmatch_strtol (regmatch_t * m, const char *src, char **endptr, int base); 1145 bool regmatch_empty (regmatch_t * m); 1146 1147 /* From "scanflags.h" */ 1148 typedef unsigned int scanflags_t; 1149 extern scanflags_t* _sf_stk; 1150 extern size_t _sf_top_ix, _sf_max; /**< stack of scanner flags. */ 1151 #define _SF_CASE_INS ((scanflags_t) 0x0001) 1152 #define _SF_DOT_ALL ((scanflags_t) 0x0002) 1153 #define _SF_SKIP_WS ((scanflags_t) 0x0004) 1154 #define sf_top() (_sf_stk[_sf_top_ix]) 1155 #define sf_case_ins() (sf_top() & _SF_CASE_INS) 1156 #define sf_dot_all() (sf_top() & _SF_DOT_ALL) 1157 #define sf_skip_ws() (sf_top() & _SF_SKIP_WS) 1158 #define sf_set_case_ins(X) ((X) ? (sf_top() |= _SF_CASE_INS) : (sf_top() &= ~_SF_CASE_INS)) 1159 #define sf_set_dot_all(X) ((X) ? (sf_top() |= _SF_DOT_ALL) : (sf_top() &= ~_SF_DOT_ALL)) 1160 #define sf_set_skip_ws(X) ((X) ? (sf_top() |= _SF_SKIP_WS) : (sf_top() &= ~_SF_SKIP_WS)) 1161 extern void sf_init(void); 1162 extern void sf_push(void); 1163 extern void sf_pop(void); 1164 1165 #ifndef __RCSID 1166 #define __RCSID(a) 1167 #endif 1168 1169 #endif /* not defined FLEXDEF_H */ 1170