2 * Copyright 2001-2004 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
35 #include "sbsttable.h"
41 /* Flags that control merging. */
42 #define SB_GRAPH1 0x01
43 #define SB_GRAPH2 0x02
45 #define SB_ISFINAL 0x04
46 #define SB_ISMARKED 0x08
52 struct LongestMatchPart;
54 /* State list element for unambiguous access to list element. */
60 /* This is the marked index for a state pair. Used in minimization. It keeps
61 * track of whether or not the state pair is marked. */
64 MarkIndex(int states);
67 void markPair(int state1, int state2);
68 bool isPairMarked(int state1, int state2);
75 extern KeyOps *keyOps;
77 /* Transistion Action Element. */
78 typedef SBstMapEl< int, Action* > ActionTableEl;
80 /* Transition Action Table. */
82 : public SBstMap< int, Action*, CmpOrd<int> >
84 void setAction( int ordering, Action *action );
85 void setActions( int *orderings, Action **actions, int nActs );
86 void setActions( const ActionTable &other );
88 bool hasAction( Action *action );
91 typedef SBstSet< Action*, CmpOrd<Action*> > ActionSet;
92 typedef CmpSTable< Action*, CmpOrd<Action*> > CmpActionSet;
94 /* Transistion Action Element. */
95 typedef SBstMapEl< int, LongestMatchPart* > LmActionTableEl;
97 /* Transition Action Table. */
99 : public SBstMap< int, LongestMatchPart*, CmpOrd<int> >
101 void setAction( int ordering, LongestMatchPart *action );
102 void setActions( const LmActionTable &other );
105 /* Compare of a whole action table element (key & value). */
106 struct CmpActionTableEl
108 static int compare( const ActionTableEl &action1,
109 const ActionTableEl &action2 )
111 if ( action1.key < action2.key )
113 else if ( action1.key > action2.key )
115 else if ( action1.value < action2.value )
117 else if ( action1.value > action2.value )
123 /* Compare for ActionTable. */
124 typedef CmpSTable< ActionTableEl, CmpActionTableEl > CmpActionTable;
126 /* Compare of a whole lm action table element (key & value). */
127 struct CmpLmActionTableEl
129 static int compare( const LmActionTableEl &lmAction1,
130 const LmActionTableEl &lmAction2 )
132 if ( lmAction1.key < lmAction2.key )
134 else if ( lmAction1.key > lmAction2.key )
136 else if ( lmAction1.value < lmAction2.value )
138 else if ( lmAction1.value > lmAction2.value )
144 /* Compare for ActionTable. */
145 typedef CmpSTable< LmActionTableEl, CmpLmActionTableEl > CmpLmActionTable;
147 /* Action table element for error action tables. Adds the encoding of transfer
149 struct ErrActionTableEl
151 ErrActionTableEl( Action *action, int ordering, int transferPoint )
152 : ordering(ordering), action(action), transferPoint(transferPoint) { }
154 /* Ordering and id of the action embedding. */
158 /* Id of point of transfere from Error action table to transtions and
162 int getKey() const { return ordering; }
165 struct ErrActionTable
166 : public SBstTable< ErrActionTableEl, int, CmpOrd<int> >
168 void setAction( int ordering, Action *action, int transferPoint );
169 void setActions( const ErrActionTable &other );
172 /* Compare of an error action table element (key & value). */
173 struct CmpErrActionTableEl
175 static int compare( const ErrActionTableEl &action1,
176 const ErrActionTableEl &action2 )
178 if ( action1.ordering < action2.ordering )
180 else if ( action1.ordering > action2.ordering )
182 else if ( action1.action < action2.action )
184 else if ( action1.action > action2.action )
186 else if ( action1.transferPoint < action2.transferPoint )
188 else if ( action1.transferPoint > action2.transferPoint )
194 /* Compare for ErrActionTable. */
195 typedef CmpSTable< ErrActionTableEl, CmpErrActionTableEl > CmpErrActionTable;
198 /* Descibe a priority, shared among PriorEls.
199 * Has key and whether or not used. */
206 /* Element in the arrays of priorities for transitions and arrays. Ordering is
207 * unique among instantiations of machines, desc is shared. */
210 PriorEl( int ordering, PriorDesc *desc )
211 : ordering(ordering), desc(desc) { }
217 /* Compare priority elements, which are ordered by the priority descriptor
221 static inline int compare( const PriorEl &pel1, const PriorEl &pel2 )
223 if ( pel1.desc->key < pel2.desc->key )
225 else if ( pel1.desc->key > pel2.desc->key )
233 /* Priority Table. */
235 : public SBstSet< PriorEl, PriorElCmp >
237 void setPrior( int ordering, PriorDesc *desc );
238 void setPriors( const PriorTable &other );
241 /* Compare of prior table elements for distinguising state data. */
244 static inline int compare( const PriorEl &pel1, const PriorEl &pel2 )
246 if ( pel1.desc < pel2.desc )
248 else if ( pel1.desc > pel2.desc )
250 else if ( pel1.ordering < pel2.ordering )
252 else if ( pel1.ordering > pel2.ordering )
258 /* Compare of PriorTable distinguising state data. Using a compare of the
259 * pointers is a little more strict than it needs be. It requires that
260 * prioritiy tables have the exact same set of priority assignment operators
261 * (from the input lang) to be considered equal.
263 * Really only key-value pairs need be tested and ordering be merged. However
264 * this would require that in the fuseing of states, priority descriptors be
265 * chosen for the new fused state based on priority. Since the out transition
266 * lists and ranges aren't necessarily going to line up, this is more work for
267 * little gain. Final compression resets all priorities first, so this would
268 * only be useful for compression at every operator, which is only an
269 * undocumented test feature.
271 typedef CmpSTable<PriorEl, CmpPriorEl> CmpPriorTable;
273 /* Plain action list that imposes no ordering. */
274 typedef Vector<int> TransFuncList;
276 /* Comparison for TransFuncList. */
277 typedef CmpTable< int, CmpOrd<int> > TransFuncListCompare;
279 /* Transition class that implements actions and priorities. */
282 TransAp() : fromState(0), toState(0) {}
283 TransAp( const TransAp &other ) :
284 lowKey(other.lowKey),
285 highKey(other.highKey),
286 fromState(0), toState(0),
287 actionTable(other.actionTable),
288 priorTable(other.priorTable)
290 assert( lmActionTable.length() == 0 && other.lmActionTable.length() == 0 );
297 /* Pointers for outlist. */
298 TransAp *prev, *next;
300 /* Pointers for in-list. */
301 TransAp *ilprev, *ilnext;
303 /* The function table and priority for the transition. */
304 ActionTable actionTable;
305 PriorTable priorTable;
307 LmActionTable lmActionTable;
310 /* In transition list. Like DList except only has head pointers, which is all
311 * that is required. Insertion and deletion is handled by the graph. This
312 * class provides the iterator of a single list. */
315 TransInList() : head(0) { }
321 /* Default construct. */
324 /* Construct, assign from a list. */
325 Iter( const TransInList &il ) : ptr(il.head) { }
326 Iter &operator=( const TransInList &dl ) { ptr = dl.head; return *this; }
329 bool lte() const { return ptr != 0; }
330 bool end() const { return ptr == 0; }
332 /* At the first, last element. */
333 bool first() const { return ptr && ptr->ilprev == 0; }
334 bool last() const { return ptr && ptr->ilnext == 0; }
336 /* Cast, dereference, arrow ops. */
337 operator TransAp*() const { return ptr; }
338 TransAp &operator *() const { return *ptr; }
339 TransAp *operator->() const { return ptr; }
341 /* Increment, decrement. */
342 inline void operator++(int) { ptr = ptr->ilnext; }
343 inline void operator--(int) { ptr = ptr->ilprev; }
345 /* The iterator is simply a pointer. */
350 typedef DList<TransAp> TransList;
352 /* Set of states, list of states. */
353 typedef BstSet<StateAp*> StateSet;
354 typedef DList<StateAp> StateList;
356 /* A element in a state dict. */
359 public AvlTreeEl<StateDictEl>
361 StateDictEl(const StateSet &stateSet)
362 : stateSet(stateSet) { }
364 const StateSet &getKey() { return stateSet; }
369 /* Dictionary mapping a set of states to a target state. */
370 typedef AvlTree< StateDictEl, StateSet, CmpTable<StateAp*> > StateDict;
372 /* Data needed for a merge operation. */
376 : stfillHead(0), stfillTail(0) { }
383 void fillListAppend( StateAp *state );
390 TransEl( Key lowKey, Key highKey )
391 : lowKey(lowKey), highKey(highKey) { }
392 TransEl( Key lowKey, Key highKey, TransAp *value )
393 : lowKey(lowKey), highKey(highKey), value(value) { }
401 static int compare( const Key key1, const Key key2 )
405 else if ( key1 > key2 )
412 /* Vector based set of key items. */
413 typedef BstSet<Key, CmpKey> KeySet;
417 MinPartition() : active(false) { }
422 MinPartition *prev, *next;
425 /* Epsilon transition stored in a state. Specifies the target */
426 typedef Vector<int> EpsilonTrans;
428 /* List of states that are to be drawn into this. */
431 EptVectEl( StateAp *targ, bool leaving )
432 : targ(targ), leaving(leaving) { }
437 typedef Vector<EptVectEl> EptVect;
439 /* Set of entry ids that go into this state. */
440 typedef BstSet<int> EntryIdSet;
442 /* Set of longest match items that may be active in a given state. */
443 typedef BstSet<LongestMatchPart*> LmItemSet;
446 typedef BstSet< Action*, CmpOrd<Action*> > CondSet;
447 typedef CmpTable< Action*, CmpOrd<Action*> > CmpCondSet;
450 : public AvlTreeEl<CondSpace>
452 CondSpace( const CondSet &condSet )
453 : condSet(condSet) {}
455 const CondSet &getKey() { return condSet; }
462 typedef Vector<CondSpace*> CondSpaceVect;
464 typedef AvlTree<CondSpace, CondSet, CmpCondSet> CondSpaceMap;
468 StateCond( Key lowKey, Key highKey ) :
469 lowKey(lowKey), highKey(highKey) {}
473 CondSpace *condSpace;
475 StateCond *prev, *next;
478 typedef DList<StateCond> StateCondList;
479 typedef Vector<long> LongVect;
483 Expansion( Key lowKey, Key highKey ) :
484 lowKey(lowKey), highKey(highKey),
485 fromTrans(0), fromCondSpace(0),
490 if ( fromTrans != 0 )
498 CondSpace *fromCondSpace;
501 CondSpace *toCondSpace;
504 Expansion *prev, *next;
507 typedef DList<Expansion> ExpansionList;
519 CondData() : nextCondKey(0) {}
521 /* Condition info. */
524 CondSpaceMap condSpaceMap;
527 extern CondData *condData;
529 /* State class that implements actions and priorities. */
533 StateAp(const StateAp &other);
536 /* Is the state final? */
537 bool isFinState() { return stateBits & SB_ISFINAL; }
539 /* Out transition list and the pointer for the default out trans. */
542 /* In transition Lists. */
545 /* Entry points into the state. */
548 /* Epsilon transitions. */
549 EpsilonTrans epsilonTrans;
551 /* Condition info. */
552 StateCondList stateCondList;
554 /* Number of in transitions from states other than ourselves. */
557 /* Temporary data for various algorithms. */
559 /* When duplicating the fsm we need to map each
560 * state to the new state representing it. */
563 /* When minimizing machines by partitioning, this maps to the group
564 * the state is in. */
565 MinPartition *partition;
567 /* When merging states (state machine operations) this next pointer is
568 * used for the list of states that need to be filled in. */
571 /* Identification for printing and stable minimization. */
576 /* Data used in epsilon operation, maybe fit into alg? */
577 StateAp *isolatedShadow;
580 /* A pointer to a dict element that contains the set of states this state
581 * represents. This cannot go into alg, because alg.next is used during
582 * the merging process. */
583 StateDictEl *stateDictEl;
585 /* When drawing epsilon transitions, holds the list of states to merge
589 /* Bits controlling the behaviour of the state during collapsing to dfa. */
592 /* State list elements. */
593 StateAp *next, *prev;
596 * Priority and Action data.
599 /* Out priorities transfered to out transitions. */
600 PriorTable outPriorTable;
602 /* The following two action tables are distinguished by the fact that when
603 * toState actions are executed immediatly after transition actions of
604 * incoming transitions and the current character will be the same as the
605 * one available then. The fromState actions are executed immediately
606 * before the transition actions of outgoing transitions and the current
607 * character is same as the one available then. */
609 /* Actions to execute upon entering into a state. */
610 ActionTable toStateActionTable;
612 /* Actions to execute when going from the state to the transition. */
613 ActionTable fromStateActionTable;
615 /* Actions to add to any future transitions that leave via this state. */
616 ActionTable outActionTable;
618 /* Conditions to add to any future transiions that leave via this sttate. */
619 ActionSet outCondSet;
621 /* Error action tables. */
622 ErrActionTable errActionTable;
624 /* Actions to execute on eof. */
625 ActionTable eofActionTable;
627 /* Set of longest match items that may be active in this state. */
631 template <class ListItem> struct NextTrans
642 lowKey = trans->lowKey;
643 highKey = trans->highKey;
647 void set( ListItem *t ) {
659 /* Encodes the different states that are meaningful to the of the iterator. */
660 enum PairIterUserState
662 RangeInS1, RangeInS2,
667 template <class ListItem1, class ListItem2 = ListItem1> struct PairIter
669 /* Encodes the different states that an fsm iterator can be in. */
672 ConsumeS1Range, ConsumeS2Range,
673 OnlyInS1Range, OnlyInS2Range,
674 S1SticksOut, S1SticksOutBreak,
675 S2SticksOut, S2SticksOutBreak,
676 S1DragsBehind, S1DragsBehindBreak,
677 S2DragsBehind, S2DragsBehindBreak,
681 PairIter( ListItem1 *list1, ListItem2 *list2 );
683 /* Query iterator. */
684 bool lte() { return itState != End; }
685 bool end() { return itState == End; }
686 void operator++(int) { findNext(); }
687 void operator++() { findNext(); }
689 /* Iterator state. */
693 PairIterUserState userState;
695 NextTrans<ListItem1> s1Tel;
696 NextTrans<ListItem2> s2Tel;
697 Key bottomLow, bottomHigh;
698 ListItem1 *bottomTrans1;
699 ListItem2 *bottomTrans2;
705 /* Init the iterator by advancing to the first item. */
706 template <class ListItem1, class ListItem2> PairIter<ListItem1, ListItem2>::PairIter(
707 ListItem1 *list1, ListItem2 *list2 )
716 /* Return and re-entry for the co-routine iterators. This should ALWAYS be
717 * used inside of a block. */
718 #define CO_RETURN(label) \
721 entry##label: backIn = true
723 /* Return and re-entry for the co-routine iterators. This should ALWAYS be
724 * used inside of a block. */
725 #define CO_RETURN2(label, uState) \
727 userState = uState; \
729 entry##label: backIn = true
731 /* Advance to the next transition. When returns, trans points to the next
732 * transition, unless there are no more, in which case end() returns true. */
733 template <class ListItem1, class ListItem2> void PairIter<ListItem1, ListItem2>::findNext()
735 /* This variable is used in dummy statements that follow the entry
736 * goto labels. The compiler needs some statement to follow the label. */
739 /* Jump into the iterator routine base on the iterator state. */
741 case Begin: goto entryBegin;
742 case ConsumeS1Range: goto entryConsumeS1Range;
743 case ConsumeS2Range: goto entryConsumeS2Range;
744 case OnlyInS1Range: goto entryOnlyInS1Range;
745 case OnlyInS2Range: goto entryOnlyInS2Range;
746 case S1SticksOut: goto entryS1SticksOut;
747 case S1SticksOutBreak: goto entryS1SticksOutBreak;
748 case S2SticksOut: goto entryS2SticksOut;
749 case S2SticksOutBreak: goto entryS2SticksOutBreak;
750 case S1DragsBehind: goto entryS1DragsBehind;
751 case S1DragsBehindBreak: goto entryS1DragsBehindBreak;
752 case S2DragsBehind: goto entryS2DragsBehind;
753 case S2DragsBehindBreak: goto entryS2DragsBehindBreak;
754 case ExactOverlap: goto entryExactOverlap;
755 case End: goto entryEnd;
759 /* Set up the next structs at the head of the transition lists. */
763 /* Concurrently scan both out ranges. */
765 if ( s1Tel.trans == 0 ) {
766 /* We are at the end of state1's ranges. Process the rest of
767 * state2's ranges. */
768 while ( s2Tel.trans != 0 ) {
769 /* Range is only in s2. */
770 CO_RETURN2( ConsumeS2Range, RangeInS2 );
775 else if ( s2Tel.trans == 0 ) {
776 /* We are at the end of state2's ranges. Process the rest of
777 * state1's ranges. */
778 while ( s1Tel.trans != 0 ) {
779 /* Range is only in s1. */
780 CO_RETURN2( ConsumeS1Range, RangeInS1 );
785 /* Both state1's and state2's transition elements are good.
786 * The signiture of no overlap is a back key being in front of a
788 else if ( s1Tel.highKey < s2Tel.lowKey ) {
789 /* A range exists in state1 that does not overlap with state2. */
790 CO_RETURN2( OnlyInS1Range, RangeInS1 );
793 else if ( s2Tel.highKey < s1Tel.lowKey ) {
794 /* A range exists in state2 that does not overlap with state1. */
795 CO_RETURN2( OnlyInS2Range, RangeInS2 );
798 /* There is overlap, must mix the ranges in some way. */
799 else if ( s1Tel.lowKey < s2Tel.lowKey ) {
800 /* Range from state1 sticks out front. Must break it into
801 * non-overlaping and overlaping segments. */
802 bottomLow = s2Tel.lowKey;
803 bottomHigh = s1Tel.highKey;
804 s1Tel.highKey = s2Tel.lowKey;
805 s1Tel.highKey.decrement();
806 bottomTrans1 = s1Tel.trans;
808 /* Notify the caller that we are breaking s1. This gives them a
809 * chance to duplicate s1Tel[0,1].value. */
810 CO_RETURN2( S1SticksOutBreak, BreakS1 );
812 /* Broken off range is only in s1. */
813 CO_RETURN2( S1SticksOut, RangeInS1 );
815 /* Advance over the part sticking out front. */
816 s1Tel.lowKey = bottomLow;
817 s1Tel.highKey = bottomHigh;
818 s1Tel.trans = bottomTrans1;
820 else if ( s2Tel.lowKey < s1Tel.lowKey ) {
821 /* Range from state2 sticks out front. Must break it into
822 * non-overlaping and overlaping segments. */
823 bottomLow = s1Tel.lowKey;
824 bottomHigh = s2Tel.highKey;
825 s2Tel.highKey = s1Tel.lowKey;
826 s2Tel.highKey.decrement();
827 bottomTrans2 = s2Tel.trans;
829 /* Notify the caller that we are breaking s2. This gives them a
830 * chance to duplicate s2Tel[0,1].value. */
831 CO_RETURN2( S2SticksOutBreak, BreakS2 );
833 /* Broken off range is only in s2. */
834 CO_RETURN2( S2SticksOut, RangeInS2 );
836 /* Advance over the part sticking out front. */
837 s2Tel.lowKey = bottomLow;
838 s2Tel.highKey = bottomHigh;
839 s2Tel.trans = bottomTrans2;
841 /* Low ends are even. Are the high ends even? */
842 else if ( s1Tel.highKey < s2Tel.highKey ) {
843 /* Range from state2 goes longer than the range from state1. We
844 * must break the range from state2 into an evenly overlaping
846 bottomLow = s1Tel.highKey;
847 bottomLow.increment();
848 bottomHigh = s2Tel.highKey;
849 s2Tel.highKey = s1Tel.highKey;
850 bottomTrans2 = s2Tel.trans;
852 /* Notify the caller that we are breaking s2. This gives them a
853 * chance to duplicate s2Tel[0,1].value. */
854 CO_RETURN2( S2DragsBehindBreak, BreakS2 );
856 /* Breaking s2 produces exact overlap. */
857 CO_RETURN2( S2DragsBehind, RangeOverlap );
859 /* Advance over the front we just broke off of range 2. */
860 s2Tel.lowKey = bottomLow;
861 s2Tel.highKey = bottomHigh;
862 s2Tel.trans = bottomTrans2;
864 /* Advance over the entire s1Tel. We have consumed it. */
867 else if ( s2Tel.highKey < s1Tel.highKey ) {
868 /* Range from state1 goes longer than the range from state2. We
869 * must break the range from state1 into an evenly overlaping
871 bottomLow = s2Tel.highKey;
872 bottomLow.increment();
873 bottomHigh = s1Tel.highKey;
874 s1Tel.highKey = s2Tel.highKey;
875 bottomTrans1 = s1Tel.trans;
877 /* Notify the caller that we are breaking s1. This gives them a
878 * chance to duplicate s2Tel[0,1].value. */
879 CO_RETURN2( S1DragsBehindBreak, BreakS1 );
881 /* Breaking s1 produces exact overlap. */
882 CO_RETURN2( S1DragsBehind, RangeOverlap );
884 /* Advance over the front we just broke off of range 1. */
885 s1Tel.lowKey = bottomLow;
886 s1Tel.highKey = bottomHigh;
887 s1Tel.trans = bottomTrans1;
889 /* Advance over the entire s2Tel. We have consumed it. */
893 /* There is an exact overlap. */
894 CO_RETURN2( ExactOverlap, RangeOverlap );
901 /* Done, go into end state. */
906 /* Compare lists of epsilon transitions. Entries are name ids of targets. */
907 typedef CmpTable< int, CmpOrd<int> > CmpEpsilonTrans;
909 /* Compare class for the Approximate minimization. */
914 int compare( const StateAp *pState1, const StateAp *pState2 );
917 /* Compare class for the initial partitioning of a partition minimization. */
918 class InitPartitionCompare
921 InitPartitionCompare() { }
922 int compare( const StateAp *pState1, const StateAp *pState2 );
925 /* Compare class for the regular partitioning of a partition minimization. */
926 class PartitionCompare
929 PartitionCompare() { }
930 int compare( const StateAp *pState1, const StateAp *pState2 );
933 /* Compare class for a minimization that marks pairs. Provides the shouldMark
939 bool shouldMark( MarkIndex &markIndex, const StateAp *pState1,
940 const StateAp *pState2 );
943 /* List of partitions. */
944 typedef DList< MinPartition > PartitionList;
946 /* List of transtions out of a state. */
947 typedef Vector<TransEl> TransListVect;
949 /* Entry point map used for keeping track of entry points in a machine. */
950 typedef BstSet< int > EntryIdSet;
951 typedef BstMapEl< int, StateAp* > EntryMapEl;
952 typedef BstMap< int, StateAp* > EntryMap;
953 typedef Vector<EntryMapEl> EntryMapBase;
955 /* Graph class that implements actions and priorities. */
958 /* Constructors/Destructors. */
960 FsmAp( const FsmAp &graph );
963 /* The list of states. */
965 StateList misfitList;
967 /* The map of entry points. */
968 EntryMap entryPoints;
970 /* The start state. */
973 /* The set of final states. */
974 StateSet finStateSet;
976 /* Misfit Accounting. Are misfits put on a separate list. */
977 bool misfitAccounting;
980 * Transition actions and priorities.
983 /* Set priorities on transtions. */
984 void startFsmPrior( int ordering, PriorDesc *prior );
985 void allTransPrior( int ordering, PriorDesc *prior );
986 void finishFsmPrior( int ordering, PriorDesc *prior );
987 void leaveFsmPrior( int ordering, PriorDesc *prior );
989 /* Action setting support. */
990 void transferErrorActions( StateAp *state, int transferPoint );
991 void setErrorAction( StateAp *state, int ordering, Action *action );
993 /* Fill all spaces in a transition list with an error transition. */
994 void fillGaps( StateAp *state );
996 /* Similar to setErrorAction, instead gives a state to go to on error. */
997 void setErrorTarget( StateAp *state, StateAp *target, int *orderings,
998 Action **actions, int nActs );
1000 /* Set actions to execute. */
1001 void startFsmAction( int ordering, Action *action );
1002 void allTransAction( int ordering, Action *action );
1003 void finishFsmAction( int ordering, Action *action );
1004 void leaveFsmAction( int ordering, Action *action );
1005 void longMatchAction( int ordering, LongestMatchPart *lmPart );
1007 /* Set conditions. */
1008 CondSpace *addCondSpace( const CondSet &condSet );
1010 void findEmbedExpansions( ExpansionList &expansionList,
1011 StateAp *destState, Action *condAction );
1012 void embedCondition( MergeData &md, StateAp *state, Action *condAction );
1013 void embedCondition( StateAp *state, Action *condAction );
1015 void startFsmCondition( Action *condAction );
1016 void allTransCondition( Action *condAction );
1017 void leaveFsmCondition( Action *condAction );
1019 /* Set error actions to execute. */
1020 void startErrorAction( int ordering, Action *action, int transferPoint );
1021 void allErrorAction( int ordering, Action *action, int transferPoint );
1022 void finalErrorAction( int ordering, Action *action, int transferPoint );
1023 void notStartErrorAction( int ordering, Action *action, int transferPoint );
1024 void notFinalErrorAction( int ordering, Action *action, int transferPoint );
1025 void middleErrorAction( int ordering, Action *action, int transferPoint );
1027 /* Set EOF actions. */
1028 void startEOFAction( int ordering, Action *action );
1029 void allEOFAction( int ordering, Action *action );
1030 void finalEOFAction( int ordering, Action *action );
1031 void notStartEOFAction( int ordering, Action *action );
1032 void notFinalEOFAction( int ordering, Action *action );
1033 void middleEOFAction( int ordering, Action *action );
1035 /* Set To State actions. */
1036 void startToStateAction( int ordering, Action *action );
1037 void allToStateAction( int ordering, Action *action );
1038 void finalToStateAction( int ordering, Action *action );
1039 void notStartToStateAction( int ordering, Action *action );
1040 void notFinalToStateAction( int ordering, Action *action );
1041 void middleToStateAction( int ordering, Action *action );
1043 /* Set From State actions. */
1044 void startFromStateAction( int ordering, Action *action );
1045 void allFromStateAction( int ordering, Action *action );
1046 void finalFromStateAction( int ordering, Action *action );
1047 void notStartFromStateAction( int ordering, Action *action );
1048 void notFinalFromStateAction( int ordering, Action *action );
1049 void middleFromStateAction( int ordering, Action *action );
1051 /* Shift the action ordering of the start transitions to start at
1052 * fromOrder and increase in units of 1. Useful before kleene star
1054 int shiftStartActionOrder( int fromOrder );
1056 /* Clear all priorities from the fsm to so they won't affcet minimization
1057 * of the final fsm. */
1058 void clearAllPriorities();
1060 /* Zero out all the function keys. */
1061 void nullActionKeys();
1063 /* Walk the list of states and verify state properties. */
1064 void verifyStates();
1066 /* Misfit Accounting. Are misfits put on a separate list. */
1067 void setMisfitAccounting( bool val )
1068 { misfitAccounting = val; }
1070 /* Set and Unset a state as final. */
1071 void setFinState( StateAp *state );
1072 void unsetFinState( StateAp *state );
1074 void setStartState( StateAp *state );
1075 void unsetStartState( );
1077 /* Set and unset a state as an entry point. */
1078 void setEntry( int id, StateAp *state );
1079 void changeEntry( int id, StateAp *to, StateAp *from );
1080 void unsetEntry( int id, StateAp *state );
1081 void unsetEntry( int id );
1082 void unsetAllEntryPoints();
1084 /* Epsilon transitions. */
1085 void epsilonTrans( int id );
1086 void shadowReadWriteStates( MergeData &md );
1089 * Basic attaching and detaching.
1092 /* Common to attaching/detaching list and default. */
1093 void attachToInList( StateAp *from, StateAp *to, TransAp *&head, TransAp *trans );
1094 void detachFromInList( StateAp *from, StateAp *to, TransAp *&head, TransAp *trans );
1096 /* Attach with a new transition. */
1097 TransAp *attachNewTrans( StateAp *from, StateAp *to,
1098 Key onChar1, Key onChar2 );
1100 /* Attach with an existing transition that already in an out list. */
1101 void attachTrans( StateAp *from, StateAp *to, TransAp *trans );
1103 /* Redirect a transition away from error and towards some state. */
1104 void redirectErrorTrans( StateAp *from, StateAp *to, TransAp *trans );
1106 /* Detach a transition from a target state. */
1107 void detachTrans( StateAp *from, StateAp *to, TransAp *trans );
1109 /* Detach a state from the graph. */
1110 void detachState( StateAp *state );
1113 * NFA to DFA conversion routines.
1116 /* Duplicate a transition that will dropin to a free spot. */
1117 TransAp *dupTrans( StateAp *from, TransAp *srcTrans );
1119 /* In crossing, two transitions both go to real states. */
1120 TransAp *fsmAttachStates( MergeData &md, StateAp *from,
1121 TransAp *destTrans, TransAp *srcTrans );
1123 /* Two transitions are to be crossed, handle the possibility of either
1124 * going to the error state. */
1125 TransAp *mergeTrans( MergeData &md, StateAp *from,
1126 TransAp *destTrans, TransAp *srcTrans );
1128 /* Compare deterimne relative priorities of two transition tables. */
1129 int comparePrior( const PriorTable &priorTable1, const PriorTable &priorTable2 );
1131 /* Cross a src transition with one that is already occupying a spot. */
1132 TransAp *crossTransitions( MergeData &md, StateAp *from,
1133 TransAp *destTrans, TransAp *srcTrans );
1135 void outTransCopy( MergeData &md, StateAp *dest, TransAp *srcList );
1137 void doRemove( MergeData &md, StateAp *destState, ExpansionList &expList1 );
1138 void doExpand( MergeData &md, StateAp *destState, ExpansionList &expList1 );
1139 void findCondExpInTrans( ExpansionList &expansionList, StateAp *state,
1140 Key lowKey, Key highKey, CondSpace *fromCondSpace, CondSpace *toCondSpace,
1141 long destVals, LongVect &toValsList );
1142 void findTransExpansions( ExpansionList &expansionList,
1143 StateAp *destState, StateAp *srcState );
1144 void findCondExpansions( ExpansionList &expansionList,
1145 StateAp *destState, StateAp *srcState );
1146 void mergeStateConds( StateAp *destState, StateAp *srcState );
1148 /* Merge a set of states into newState. */
1149 void mergeStates( MergeData &md, StateAp *destState,
1150 StateAp **srcStates, int numSrc );
1151 void mergeStatesLeaving( MergeData &md, StateAp *destState, StateAp *srcState );
1152 void mergeStates( MergeData &md, StateAp *destState, StateAp *srcState );
1154 /* Make all states that are combinations of other states and that
1155 * have not yet had their out transitions filled in. This will
1156 * empty out stateDict and stFil. */
1157 void fillInStates( MergeData &md );
1160 * Transition Comparison.
1163 /* Compare transition data. Either of the pointers may be null. */
1164 static inline int compareDataPtr( TransAp *trans1, TransAp *trans2 );
1166 /* Compare target state and transition data. Either pointer may be null. */
1167 static inline int compareFullPtr( TransAp *trans1, TransAp *trans2 );
1169 /* Compare target partitions. Either pointer may be null. */
1170 static inline int comparePartPtr( TransAp *trans1, TransAp *trans2 );
1172 /* Check marked status of target states. Either pointer may be null. */
1173 static inline bool shouldMarkPtr( MarkIndex &markIndex,
1174 TransAp *trans1, TransAp *trans2 );
1180 /* Compare priority and function table of transitions. */
1181 static int compareTransData( TransAp *trans1, TransAp *trans2 );
1183 /* Add in the properties of srcTrans into this. */
1184 void addInTrans( TransAp *destTrans, TransAp *srcTrans );
1186 /* Compare states on data stored in the states. */
1187 static int compareStateData( const StateAp *state1, const StateAp *state2 );
1189 /* Out transition data. */
1190 void clearOutData( StateAp *state );
1191 bool hasOutData( StateAp *state );
1192 void transferOutData( StateAp *destState, StateAp *srcState );
1198 /* New up a state and add it to the graph. */
1199 StateAp *addState();
1202 * Building basic machines
1205 void concatFsm( Key c );
1206 void concatFsm( Key *str, int len );
1207 void concatFsmCI( Key *str, int len );
1208 void orFsm( Key *set, int len );
1209 void rangeFsm( Key low, Key high );
1210 void rangeStarFsm( Key low, Key high );
1219 void repeatOp( int times );
1220 void optionalRepeatOp( int times );
1221 void concatOp( FsmAp *other );
1222 void unionOp( FsmAp *other );
1223 void intersectOp( FsmAp *other );
1224 void subtractOp( FsmAp *other );
1226 void joinOp( int startId, int finalId, FsmAp **others, int numOthers );
1227 void globOp( FsmAp **others, int numOthers );
1228 void deterministicEntry();
1234 /* Determine if there are any entry points into a start state other than
1235 * the start state. */
1236 bool isStartStateIsolated();
1238 /* Make a new start state that has no entry points. Will not change the
1239 * identity of the fsm. */
1240 void isolateStartState();
1242 /* Workers for resolving epsilon transitions. */
1243 bool inEptVect( EptVect *eptVect, StateAp *targ );
1244 void epsilonFillEptVectFrom( StateAp *root, StateAp *from, bool parentLeaving );
1245 void resolveEpsilonTrans( MergeData &md );
1247 /* Workers for concatenation and union. */
1248 void doConcat( FsmAp *other, StateSet *fromStates, bool optional );
1249 void doOr( FsmAp *other );
1255 /* Unset any final states that are no longer to be final
1256 * due to final bits. */
1257 void unsetIncompleteFinals();
1258 void unsetKilledFinals();
1260 /* Bring in other's entry points. Assumes others states are going to be
1261 * copied into this machine. */
1262 void copyInEntryPoints( FsmAp *other );
1264 /* Set State numbers starting at 0. */
1265 void setStateNumbers();
1267 /* Unset all final states. */
1268 void unsetAllFinStates();
1270 /* Set the bits of final states and clear the bits of non final states. */
1271 void setFinBits( int finStateBits );
1274 * Self-consistency checks.
1277 /* Run a sanity check on the machine. */
1278 void verifyIntegrity();
1280 /* Verify that there are no unreachable states, or dead end states. */
1281 void verifyReachability();
1282 void verifyNoDeadEndStates();
1288 /* Mark all states reachable from state. */
1289 void markReachableFromHereReverse( StateAp *state );
1291 /* Mark all states reachable from state. */
1292 void markReachableFromHere( StateAp *state );
1293 void markReachableFromHereStopFinal( StateAp *state );
1295 /* Removes states that cannot be reached by any path in the fsm and are
1296 * thus wasted silicon. */
1297 void removeDeadEndStates();
1299 /* Removes states that cannot be reached by any path in the fsm and are
1300 * thus wasted silicon. */
1301 void removeUnreachableStates();
1303 /* Remove error actions from states on which the error transition will
1304 * never be taken. */
1305 bool outListCovers( StateAp *state );
1306 bool anyErrorRange( StateAp *state );
1308 /* Remove states that are on the misfit list. */
1309 void removeMisfits();
1315 /* Minimization by partitioning. */
1316 void minimizePartition1();
1317 void minimizePartition2();
1319 /* Minimize the final state Machine. The result is the minimal fsm. Slow
1320 * but stable, correct minimization. Uses n^2 space (lookout) and average
1321 * n^2 time. Worst case n^3 time, but a that is a very rare case. */
1322 void minimizeStable();
1324 /* Minimize the final state machine. Does not find the minimal fsm, but a
1325 * pretty good approximation. Does not use any extra space. Average n^2
1326 * time. Worst case n^3 time, but a that is a very rare case. */
1327 void minimizeApproximate();
1329 /* This is the worker for the minimize approximate solution. It merges
1330 * states that have identical out transitions. */
1331 bool minimizeRound( );
1333 /* Given an intial partioning of states, split partitions that have out trans
1334 * to differing partitions. */
1335 int partitionRound( StateAp **statePtrs, MinPartition *parts, int numParts );
1337 /* Split partitions that have a transition to a previously split partition, until
1338 * there are no more partitions to split. */
1339 int splitCandidates( StateAp **statePtrs, MinPartition *parts, int numParts );
1341 /* Fuse together states in the same partition. */
1342 void fusePartitions( MinPartition *parts, int numParts );
1344 /* Mark pairs where out final stateness differs, out trans data differs,
1345 * trans pairs go to a marked pair or trans data differs. Should get
1347 void initialMarkRound( MarkIndex &markIndex );
1349 /* One marking round on all state pairs. Considers if trans pairs go
1350 * to a marked state only. Returns whether or not a pair was marked. */
1351 bool markRound( MarkIndex &markIndex );
1353 /* Move the in trans into src into dest. */
1354 void inTransMove(StateAp *dest, StateAp *src);
1356 /* Make state src and dest the same state. */
1357 void fuseEquivStates(StateAp *dest, StateAp *src);
1359 /* Find any states that didn't get marked by the marking algorithm and
1360 * merge them into the primary states of their equivalence class. */
1361 void fuseUnmarkedPairs( MarkIndex &markIndex );
1363 /* Merge neighboring transitions go to the same state and have the same
1364 * transitions data. */
1365 void compressTransitions();
1369 #endif /* _FSMGRAPH_H */