Пример #1
0
        public void TestAccepts05()
        {
            var productions = new HashSet <Production> {
                CFGParser.Production("<X_2> -> <X_1> <X_0>"),
                CFGParser.Production("<X_1> -> <X_2> <X_1> <X_3> 'x2'"),
                CFGParser.Production("<X_3> -> <X_0> <X_0> <X_1> <X_3>"),
                CFGParser.Production("<X_3> -> ε"),
                CFGParser.Production("<X_2> -> <X_0> <X_1> <X_3> <X_1> <X_3>"),
                CFGParser.Production("<X_2> -> <X_1> <X_2> <X_2> <X_0>"),
                CFGParser.Production("<X_0> -> <X_3> 'x3'"),
                CFGParser.Production("<X_2> -> ε"),
                CFGParser.Production("<X_0> -> <X_2> <X_1>"),
                CFGParser.Production("<X_2> -> <X_0> <X_1> <X_2>"),
                CFGParser.Production("<X_1> -> <X_3> <X_3>"),
                CFGParser.Production("<X_3> -> 'x3' 'x4'"),
                CFGParser.Production("<X_3> -> <X_3> 'x4'"),
                CFGParser.Production("<X_1> -> 'x0' 'x4' 'x0' <X_2> <X_0>"),
            };
            var g = new Grammar(productions, Nonterminal.Of("X_0"));
            var h = g.ToCNF();

            Assert.IsTrue(h.Accepts(Sentence.FromWords("")));
            Assert.IsTrue(h.Accepts(Sentence.FromWords("x3")));
            Assert.IsTrue(h.Accepts(Sentence.FromWords("x3 x4 x3")));
            Assert.IsTrue(h.Accepts(Sentence.FromWords("x4 x3")));
            Assert.IsTrue(h.Accepts(Sentence.FromWords("x3 x4 x4 x3")));
            Assert.IsTrue(h.Accepts(Sentence.FromWords("x3 x4")));
            Assert.IsTrue(h.Accepts(Sentence.FromWords("x3 x4 x3 x4")));
        }
Пример #2
0
        public void TestAccepts03()
        {
            var productions = new HashSet <Production> {
                CFGParser.Production("<S> -> <S> <S> <S>"),
                CFGParser.Production("<S> -> ε"),
                CFGParser.Production("<S> -> 'x2' 'x0'"),
                CFGParser.Production("<S> -> 'x0' <S>"),
                CFGParser.Production("<S> -> 'x4'"),
                CFGParser.Production("<S> -> <S> 'x0' 'x4'"),
                CFGParser.Production("<S> -> 'x3' <S> 'x3' <S> 'x0'"),
                CFGParser.Production("<S> -> 'x2' <S> <S> <S> <S>"),
                CFGParser.Production("<S> -> <S> <S> <S> <S> <S>"),
                CFGParser.Production("<S> -> 'x0' <S> <S>"),
                CFGParser.Production("<S> -> <S>"),
                CFGParser.Production("<S> -> 'x0' <S> <S> 'x1'"),
                CFGParser.Production("<S> -> 'x3' 'x2' 'x1'"),
                CFGParser.Production("<S> -> 'x0' 'x0' 'x2'"),
            };
            Grammar    g = new Grammar(productions, Nonterminal.Of("S"));
            CNFGrammar h = g.ToCNF();

            Assert.IsTrue(h.Accepts(Sentence.FromLetters("")));
            Assert.IsTrue(h.Accepts(Sentence.FromWords("x4")));
            Assert.IsTrue(h.Accepts(Sentence.FromWords("x4 x0 x4")));
        }
