Exemple #1
0
        public void prefixTest1() //   balanced parenthesis
        {
            String             sg1 = "S -> S S|(S)|";
            ContextFreeGrammar g   = GrammarParser <char> .Parse(f1, sg1);

            g = GrammarUtilities.getEquivalentCNF(g);

            Assert.AreEqual(-2, GrammarUtilities.longestPrefixLength(g, "()(())()()"));  // full
            Assert.AreEqual("()(())".Length, GrammarUtilities.longestPrefixLength(g, "()(()))()()"));
            Assert.AreEqual("()(())((((".Length, GrammarUtilities.longestPrefixLength(g, "()(())(((("));
            Assert.AreEqual("".Length, GrammarUtilities.longestPrefixLength(g, ")(()()())"));
        }
Exemple #2
0
        public void prefixTest2() //   a^n b^n
        {
            String             sg1 = "S->aSb|S X| X->cX";
            ContextFreeGrammar g   = GrammarParser <char> .Parse(f1, sg1);

            g = GrammarUtilities.getEquivalentCNF(g);

            Assert.AreEqual(-2, GrammarUtilities.longestPrefixLength(g, "aaaaaaaaaaaaaaaabbbbbbbbbbbbbbbb"));  // full
            Assert.AreEqual("a".Length, GrammarUtilities.longestPrefixLength(g, "a"));
            Assert.AreEqual("".Length, GrammarUtilities.longestPrefixLength(g, "b"));
            Assert.AreEqual("aab".Length, GrammarUtilities.longestPrefixLength(g, "aab"));
            Assert.AreEqual("aabb".Length, GrammarUtilities.longestPrefixLength(g, "aabbb"));
            Assert.AreEqual("aaaaaaaaaaaabbbbbbbbbbbb".Length, GrammarUtilities.longestPrefixLength(g, "aaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbababc"));
        }
Exemple #3
0
        public void prefixTest3() //   balanced parenthesis () and {}   Exercise 8.5
        {
            String             sg1 = "S -> S S|(S)|{S}|";
            ContextFreeGrammar g   = GrammarParser <char> .Parse(f1, sg1);

            g = GrammarUtilities.getEquivalentCNF(g);

            Assert.AreEqual(-2, GrammarUtilities.longestPrefixLength(g, "(){()}"));  // full
            Assert.AreEqual("(){(".Length, GrammarUtilities.longestPrefixLength(g, "(){("));
            Assert.AreEqual("()".Length, GrammarUtilities.longestPrefixLength(g, "()}"));

            String sg2 = "S->A B| C D | A T | C U | S S  T-> A B   U->S D  A->(  B->) C->{ D->}";

            g = GrammarParser <char> .Parse(f1, sg2);

            g = GrammarUtilities.getEquivalentCNF(g);

            Assert.AreEqual(-2, GrammarUtilities.longestPrefixLength(g, "(){()}"));  // full
            Assert.AreEqual("(){(".Length, GrammarUtilities.longestPrefixLength(g, "(){("));
            Assert.AreEqual("()".Length, GrammarUtilities.longestPrefixLength(g, "()}"));
        }