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 >
Wrap
C/C++ Source or Header
|
1994-01-04
|
26KB
|
1,086 lines
/* dfa - DFA construction routines */
/*-
* Copyright (c) 1990 The Regents of the University of California.
* All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Vern Paxson.
*
* The United States Government has rights in this work pursuant
* to contract no. DE-AC03-76SF00098 between the United States
* Department of Energy and the University of California.
*
* Redistribution and use in source and binary forms are permitted provided
* that: (1) source distributions retain this entire copyright notice and
* comment, and (2) distributions including binaries display the following
* acknowledgement: ``This product includes software developed by the
* University of California, Berkeley and its contributors'' in the
* documentation or other materials provided with the distribution and in
* all advertising materials mentioning features or use of this software.
* Neither the name of the University nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
/* $Header: dfa.c,v 1.2 94/01/04 14:33:16 vern Exp $ */
#include "flexdef.h"
/* declare functions that have forward references */
void dump_associated_rules PROTO((FILE*, int));
void dump_transitions PROTO((FILE*, int[]));
void sympartition PROTO((int[], int, int[], int[]));
int symfollowset PROTO((int[], int, int, int[]));
/* check_for_backing_up - check a DFA state for backing up
*
* synopsis
* void check_for_backing_up( int ds, int state[numecs] );
*
* ds is the number of the state to check and state[] is its out-transitions,
* indexed by equivalence class.
*/
void check_for_backing_up( ds, state )
int ds;
int state[];
{
if ( (reject && ! dfaacc[ds].dfaacc_set) ||
(! reject && ! dfaacc[ds].dfaacc_state) )
{ /* state is non-accepting */
++num_backing_up;
if ( backing_up_report )
{
fprintf( backing_up_file,
"State #%d is non-accepting -\n", ds );
/* identify the state */
dump_associated_rules( backing_up_file, ds );
/* Now identify it further using the out- and
* jam-transitions.
*/
dump_transitions( backing_up_file, state );
putc( '\n', backing_up_file );
}
}
}
/* check_trailing_context - check to see if NFA state set constitutes
* "dangerous" trailing context
*
* synopsis
* void check_trailing_context( int nfa_states[num_states+1], int num_states,
* int accset[nacc+1], int nacc );
*
* NOTES
* Trailing context is "dangerous" if both the head and the trailing
* part are of variable size \and/ there's a DFA state which contains
* both an accepting state for the head part of the rule and NFA states
* which occur after the beginning of the trailing context.
*
* When such a rule is matched, it's impossible to tell if having been
* in the DFA state indicates the beginning of the trailing context or
* further-along scanning of the pattern. In these cases, a warning
* message is issued.
*
* nfa_states[1 .. num_states] is the list of NFA states in the DFA.
* accset[1 .. nacc] is the list of accepting numbers for the DFA state.
*/
void check_trailing_context( nfa_states, num_states, accset, nacc )
int *nfa_states, num_states;
int *accset;
register int nacc;
{
register int i, j;
for ( i = 1; i <= num_states; ++i )
{
int ns = nfa_states[i];
register int type = state_type[ns];
register int ar = assoc_rule[ns];
if ( type == STATE_NORMAL || rule_type[ar] != RULE_VARIABLE )
{ /* do nothing */
}
else if ( type == STATE_TRAILING_CONTEXT )
{
/* Potential trouble. Scan set of accepting numbers
* for the one marking the end of the "head". We
* assume that this looping will be fairly cheap
* since it's rare that an accepting number set
* is large.
*/
for ( j = 1; j <= nacc; ++j )
if ( accset[j] & YY_TRAILING_HEAD_MASK )
{
line_warning(
"dangerous trailing context",
rule_linenum[ar] );
return;
}
}
}
}
/* dump_associated_rules - list the rules associated with a DFA state
*
* Goes through the set of NFA states associated with the DFA and
* extracts the first MAX_ASSOC_RULES unique rules, sorts them,
* and writes a report to the given file.
*/
void dump_associated_rules( file, ds )
FILE *file;
int ds;
{
register int i, j;
register int num_associated_rules = 0;
int rule_set[MAX_ASSOC_RULES + 1];
int *dset = dss[ds];
int size = dfasiz[ds];
for ( i = 1; i <= size; ++i )
{
register int rule_num = rule_linenum[assoc_rule[dset[i]]];
for ( j = 1; j <= num_associated_rules; ++j )
if ( rule_num == rule_set[j] )
break;
if ( j > num_associated_rules )
{ /* new rule */
if ( num_associated_rules < MAX_ASSOC_RULES )
rule_set[++num_associated_rules] = rule_num;
}
}
bubble( rule_set, num_associated_rules );
fprintf( file, " associated rule line numbers:" );
for ( i = 1; i <= num_associated_rules; ++i )
{
if ( i % 8 == 1 )
putc( '\n', file );
fprintf( file, "\t%d", rule_set[i] );
}
putc( '\n', file );
}
/* dump_transitions - list the transitions associated with a DFA state
*
* synopsis
* dump_transitions( FILE *file, int state[numecs] );
*
* Goes through the set of out-transitions and lists them in human-readable
* form (i.e., not as equivalence classes); also lists jam transitions
* (i.e., all those which are not out-transitions, plus EOF). The dump
* is done to the given file.
*/
void dump_transitions( file, state )
FILE *file;
int state[];
{
register int i, ec;
int out_char_set[CSIZE];
for ( i = 0; i < csize; ++i )
{
ec = ABS( ecgroup[i] );
out_char_set[i] = state[ec];
}
fprintf( file, " out-transitions: " );
list_character_set( file, out_char_set );
/* now invert the members of the set to get the jam transitions */
for ( i = 0; i < csize; ++i )
out_char_set[i] = ! out_char_set[i];
fprintf( file, "\n jam-transitions: EOF " );
list_character_set( file, out_char_set );
putc( '\n', file );
}
/* epsclosure - construct the epsilon closure of a set of ndfa states
*
* synopsis
* int *epsclosure( int t[num_states], int *numstates_addr,
* int accset[num_rules+1], int *nacc_addr,
* int *hashval_addr );
*
* NOTES
* The epsilon closure is the set of all states reachable by an arbitrary
* number of epsilon transitions, which themselves do not have epsilon
* transitions going out, unioned with the set of states which have non-null
* accepting numbers. t is an array of size numstates of nfa state numbers.
* Upon return, t holds the epsilon closure and *numstates_addr is updated.
* accset holds a list of the accepting numbers, and the size of accset is
* given by *nacc_addr. t may be subjected to reallocation if it is not
* large enough to hold the epsilon closure.
*
* hashval is the hash value for the dfa corresponding to the state set.
*/
int *epsclosure( t, ns_addr, accset, nacc_addr, hv_addr )
int *t, *ns_addr, accset[], *nacc_addr, *hv_addr;
{
register int stkpos, ns, tsp;
int numstates = *ns_addr, nacc, hashval, transsym, nfaccnum;
int stkend, nstate;
static int did_stk_init = false, *stk;
#define MARK_STATE(state) \
trans1[state] = trans1[state] - MARKER_DIFFERENCE;
#define IS_MARKED(state) (trans1[state] < 0)
#define UNMARK_STATE(state) \
trans1[state] = trans1[state] + MARKER_DIFFERENCE;
#define CHECK_ACCEPT(state) \
{ \
nfaccnum = accptnum[state]; \
if ( nfaccnum != NIL ) \
accset[++nacc] = nfaccnum; \
}
#define DO_REALLOCATION \
{ \
current_max_dfa_size += MAX_DFA_SIZE_INCREMENT; \
++num_reallocs; \
t = reallocate_integer_array( t, current_max_dfa_size ); \
stk = reallocate_integer_array( stk, current_max_dfa_size ); \
} \
#define PUT_ON_STACK(state) \
{ \
if ( ++stkend >= current_max_dfa_size ) \
DO_REALLOCATION \
stk[stkend] = state; \
MARK_STATE(state) \
}
#define ADD_STATE(state