public WordsInGrammarProblem Generate(int targetLevel)
        {
            ContextFreeGrammar grammar = null;
            var inNeeded  = 1;
            var outNeeded = 1;

            // We iterate until we get a CNF
            grammar = GrammarGenerator.GenerateRandom();
            while (GrammarUtilities.getEquivalentCNF(grammar) == null)
            {
                grammar = GrammarGenerator.GenerateRandom();
            }

            inNeeded  = rnd.Next(1, 5);
            outNeeded = rnd.Next(1, 5);
            if (targetLevel == Generation.HIGH)
            {
                inNeeded  = rnd.Next(3, 5);
                outNeeded = rnd.Next(3, 5);
            }
            else if (targetLevel == Generation.MEDIUM)
            {
                inNeeded  = rnd.Next(2, 4);
                outNeeded = rnd.Next(2, 4);
            }
            else if (targetLevel == Generation.LOW)
            {
                inNeeded  = rnd.Next(1, 4);
                outNeeded = rnd.Next(1, 4);
            }
            return(new WordsInGrammarProblem(grammar, inNeeded, outNeeded));
        }
        Tuple <HashSet <Nonterminal>, List <Tuple <Production, int> > >[][] cykTable; //resulting CYK table

        public CYKProblem(ContextFreeGrammar grammar, string word)
        {
            this.grammar = GrammarUtilities.getEquivalentCNF(grammar);
            this.word    = word;

            this.cykTable = GrammarUtilities.cyk(grammar, word);
        }
Esempio n. 3
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, ")(()()())"));
        }
        // Test for generated Grammars
        public override bool isValid()
        {
            // Important! Set the length of words to be tested
            var testWordsUpToLength = 5;

            if (GrammarUtilities.getEquivalentCNF(this.grammar) == null)
            {
                return(false);
            }
            Tuple <int, int> tup = GrammarUtilities.getMinWordInAndOut(GrammarUtilities.getEquivalentCNF(this.grammar), testWordsUpToLength, this.inNeeded, this.outNeeded);

            return(tup.Item1 >= this.inNeeded && tup.Item2 >= this.outNeeded);
        }
Esempio n. 5
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"));
        }
Esempio n. 6
0
        public void CYK_Test3()
        {
            String             sg1 = "S -> A B C   A->   B->   C->"; // = {""}       Testing epsilon
            ContextFreeGrammar g   = GrammarParser <char> .Parse(f1, sg1);

            g = GrammarUtilities.getEquivalentCNF(g);

            Assert.IsTrue(GrammarUtilities.isWordInGrammar(g, ""));

            Assert.IsFalse(GrammarUtilities.isWordInGrammar(g, "ba"));
            Assert.IsFalse(GrammarUtilities.isWordInGrammar(g, "ca"));
            Assert.IsFalse(GrammarUtilities.isWordInGrammar(g, "d"));
            Assert.IsFalse(GrammarUtilities.isWordInGrammar(g, "abcc"));
            Assert.IsFalse(GrammarUtilities.isWordInGrammar(g, "cba"));
            Assert.IsFalse(GrammarUtilities.isWordInGrammar(g, "ababababababababababababababababababab"));
        }
        private void AddCFGOfPDA(PDA <A, S> pda, string id, CancellationToken token)
        {
            var cfg = PDAToCFGConverter <A, S> .ToCFG(pda, token);

            var cleanedCfg = CFGCleaner.RemoveUselessSymbols(cfg, token);

            if (cleanedCfg == null)
            {
                noWordIsAccepted = true;
            }
            else
            {
                var cfgInCNF = GrammarUtilities.getEquivalentCNF(cleanedCfg);
                contextFreeGrammarsInCNF.Add(id, cfgInCNF);
            }
        }
