예제 #1
0
        public void DeadStatesTest()
        {
            var dfa = new Dfa <string>();

            dfa.AddEdge(0, "A", 3);
            dfa.AddEdge(1, "B", 3);
            dfa.AddEdge(2, "C", 3);
            dfa.AddEdge(0, "E", 1);
            dfa.AddEdge(1, "E", 2);

            dfa.AddBadState(3);

            var emptyDfa = new Dfa <string>();

            var rules = new Dictionary <string, IDfa <Optional <Rule <string> >, string> >
            {
                ["A"] = dfa,
                ["E"] = emptyDfa
            };

            var grammar = new CompiledGrammar <string> {
                Rules = rules
            };

            var nullables = new List <DfaAndState <string> >
            {
                new DfaAndState <string> {
                    Dfa = emptyDfa, State = new ValueState <int>(0)
                }
            };

            var firstSymbols = FirstHelper <string> .GetFirstSymbols(grammar, nullables);

            var expected = new[]
            {
                "E",
                "E",
                string.Empty,
                string.Empty
            };

            for (int i = 0; i < 4; ++i)
            {
                var stateEntity = new DfaAndState <string> {
                    Dfa = dfa, State = new ValueState <int>(i)
                };
                string output = string.Join(',', firstSymbols[stateEntity].OrderBy(x => x));
                Assert.AreEqual(expected[i], output, $"Unexpected output on test {i}: expected is [{expected[i]}], but found [{output}]");
            }
        }