public Grammar(List<Nonterminal> nonterminals, List<Terminal> terminals, List<Rule> rules, Nonterminal distinguishedSymbol) { _nonterminals = new List<Nonterminal>(nonterminals); _terminals = new List<Terminal>(terminals); _rules = new List<Rule>(rules); _distinguishedSymbol = distinguishedSymbol; //findVanishingSymbols(); }
public Grammar(Grammar g) { _nonterminals = new List<Nonterminal>(g.Nonterminals); _terminals = new List<Terminal>(g.Terminals); _rules = new List<Rule>(g.Rules); _distinguishedSymbol = new Nonterminal(g.DistinguishedSymbol); _vanishingSymbols = new List<Nonterminal>(g.VanishingSymbols); }
public Nonterminal(Nonterminal nt) { _sym = nt.Sym; }
public Rule(Nonterminal left, List<Symbol> right) { _left = new Nonterminal(left); _right = new List<Symbol>(right); }
public Rule(Rule r) { _left = new Nonterminal(r.Left); _right = new List<Symbol>(r.Right); }
public Rule() { _left = new Nonterminal(); _right = new List<Symbol>(); }
private List<Symbol> nonterminalToSymbolString(Nonterminal nt) { return _nonterminalSymbolStringMap[nt.Sym]; }
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<Nonterminal> bulidNonterminalsForNonnullableGrammar(Grammar g) { _nonnullableNonterminalsMap = new Dictionary<string, Nonterminal>(); List<Nonterminal> N = new List<Nonterminal>(g.Nonterminals); foreach (Nonterminal vs in g.VanishingSymbols) { Nonterminal newNonterminal = new Nonterminal(String.Format("{0}*", vs.Sym)); N.Add(newNonterminal); _nonnullableNonterminalsMap.Add(vs.Sym, newNonterminal); } return N; }
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; }