Esempio n. 8
0
        public void CYK_Test1()
        {
            String             sg1 = "S->aX X->bS|b"; // =  (ab)*       Aufg 1.1a
            ContextFreeGrammar g   = GrammarParser <char> .Parse(f1, sg1);

            g = GrammarUtilities.getEquivalentCNF(g);

            Assert.IsTrue(GrammarUtilities.isWordInGrammar(g, "ab"));
            Assert.IsTrue(GrammarUtilities.isWordInGrammar(g, "abab"));
            Assert.IsTrue(GrammarUtilities.isWordInGrammar(g, "ababab"));
            Assert.IsTrue(GrammarUtilities.isWordInGrammar(g, "ababababababababababababababababababababab"));

            Assert.IsFalse(GrammarUtilities.isWordInGrammar(g, "a"));
            Assert.IsFalse(GrammarUtilities.isWordInGrammar(g, "b"));
            Assert.IsFalse(GrammarUtilities.isWordInGrammar(g, "c"));
            Assert.IsFalse(GrammarUtilities.isWordInGrammar(g, "aab"));
            Assert.IsFalse(GrammarUtilities.isWordInGrammar(g, "ba"));
            Assert.IsFalse(GrammarUtilities.isWordInGrammar(g, "aabaabbabb"));
            Assert.IsFalse(GrammarUtilities.isWordInGrammar(g, "ababababababababababababababababababa"));
        }
Esempio n. 9
0
        public void CYK_Test2()
        {
            String             sg1 = "S->aSb|S X| X->cX"; // = a^n b^n            Aufg 1.1b
            ContextFreeGrammar g   = GrammarParser <char> .Parse(f1, sg1);

            g = GrammarUtilities.getEquivalentCNF(g);

            Assert.IsTrue(GrammarUtilities.isWordInGrammar(g, ""));
            Assert.IsTrue(GrammarUtilities.isWordInGrammar(g, "ab"));
            Assert.IsTrue(GrammarUtilities.isWordInGrammar(g, "aabb"));
            Assert.IsTrue(GrammarUtilities.isWordInGrammar(g, "aaabbb"));
            Assert.IsTrue(GrammarUtilities.isWordInGrammar(g, "aaaabbbb"));

            Assert.IsFalse(GrammarUtilities.isWordInGrammar(g, "a"));
            Assert.IsFalse(GrammarUtilities.isWordInGrammar(g, "b"));
            Assert.IsFalse(GrammarUtilities.isWordInGrammar(g, "c"));
            Assert.IsFalse(GrammarUtilities.isWordInGrammar(g, "aab"));
            Assert.IsFalse(GrammarUtilities.isWordInGrammar(g, "ba"));
            Assert.IsFalse(GrammarUtilities.isWordInGrammar(g, "aabaabbabb"));
            Assert.IsFalse(GrammarUtilities.isWordInGrammar(g, "ababababababababababababababababababab"));
        }
Esempio n. 10
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, "()}"));
        }
Esempio n. 11
0
        public void CYK_Test4()
        {
            String             sg1 = "S->A B C    A->|a    B->b|    C->|c"; // = {"", a, b, c, ab, ac, bc, abc}
            ContextFreeGrammar g   = GrammarParser <char> .Parse(f1, sg1);

            g = GrammarUtilities.getEquivalentCNF(g);

            Assert.IsTrue(GrammarUtilities.isWordInGrammar(g, ""));
            Assert.IsTrue(GrammarUtilities.isWordInGrammar(g, "a"));
            Assert.IsTrue(GrammarUtilities.isWordInGrammar(g, "b"));
            Assert.IsTrue(GrammarUtilities.isWordInGrammar(g, "c"));
            Assert.IsTrue(GrammarUtilities.isWordInGrammar(g, "ab"));
            Assert.IsTrue(GrammarUtilities.isWordInGrammar(g, "ac"));
            Assert.IsTrue(GrammarUtilities.isWordInGrammar(g, "bc"));
            Assert.IsTrue(GrammarUtilities.isWordInGrammar(g, "abc"));

            Assert.IsFalse(GrammarUtilities.isWordInGrammar(g, "ba"));
            Assert.IsFalse(GrammarUtilities.isWordInGrammar(g, "ca"));
            Assert.IsFalse(GrammarUtilities.isWordInGrammar(g, "d"));
            Assert.IsFalse(GrammarUtilities.isWordInGrammar(g, "abcc"));
            Assert.IsFalse(GrammarUtilities.isWordInGrammar(g, "cba"));
            Assert.IsFalse(GrammarUtilities.isWordInGrammar(g, "ababababababababababababababababababab"));
        }
        public GrammarToCNFProblem(ContextFreeGrammar grammar)
        {
            this.grammar = grammar;

            sol = GrammarUtilities.getEquivalentCNF(grammar);
        }