//проверка пустоты правой цепочки private bool ContainEps(Prule p) { if (p.rightChain.Contains("")) { return(true); } return(false); }
public void DebugPrule(Prule p) { string right = ""; for (int i = 0; i < p.rightChain.Count; i++) { right += p.rightChain[i].ToString(); } Console.WriteLine(p.leftNoTerm + " -> " + right + " "); }
//все символы в правиле private ArrayList SymbInRules(Prule p) { ArrayList SymbInRules = new ArrayList() { p.LeftNoTerm }; for (int i = 0; i < p.rightChain.Count; i++) { SymbInRules.Add(p.rightChain[i].ToString()); } return(SymbInRules); }
public List <Prule> getRules(string noTermSymbol) { List <Prule> result = new List <Prule>(); for (int i = 0; i < G.Prules.Count; ++i) { Prule currRule = (Prule)G.Prules[i]; if (currRule.leftNoTerm == noTermSymbol) { result.Add(currRule); } } return(result); }
public void ComputeFirstSets(myGrammar grammar) { for (int i = 0; i < grammar.T.Count; i++) { FirstSet[(string)grammar.T[i]] = new HashSet <string>() { grammar.T[i].ToString() } } ; // FIRST[c] = {c}*/ for (int i = 0; i < grammar.V.Count; i++) { FirstSet[(string)grammar.V[i]] = new HashSet <string>(); //First[x] = empty list } bool changes = true; while (changes) { changes = false; for (int i = 0; i < grammar.Prules.Count; ++i) { // Для каждого правила X-> Y0Y1…Yn Prule currRule = (Prule)(grammar.Prules[i]); string X = currRule.LeftNoTerm; List <string> Y = currRule.rightChain.Cast <string>().ToList(); for (int k = 0; k < Y.Count; k++) { foreach (string currFirstSymb in (HashSet <string>)FirstSet[Y[k]]) { if (((HashSet <string>)FirstSet[X]).Add(currFirstSymb)) //Добавить а в FirstSets[X] { changes = true; } } if (!((HashSet <string>)FirstSet[Y[k]]).Contains("")) { break; } } } } // пока вносятся изменения }
// public myGrammar EpsDelete() // { // Console.WriteLine("\t\tDeleting epsylon rules"); // Console.WriteLine("Executing: "); // ArrayList Ve = ShortNoTerm(); Debug("Ve", Ve); // ArrayList P1 = new ArrayList(); // ArrayList V1 = this.V; // foreach (Prule p in Prules) // { // if (!ContainEps(p)) // { // //DebugPrule(p); // P1.Add(p); // Prule p1 = new Prule(p.leftNoTerm, TermReturn(p.rightChain)); //// DebugPrule(p1); // if (p.rightChain.Count != 1) // { // Console.WriteLine("No contain"); // P1.Add(p1); // } // else Console.WriteLine("Contain"); // } // } // if (Ve.Contains(this.S0)) // { // V1.Add("S1"); // P1.Add(new Prule("S1", new ArrayList() { this.S0 })); // P1.Add(new Prule("S1", new ArrayList() { "" })); // return new myGrammar(this.T, V1, P1, "S1"); // } // else // return new myGrammar(this.T, V1, P1, this.S0); // } //удаление цепных правил public myGrammar ChainRuleDelete() { Console.WriteLine("\tChainRule Deleting:"); Console.WriteLine("Executing: "); // Поиск цепных пар List <List <string> > chain_pair_list = new List <List <string> >(); ArrayList chain_rules = new ArrayList(); foreach (string v in this.V) { List <string> chain_pair = new List <string>(); chain_pair.Add(v); chain_pair.Add(v); chain_pair_list.Add(chain_pair); } Console.WriteLine("ChainRules:"); foreach (Prule p in this.Prules) { if (TermReturn(p.RightChain) == null && NoTermReturn(p.RightChain) != null && NoTermReturn(p.RightChain).Count == 1) { chain_rules.Add(p); DebugPrule(p); for (int i = 0; i < chain_pair_list.Count; ++i) { List <string> chain_pair = new List <string>(chain_pair_list[i]); if (chain_pair[1] == p.LeftNoTerm) { List <string> chain_pair1 = new List <string>(); chain_pair1.Add(chain_pair[0]); chain_pair1.Add(NoTermReturn(p.RightChain)[0].ToString()); chain_pair_list.Add(chain_pair1); } } } } Console.WriteLine("Deleting..."); // Работа оснавная ArrayList P = new ArrayList(); foreach (List <string> chain_pair in chain_pair_list) { foreach (Prule p in this.Prules) { if (p.LeftNoTerm == chain_pair[1] && !(TermReturn(p.RightChain) == null && NoTermReturn(p.RightChain) != null && NoTermReturn(p.RightChain).Count == 1)) { Prule P_1 = new Prule(chain_pair[0], p.RightChain); if (!P.Contains(P_1)) { P.Add(P_1); } } } } /* foreach (Prule chr in chain_rules) * { * P.Remove(chr); * } */ Console.WriteLine("\tChainrules have been deleted;"); return(new myGrammar(this.T, this.V, P, this.S0)); }
public void ComputeFollowSets(myGrammar grammar) { for (int i = 0; i < grammar.V.Count; i++) { FollowSet[(string)grammar.V[i]] = new HashSet <string>(); } FollowSet[G.S0] = new HashSet <string>() { "" }; bool changes = true; while (changes) { changes = false; for (int i = 0; i < grammar.Prules.Count; ++i) { // Для каждого правила X-> Y0Y1…Yn Prule currRule = (Prule)(grammar.Prules[i]); List <string> rightChain = currRule.RightChain.Cast <string>().ToList(); for (int indexOfSymbol = 0; indexOfSymbol < rightChain.Count; ++indexOfSymbol) { string currSymbol = rightChain[indexOfSymbol]; if (G.T.Contains(currSymbol)) { continue; } if (indexOfSymbol == rightChain.Count - 1) { foreach (string currFollowSymbol in (HashSet <string>)FollowSet[currRule.LeftNoTerm]) { if (((HashSet <string>)FollowSet[rightChain[indexOfSymbol]]).Add(currFollowSymbol)) { changes = true; } } } else { List <string> currFirst = First(rightChain[indexOfSymbol + 1]); bool epsFound = false; foreach (var currFirstSymbol in currFirst) { if (currFirstSymbol != "") { if (((HashSet <string>)FollowSet[rightChain[indexOfSymbol]]).Add(currFirstSymbol)) { changes = true; } } else { epsFound = true; } } if (epsFound) { foreach (string currFollowSymbol in (HashSet <string>)FollowSet[currRule.LeftNoTerm]) { if (((HashSet <string>)FollowSet[rightChain[indexOfSymbol]]).Add(currFollowSymbol)) { changes = true; } } } } } } } }