private Rule parseRule(string s, List<string> terminals, List<string> nonterminals) { Rule r = new Rule(); int leftLength = s.IndexOf(" -> "); if (leftLength != 1) throw new FormatException("Некорректный формат файла."); r.Left.Sym = s.First().ToString(); if (s.Length == 6 && s[5] == 'e') { r.Right.Add(new EmptySymbol()); return r; } for (int i=5; i<s.Length; i++) { if (s[i] == 'e') throw new FormatException("Некорректный формат файла."); if (terminals.Contains(s[i].ToString())) r.Right.Add(new Terminal(s[i].ToString())); else if (nonterminals.Contains(s[i].ToString())) r.Right.Add(new Nonterminal(s[i].ToString())); else throw new FormatException("Некорректный формат файла."); } return r; }
private List<Symbol> buildRightPartOfNewRule(List<Symbol> firstTerminals, List<Symbol> restOfString, Rule r) { List<Symbol> rightPart = new List<Symbol>(firstTerminals); List<Symbol> symStrForNewNonterminal = new List<Symbol>(r.Right); Nonterminal newNonterminal; symStrForNewNonterminal.AddRange(restOfString); newNonterminal = symbolStringToNonterminal(symStrForNewNonterminal); if (!_nonterminalsForGrammarWithoutERules.Exists(A => A.Sym == newNonterminal.Sym)) _nonterminalsForGrammarWithoutERules.Add(newNonterminal); rightPart.Add(newNonterminal); return rightPart; }
private void breakRuleForNonnullableGrammar(Rule r, Grammar g, out List<Nonterminal> firstVanishing, out List<Symbol> rest) { firstVanishing = new List<Nonterminal>(); rest = new List<Symbol>(); foreach (Symbol s in r.Right) { if (rest.Count > 0) rest.Add(s); else if (g.isVanishingSymbol(s)) firstVanishing.Add((Nonterminal)s); else rest.Add(s); } }
public Rule(Rule r) { _left = new Nonterminal(r.Left); _right = new List<Symbol>(r.Right); }
private Rule createRuleWithNoFisrtVanishing(Rule rule, List<Symbol> rest) { Rule r = new Rule(); r.Left = new Nonterminal(rule.Left); r.Right.AddRange(rest); return r; }
private List<Rule> createRulesForLeftVanishing(List<Rule> curRules, Nonterminal left) { List<Rule> newRules = new List<Rule>(); foreach (Rule r in curRules) { Rule newRule = new Rule(r); newRule.Left = _nonnullableNonterminalsMap[left.Sym]; newRules.Add(newRule); } return newRules; }
private List<Rule> createNewRulesForFisrtVanishing(Rule rule, List<Nonterminal> firstVanishing, List<Symbol> rest) { List<Rule> rules= new List<Rule>(); List<Nonterminal> firstPart = new List<Nonterminal>(firstVanishing); foreach (Nonterminal nt in firstVanishing) { firstPart[0] = _nonnullableNonterminalsMap[firstPart.First().Sym]; Rule r = new Rule(); r.Left = new Nonterminal(rule.Left); r.Right.AddRange(firstPart); r.Right.Concat(rest); rules.Add(r); firstPart.RemoveAt(0); } return rules; }
private List<Rule> buildRulesForNonterminal(Nonterminal nonterminal, Grammar g) { List<Rule> newRules = new List<Rule>(); List<Rule> rules = new List<Rule>(); List<Symbol> symStr = nonterminalToSymbolString(nonterminal); List<Symbol> firstTerminals = symStr.TakeWhile(ss => ss is Terminal).ToList(); foreach (Symbol nt in symStr) if (nt is Nonterminal) { int ind = symStr.IndexOf(nt); List<Symbol> restOfString = symStr.GetRange(ind + 1, symStr.Count - ind - 1); foreach (Rule r in g.Rules) { if (r.Left.Sym == nt.Sym && !r.isERule()) { Rule newRule = new Rule(); newRule.Left = new Nonterminal(nonterminal); newRule.Right = buildRightPartOfNewRule(firstTerminals, restOfString, r); newRules.Add(newRule); } } if (ind == 0) break; } if (firstTerminals.Count != 0) newRules.Add(new Rule((Nonterminal)nonterminal, firstTerminals)); return newRules; }