private void PreencherRegrasArParaArEArParaMaiorAr() { RolesArToGreatAs = new List <RuleProduction>(); RolesArToAr = new List <RuleProduction>(); RolesSubstituiArGreatAs = new List <RuleProduction>(); RolesReplaceArParaAr = new List <RuleProduction>(); for (int i = 0; i < Normalized.Rules.Count; i++) { RuleProduction rAtual = Normalized.Rules[i]; var destino = rAtual.Destiny.Copy(); if (rAtual.IsFirstVariable()) { if (NewNames.ContainsKey(rAtual.Source)) { if (NewNames[rAtual.Source].Id > NewNames[rAtual.FirstDestiny()].Id) { Normalized.Rules.Remove(rAtual); i--; List <RuleProduction> regrasDestino = Normalized.GetRules(rAtual.FirstDestiny()); RolesArToGreatAs.Add(rAtual); //Remoção de Regra, decrementa i for (int j = 0; j < regrasDestino.Count; j++) { RuleProduction regraTemp = Normalized.AddRule(rAtual.Source, regrasDestino[j].Destiny.AddRange(rAtual.SkipFirstDestiny())); RolesSubstituiArGreatAs.Add(regraTemp); } } else if (NewNames[rAtual.Source].Name == NewNames[rAtual.FirstDestiny()].Name) { RolesArToAr.Add(rAtual); var bAtual = new Symbol(GetNewId(), string.Empty, false); Normalized.Variables.Add(bAtual); destino = destino.SkipFirst(); RuleProduction regraTemp = Normalized.AddRule(bAtual, destino); RolesReplaceArParaAr.Add(regraTemp); regraTemp = Normalized.AddRule(bAtual, destino.Add(bAtual)); RolesReplaceArParaAr.Add(regraTemp); //Removi volta o i Normalized.Rules.Remove(rAtual); i--; List <RuleProduction> regrasOrigem = Normalized.GetRules(rAtual.Source); for (int j = 0; j < regrasOrigem.Count; j++) { regraTemp = Normalized.AddRule(rAtual.Source, regrasOrigem[j].Destiny.Add(bAtual)); RolesReplaceArParaAr.Add(regraTemp); } } } } } }
private void ColocarTerminalInicio() { for (int i = 0; i < Normalized.Rules.Count; i++) { RuleProduction rAtual = Normalized.Rules[i]; if (!rAtual.Destiny[0].Terminal) { //Removi regra volta regra Normalized.Rules.Remove(rAtual); i--; var destino = rAtual.SkipFirstDestiny(); List <RuleProduction> regras = Normalized.GetRules(rAtual.FirstDestiny()); for (int j = 0; j < regras.Count; j++) { RuleProduction regraTemp = Normalized.AddRule(rAtual.Source, regras[j].Destiny.Copy().AddRange(destino)); } } } }
//Removendo terminais no meio private void RemoveTerminalsNoMeio() { for (int i = 0; i < Normalized.Rules.Count; i++) { RuleProduction rAtual = Normalized.Rules[i]; for (int j = 1; j < rAtual.Destiny.Count; j++) { if (rAtual.Destiny[j].Terminal) { var variavelExclusiva = Normalized.CapturarVariavelExclusiva(rAtual.Destiny[j]); if (variavelExclusiva == Symbol.EmptyVariable) { variavelExclusiva = new Symbol(GetNewId(), string.Empty, false); Normalized.Variables.Add(variavelExclusiva); Normalized.AddRule(variavelExclusiva, rAtual.Destiny); } rAtual.Destiny[j] = variavelExclusiva; } } } }
private void Normalize() { Normalized.VariableStart = simplified.VariableStart; Normalized.Terminals.AddRange(simplified.Terminals); Normalized.Variables.AddRange(simplified.Variables); for (int i = 0; i < simplified.Rules.Count; i++) { RuleProduction rgActual = simplified.Rules[i]; var destiny = rgActual.Destiny; foreach (var item in DicVariablesTerminals) { destiny = destiny.Replace(item.Key, item.Value); } if (destiny.Unitary) { Normalized.AddRule(rgActual.Source, destiny); } else { if (destiny.Count == 2 && !rgActual.DestinyContainsTerminal()) { Normalized.AddRule(rgActual.Source, destiny); } else { List <SymbolList> destinys = new List <SymbolList>(); while (destiny.Count > 2) { destinys.Clear(); for (int k = 0; k < destiny.Count; k += 2) { if (k + 1 < destiny.Count) { destinys.Add(new SymbolList(destiny[k], destiny[k + 1])); } else { destinys.Add(new SymbolList(destiny[k])); } } destiny = new SymbolList(); foreach (var des in destinys) { Symbol destinyVariable = des[0]; if (!des.Unitary) { destinyVariable = Normalized.GetExclusiveVars(des); if (destinyVariable == Symbol.EmptyVariable) { destinyVariable = new Symbol(GetNewId(), string.Empty, false); Normalized.Variables.Add(destinyVariable); Normalized.AddRule(destinyVariable, des); } } destiny.Add(destinyVariable); } } Normalized.AddRule(rgActual.Source, destiny); } } } }