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