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;
}
}