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 AddRule(ProductionRule rule) { //ASSUMPTION: LHS of first rule in grammar file is root symbol if (_rules.Count == 0) { _rootSymbol = rule.LHS; _rhsIndex.Add(_rootSymbol.Text, new List<ProductionRule>()); } _rules.Add(rule); AddToLhsIndex(rule); AddToRhsIndex(rule); if (rule.RHS.Length == 1 && rule.RHS[0].Type == SymbolType.Terminal) AddToTerminalIndex(rule); if (rule.RHS[0].Type == SymbolType.Nonterminal) AddToNonterminalIndex(rule); }