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));
        }
        public CYKProblem Generate(int targetLevel)
        {
            var grammar = GrammarGenerator.GenerateRandomCNF();

            //generate word
            int wordLength = rnd.Next(4, 6);

            if (targetLevel == Generation.LOW)
            {
                wordLength = rnd.Next(4, 5);                                //shorter is easier
            }
            if (targetLevel == Generation.HIGH)
            {
                wordLength = rnd.Next(5, 6);                                 //longer is harder
            }
            String word      = "";
            var    terminals = new HashSet <GrammarSymbol>();

            terminals.UnionWith(grammar.GetNonVariableSymbols());
            var terminalsArray = terminals.ToArray();

            if (terminalsArray.Count() == 0)
            {
                return(new CYKProblem(grammar, word));                              //no terminals
            }
            //build the word
            for (int i = 0; i < wordLength; i++)
            {
                word += terminalsArray[rnd.Next(terminalsArray.Count())];
            }

            return(new CYKProblem(grammar, word));
        }
Esempio n. 3
0
        public FindDerivationProblem Generate(int targetLevel)
        {
            ContextFreeGrammar grammar = GrammarGenerator.GenerateRandom();
            var productions            = grammar.GetProductions();
            var toAdd = new List <Production>();

            //make sure every varaible has a resolution (production with #NT = 0) and an continuation (production with #NT > 0)
            foreach (Nonterminal nt in grammar.Variables)
            {
                var nt_prods = grammar.GetProductions(nt);
                //resolution check
                var p = nt_prods.ToList().Find(pp => pp.ContainsNoVariables);
                if (p == null)
                {
                    GrammarSymbol t = Choose(grammar.GetNonVariableSymbols());
                    if (t == null)
                    {
                        t = new Exprinal <char>('x', "x");
                    }
                    p = new Production(nt, new GrammarSymbol[] { t });
                    toAdd.Add(p);
                }
                //continuation check
                p = nt_prods.ToList().Find(pp => Derivation.countNT(pp.Rhs) >= 1);
                if (p == null)
                {
                    //resolution check
                    GrammarSymbol t = Choose(grammar.Variables);
                    p = new Production(nt, new GrammarSymbol[] { Choose(grammar.Variables), Choose(grammar.Variables) });
                    toAdd.Add(p);
                }
            }
            //add new productions (for resolution and continuation)
            if (toAdd.Count != 0)
            {
                grammar = new ContextFreeGrammar(grammar.StartSymbol, grammar.GetProductions().Concat(toAdd));
            }
            productions = grammar.GetProductions();

            //type
            bool shouldBeAny = true;

            if (targetLevel == Generation.ANY)
            {
                shouldBeAny = (rnd.Next(0, 9) < 3);                               // 30%
            }
            if (targetLevel == Generation.MEDIUM)
            {
                shouldBeAny = (rnd.Next(0, 9) < 2);                                   // 20%
            }
            if (targetLevel == Generation.HIGH)
            {
                shouldBeAny = (rnd.Next(0, 9) < 9);                                 // 90%
            }
            int type = Derivation.DERIVATION_ALL;

            if (!shouldBeAny)
            {
                type = Derivation.DERIVATION_LEFTMOST;
                if (rnd.Next(0, 9) < 3)
                {
                    type = Derivation.DERIVATION_RIGHTMOST;
                }
            }

            //nr of steps
            int steps = rnd.Next(5, 10);

            if (targetLevel == Generation.LOW)
            {
                steps = rnd.Next(3, 6);
            }
            if (targetLevel == Generation.MEDIUM)
            {
                steps = rnd.Next(7, 9);
            }
            if (targetLevel == Generation.HIGH)
            {
                steps = rnd.Next(10, 12);
            }

            List <GrammarSymbol[]> derivation = new List <GrammarSymbol[]>(steps + 1);
            var cur = new GrammarSymbol[] { grammar.StartSymbol };

            derivation.Add(cur);
            for (int i = 0; i < steps; i++)
            {
                //get possible next steps
                IEnumerable <GrammarSymbol[]> next = Derivation.findAllDerivations(grammar.GetProductions(), cur, type);

                if (Derivation.countNT(cur) <= 1 && i != steps - 1) //don't end yet!
                {
                    next = next.Where(pw => Derivation.countNT(pw) >= 1);
                }

                //try not to repeat
                var next_part = next.Except(derivation);
                if (next_part.Count() > 0)
                {
                    next = next_part;
                }

                cur = Choose(next);
                //cut out repeat
                if (derivation.Contains(cur))
                {
                    int r_i = derivation.IndexOf(cur);
                    derivation = derivation.GetRange(0, r_i);
                }

                //add next step
                derivation.Add(cur);
            }
            //replace all NTs
            while (Derivation.countNT(cur) > 0)
            {
                //get possible next steps
                IEnumerable <GrammarSymbol[]> next = Derivation.findAllDerivations(grammar.GetProductions(), cur, type);

                //filter
                int curNTs = Derivation.countNT(cur);
                next = next.Where(pw => Derivation.countNT(pw) < curNTs);

                cur = Choose(next);
                derivation.Add(cur);
            }

            String word = Derivation.partialWordToString(cur);

            return(new FindDerivationProblem(grammar, word, type, derivation));
        }
        public GrammarToCNFProblem Generate(int targetLevel)
        {
            var grammar = GrammarGenerator.GenerateRandom();

            return(new GrammarToCNFProblem(grammar));
        }