/// <summary> /// Produces a random sentence and returns whether or not it was null /// </summary> /// <returns></returns> private bool ProduceNull(Nonterminal nt) { var sentence = new Sentence { nt }; while (sentence.Count > 0) { for (int i = sentence.Count - 1; i >= 0; i--) { var word = sentence[i]; var newStuff = ProduceNonterminal((Nonterminal)word); if (!newStuff.OnlyNonterminals()) { return(false); } sentence.RemoveAt(i); sentence.InsertRange(i, newStuff); } } return(true); }
// TODO: MIGHT NOT TERMINATE! /// <summary> /// Returns a random sentence produced by this grammar. /// </summary> /// <returns></returns> public Sentence ProduceRandom() { var sentence = new Sentence { this.Start }; while (sentence.ContainsNonterminal()) { // for (int i = 0; i < sentence.Count; i++) { for (int i = sentence.Count - 1; i >= 0; i--) { var word = sentence[i]; if (!word.IsNonterminal) { continue; } var newStuff = ProduceNonterminal((Nonterminal)word); sentence.RemoveAt(i); sentence.InsertRange(i, newStuff); } } return(sentence); }