Beispiel #1
0
        public void TestGreibachNormalForm()
        {
            Assert.IsFalse(grammar1.InGreibachNormalForm());
            Assert.IsFalse(grammar2.InGreibachNormalForm());
            Assert.IsFalse(grammar3.InGreibachNormalForm());
            Assert.IsTrue(grammar1.ToGreibachNormalForm().InGreibachNormalForm());
            Assert.IsTrue(grammar2.ToGreibachNormalForm().InGreibachNormalForm());
            CFG grammar3Greibach = grammar3.ToGreibachNormalForm();

            Assert.IsTrue(grammar3Greibach.InGreibachNormalForm());
            Assert.IsTrue(derives(grammar3Greibach, ""));
            Assert.IsTrue(derives(grammar3Greibach, "()"));
            Assert.IsTrue(derives(grammar3Greibach, "()()"));
            Assert.IsTrue(derives(grammar3Greibach, "(())"));
            Assert.IsTrue(derives(grammar3Greibach, "(()())"));
            Assert.IsTrue(derives(grammar3Greibach, "()(()(()))(())"));
            Assert.IsFalse(derives(grammar3Greibach, "("));
            Assert.IsFalse(derives(grammar3Greibach, ")"));
            Assert.IsFalse(derives(grammar3Greibach, "())"));
            Assert.IsFalse(derives(grammar3Greibach, "(()()"));
            Assert.IsFalse(derives(grammar3Greibach, "((())))"));
            Assert.IsFalse(derives(grammar3Greibach, "(()(())"));
            Assert.IsFalse(derives(grammar3Greibach, "()(()((()))(())))"));
            Assert.IsTrue(CFGBuilder.Start("A")
                          .Derive("A").To("a")
                          .Derive("A").ToEpsilon()
                          .Build().InGreibachNormalForm());
            Assert.IsFalse(CFGBuilder.Start("A")
                           .Derive("A").To("a", "A")
                           .Derive("A").ToEpsilon()
                           .Build().InGreibachNormalForm());
        }
Beispiel #2
0
        public void TestReversed()
        {
            CFG reversed = CFGBuilder.Start("a")
                           .Derive("a").To("c", "b")
                           .Derive("a").To("f", "e", "d")
                           .Derive("b").To("a")
                           .Derive("c").ToEpsilon()
                           .Build();

            Assert.AreEqual(reversed, grammar1.Reversed());
        }
Beispiel #3
0
        public void TestRemoveEpsilonProductions()
        {
            CFG grammar2Transformed = grammar2.RemoveEpsilonProductions();
            CFG expectedresult      = CFGBuilder.Start("a")
                                      .Derive("a").ToEpsilon()
                                      .Derive("a").To("c", "d")
                                      .Derive("a").To("d")
                                      .Derive("c").To("d")
                                      .Build();

            Assert.AreEqual(expectedresult, grammar2Transformed);
        }
Beispiel #4
0
        public void TestSimplify()
        {
            Assert.AreEqual(grammar1, grammar1.Simplify());
            Assert.AreEqual(grammar2, grammar2.Simplify());
            Assert.AreEqual(grammar3, grammar3.Simplify());
            CFG expectedResult = CFGBuilder.Start("S")
                                 .Derive("S").To("S", "A")
                                 .Derive("A").ToEpsilon()
                                 .Build();

            Assert.AreEqual(expectedResult, grammar4.Simplify());
        }
Beispiel #5
0
        public void TestMoreGrammars()
        {
            CFG grammar = CFGBuilder.Start("X")
                          .Derive("X").To("a", "Y")
                          .Derive("X").ToEpsilon()
                          .Derive("Y").To("b", "Z")
                          .Derive("Z").To("c", "X")
                          .Build().Reversed();

            Assert.IsTrue(grammar.HasLeftRecursion());
            grammar.ToGreibachNormalForm();
        }