// todo: horrible private Nonterminal GetFresh() { var originalNonterminals = _grammar.GetNonterminals(); Nonterminal result; do { result = Nonterminal.Of("X_" + _freshx); _freshx++; } while (originalNonterminals.Contains(result)); return(result); }
private Nonterminal RandomNonterminal(int numNonterminals, bool allowStart = true) { int num; if (allowStart) { num = _rand.Next(0, numNonterminals); } else { num = _rand.Next(1, numNonterminals); } return(Nonterminal.Of("X_" + num)); }
/// <summary> /// Turns a string like /// <X> -> <X> 'a' <X> /// into a production. /// Nonterminals must be surrounded by angled brackets, terminals must be surrounded by single quotes, and everything must be separated by spaces. /// </summary> /// <param name="s"></param> /// <returns></returns> public static Production Production(string s) { var match = ProductionRegex.Match(s); if (!match.Success) { throw new Exception("Didn't find valid string"); } var lhsMatch = match.Groups["lhs"]; var ntMatch = match.Groups["nt"]; var tMatch = match.Groups["t"]; var weightMatch = match.Groups["weight"]; if (!lhsMatch.Success) { throw new Exception("Didn't find LHS"); } double weight; if (!double.TryParse(weightMatch.Value, out weight)) { weight = 1.0; } var rhsList = new SortedList <int, Word>(); foreach (Capture capture in ntMatch.Captures) { var word = Nonterminal.Of(capture.Value); rhsList.Add(capture.Index, word); } foreach (Capture capture in tMatch.Captures) { var word = Terminal.Of(capture.Value); rhsList.Add(capture.Index, word); } var rhs = new Sentence(rhsList.Values); var lhs = Nonterminal.Of(lhsMatch.Value); var retval = new Production(lhs, rhs, weight); return(retval); }
public Production(string lhsName, Word rhsOnlyWord, double weight = 1.0) : this(Nonterminal.Of(lhsName), new Sentence(rhsOnlyWord), weight) { }
public Production(string lhsName, Sentence rhs, double weight = 1.0) : this(Nonterminal.Of(lhsName), rhs, weight) { }