Exemplo n.º 1
0
        public override NfaComponent ToNfa()
        {
            var dispatchState  = new NfaState();
            var newEndingState = new NfaState()
            {
                IsFinalState = true
            };
            var result = new NfaComponent()
            {
                InputEdge = new NfaEdge(NfaEdge.SymEpsilon, dispatchState),
                Ending    = newEndingState
            };

            var innerComponents = (from exp in ExpList select exp.ToNfa()).ToList();

            foreach (var com in innerComponents)
            {
                dispatchState.EdgesToOther.Add(com.InputEdge);
                com.Ending.EdgesToOther.Add(new NfaEdge(NfaEdge.SymEpsilon,
                                                        newEndingState));
                com.Ending.IsFinalState = false;
                result.AddState(com._innerStates);
            }

            return(result);
        }
Exemplo n.º 2
0
        public static DfaModel NfaToDfa(NfaComponent nfa)
        {
            char[] charset = new char[255];
            for (int i = 1; i < 256; i++)   // 0 is epsilon
            {
                charset[i - 1] = (char)i;
            }

            var states = new List <HashSet <NfaState> > {
                new HashSet <NfaState>(),
                ConvertionHelper.Closure(nfa.InputEdge.Target)
            };
            var p = 1;
            var j = 0;

            while (j <= p)
            {
                foreach (var c in charset)
                {
                    var  e     = ConvertionHelper.DfaEdges(states[j].ToArray(), c);
                    bool setEq = false;
                    for (int i = 0; i <= p; i++)
                    {
                        if (e.SetEquals(states[i]))
                        {
                            setEq = true;
                            // trans[j, c] <- i
                        }
                    }

                    if (!setEq)
                    {
                        p++;
                        states.Add(e);
                        // trans[j, c] <- p
                    }
                }

                j++;
            }

            return(null);
        }