Пример #3
0
        private static void TraversePlay()
        {
            var g = new Grammar(new List <Production> {
                CFGParser.Production("<A> → <B>"),
                CFGParser.Production("<B> → <A>"),
                CFGParser.Production("<B> → 'x'"),
            }, Nonterminal.Of("A"));

            g = IdentityActions.Annotate(g);

            var earley2  = new EarleyParser2(g);
            var sentence = Sentence.FromWords("x");
            var sppf2    = earley2.ParseGetForest(sentence);

            DotRunner.Run(DotBuilder.GetRawDot(sppf2), "infinite");

            var t2 = new Traversal(sppf2, g);
            var r2 = t2.Traverse();
            //foreach (var option in r2) {
            //	var s2 = (Sentence)option.Payload;
            //	if (!sentence.SequenceEqual(s2)) {
            //		throw new Exception();
            //	}
            //}
        }
Пример #4
0
        public void TestProduction()
        {
            var actualp   = CFGParser.Production(@"<S> -> <X> 'a' <S> [3.0]");
            var expectedp = new Production(
                Nonterminal.Of("S"),
                new Sentence {
                Nonterminal.Of("X"),
                Terminal.Of("a"),
                Nonterminal.Of("S")
            },
                3.0
                );
            var unexpectedp = new Production(
                Nonterminal.Of("S"),
                new Sentence {
                Terminal.Of("a"),
                Nonterminal.Of("X"),
                Nonterminal.Of("S")
            },
                3.0
                );

            Assert.IsTrue(actualp.ValueEquals(expectedp));
            Assert.IsFalse(actualp.ValueEquals(unexpectedp));
        }
Пример #5
0
 public void TestEarleys01()
 {
     ExecuteTest(new Grammar(new List <Production> {
         CFGParser.Production("<X_0> → <X_5> 'x5' [66.743324284322242]"),
         CFGParser.Production("<X_5> → 'x6' 'x6' [18.445467280897063]")
     }, Nonterminal.Of("X_0")), "x6");
 }
Пример #6
0
 public void TestTraversal05()
 {
     ExecuteTest(new Grammar(new List <Production> {
         CFGParser.Production("<S> → <S> <S>"),
         CFGParser.Production("<S> → 'x'"),
     }, Nonterminal.Of("S")), "x x x x x x x x x x x x x x x x");
 }
Пример #7
0
 public void TestTraversal01()
 {
     ExecuteTest(new Grammar(new List <Production> {
         CFGParser.Production("<X_0> → ε [115.49728913674936]"),
         CFGParser.Production("<X_0> → 'x0' 'x0' <X_0> 'x0' 'x0' [32.857227595456521]")
     }, Nonterminal.Of("X_0")), "ε");
 }
Пример #8
0
        public void TestParsing15()
        {
            // S -> aSa | bSb | ε
            var g = new Grammar(new List <Production> {
                CFGParser.Production(@"<S> -> 'a' <S> 'a'"),
                CFGParser.Production(@"<S> -> 'b' <S> 'b'"),
                CFGParser.Production(@"<S> -> ε"),
            }, Nonterminal.Of("S"));

            var sentences = new List <Sentence> {
                Sentence.FromLetters("ab"),
                Sentence.FromLetters("abc"),
                Sentence.FromLetters("aaa"),
                Sentence.FromLetters("abbba"),
                Sentence.FromLetters(""),
                Sentence.FromLetters("aa"),
                Sentence.FromLetters("bb"),
                Sentence.FromLetters("abba"),
                Sentence.FromLetters("baab"),
                Sentence.FromLetters("aaaa"),
                Sentence.FromLetters("bbbb"),
                Sentence.FromLetters("aaabbabbabbaaa"),
            };

            ExecuteTest(g, sentences);
        }
Пример #9
0
 public void TestTraversalAddition()
 {
     ExecuteTest(new Grammar(new List <Production> {
         CFGParser.Production("<S> → <S> '+' <S>"),
         CFGParser.Production("<S> → '1'")
     }, Nonterminal.Of("S")), "1 + 1 + 1");
 }
