/* * Function: in_dstates */ private static int in_dstates(Spec s, Bunch bunch) { Dfa dfa; #if OLD_DEBUG Console.Write("Looking for set : "); Print_Set(bunch.GetNFASet()); bunch.dump(); #endif Object o = s.dfa_sets[bunch.GetNFABit()]; if (null != o) { dfa = (Dfa)o; #if OLD_DUMP_DEBUG Console.WriteLine(" FOUND!"); #endif return(dfa.GetLabel()); } #if OLD_DUMP_DEBUG Console.WriteLine(" NOT FOUND!"); #endif return(NOT_IN_DSTATES); }
private static int in_dstates(Spec s, Bunch bunch) { Dfa dfa; if (!s.dfa_sets.TryGetValue(bunch.GetNFABit(), out dfa)) { return(-1); } return(dfa.Label); }
private static int add_to_dstates(Spec s, Bunch bunch) { Dfa dfa = Alloc.NewDfa(s); dfa.SetNFASet(new List <Nfa>(bunch.GetNFASet())); dfa.SetNFABit(new BitSet(bunch.GetNFABit())); dfa.SetAccept(bunch.GetAccept()); dfa.SetAnchor(bunch.GetAnchor()); dfa.ClearMarked(); s.dfa_sets[dfa.GetNFABit()] = dfa; return(dfa.Label); }
private static void make_dtrans(Spec s) { Console.WriteLine("Working on DFA states."); s.InitUnmarkedDFA(); int num = s.state_rules.Length; s.state_dtrans = new int[num]; for (int i = 0; i < num; i++) { Bunch bunch = new Bunch(s.state_rules[i]); bunch.e_closure(); Nfa2Dfa.add_to_dstates(s, bunch); s.state_dtrans[i] = s.dtrans_list.Count; Dfa nextUnmarkedDFA; while ((nextUnmarkedDFA = s.GetNextUnmarkedDFA()) != null) { nextUnmarkedDFA.SetMarked(); DTrans dTrans = new DTrans(s, nextUnmarkedDFA); for (int j = 0; j < s.dtrans_ncols; j++) { bunch.move(nextUnmarkedDFA, j); if (!bunch.IsEmpty()) { bunch.e_closure(); } int num2; if (bunch.IsEmpty()) { num2 = -1; } else { num2 = Nfa2Dfa.in_dstates(s, bunch); if (num2 == -1) { num2 = Nfa2Dfa.add_to_dstates(s, bunch); } } dTrans.SetDTrans(j, num2); } s.dtrans_list.Add(dTrans); } } Console.WriteLine(""); }
/* * function: add_to_dstates * Description: Takes as input a CBunch with details of * a dfa state that needs to be created. * 1) Allocates a new dfa state and saves it in the appropriate Spec list * 2) Initializes the fields of the dfa state with the information in the CBunch. * 3) Returns index of new dfa. */ private static int add_to_dstates(Spec s, Bunch bunch) { Dfa dfa; #if DEBUG Utility.assert(null != bunch.GetNFASet()); Utility.assert(null != bunch.GetNFABit()); Utility.assert(null != bunch.GetAccept() || Spec.NONE == bunch.GetAnchor()); #endif /* Allocate, passing Spec so dfa label can be set. */ dfa = Alloc.NewDfa(s); /* Initialize fields, including the mark field. */ dfa.SetNFASet(new ArrayList(bunch.GetNFASet())); dfa.SetNFABit(new BitSet(bunch.GetNFABit())); dfa.SetAccept(bunch.GetAccept()); dfa.SetAnchor(bunch.GetAnchor()); dfa.ClearMarked(); #if OLD_DUMP_DEBUG Console.WriteLine("[Created new dfa_state #" + Int32.ToString(dfa.GetLabel()) + "]"); dfa.dump(); #endif /* Register dfa state using BitSet in spec Hashtable. */ s.dfa_sets[dfa.GetNFABit()] = dfa; #if OLD_DUMP_DEBUG Console.Write("Registering set : "); Print_Set(dfa.GetNFASet()); Console.WriteLine(""); #endif return(dfa.GetLabel()); }
/* * Function: make_dtrans * Description: Creates uncompressed CDTrans transition table. */ //private void make_dtrans() private static void make_dtrans(Spec s) { Dfa dfa; int nextstate; Console.WriteLine("Working on DFA states."); /* Reference passing type and initializations. */ s.InitUnmarkedDFA(); Console.WriteLine("done init"); /* Allocate mapping array. */ int nstates = s.state_rules.Length; s.state_dtrans = new int[nstates]; for (int istate = 0; istate < nstates; istate++) { /* Create start state and initialize fields. */ Bunch bunch = new Bunch(s.state_rules[istate]); bunch.e_closure(); add_to_dstates(s, bunch); s.state_dtrans[istate] = s.dtrans_list.Count; /* Main loop of DTrans creation. */ while (null != (dfa = s.GetNextUnmarkedDFA())) { Console.Write("."); #if DEBUG Utility.assert(!dfa.IsMarked()); #endif /* Get first unmarked node, then mark it. */ dfa.SetMarked(); /* Allocate new DTrans, then initialize fields. */ DTrans dt = new DTrans(s, dfa); /* Set dt array for each character transition. */ for (int i = 0; i < s.dtrans_ncols; i++) { /* Create new dfa set by attempting character transition. */ bunch.move(dfa, i); if (!bunch.IsEmpty()) { bunch.e_closure(); } #if DEBUG Utility.assert((null == bunch.GetNFASet() && null == bunch.GetNFABit()) || (null != bunch.GetNFASet() && null != bunch.GetNFABit())); #endif /* Create new state or set state to empty. */ if (bunch.IsEmpty()) { nextstate = DTrans.F; } else { nextstate = in_dstates(s, bunch); if (nextstate == NOT_IN_DSTATES) { nextstate = add_to_dstates(s, bunch); } } #if DEBUG Utility.assert(nextstate < s.dfa_states.Count); #endif dt.SetDTrans(i, nextstate); } #if DEBUG Utility.assert(s.dtrans_list.Count == dfa.GetLabel()); #endif #if DEBUG StringBuilder sb1 = new StringBuilder(Lex.MAXSTR); sb1.Append("Current count = " + s.dtrans_list.Count.ToString() + "\n"); for (int i1 = 0; i1 < dt.GetDTransLength(); i1++) { sb1.Append(dt.GetDTrans(i1).ToString() + ","); } sb1.Append("end\n"); Console.Write(sb1.ToString()); #endif s.dtrans_list.Add(dt); } } Console.WriteLine(""); }