home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 5 / FreshFish_July-August1994.bin / bbs / gnu / flex-2.4.6-src.lha / src / amiga / flex-2.4.6 / dfa.c < prev    next >
C/C++ Source or Header  |  1994-01-04  |  26KB  |  1,086 lines

  1. /* dfa - DFA construction routines */
  2.  
  3. /*-
  4.  * Copyright (c) 1990 The Regents of the University of California.
  5.  * All rights reserved.
  6.  *
  7.  * This code is derived from software contributed to Berkeley by
  8.  * Vern Paxson.
  9.  * 
  10.  * The United States Government has rights in this work pursuant
  11.  * to contract no. DE-AC03-76SF00098 between the United States
  12.  * Department of Energy and the University of California.
  13.  *
  14.  * Redistribution and use in source and binary forms are permitted provided
  15.  * that: (1) source distributions retain this entire copyright notice and
  16.  * comment, and (2) distributions including binaries display the following
  17.  * acknowledgement:  ``This product includes software developed by the
  18.  * University of California, Berkeley and its contributors'' in the
  19.  * documentation or other materials provided with the distribution and in
  20.  * all advertising materials mentioning features or use of this software.
  21.  * Neither the name of the University nor the names of its contributors may
  22.  * be used to endorse or promote products derived from this software without
  23.  * specific prior written permission.
  24.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  25.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  26.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  27.  */
  28.  
  29. /* $Header: dfa.c,v 1.2 94/01/04 14:33:16 vern Exp $ */
  30.  
  31. #include "flexdef.h"
  32.  
  33.  
  34. /* declare functions that have forward references */
  35.  
  36. void dump_associated_rules PROTO((FILE*, int));
  37. void dump_transitions PROTO((FILE*, int[]));
  38. void sympartition PROTO((int[], int, int[], int[]));
  39. int symfollowset PROTO((int[], int, int, int[]));
  40.  
  41.  
  42. /* check_for_backing_up - check a DFA state for backing up
  43.  *
  44.  * synopsis
  45.  *     void check_for_backing_up( int ds, int state[numecs] );
  46.  *
  47.  * ds is the number of the state to check and state[] is its out-transitions,
  48.  * indexed by equivalence class.
  49.  */
  50.  
  51. void check_for_backing_up( ds, state )
  52. int ds;
  53. int state[];
  54.     {
  55.     if ( (reject && ! dfaacc[ds].dfaacc_set) ||
  56.          (! reject && ! dfaacc[ds].dfaacc_state) )
  57.         { /* state is non-accepting */
  58.         ++num_backing_up;
  59.  
  60.         if ( backing_up_report )
  61.             {
  62.             fprintf( backing_up_file,
  63.                 "State #%d is non-accepting -\n", ds );
  64.  
  65.             /* identify the state */
  66.             dump_associated_rules( backing_up_file, ds );
  67.  
  68.             /* Now identify it further using the out- and
  69.              * jam-transitions.
  70.              */
  71.             dump_transitions( backing_up_file, state );
  72.  
  73.             putc( '\n', backing_up_file );
  74.             }
  75.         }
  76.     }
  77.  
  78.  
  79. /* check_trailing_context - check to see if NFA state set constitutes
  80.  *                          "dangerous" trailing context
  81.  *
  82.  * synopsis
  83.  *    void check_trailing_context( int nfa_states[num_states+1], int num_states,
  84.  *                int accset[nacc+1], int nacc );
  85.  *
  86.  * NOTES
  87.  *  Trailing context is "dangerous" if both the head and the trailing
  88.  *  part are of variable size \and/ there's a DFA state which contains
  89.  *  both an accepting state for the head part of the rule and NFA states
  90.  *  which occur after the beginning of the trailing context.
  91.  *
  92.  *  When such a rule is matched, it's impossible to tell if having been
  93.  *  in the DFA state indicates the beginning of the trailing context or
  94.  *  further-along scanning of the pattern.  In these cases, a warning
  95.  *  message is issued.
  96.  *
  97.  *    nfa_states[1 .. num_states] is the list of NFA states in the DFA.
  98.  *    accset[1 .. nacc] is the list of accepting numbers for the DFA state.
  99.  */
  100.  
  101. void check_trailing_context( nfa_states, num_states, accset, nacc )
  102. int *nfa_states, num_states;
  103. int *accset;
  104. register int nacc;
  105.     {
  106.     register int i, j;
  107.  
  108.     for ( i = 1; i <= num_states; ++i )
  109.         {
  110.         int ns = nfa_states[i];
  111.         register int type = state_type[ns];
  112.         register int ar = assoc_rule[ns];
  113.  
  114.         if ( type == STATE_NORMAL || rule_type[ar] != RULE_VARIABLE )
  115.             { /* do nothing */
  116.             }
  117.  
  118.         else if ( type == STATE_TRAILING_CONTEXT )
  119.             {
  120.             /* Potential trouble.  Scan set of accepting numbers
  121.              * for the one marking the end of the "head".  We
  122.              * assume that this looping will be fairly cheap
  123.              * since it's rare that an accepting number set
  124.              * is large.
  125.              */
  126.             for ( j = 1; j <= nacc; ++j )
  127.                 if ( accset[j] & YY_TRAILING_HEAD_MASK )
  128.                     {
  129.                     line_warning(
  130.                         "dangerous trailing context",
  131.                         rule_linenum[ar] );
  132.                     return;
  133.                     }
  134.             }
  135.         }
  136.     }
  137.  
  138.  
  139. /* dump_associated_rules - list the rules associated with a DFA state
  140.  *
  141.  * Goes through the set of NFA states associated with the DFA and
  142.  * extracts the first MAX_ASSOC_RULES unique rules, sorts them,
  143.  * and writes a report to the given file.
  144.  */
  145.  
  146. void dump_associated_rules( file, ds )
  147. FILE *file;
  148. int ds;
  149.     {
  150.     register int i, j;
  151.     register int num_associated_rules = 0;
  152.     int rule_set[MAX_ASSOC_RULES + 1];
  153.     int *dset = dss[ds];
  154.     int size = dfasiz[ds];
  155.  
  156.     for ( i = 1; i <= size; ++i )
  157.         {
  158.         register int rule_num = rule_linenum[assoc_rule[dset[i]]];
  159.  
  160.         for ( j = 1; j <= num_associated_rules; ++j )
  161.             if ( rule_num == rule_set[j] )
  162.                 break;
  163.  
  164.         if ( j > num_associated_rules )
  165.             { /* new rule */
  166.             if ( num_associated_rules < MAX_ASSOC_RULES )
  167.                 rule_set[++num_associated_rules] = rule_num;
  168.             }
  169.         }
  170.  
  171.     bubble( rule_set, num_associated_rules );
  172.  
  173.     fprintf( file, " associated rule line numbers:" );
  174.  
  175.     for ( i = 1; i <= num_associated_rules; ++i )
  176.         {
  177.         if ( i % 8 == 1 )
  178.             putc( '\n', file );
  179.  
  180.         fprintf( file, "\t%d", rule_set[i] );
  181.         }
  182.  
  183.     putc( '\n', file );
  184.     }
  185.  
  186.  
  187. /* dump_transitions - list the transitions associated with a DFA state
  188.  *
  189.  * synopsis
  190.  *     dump_transitions( FILE *file, int state[numecs] );
  191.  *
  192.  * Goes through the set of out-transitions and lists them in human-readable
  193.  * form (i.e., not as equivalence classes); also lists jam transitions
  194.  * (i.e., all those which are not out-transitions, plus EOF).  The dump
  195.  * is done to the given file.
  196.  */
  197.  
  198. void dump_transitions( file, state )
  199. FILE *file;
  200. int state[];
  201.     {
  202.     register int i, ec;
  203.     int out_char_set[CSIZE];
  204.  
  205.     for ( i = 0; i < csize; ++i )
  206.         {
  207.         ec = ABS( ecgroup[i] );
  208.         out_char_set[i] = state[ec];
  209.         }
  210.  
  211.     fprintf( file, " out-transitions: " );
  212.  
  213.     list_character_set( file, out_char_set );
  214.  
  215.     /* now invert the members of the set to get the jam transitions */
  216.     for ( i = 0; i < csize; ++i )
  217.         out_char_set[i] = ! out_char_set[i];
  218.  
  219.     fprintf( file, "\n jam-transitions: EOF " );
  220.  
  221.     list_character_set( file, out_char_set );
  222.  
  223.     putc( '\n', file );
  224.     }
  225.  
  226.  
  227. /* epsclosure - construct the epsilon closure of a set of ndfa states
  228.  *
  229.  * synopsis
  230.  *    int *epsclosure( int t[num_states], int *numstates_addr,
  231.  *            int accset[num_rules+1], int *nacc_addr,
  232.  *            int *hashval_addr );
  233.  *
  234.  * NOTES
  235.  *  The epsilon closure is the set of all states reachable by an arbitrary
  236.  *  number of epsilon transitions, which themselves do not have epsilon
  237.  *  transitions going out, unioned with the set of states which have non-null
  238.  *  accepting numbers.  t is an array of size numstates of nfa state numbers.
  239.  *  Upon return, t holds the epsilon closure and *numstates_addr is updated.
  240.  *  accset holds a list of the accepting numbers, and the size of accset is
  241.  *  given by *nacc_addr.  t may be subjected to reallocation if it is not
  242.  *  large enough to hold the epsilon closure.
  243.  *
  244.  *  hashval is the hash value for the dfa corresponding to the state set.
  245.  */
  246.  
  247. int *epsclosure( t, ns_addr, accset, nacc_addr, hv_addr )
  248. int *t, *ns_addr, accset[], *nacc_addr, *hv_addr;
  249.     {
  250.     register int stkpos, ns, tsp;
  251.     int numstates = *ns_addr, nacc, hashval, transsym, nfaccnum;
  252.     int stkend, nstate;
  253.     static int did_stk_init = false, *stk; 
  254.  
  255. #define MARK_STATE(state) \
  256. trans1[state] = trans1[state] - MARKER_DIFFERENCE;
  257.  
  258. #define IS_MARKED(state) (trans1[state] < 0)
  259.  
  260. #define UNMARK_STATE(state) \
  261. trans1[state] = trans1[state] + MARKER_DIFFERENCE;
  262.  
  263. #define CHECK_ACCEPT(state) \
  264. { \
  265. nfaccnum = accptnum[state]; \
  266. if ( nfaccnum != NIL ) \
  267. accset[++nacc] = nfaccnum; \
  268. }
  269.  
  270. #define DO_REALLOCATION \
  271. { \
  272. current_max_dfa_size += MAX_DFA_SIZE_INCREMENT; \
  273. ++num_reallocs; \
  274. t = reallocate_integer_array( t, current_max_dfa_size ); \
  275. stk = reallocate_integer_array( stk, current_max_dfa_size ); \
  276. } \
  277.  
  278. #define PUT_ON_STACK(state) \
  279. { \
  280. if ( ++stkend >= current_max_dfa_size ) \
  281. DO_REALLOCATION \
  282. stk[stkend] = state; \
  283. MARK_STATE(state) \
  284. }
  285.  
  286. #define ADD_STATE(state