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