Example #1
0
        /// <summary>
        /// Finds the first nonterminal, and replaces it with one of the values found in its production.
        /// </summary>
        /// <param name="String">The string that will be analyzed.</param>
        /// <param name="productionList">The list of productions.</param>
        /// <param name="characterLimit">The maximum number of characters the final string should have. Default is 60.</param>
        /// <returns>The string after a succesful replace, or the same string if there are no more nonterminals or
        /// if it's above the limit.</returns>
        string Replace(string String, List <List <string> > productionList, int characterLimit = 60)
        {
            foreach (char character in String)
            {
                foreach (List <string> production in productionList)
                {
                    // If the character is found in a production
                    if (character == production[0][0] && String.Length < characterLimit)
                    {
                        // Get a random value from the production
                        int indexProductionValue    = randomNumberGenerator.Next(1, production.Count);
                        int indexCharacterToReplace = UsefulStuff.GetCharacterIndex(String, character);

                        // Replace character
                        String = String.Remove(indexCharacterToReplace, 1);
                        String = String.Insert(indexCharacterToReplace, production[indexProductionValue]);

                        // Return after replace
                        return(String);
                    }
                }
            }

            // In this phase, the string should be over the limit or it wouldn't contain any nonterminals
            return(String);
        }