public ProductionRule(string line) { string[] ruleAndConditions = line.Trim().Split('&'); //Parse the production rule string[] ruleParts = ruleAndConditions[0].Trim().Split(' '); Probability = Convert.ToDouble(ruleParts[0]); LHS = new Symbol() { Type = SymbolType.Nonterminal, Text = ruleParts[1].Trim() }; List<Symbol> rhs = new List<Symbol>(); for (int i = 3; i < ruleParts.Length; i++) { Symbol newSymbol; ruleParts[i] = ruleParts[i].Trim(); if (ruleParts[i].StartsWith(@"'")) newSymbol = new Symbol() { Type = SymbolType.Terminal, Text = ruleParts[i].Replace('\'', ' ').Trim() }; else newSymbol = new Symbol() { Type = SymbolType.Nonterminal, Text = ruleParts[i] }; rhs.Add(newSymbol); } RHS = rhs.ToArray(); //Parse the special conditions if (ruleAndConditions.Length > 1) Conditions = SpecialCondition.ParseConditions(ruleAndConditions[1]); else Conditions = new SpecialCondition[0]; }
private void HandleRulesWithSpecialConditions( ProductionRule rule, int begin, int part, int span, string key) { bool isBinary = rule.RHS.Length == 2; int[] tableKey = GetTableKey(begin + part, span - part); bool hasMatch = _table[tableKey].ContainsKey(rule.RHS[1].Text); if (isBinary && hasMatch) { bool satisfied = true; TableEntry left = _table[GetTableKey(begin, part)][key]; TableEntry right = _table[GetTableKey(begin + part, span - part)][rule.RHS[1].Text]; // Loop through all special conditions for (int i = 0; i < rule.Conditions.Length; i++) { SpecialCondition condition = rule.Conditions[i]; satisfied = condition.Check(left, right, _tokens); if (!satisfied) { break; } } // add entry to table if all rule conditions are satisfied if (satisfied) { AddEntry(new TableEntry(GetTableKey(begin, span), rule, new TableEntry[] { left, right })); } } }