2 * Copyright 2002 Adrian Thurston <thurston@cs.queensu.ca>
5 /* This file is part of Ragel.
7 * Ragel is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * Ragel is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Ragel; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 /* Construct a mark index for a specified number of states. Must new up
30 * an array that is states^2 in size. */
31 MarkIndex::MarkIndex( int states
) : numStates(states
)
33 /* Total pairs is states^2. Actually only use half of these, but we allocate
34 * them all to make indexing into the array easier. */
35 int total
= states
* states
;
37 /* New up chars so that individual DListEl constructors are
38 * not called. Zero out the mem manually. */
39 array
= new bool[total
];
40 memset( array
, 0, sizeof(bool) * total
);
43 /* Free the array used to store state pairs. */
44 MarkIndex::~MarkIndex()
49 /* Mark a pair of states. States are specified by their number. The
50 * marked states are moved from the unmarked list to the marked list. */
51 void MarkIndex::markPair(int state1
, int state2
)
53 int pos
= ( state1
>= state2
) ?
54 ( state1
* numStates
) + state2
:
55 ( state2
* numStates
) + state1
;
60 /* Returns true if the pair of states are marked. Returns false otherwise.
61 * Ordering of states given does not matter. */
62 bool MarkIndex::isPairMarked(int state1
, int state2
)
64 int pos
= ( state1
>= state2
) ?
65 ( state1
* numStates
) + state2
:
66 ( state2
* numStates
) + state1
;
71 /* Create a new fsm state. State has not out transitions or in transitions, not
72 * out out transition data and not number. */
75 /* No out or in transitions. */
82 /* No entry points, or epsilon trans. */
89 /* No transitions in from other states. */
92 /* Only used during merging. Normally null. */
96 /* No state identification bits. */
99 /* No Priority data. */
102 /* No Action data. */
103 toStateActionTable(),
104 fromStateActionTable(),
112 /* Copy everything except actual the transitions. That is left up to the
113 * FsmAp copy constructor. */
114 StateAp::StateAp(const StateAp
&other
)
116 /* All lists are cleared. They will be filled in when the
117 * individual transitions are duplicated and attached. */
121 /* Set this using the original state's eofTarget. It will get mapped back
122 * to the new machine in the Fsm copy constructor. */
123 eofTarget(other
.eofTarget
),
125 /* Duplicate the entry id set and epsilon transitions. These
126 * are sets of integers and as such need no fixing. */
127 entryIds(other
.entryIds
),
128 epsilonTrans(other
.epsilonTrans
),
130 /* Copy in the elements of the conditions. */
131 stateCondList( other
.stateCondList
),
133 /* No transitions in from other states. */
136 /* This is only used during merging. Normally null. */
140 /* Fsm state data. */
141 stateBits(other
.stateBits
),
143 /* Copy in priority data. */
144 outPriorTable(other
.outPriorTable
),
146 /* Copy in action data. */
147 toStateActionTable(other
.toStateActionTable
),
148 fromStateActionTable(other
.fromStateActionTable
),
149 outActionTable(other
.outActionTable
),
150 outCondSet(other
.outCondSet
),
151 errActionTable(other
.errActionTable
),
152 eofActionTable(other
.eofActionTable
)
154 /* Duplicate all the transitions. */
155 for ( TransList::Iter trans
= other
.outList
; trans
.lte(); trans
++ ) {
156 /* Dupicate and store the orginal target in the transition. This will
157 * be corrected once all the states have been created. */
158 TransAp
*newTrans
= new TransAp(*trans
);
159 newTrans
->toState
= trans
->toState
;
160 outList
.append( newTrans
);
164 /* If there is a state dict element, then delete it. Everything else is left
165 * up to the FsmGraph destructor. */
168 if ( stateDictEl
!= 0 )
172 /* Compare two states using pointers to the states. With the approximate
173 * compare, the idea is that if the compare finds them the same, they can
174 * immediately be merged. */
175 int ApproxCompare::compare( const StateAp
*state1
, const StateAp
*state2
)
179 /* Test final state status. */
180 if ( (state1
->stateBits
& STB_ISFINAL
) && !(state2
->stateBits
& STB_ISFINAL
) )
182 else if ( !(state1
->stateBits
& STB_ISFINAL
) && (state2
->stateBits
& STB_ISFINAL
) )
185 /* Test epsilon transition sets. */
186 compareRes
= CmpEpsilonTrans::compare( state1
->epsilonTrans
,
187 state2
->epsilonTrans
);
188 if ( compareRes
!= 0 )
191 /* Compare the out transitions. */
192 compareRes
= FsmAp::compareStateData( state1
, state2
);
193 if ( compareRes
!= 0 )
196 /* Use a pair iterator to get the transition pairs. */
197 PairIter
<TransAp
> outPair( state1
->outList
.head
, state2
->outList
.head
);
198 for ( ; !outPair
.end(); outPair
++ ) {
199 switch ( outPair
.userState
) {
202 compareRes
= FsmAp::compareFullPtr( outPair
.s1Tel
.trans
, 0 );
203 if ( compareRes
!= 0 )
208 compareRes
= FsmAp::compareFullPtr( 0, outPair
.s2Tel
.trans
);
209 if ( compareRes
!= 0 )
214 compareRes
= FsmAp::compareFullPtr(
215 outPair
.s1Tel
.trans
, outPair
.s2Tel
.trans
);
216 if ( compareRes
!= 0 )
226 /* Check EOF targets. */
227 if ( state1
->eofTarget
< state2
->eofTarget
)
229 else if ( state1
->eofTarget
> state2
->eofTarget
)
232 /* Got through the entire state comparison, deem them equal. */
236 /* Compare class used in the initial partition. */
237 int InitPartitionCompare::compare( const StateAp
*state1
, const StateAp
*state2
)
241 /* Test final state status. */
242 if ( (state1
->stateBits
& STB_ISFINAL
) && !(state2
->stateBits
& STB_ISFINAL
) )
244 else if ( !(state1
->stateBits
& STB_ISFINAL
) && (state2
->stateBits
& STB_ISFINAL
) )
247 /* Test epsilon transition sets. */
248 compareRes
= CmpEpsilonTrans::compare( state1
->epsilonTrans
,
249 state2
->epsilonTrans
);
250 if ( compareRes
!= 0 )
253 /* Compare the out transitions. */
254 compareRes
= FsmAp::compareStateData( state1
, state2
);
255 if ( compareRes
!= 0 )
258 /* Use a pair iterator to test the condition pairs. */
259 PairIter
<StateCond
> condPair( state1
->stateCondList
.head
, state2
->stateCondList
.head
);
260 for ( ; !condPair
.end(); condPair
++ ) {
261 switch ( condPair
.userState
) {
268 CondSpace
*condSpace1
= condPair
.s1Tel
.trans
->condSpace
;
269 CondSpace
*condSpace2
= condPair
.s2Tel
.trans
->condSpace
;
270 if ( condSpace1
< condSpace2
)
272 else if ( condSpace1
> condSpace2
)
282 /* Use a pair iterator to test the transition pairs. */
283 PairIter
<TransAp
> outPair( state1
->outList
.head
, state2
->outList
.head
);
284 for ( ; !outPair
.end(); outPair
++ ) {
285 switch ( outPair
.userState
) {
288 compareRes
= FsmAp::compareDataPtr( outPair
.s1Tel
.trans
, 0 );
289 if ( compareRes
!= 0 )
294 compareRes
= FsmAp::compareDataPtr( 0, outPair
.s2Tel
.trans
);
295 if ( compareRes
!= 0 )
300 compareRes
= FsmAp::compareDataPtr(
301 outPair
.s1Tel
.trans
, outPair
.s2Tel
.trans
);
302 if ( compareRes
!= 0 )
315 /* Compare class for the sort that does the partitioning. */
316 int PartitionCompare::compare( const StateAp
*state1
, const StateAp
*state2
)
320 /* Use a pair iterator to get the transition pairs. */
321 PairIter
<TransAp
> outPair( state1
->outList
.head
, state2
->outList
.head
);
322 for ( ; !outPair
.end(); outPair
++ ) {
323 switch ( outPair
.userState
) {
326 compareRes
= FsmAp::comparePartPtr( outPair
.s1Tel
.trans
, 0 );
327 if ( compareRes
!= 0 )
332 compareRes
= FsmAp::comparePartPtr( 0, outPair
.s2Tel
.trans
);
333 if ( compareRes
!= 0 )
338 compareRes
= FsmAp::comparePartPtr(
339 outPair
.s1Tel
.trans
, outPair
.s2Tel
.trans
);
340 if ( compareRes
!= 0 )
350 /* Test eof targets. */
351 if ( state1
->eofTarget
== 0 && state2
->eofTarget
!= 0 )
353 else if ( state1
->eofTarget
!= 0 && state2
->eofTarget
== 0 )
355 else if ( state1
->eofTarget
!= 0 ) {
356 /* Both eof targets are set. */
357 compareRes
= CmpOrd
< MinPartition
* >::compare(
358 state1
->eofTarget
->alg
.partition
, state2
->eofTarget
->alg
.partition
);
359 if ( compareRes
!= 0 )
366 /* Compare class for the sort that does the partitioning. */
367 bool MarkCompare::shouldMark( MarkIndex
&markIndex
, const StateAp
*state1
,
368 const StateAp
*state2
)
370 /* Use a pair iterator to get the transition pairs. */
371 PairIter
<TransAp
> outPair( state1
->outList
.head
, state2
->outList
.head
);
372 for ( ; !outPair
.end(); outPair
++ ) {
373 switch ( outPair
.userState
) {
376 if ( FsmAp::shouldMarkPtr( markIndex
, outPair
.s1Tel
.trans
, 0 ) )
381 if ( FsmAp::shouldMarkPtr( markIndex
, 0, outPair
.s2Tel
.trans
) )
386 if ( FsmAp::shouldMarkPtr( markIndex
,
387 outPair
.s1Tel
.trans
, outPair
.s2Tel
.trans
) )
401 * Transition Comparison.
404 /* Compare target partitions. Either pointer may be null. */
405 int FsmAp::comparePartPtr( TransAp
*trans1
, TransAp
*trans2
)
408 /* If trans1 is set then so should trans2. The initial partitioning
409 * guarantees this for us. */
410 if ( trans1
->toState
== 0 && trans2
->toState
!= 0 )
412 else if ( trans1
->toState
!= 0 && trans2
->toState
== 0 )
414 else if ( trans1
->toState
!= 0 ) {
415 /* Both of targets are set. */
416 return CmpOrd
< MinPartition
* >::compare(
417 trans1
->toState
->alg
.partition
, trans2
->toState
->alg
.partition
);
424 /* Compares two transition pointers according to priority and functions.
425 * Either pointer may be null. Does not consider to state or from state. */
426 int FsmAp::compareDataPtr( TransAp
*trans1
, TransAp
*trans2
)
428 if ( trans1
== 0 && trans2
!= 0 )
430 else if ( trans1
!= 0 && trans2
== 0 )
432 else if ( trans1
!= 0 ) {
433 /* Both of the transition pointers are set. */
434 int compareRes
= compareTransData( trans1
, trans2
);
435 if ( compareRes
!= 0 )
441 /* Compares two transitions according to target state, priority and functions.
442 * Does not consider from state. Either of the pointers may be null. */
443 int FsmAp::compareFullPtr( TransAp
*trans1
, TransAp
*trans2
)
445 if ( (trans1
!= 0) ^ (trans2
!= 0) ) {
446 /* Exactly one of the transitions is set. */
452 else if ( trans1
!= 0 ) {
453 /* Both of the transition pointers are set. Test target state,
454 * priority and funcs. */
455 if ( trans1
->toState
< trans2
->toState
)
457 else if ( trans1
->toState
> trans2
->toState
)
459 else if ( trans1
->toState
!= 0 ) {
460 /* Test transition data. */
461 int compareRes
= compareTransData( trans1
, trans2
);
462 if ( compareRes
!= 0 )
470 bool FsmAp::shouldMarkPtr( MarkIndex
&markIndex
, TransAp
*trans1
,
473 if ( (trans1
!= 0) ^ (trans2
!= 0) ) {
474 /* Exactly one of the transitions is set. The initial mark round
475 * should rule out this case. */
478 else if ( trans1
!= 0 ) {
479 /* Both of the transitions are set. If the target pair is marked, then
480 * the pair we are considering gets marked. */
481 return markIndex
.isPairMarked( trans1
->toState
->alg
.stateNum
,
482 trans2
->toState
->alg
.stateNum
);
485 /* Neither of the transitiosn are set. */