Пример #10
0
        public void TestParsing18()
        {
            var g = new Grammar(new List <Production> {
                CFGParser.Production("<X_9> → 'x3' <X_4> <X_9> [69.71867415901211]"),
                CFGParser.Production("<X_6> → 'x4' [43.169519673180545]"),
                CFGParser.Production("<X_0> → 'x0' 'x3' <X_6> <X_9> <X_9> [95.5660355475573]"),
                CFGParser.Production("<X_5> → <X_9> 'x1' 'x0' 'x1' 'x3' <X_2> [35.638882444537657]"),
                CFGParser.Production("<X_1> → 'x4' 'x3' 'x1' 'x1' <X_9> <X_8> [60.963767072169006]"),
                CFGParser.Production("<X_9> → <X_6> [96.869668710916145]"),
                CFGParser.Production("<X_8> → 'x1' <X_0> 'x0' <X_2> <X_2> [10.412202848779131]"),
                CFGParser.Production("<X_4> → ε [89.394112460498746]"),
                CFGParser.Production("<X_4> → <X_8> 'x2' <X_5> 'x1' [41.46934854261081]"),
                CFGParser.Production("<X_2> → ε [28.04076097674703]"),
                CFGParser.Production("<X_8> → ε [55.798571558109757]"),
                CFGParser.Production("<X_0> → 'x2' 'x2' 'x3' <X_6> [48.407048357374521]"),
                CFGParser.Production("<X_0> → <X_1> 'x3' 'x2' [82.3935272774629]"),
                CFGParser.Production("<X_1> → <X_8> <X_1> <X_2> [68.051246746932733]")
            }, Nonterminal.Of("X_0"));

            var sentences = new List <Sentence>();

            sentences.Add(Sentence.FromWords("x3 x2"));

            ExecuteTest(g, sentences);
        }
Пример #11
0
        public void TestMissingStart02()
        {
            var productions = new List <Production> {
                CFGParser.Production(@"<X_0> -> 'a'"),
            };
            CNFGrammar h = new CNFGrammar(productions, Nonterminal.Of("S"));

            Helpers.IsNear(0.0, h.Cyk(Sentence.FromLetters("a")));
        }
Пример #12
0
        public void TestTraversalEmpty()
        {
            var g = new Grammar(new List <Production> {
                CFGParser.Production("<S> → <A>"),
                CFGParser.Production("<A> → ε"),
            }, Nonterminal.Of("S"));

            ExecuteTest(g, "");
        }
Пример #13
0
 public void TestTraversal03()
 {
     ExecuteTest(new Grammar(new List <Production> {
         CFGParser.Production("<X_0> → <X_1> <X_0> <X_1> 'x1' 'x1' <X_0> <X_0> <X_0> 'x0' 'x1' [53.389437636541871]"),
         CFGParser.Production("<X_0> → <X_1> [11.500305104302385]"),
         CFGParser.Production("<X_0> → <X_0> <X_0> <X_0> 'x0' [90.413361106726043]"),
         CFGParser.Production("<X_1> → 'x0' [13.006726633760485]")
     }, Nonterminal.Of("X_0")), "x0");
 }
Пример #14
0
 public void TestMissingStart01()
 {
     var productions = new List <Production> {
         CFGParser.Production(@"<X_0> -> <X_0> <X_0>"),
         CFGParser.Production(@"<X_0> -> 'a'"),
     };
     Grammar    g = new Grammar(productions, Nonterminal.Of("S"));
     CNFGrammar h = g.ToCNF();
 }
        //    private static string BNF = @"<expr> ::= <val> <op> <val> | <val> | 1 | 2
        //
        //<op> ::= + | - | *
        //
        //<val> ::= N_m | N_s";


        public void Start(Options options)
        {
            CFGParser pars = new CFGParser();

            var grammar = pars.readGrammarBNF(BNF);

            long possibilities = CalcPossibilitiesOfDepth(grammar, 2);

            System.Console.WriteLine(possibilities);
        }
Пример #16
0
        public void TestTraversalInfinite()
        {
            var g = new Grammar(new List <Production> {
                CFGParser.Production("<A> → <B>"),
                CFGParser.Production("<B> → <A>"),
                CFGParser.Production("<B> → 'x'"),
            }, Nonterminal.Of("A"));

            ExecuteTest(g, "x");
        }
