Esempio n. 1
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")));
        }
Esempio n. 2
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")));
        }
Esempio n. 3
0
        public void TestAccepts02()
        {
            var productions = new HashSet <Production> {
                CFGParser.Production("<X_0> -> <X_0> 'x4' <X_0> 'x0'"),
                CFGParser.Production("<X_0> -> <X_0> <X_0> 'x2' <X_0> 'x3'"),
                CFGParser.Production("<X_0> -> <X_0> 'x1' <X_0>"),
                CFGParser.Production("<X_0> -> <X_0> 'x1' 'x1' 'x1' 'x3'"),
                CFGParser.Production("<X_0> -> ε"),
            };
            Grammar    g = new Grammar(productions, Nonterminal.Of("X_0"));
            CNFGrammar h = g.ToCNF();

            Assert.IsTrue(h.Accepts(Sentence.FromLetters("")));
            Assert.IsTrue(h.Accepts(Sentence.FromWords("x4 x0")));
            Assert.IsTrue(h.Accepts(Sentence.FromWords("x4 x0 x4 x2 x3 x0")));
        }
Esempio n. 4
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("")));
        }