home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume14 / flex / part03 / flexdef.h < prev    next >
Encoding:
C/C++ Source or Header  |  1988-05-02  |  16.9 KB  |  490 lines

  1. /*
  2.  *  Definitions for flex.
  3.  *
  4.  * modification history
  5.  * --------------------
  6.  * 02b kg, vp   30sep87  .added definitions for fast scanner; misc. cleanup
  7.  * 02a vp       27jun86  .translated into C/FTL
  8.  */
  9.  
  10. /*
  11.  * Copyright (c) 1987, the University of California
  12.  * 
  13.  * The United States Government has rights in this work pursuant to
  14.  * contract no. DE-AC03-76SF00098 between the United States Department of
  15.  * Energy and the University of California.
  16.  * 
  17.  * This program may be redistributed.  Enhancements and derivative works
  18.  * may be created provided the new works, if made available to the general
  19.  * public, are made available for use by anyone.
  20.  */
  21.  
  22. #include <stdio.h>
  23.  
  24. #ifdef SV
  25. #include <string.h>
  26. #define bzero(s, n) memset((char *)(s), '\000', (unsigned)(n))
  27. #else
  28. #include <strings.h>
  29. #endif
  30.  
  31. char *sprintf(); /* keep lint happy */
  32.  
  33.  
  34. /* maximum line length we'll have to deal with */
  35. #define MAXLINE BUFSIZ
  36.  
  37. /* maximum size of file name */
  38. #define FILENAMESIZE 1024
  39.  
  40. #define min(x,y) (x < y ? x : y)
  41. #define max(x,y) (x > y ? x : y)
  42.  
  43. #define true 1
  44. #define false 0
  45.  
  46.  
  47. #ifndef DEFAULT_SKELETON_FILE
  48. #define DEFAULT_SKELETON_FILE "flex.skel"
  49. #endif
  50.  
  51. #ifndef FAST_SKELETON_FILE
  52. #define FAST_SKELETON_FILE "flex.fastskel"
  53. #endif
  54.  
  55. /* special nxt[] action number for the "at the end of the input buffer" state */
  56. /* note: -1 is already taken by YY_NEW_FILE */
  57. #define END_OF_BUFFER_ACTION -3
  58. /* action number for default action for fast scanners */
  59. #define DEFAULT_ACTION -2
  60.  
  61. /* special chk[] values marking the slots taking by end-of-buffer and action
  62.  * numbers
  63.  */
  64. #define EOB_POSITION -1
  65. #define ACTION_POSITION -2
  66.  
  67. /* number of data items per line for -f output */
  68. #define NUMDATAITEMS 10
  69.  
  70. /* number of lines of data in -f output before inserting a blank line for
  71.  * readability.
  72.  */
  73. #define NUMDATALINES 10
  74.  
  75. /* transition_struct_out() definitions */
  76. #define TRANS_STRUCT_PRINT_LENGTH 15
  77.  
  78. /* returns true if an nfa state has an epsilon out-transition slot
  79.  * that can be used.  This definition is currently not used.
  80.  */
  81. #define FREE_EPSILON(state) \
  82.     (transchar[state] == SYM_EPSILON && \
  83.      trans2[state] == NO_TRANSITION && \
  84.      finalst[state] != state)
  85.  
  86. /* returns true if an nfa state has an epsilon out-transition character
  87.  * and both slots are free
  88.  */
  89. #define SUPER_FREE_EPSILON(state) \
  90.     (transchar[state] == SYM_EPSILON && \
  91.      trans1[state] == NO_TRANSITION) \
  92.  
  93. /* maximum number of NFA states that can comprise a DFA state.  It's real
  94.  * big because if there's a lot of rules, the initial state will have a
  95.  * huge epsilon closure.
  96.  */
  97. #define INITIAL_MAX_DFA_SIZE 750
  98. #define MAX_DFA_SIZE_INCREMENT 750
  99.  
  100. /* array names to be used in generated machine.  They're short because
  101.  * we write out one data statement (which names the array) for each element
  102.  * in the array.
  103.  */
  104.  
  105. #define ALIST 'l'    /* points to list of rules accepted for a state */
  106. #define ACCEPT 'a'    /* list of rules accepted for a state */
  107. #define ECARRAY 'e'    /* maps input characters to equivalence classes */
  108. #define MATCHARRAY 'm'    /* maps equivalence classes to meta-equivalence classes */
  109. #define BASEARRAY 'b'    /* "base" array */
  110. #define DEFARRAY 'd'    /* "default" array */
  111. #define NEXTARRAY 'n'    /* "next" array */
  112. #define CHECKARRAY 'c'    /* "check" array */
  113.  
  114. /* NIL must be 0.  If not, its special meaning when making equivalence classes
  115.  * (it marks the representative of a given e.c.) will be unidentifiable
  116.  */
  117. #define NIL 0
  118.  
  119. #define JAM -1    /* to mark a missing DFA transition */
  120. #define NO_TRANSITION NIL
  121. #define UNIQUE -1    /* marks a symbol as an e.c. representative */
  122. #define INFINITY -1    /* for x{5,} constructions */
  123.  
  124. /* size of input alphabet - should be size of ASCII set */
  125. #define CSIZE 127
  126.  
  127. #define INITIAL_MAXCCLS 100    /* max number of unique character classes */
  128. #define MAXCCLS_INCREMENT 100
  129.  
  130. /* size of table holding members of character classes */
  131. #define INITIAL_MAX_CCL_TBL_SIZE 500
  132. #define MAX_CCL_TBL_SIZE_INCREMENT 250
  133.  
  134. #define INITIAL_MNS 2000    /* default maximum number of nfa states */
  135. #define MNS_INCREMENT 1000    /* amount to bump above by if it's not enough */
  136.  
  137. #define INITIAL_MAX_DFAS 1000    /* default maximum number of dfa states */
  138. #define MAX_DFAS_INCREMENT 1000
  139.  
  140. #define JAMSTATE -32766    /* marks a reference to the state that always jams */
  141.  
  142. /* enough so that if it's subtracted from an NFA state number, the result
  143.  * is guaranteed to be negative
  144.  */
  145. #define MARKER_DIFFERENCE 32000
  146. #define MAXIMUM_MNS 31999
  147.  
  148. /* maximum number of nxt/chk pairs for non-templates */
  149. #define INITIAL_MAX_XPAIRS 2000
  150. #define MAX_XPAIRS_INCREMENT 2000
  151.  
  152. /* maximum number of nxt/chk pairs needed for templates */
  153. #define INITIAL_MAX_TEMPLATE_XPAIRS 2500
  154. #define MAX_TEMPLATE_XPAIRS_INCREMENT 2500
  155.  
  156. #define SYM_EPSILON 0    /* to mark transitions on the symbol epsilon */
  157.  
  158. #define INITIAL_MAX_SCS 40    /* maximum number of start conditions */
  159. #define MAX_SCS_INCREMENT 40    /* amount to bump by if it's not enough */
  160.  
  161. #define ONE_STACK_SIZE 500    /* stack of states with only one out-transition */
  162. #define SAME_TRANS -1    /* transition is the same as "default" entry for state */
  163.  
  164. /* the following percentages are used to tune table compression:
  165.  
  166.  * the percentage the number of out-transitions a state must be of the
  167.  * number of equivalence classes in order to be considered for table
  168.  * compaction by using protos
  169.  */
  170. #define PROTO_SIZE_PERCENTAGE 15
  171.  
  172. /* the percentage the number of homogeneous out-transitions of a state
  173.  * must be of the number of total out-transitions of the state in order
  174.  * that the state's transition table is first compared with a potential 
  175.  * template of the most common out-transition instead of with the first
  176.  * proto in the proto queue
  177.  */
  178. #define CHECK_COM_PERCENTAGE 50
  179.  
  180. /* the percentage the number of differences between a state's transition
  181.  * table and the proto it was first compared with must be of the total
  182.  * number of out-transitions of the state in order to keep the first
  183.  * proto as a good match and not search any further
  184.  */
  185. #define FIRST_MATCH_DIFF_PERCENTAGE 10
  186.  
  187. /* the percentage the number of differences between a state's transition
  188.  * table and the most similar proto must be of the state's total number
  189.  * of out-transitions to use the proto as an acceptable close match
  190.  */
  191. #define ACCEPTABLE_DIFF_PERCENTAGE 50
  192.  
  193. /* the percentage the number of homogeneous out-transitions of a state
  194.  * must be of the number of total out-transitions of the state in order
  195.  * to consider making a template from the state
  196.  */
  197. #define TEMPLATE_SAME_PERCENTAGE 60
  198.  
  199. /* the percentage the number of differences between a state's transition
  200.  * table and the most similar proto must be of the state's total number
  201.  * of out-transitions to create a new proto from the state
  202.  */
  203. #define NEW_PROTO_DIFF_PERCENTAGE 20
  204.  
  205. /* the percentage the total number of out-transitions of a state must be
  206.  * of the number of equivalence classes in order to consider trying to
  207.  * fit the transition table into "holes" inside the nxt/chk table.
  208.  */
  209. #define INTERIOR_FIT_PERCENTAGE 15
  210.  
  211. /* size of region set aside to cache the complete transition table of
  212.  * protos on the proto queue to enable quick comparisons
  213.  */
  214. #define PROT_SAVE_SIZE 2000
  215.  
  216. #define MSP 50    /* maximum number of saved protos (protos on the proto queue) */
  217.  
  218. /* maximum number of out-transitions a state can have that we'll rummage
  219.  * around through the interior of the internal fast table looking for a
  220.  * spot for it
  221.  */
  222. #define MAX_XTIONS_FOR_FULL_INTERIOR_FIT 4
  223.  
  224. /* number that, if used to subscript an array, has a good chance of producing
  225.  * an error; should be small enough to fit into a short
  226.  */
  227. #define BAD_SUBSCRIPT -32767
  228.  
  229. /* absolute value of largest number that can be stored in a short, with a
  230.  * bit of slop thrown in for general paranoia.
  231.  */
  232. #define MAX_SHORT 32766
  233.  
  234.  
  235. /* Declarations for global variables. */
  236.  
  237. /* variables for symbol tables:
  238.  * sctbl - start-condition symbol table
  239.  * ndtbl - name-definition symbol table
  240.  * ccltab - character class text symbol table
  241.  */
  242.  
  243. struct hash_entry
  244.     {
  245.     struct hash_entry *prev, *next;
  246.     char *name;
  247.     char *str_val;
  248.     int int_val;
  249.     } ;
  250.  
  251. typedef struct hash_entry *hash_table[];
  252.  
  253. #define NAME_TABLE_HASH_SIZE 101
  254. #define START_COND_HASH_SIZE 101
  255. #define CCL_HASH_SIZE 101
  256.  
  257. extern struct hash_entry *ndtbl[NAME_TABLE_HASH_SIZE]; 
  258. extern struct hash_entry *sctbl[START_COND_HASH_SIZE];
  259. extern struct hash_entry *ccltab[CCL_HASH_SIZE];
  260.  
  261.  
  262. /* variables for flags:
  263.  * printstats - if true (-v), dump statistics
  264.  * syntaxerror - true if a syntax error has been found
  265.  * eofseen - true if we've seen an eof in the input file
  266.  * ddebug - if true (-d), make a "debug" scanner
  267.  * trace - if true (-T), trace processing
  268.  * spprdflt - if true (-s), suppress the default rule
  269.  * interactive - if true (-I), generate an interactive scanner
  270.  * caseins - if true (-i), generate a case-insensitive scanner
  271.  * useecs - if true (-ce flag), use equivalence classes
  272.  * fulltbl - if true (-cf flag), don't compress the DFA state table
  273.  * usemecs - if true (-cm flag), use meta-equivalence classes
  274.  * reject - if true (-r flag), generate tables for REJECT macro
  275.  * fullspd - if true (-F flag), use Jacobson method of table representation
  276.  * gen_line_dirs - if true (i.e., no -L flag), generate #line directives
  277.  */
  278.  
  279. extern int printstats, syntaxerror, eofseen, ddebug, trace, spprdflt;
  280. extern int interactive, caseins, useecs, fulltbl, usemecs, reject;
  281. extern int fullspd, gen_line_dirs;
  282.  
  283.  
  284. /* variables used in the flex input routines:
  285.  * datapos - characters on current output line
  286.  * dataline - number of contiguous lines of data in current data
  287.  *    statement.  Used to generate readable -f output
  288.  * skelfile - fd of the skeleton file
  289.  * yyin - input file
  290.  * temp_action_file - temporary file to hold actions
  291.  * action_file_name - name of the temporary file
  292.  * infilename - name of input file
  293.  * linenum - current input line number
  294.  */
  295.  
  296. extern int datapos, dataline, linenum;
  297. extern FILE *skelfile, *yyin, *temp_action_file;
  298. extern char *infilename;
  299. extern char *action_file_name;
  300.  
  301.  
  302. /* variables for stack of states having only one out-transition:
  303.  * onestate - state number
  304.  * onesym - transition symbol
  305.  * onenext - target state
  306.  * onedef - default base entry
  307.  * onesp - stack pointer
  308.  */
  309.  
  310. extern int onestate[ONE_STACK_SIZE], onesym[ONE_STACK_SIZE];
  311. extern int onenext[ONE_STACK_SIZE], onedef[ONE_STACK_SIZE], onesp;
  312.  
  313.  
  314. /* variables for nfa machine data:
  315.  * current_mns - current maximum on number of NFA states
  316.  * accnum - number of the last accepting state
  317.  * firstst - physically the first state of a fragment
  318.  * lastst - last physical state of fragment
  319.  * finalst - last logical state of fragment
  320.  * transchar - transition character
  321.  * trans1 - transition state
  322.  * trans2 - 2nd transition state for epsilons
  323.  * accptnum - accepting number
  324.  * lastnfa - last nfa state number created
  325.  */
  326.  
  327. extern int current_mns;
  328. extern int accnum, *firstst, *lastst, *finalst, *transchar;
  329. extern int *trans1, *trans2, *accptnum, lastnfa;
  330.  
  331.  
  332. /* variables for protos:
  333.  * numtemps - number of templates created
  334.  * numprots - number of protos created
  335.  * protprev - backlink to a more-recently used proto
  336.  * protnext - forward link to a less-recently used proto
  337.  * prottbl - base/def table entry for proto
  338.  * protcomst - common state of proto
  339.  * firstprot - number of the most recently used proto
  340.  * lastprot - number of the least recently used proto
  341.  * protsave contains the entire state array for protos
  342.  */
  343.  
  344. extern int numtemps, numprots, protprev[MSP], protnext[MSP], prottbl[MSP];
  345. extern int protcomst[MSP], firstprot, lastprot, protsave[PROT_SAVE_SIZE];
  346.  
  347.  
  348. /* variables for managing equivalence classes:
  349.  * numecs - number of equivalence classes
  350.  * nextecm - forward link of Equivalence Class members
  351.  * ecgroup - class number or backward link of EC members
  352.  * nummecs - number of meta-equivalence classes (used to compress
  353.  *   templates)
  354.  * tecfwd - forward link of meta-equivalence classes members
  355.  * tecbck - backward link of MEC's
  356.  */
  357.  
  358. extern int numecs, nextecm[CSIZE + 1], ecgroup[CSIZE + 1], nummecs;
  359. extern int tecfwd[CSIZE + 1], tecbck[CSIZE + 1];
  360.  
  361.  
  362. /* variables for start conditions:
  363.  * lastsc - last start condition created
  364.  * current_max_scs - current limit on number of start conditions
  365.  * scset - set of rules active in start condition
  366.  * scbol - set of rules active only at the beginning of line in a s.c.
  367.  * scxclu - true if start condition is exclusive
  368.  * actvsc - stack of active start conditions for the current rule
  369.  */
  370.  
  371. extern int lastsc, current_max_scs, *scset, *scbol, *scxclu, *actvsc;
  372.  
  373.  
  374. /* variables for dfa machine data:
  375.  * current_max_dfa_size - current maximum number of NFA states in DFA
  376.  * current_max_xpairs - current maximum number of non-template xtion pairs
  377.  * current_max_template_xpairs - current maximum number of template pairs
  378.  * current_max_dfas - current maximum number DFA states
  379.  * lastdfa - last dfa state number created
  380.  * nxt - state to enter upon reading character
  381.  * chk - check value to see if "nxt" applies
  382.  * tnxt - internal nxt table for templates
  383.  * base - offset into "nxt" for given state
  384.  * def - where to go if "chk" disallows "nxt" entry
  385.  * tblend - last "nxt/chk" table entry being used
  386.  * firstfree - first empty entry in "nxt/chk" table
  387.  * dss - nfa state set for each dfa
  388.  * dfasiz - size of nfa state set for each dfa
  389.  * dfaacc - accepting set for each dfa state (or accepting number, if
  390.  *    -r is not given)
  391.  * accsiz - size of accepting set for each dfa state
  392.  * dhash - dfa state hash value
  393.  * todo - queue of DFAs still to be processed
  394.  * todo_head - head of todo queue
  395.  * todo_next - next available entry on todo queue
  396.  * numas - number of DFA accepting states created; note that this
  397.  *    is not necessarily the same value as accnum, which is the analogous
  398.  *    value for the NFA
  399.  * numsnpairs - number of state/nextstate transition pairs
  400.  * jambase - position in base/def where the default jam table starts
  401.  * jamstate - state number corresponding to "jam" state
  402.  * end_of_buffer_state - end-of-buffer dfa state number
  403.  */
  404.  
  405. extern int current_max_dfa_size, current_max_xpairs;
  406. extern int current_max_template_xpairs, current_max_dfas;
  407. extern int lastdfa, lasttemp, *nxt, *chk, *tnxt;
  408. extern int *base, *def, tblend, firstfree, **dss, *dfasiz;
  409. extern union dfaacc_union
  410.     {
  411.     int *dfaacc_set;
  412.     int dfaacc_state;
  413.     } *dfaacc;
  414. extern int *accsiz, *dhash, *todo, todo_head, todo_next, numas;
  415. extern int numsnpairs, jambase, jamstate;
  416. extern int end_of_buffer_state;
  417.  
  418. /* variables for ccl information:
  419.  * lastccl - ccl index of the last created ccl
  420.  * current_maxccls - current limit on the maximum number of unique ccl's
  421.  * cclmap - maps a ccl index to its set pointer
  422.  * ccllen - gives the length of a ccl
  423.  * cclng - true for a given ccl if the ccl is negated
  424.  * cclreuse - counts how many times a ccl is re-used
  425.  * current_max_ccl_tbl_size - current limit on number of characters needed
  426.  *    to represent the unique ccl's
  427.  * ccltbl - holds the characters in each ccl - indexed by cclmap
  428.  */
  429.  
  430. extern int lastccl, current_maxccls, *cclmap, *ccllen, *cclng, cclreuse;
  431. extern int current_max_ccl_tbl_size;
  432. extern char *ccltbl;
  433.  
  434.  
  435. /* variables for miscellaneous information:
  436.  * starttime - real-time when we started
  437.  * endtime - real-time when we ended
  438.  * nmstr - last NAME scanned by the scanner
  439.  * sectnum - section number currently being parsed
  440.  * nummt - number of empty nxt/chk table entries
  441.  * hshcol - number of hash collisions detected by snstods
  442.  * dfaeql - number of times a newly created dfa was equal to an old one
  443.  * numeps - number of epsilon NFA states created
  444.  * eps2 - number of epsilon states which have 2 out-transitions
  445.  * num_reallocs - number of times it was necessary to realloc() a group
  446.  *          of arrays
  447.  * tmpuses - number of DFA states that chain to templates
  448.  * totnst - total number of NFA states used to make DFA states
  449.  * peakpairs - peak number of transition pairs we had to store internally
  450.  * numuniq - number of unique transitions
  451.  * numdup - number of duplicate transitions
  452.  * hshsave - number of hash collisions saved by checking number of states
  453.  */
  454.  
  455. extern char *starttime, *endtime, nmstr[MAXLINE];
  456. extern int sectnum, nummt, hshcol, dfaeql, numeps, eps2, num_reallocs;
  457. extern int tmpuses, totnst, peakpairs, numuniq, numdup, hshsave;
  458.  
  459. char *allocate_array(), *reallocate_array();
  460.  
  461. #define allocate_integer_array(size) \
  462.     (int *) allocate_array( size, sizeof( int ) )
  463.  
  464. #define reallocate_integer_array(array,size) \
  465.     (int *) reallocate_array( (char *) array, size, sizeof( int ) )
  466.  
  467. #define allocate_integer_pointer_array(size) \
  468.     (int **) allocate_array( size, sizeof( int * ) )
  469.  
  470. #define allocate_dfaacc_union(size) \
  471.     (union dfaacc_union *) \
  472.         allocate_array( size, sizeof( union dfaacc_union ) )
  473.  
  474. #define reallocate_integer_pointer_array(array,size) \
  475.     (int **) reallocate_array( (char *) array, size, sizeof( int * ) )
  476.  
  477. #define reallocate_dfaacc_union(array, size) \
  478.     (union dfaacc_union *)  reallocate_array( (char *) array, size, sizeof( union dfaacc_union ) )
  479.  
  480. #define allocate_character_array(size) allocate_array( size, sizeof( char ) )
  481.  
  482. #define reallocate_character_array(array,size) \
  483.     reallocate_array( array, size, sizeof( char ) )
  484.  
  485.  
  486. /* used to communicate between scanner and parser.  The type should really
  487.  * be YYSTYPE, but we can't easily get our hands on it.
  488.  */
  489. extern int yylval;
  490.