Пример #17
0
 public void TestTraversal04()
 {
     ExecuteTest(new Grammar(new List <Production> {
         CFGParser.Production("<X_0> → 'x0' <X_0> [53.221112815766176]"),
         CFGParser.Production("<X_0> → ε [172.512627911527]"),
         CFGParser.Production("<X_0> → 'x0' 'x0' <X_0> <X_0> [52.465847087775288]"),
         CFGParser.Production("<X_0> → <X_0> <X_0> <X_0> 'x0' 'x0' [72.139131426410344]"),
         CFGParser.Production("<X_0> → <X_0> <X_0> 'x0' 'x0' 'x0' [87.645744548479911]")
     }, Nonterminal.Of("X_0")), "x0 x0 x0 x0 x0 x0 x0 x0 x0 x0 x0 x0 x0 x0");
 }
Пример #18
0
        public void TestToCNF06()
        {
            Grammar g = new Grammar(new List <Production> {
                CFGParser.Production("<S> → ε"),
                CFGParser.Production("<S> → 'x' <A>")
            }, Nonterminal.Of("S"));
            CNFGrammar h = g.ToCNF();

            Helpers.AssertNear(1.0, h.Cyk(Sentence.FromWords("")));
        }
Пример #19
0
        public void TestAccepts09()
        {
            var productions = new HashSet <Production> {
                CFGParser.Production("<S> -> ε"),
                // CFGParser.Production("<S> -> 'x'"),
                CFGParser.Production("<S> -> <S> <S> <S>"),
            };
            Grammar    g = new Grammar(productions, Nonterminal.Of("S"));
            CNFGrammar h = g.ToCNF();

            Assert.IsTrue(h.Accepts(Sentence.FromLetters("")));
        }
Пример #20
0
        public void TestGetNullable07()
        {
            var productions = new HashSet <Production> {
                CFGParser.Production("<S> -> 'x'"),
                CFGParser.Production("<S> -> <S> <S> <S>"),
            };

            var result = (Dictionary <Nonterminal, double>)nullableClass.InvokeStatic("GetNullable", new object[] { productions });

            Assert.IsTrue(result.Count == 1);
            Helpers.AssertNear(0, result[Nonterminal.Of("S")]);
        }
Пример #21
0
        public void TestFreshNames()
        {
            var productions = new List <Production> {
                CFGParser.Production(@"<S> -> 'a'"),
                CFGParser.Production(@"<X_0> -> 'b'"),
            };
            Grammar    g = new Grammar(productions, Nonterminal.Of("S"));
            CNFGrammar h = g.ToCNF();

            Assert.IsTrue(h.Accepts(Sentence.FromLetters("a")));
            Assert.IsFalse(h.Accepts(Sentence.FromLetters("b")));
        }
Пример #22
0
        public void TestParsing02()
        {
            var g = new Grammar(new List <Production> {
                CFGParser.Production("<X_0> → <X_1>"),
                CFGParser.Production("<X_1> → 'x1' 'x2'"),
                CFGParser.Production("<X_1> → ε")
            }, Nonterminal.Of("X_0"));

            var sentences = new List <Sentence>();

            sentences.Add(Sentence.FromWords("x1 x2"));
            ExecuteTest(g, sentences);
        }
Пример #23
0
        public void TestParsing21()
        {
            var g = new Grammar(new List <Production> {
                CFGParser.Production("<S> → ε [45.90244483803513]"),
                CFGParser.Production("<S> → <S> <S> [83.928846908234448]"),
            }, Nonterminal.Of("S"));

            var sentences = new List <Sentence>();

            sentences.Add(Sentence.FromWords(""));

            ExecuteTest(g, sentences);
        }
