Пример #1
0
        static void GetAllStateAndInput(TransitionState stateStart, Hashset setAllState, Hashset setInputSymbols)
        {
            Hashset setUnprocessed = new Hashset();

            setUnprocessed.AddElement(stateStart);

            while (setUnprocessed.Count > 0)
            {
                TransitionState state = (TransitionState)setUnprocessed[0];

                setAllState.AddElement(state);
                setUnprocessed.RemoveElement(state);

                foreach (object objToken in state.GetAllKeys())
                {
                    string sSymbol = (string)objToken;
                    setInputSymbols.AddElement(sSymbol);

                    TransitionState[] arrTrans = state.GetTransitions(sSymbol);

                    if (arrTrans != null)
                    {
                        foreach (TransitionState stateEpsilon in arrTrans)
                        {
                            if (!setAllState.ElementExist(stateEpsilon))
                            {
                                setUnprocessed.AddElement(stateEpsilon);
                            }
                        }
                    }
                }
            }
        }
Пример #2
0
        private Hashset Eclosure(TransitionState stateStart)
        {
            Hashset setProcessed   = new Hashset();
            Hashset setUnprocessed = new Hashset();

            setUnprocessed.AddElement(stateStart);

            while (setUnprocessed.Count > 0)
            {
                TransitionState   state    = (TransitionState)setUnprocessed[0];
                TransitionState[] arrTrans = state.GetTransitions(MetaSymbol.EPSILON);
                setProcessed.AddElement(state);
                setUnprocessed.RemoveElement(state);

                if (arrTrans != null)
                {
                    foreach (TransitionState stateEpsilon in arrTrans)
                    {
                        if (!setProcessed.ElementExist(stateEpsilon))
                        {
                            setUnprocessed.AddElement(stateEpsilon);
                        }
                    }
                }
            }

            return(setProcessed);
        }
Пример #3
0
        static int GetSerializedFsa(TransitionState stateStart, Hashset setAllState,
                                    Hashset setAllSymbols, StringBuilder sb)
        {
            int    lineLength = 0;
            int    minWidth   = 6;
            string line       = String.Empty;
            string format     = String.Empty;

            setAllSymbols.RemoveElement(MetaSymbol.EPSILON);
            setAllSymbols.AddElement(MetaSymbol.EPSILON);

            object[] symbolObjects = new object[setAllSymbols.Count + 1];
            symbolObjects[0] = "State";
            format           = "{0,-8}";
            for (int i = 0; i < setAllSymbols.Count; i++)
            {
                string sSymbol = setAllSymbols[i].ToString();
                symbolObjects[i + 1] = sSymbol;

                format += " | ";
                format += "{" + (i + 1).ToString() + ",-" + Math.Max(Math.Max(sSymbol.Length, minWidth), sSymbol.ToString().Length) + "}";
            }

            line       = String.Format(format, symbolObjects);
            lineLength = Math.Max(lineLength, line.Length);
            sb.AppendLine(("").PadRight(lineLength, '-'));
            sb.AppendLine(line);
            sb.AppendLine(("").PadRight(lineLength, '-'));

            int nTransCount = 0;

            foreach (object objState in setAllState)
            {
                TransitionState state = (TransitionState)objState;
                symbolObjects[0] = (state.Equals(stateStart) ? ">" + state.ToString() : state.ToString());

                for (int i = 0; i < setAllSymbols.Count; i++)
                {
                    string sSymbol = setAllSymbols[i].ToString();

                    TransitionState[] arrStateTo = state.GetTransitions(sSymbol);
                    string            sTo        = String.Empty;
                    if (arrStateTo != null)
                    {
                        nTransCount += arrStateTo.Length;
                        sTo          = arrStateTo[0].ToString();

                        for (int j = 1; j < arrStateTo.Length; j++)
                        {
                            sTo += ", " + arrStateTo[j].ToString();
                        }
                    }
                    else
                    {
                        sTo = "--";
                    }
                    symbolObjects[i + 1] = sTo;
                }

                line = String.Format(format, symbolObjects);
                sb.AppendLine(line);
                lineLength = Math.Max(lineLength, line.Length);
            }

            format     = "State Count: {0}, Input Symbol Count: {1}, Transition Count: {2}";
            line       = String.Format(format, setAllState.Count, setAllSymbols.Count, nTransCount);
            lineLength = Math.Max(lineLength, line.Length);
            sb.AppendLine(("").PadRight(lineLength, '-'));
            sb.AppendLine(line);
            lineLength = Math.Max(lineLength, line.Length);
            setAllSymbols.RemoveElement(MetaSymbol.EPSILON);

            return(lineLength);
        }
Пример #4
0
        private ArrayList PartitionDfaGroups(Hashset setMasterDfa, Hashset setInputSymbol)
        {
            ArrayList arrGroup        = new ArrayList();
            HashMap   hashMap         = new HashMap();
            Hashset   emptySet        = new Hashset();
            Hashset   acceptingSet    = new Hashset();
            Hashset   nonAcceptingSet = new Hashset();

            foreach (object objState in setMasterDfa)
            {
                TransitionState state = (TransitionState)objState;

                if (state.AcceptingState == true)
                {
                    acceptingSet.AddElement(state);
                }
                else
                {
                    nonAcceptingSet.AddElement(state);
                }
            }

            if (nonAcceptingSet.GetCardinality() > 0)
            {
                arrGroup.Add(nonAcceptingSet);
            }

            arrGroup.Add(acceptingSet);

            IEnumerator iterInput = setInputSymbol.GetEnumerator();

            iterInput.Reset();

            while (iterInput.MoveNext())
            {
                string sInputSymbol = iterInput.Current.ToString();

                int nPartionIndex = 0;
                while (nPartionIndex < arrGroup.Count)
                {
                    Hashset setToBePartitioned = (Hashset)arrGroup[nPartionIndex];
                    nPartionIndex++;

                    if (setToBePartitioned.IsEmpty() || setToBePartitioned.GetCardinality() == 1)
                    {
                        continue;
                    }

                    foreach (object objState in setToBePartitioned)
                    {
                        TransitionState   state    = (TransitionState)objState;
                        TransitionState[] arrState = state.GetTransitions(sInputSymbol.ToString());

                        if (arrState != null)
                        {
                            Debug.Assert(arrState.Length == 1);

                            TransitionState stateTransionTo = arrState[0];

                            Hashset setFound = FindGroup(arrGroup, stateTransionTo);
                            hashMap.Add(setFound, state);
                        }
                        else
                        {
                            hashMap.Add(emptySet, state);
                        }
                    }

                    if (hashMap.Count > 1)
                    {
                        arrGroup.Remove(setToBePartitioned);
                        foreach (DictionaryEntry de in hashMap)
                        {
                            Hashset setValue = (Hashset)de.Value;
                            arrGroup.Add(setValue);
                        }
                        nPartionIndex = 0;
                        iterInput.Reset();
                    }
                    hashMap.Clear();
                }
            }

            return(arrGroup);
        }