private static Expression ParseHelper(string[] words, int start, int end, Dictionary<string, Operator> operators) { Expression value1 = null; Expression value2 = null; Operator op = null; for (int i = start; i <= end; ++i) { string word = words[i]; // Console.WriteLine ("process " + word); // ignore parens for now if (word == "(" || word == ")") continue; int n = 0; bool isNumeric = int.TryParse (word, out n); if (isNumeric) { if (value1 == null) value1 = new Value (n); else if (value2 == null) value2 = new Value (n); else return null; } else if (op == null) { if (operators.ContainsKey (word)) { op = operators [word]; } else return null; } else if (value1 != null && value2 != null && operators.ContainsKey(word)) { Operator newOp = operators [word]; if (newOp.Priority >= op.Priority) { value1 = new Formula (value1, value2, op); op = operators [word]; value2 = null; } else { value2 = ParseHelper (words, i - 1, end, operators); break; } } else { return null; } } if (value1 == null || value2 == null || op == null) { return null; } return new Formula (value1, value2, op); }