Пример #24
0
        public void TestTraversalLinear()
        {
            var g = new Grammar(new List <Production> {
                CFGParser.Production("<S> → <A>"),
                CFGParser.Production("<S> → <B>"),
                CFGParser.Production("<S> → <C>"),
                CFGParser.Production("<A> → '1'"),
                CFGParser.Production("<B> → '1'"),
                CFGParser.Production("<C> → '1'")
            }, Nonterminal.Of("S"));

            ExecuteTest(g, "1");
        }
Пример #25
0
        public void TestParsing14()
        {
            var g = new Grammar(new List <Production> {
                CFGParser.Production("<S> → ε"),
                CFGParser.Production("<S> → <A> 'x'"),
                CFGParser.Production("<A> → <S>"),
            }, Nonterminal.Of("S"));

            var sentences = new List <Sentence>();

            sentences.Add(Sentence.FromWords("x"));
            ExecuteTest(g, sentences);
        }
Пример #26
0
        public void TestToCNF01()
        {
            var productions = new List <Production> {
                CFGParser.Production(@"<X_0> -> 'x3' <X_0> [23]"),
                CFGParser.Production(@"<X_0> -> ε [85]"),
            };
            Grammar    g = new Grammar(productions, Nonterminal.Of("X_0"));
            CNFGrammar h = g.ToCNF();

            Helpers.AssertNear(85.0 / 108.0, h.Cyk(Sentence.FromLetters("")));
            Helpers.AssertNear((23.0 / 108.0) * (85.0 / 108.0), h.Cyk(Sentence.FromWords("x3")));
            Helpers.AssertNear((23.0 / 108.0) * (23.0 / 108.0) * (85.0 / 108.0), h.Cyk(Sentence.FromWords("x3 x3")));
        }
Пример #27
0
        public void TestAccepts07()
        {
            var productions = new HashSet <Production> {
                CFGParser.Production("<A> -> 'a'"),
                CFGParser.Production("<A> -> <B>"),
                CFGParser.Production("<B> -> 'b'"),
                CFGParser.Production("<B> -> <A>"),
            };
            var g = new Grammar(productions, Nonterminal.Of("A"));
            var h = g.ToCNF();

            Assert.IsTrue(h.Accepts(Sentence.FromWords("a")));
            Assert.IsTrue(h.Accepts(Sentence.FromWords("b")));
        }
Пример #28
0
        public void TestParsing03()
        {
            var g = new Grammar(new List <Production> {
                CFGParser.Production("<S> → <X> 'b'"),
                CFGParser.Production("<S> → 'a'"),
                CFGParser.Production("<X> → <S>"),
                CFGParser.Production("<X> → 'x'")
            }, Nonterminal.Of("S"));

            var sentences = new List <Sentence>();

            sentences.Add(Sentence.FromWords("a b b"));
            ExecuteTest(g, sentences);
        }
Пример #29
0
        public void TestProbabilityUnit01()
        {
            var productions = new HashSet <Production> {
                CFGParser.Production("<A> -> 'a'"),
                CFGParser.Production("<A> -> <B>"),
                CFGParser.Production("<B> -> 'b'"),
                CFGParser.Production("<B> -> <A>"),
            };
            var g = new Grammar(productions, Nonterminal.Of("A"));
            var h = g.ToCNF();

            Helpers.IsNear(2.0 / 3, h.Cyk(Sentence.FromLetters("a")));
            Helpers.IsNear(1.0 / 3, h.Cyk(Sentence.FromLetters("b")));
        }
Пример #30
0
        public void TestGetNullable01()
        {
            var productions = new HashSet <Production> {
                CFGParser.Production("<X> -> <B> <B>"),
                CFGParser.Production("<B> -> 'b'"),
                CFGParser.Production("<B> -> ε"),
            };

            var result = (Dictionary <Nonterminal, double>)nullableClass.InvokeStatic("GetNullable", new object[] { productions });

            Assert.IsTrue(result.Count == 2);
            Helpers.AssertNear(0.5, result[Nonterminal.Of("B")]);
            Helpers.AssertNear(0.25, result[Nonterminal.Of("X")]);
        }