コード例 #1
0
        public void DFA_RemoveTrapStates()
        {
            var dfa = new DFATable();
            var a   = dfa.CreateNode();
            var c   = dfa.CreateNode();

            a.IsFinal = true;

            dfa.StartState = a;

            dfa.AddTransition(a, a, "0");
            dfa.AddTransition(a, c, "1");

            dfa.AddTransition(c, c, "1");
            dfa.AddTransition(c, c, "0");

            var nfa = dfa.RemoveTrapStates();

            Assert.AreEqual(1, nfa.Nodes.Count());
            Assert.AreEqual(1, nfa.Transitions.Count());
            Assert.AreEqual(0, nfa.Transitions.First().Source.ID);
            Assert.AreEqual(0, nfa.Transitions.First().Destination.ID);
            Assert.AreEqual("0", nfa.Transitions.First().Symbol);
            Assert.AreEqual(1, nfa.GetAllPossibleSymbols().Count);
            Assert.IsTrue(nfa.GetAllPossibleSymbols().Contains("0"));
        }
コード例 #2
0
        internal DFATable ToDFATable()
        {
            var dfa   = new DFATable();
            var nodes = dfa.CreateNodes(this._rows.Count);

            foreach (var row in this._rows)
            {
                var node = nodes[this._rowMapping[row.GetStatesIdentifier()]];
                node.IsFinal = row.IsRowFinal();

                foreach (var n in row.States)
                {
                    node.UnionTags(n.GetTags());
                }

                foreach (var symbol in this._symbols)
                {
                    if (symbol == NFATable.Epsilon)
                    {
                        continue;
                    }

                    var source      = node;
                    var destination = nodes[this._rowMapping[row.GetTransition(symbol).GetIdentifier()]];
                    dfa.AddTransition(source, destination, symbol);
                }
            }

            // First row should be the start state.
            dfa.StartState = nodes[this._rowMapping[this._rows[0].GetStatesIdentifier()]];

            return(dfa);
        }
コード例 #3
0
        public DFATests()
        {
            dfa = new DFATable();
            a   = dfa.CreateNode();
            b   = dfa.CreateNode();
            c   = dfa.CreateNode();
            d   = dfa.CreateNode();
            e   = dfa.CreateNode();
            f   = dfa.CreateNode();

            dfa.StartState = a;

            c.IsFinal = true;
            d.IsFinal = true;
            e.IsFinal = true;

            dfa.AddTransition(a, b, "0");
            dfa.AddTransition(a, c, "1");

            dfa.AddTransition(b, a, "0");
            dfa.AddTransition(b, d, "1");

            dfa.AddTransition(c, f, "1");
            dfa.AddTransition(c, e, "0");

            dfa.AddTransition(d, e, "0");
            dfa.AddTransition(d, f, "1");

            dfa.AddTransition(e, e, "0");
            dfa.AddTransition(e, f, "1");

            dfa.AddTransition(f, f, "1");
            dfa.AddTransition(f, f, "0");
        }