1 /* RCS $Id: percent.c,v 1.1.1.1 2000-09-22 15:33:25 hr Exp $
4 -- Handle building or %-rule meta-target nfa.
7 -- Builds the NFA used by dmake to match targets against %-meta
8 -- rule constructs. The NFA is built as a set of DFA's.
11 -- Dennis Vadura, dvadura@dmake.wticorp.com
14 -- http://dmake.wticorp.com/
17 -- Copyright (c) 1996,1997 by WTI Corp. All rights reserved.
19 -- This program is NOT free software; you can redistribute it and/or
20 -- modify it under the terms of the Software License Agreement Provided
21 -- in the file <distribution-root>/readme/license.txt.
24 -- Use cvs log to obtain detailed change logs.
29 static DFAPTR _build_dfa
ANSI((char *));
30 static char _shift_dfa
ANSI((DFAPTR
, char *));
34 #define START_PERCENT 1
39 static NFAPTR _nfa
= NIL( NFA
);
45 This routines runs all DFA's in parrallel and selects the one that best
46 matches the string. If no match then it returns NIL( DFA ) */
51 DFALINKPTR dfa_list
= NIL(DFALINK
);
53 DB_ENTER( "Match_dfa" );
54 DB_PRINT( "dfa", ("Matching %s", buf
) );
56 /* Run each of the DFA's on the input string in parallel, we terminate
57 * when all DFA's have either failed or ACCEPTED, if more than one DFA
58 * accepts we build a list of all accepting DFA's sorted on states with
59 * those matching in a higher numbered state heading the list. */
64 for( nfa
= _nfa
; nfa
!= NIL( NFA
); nfa
= nfa
->next
)
65 if( nfa
->status
!= (char) FAIL
&& nfa
->status
!= (char) ACCEPT
) {
67 nfa
->status
= _shift_dfa( nfa
->dfa
, buf
);
69 /* Construct the list of matching DFA's */
70 if( nfa
->status
== (char) ACCEPT
) {
73 TALLOC( dl
, 1, DFALINK
);
74 dl
->dl_meta
= nfa
->dfa
->node
;
75 dl
->dl_per
= DmSubStr( nfa
->dfa
->pstart
, nfa
->dfa
->pend
);
76 dl
->dl_state
= nfa
->dfa
->states
- nfa
->dfa
->c_state
;
78 if( dfa_list
== NIL(DFALINK
) )
81 DFALINKPTR tdli
= dfa_list
;
82 DFALINKPTR tdlp
= NIL(DFALINK
);
84 for( ; tdli
!= NIL(DFALINK
); tdli
= tdli
->dl_next
) {
85 if( dl
->dl_state
>= tdli
->dl_state
)
90 if( tdli
!= NIL(DFALINK
) ) {
95 if( tdlp
!= NIL(DFALINK
) ) {
103 DB_PRINT( "dfa", ("Matched [%s]", dl
->dl_meta
->CE_NAME
) );
111 for( nfa
= _nfa
; nfa
!= NIL( NFA
); nfa
= nfa
->next
) {
113 nfa
->dfa
->c_state
= nfa
->dfa
->states
;
116 DB_RETURN( dfa_list
);
123 This function is called to test for circularities in the DFA lists
124 constructed from %-meta targets. */
128 for( nfa
= _nfa
; nfa
!= NIL(NFA
); nfa
= nfa
->next
)
129 if( Test_circle( nfa
->dfa
->node
, FALSE
) )
130 Fatal( "Detected circular dependency in inference graph at [%s]",
131 nfa
->dfa
->node
->CE_NAME
);
138 Given name, build a DFA and add it to the NFA. The NFA is maintained as
139 a singly linked list of DFA's. */
145 nfa
->dfa
= _build_dfa(name
);
147 if( _nfa
!= NIL(NFA
) ) nfa
->next
= _nfa
;
156 Construct a dfa for the passed in cell name. The routine returns a struct
157 that represents a finite state machine that can recognize a regular
158 expression with exactly one '%' sign in it. The '%' symbol is used as a
159 wildcard character that will match anything except the character that
160 immediately follows it or NUL.
162 The Construction of DFA's is well known and can be found in Hopcroft and
163 Ullman or any other book discussing formal language theory.
164 A more practical treatise can be found in Compilers, Aho, Sethi and Ullman.
170 register STATEPTR sp
;
171 STATEPTR per_state
= NIL(STATE
);
173 int end_percent
=FALSE
;
175 nstates
= strlen(name
)+2;
177 /* Allocate a DFA node and the right number of states. */
179 TALLOC(sp
=dfa
->c_state
=dfa
->states
, nstates
, STATE
);
180 dfa
->node
= Def_cell( name
);
182 /* Now construct the state table for the DFA */
186 Error( "Only one %% allowed within a %%-meta target" );
189 sp
->action
= START_PERCENT
;
190 sp
->no_match
= sp
->match
= per_state
= sp
+1;
195 sp
->no_match
= per_state
;
197 if( *name
== '\0' ) {
199 sp
->match
= dfa
->states
;
202 sp
->action
= NO_ACTION
;
207 sp
->action
|= END_PERCENT
;
221 _shift_dfa( dfa
, data
)/*
222 =========================
223 Take a given dfa and advance it based on the current state, the shift
224 action in that state, and the current data value. */
228 register STATEPTR sp
= dfa
->c_state
;
231 /* Check if it is a START_PERCENT action if so then we need to save
232 * a pointer to the start of the string and advance to the next state. */
233 if( sp
->action
& START_PERCENT
) {
238 /* Now check if the current char matches the character expected in the
239 * current state. If it does then perform the specified action, otherwise
240 * either shift it or fail. We fail if the next state on no-match is
242 if( sp
->symbol
== c
) {
243 if( sp
->action
& END_PERCENT
) dfa
->pend
= data
;
244 if( sp
->action
& ACCEPT
) return(ACCEPT
);
245 dfa
->c_state
= sp
->match
;
247 else if( (dfa
->c_state
= sp
->no_match
) == NIL(STATE
) || !c
)
248 return((unsigned char) FAIL
);