public void DeterminizarAutomato() { CriacaoNovoEstadoDeterministico(); tokens.Sort(); AutomatoFinito afd; for (int i = 0; i < TabelaAFND.Count; i++) { afd = new AutomatoFinito(); afd.estadoDestino = new List <Estado>(); afd.estadoOrigem = new List <Estado>(); string estadoOrigem = ""; for (int b = 0; b < TabelaAFND[i].estadoOrigem.Count; b++) { estadoOrigem += TabelaAFND[i].estadoOrigem[b].nome; } if (AuxEstados.Contains(estadoOrigem)) { string estado = ""; bool final = false; afd.token = TabelaAFND[i].token; afd.estadoOrigem = TabelaAFND[i].estadoOrigem; afd.estadoDestino = TabelaAFND[i].estadoDestino; for (int j = 0; j < TabelaAFND[i].estadoOrigem.Count; j++) { estado += TabelaAFND[i].estadoOrigem[j].nome; if (!final) { final = TabelaAFND[i].estadoOrigem[j].final; } } if (!ExisteEstadoAFD(estado)) { estadosAFD.Add(new Estado(nome: estado, inicial: false, final: final)); } estado = ""; final = false; for (int j = 0; j < TabelaAFND[i].estadoDestino.Count; j++) { estado += TabelaAFND[i].estadoDestino[j].nome; if (!final) { final = TabelaAFND[i].estadoDestino[j].final; } } if (!ExisteEstadoAFD(estado)) { estadosAFD.Add(new Estado(nome: estado, inicial: false, final: final)); } TabelaAFD.Add(afd); } } estadosAFD.Add(new Estado(nome: "X", inicial: false, final: true)); }
public void criarTokens() { var afnd = new AutomatoFinito(); bool Existe = false; int n = 0; afnd.estadoOrigem = new List <Estado>(); if (linha.texto[counter] == ';') { estados[estados.Count - 1].final = true; return; } else if (TabelaAFND.Count == 0 || counter == 0) { afnd.token = linha.texto[counter].ToString(); afnd.estadoOrigem.Add(CriarEstadosTokens(origem: true, final: false)); for (n = 0; n < TabelaAFND.Count && !Existe; n++) { for (int x = 0; x < TabelaAFND[n].estadoOrigem.Count; x++) { if ((TabelaAFND[n].token == afnd.token) && (TabelaAFND[n].estadoOrigem[x].nome == afnd.estadoOrigem[afnd.estadoOrigem.Count - 1].nome)) { Existe = true; break; } } } } else { afnd.token = linha.texto[counter].ToString(); afnd.estadoOrigem.Add(estados[estados.Count - 1]); for (n = 0; n < TabelaAFND.Count && !Existe; n++) { for (int x = 0; x < TabelaAFND[n].estadoOrigem.Count; x++) { if ((TabelaAFND[n].token == afnd.token) && (TabelaAFND[n].estadoOrigem[x].nome == afnd.estadoOrigem[afnd.estadoOrigem.Count - 1].nome)) { Existe = true; break; } } } } if (Existe) { afnd.estadoDestino = new List <Estado>(); TabelaAFND[n - 1].estadoDestino.Add(CriarEstadosTokens(origem: false, final: false)); } else { afnd.estadoDestino = new List <Estado>(); afnd.estadoDestino.Add(CriarEstadosTokens(origem: false, final: false)); TabelaAFND.Add(afnd); } if (!ExisteToken(linha.texto[counter].ToString()) && linha.texto[counter] != ';') { tokens.Add(linha.texto[counter].ToString()); } }
public void CriarTokensGramatica() { int index, aux = 0, n = 0; string token = ""; Estado estado = new Estado(null, false, false); var afnd = new AutomatoFinito(); bool Existe = false; afnd.estadoDestino = new List <Estado>(); afnd.estadoOrigem = new List <Estado>(); if (counter == 0) { for (index = 1; index < linha.texto.Length; index++) { if (linha.texto[counter + index] == '>') { break; } } linha.estadoOrigem = CriarEstadosGramatica(linha.texto.Substring(counter + 1, index - 1)); } else if (linha.texto[counter] == '=' || linha.texto[counter] == '|') { for (index = 1; (index + counter) < linha.texto.Length; index++) { if ((linha.texto[counter + index] == '<') && (aux == 0)) { token = linha.texto.Substring(counter + 1, index - 1); aux = counter + index; index = 0; } else if (aux != 0) { if (linha.texto[aux + index] == '>') { estado = new Estado(nome: linha.texto.Substring(aux + 1, index - 1), inicial: false, final: false); break; } } else if (linha.texto[counter + index] == '|') { string nome = "";; token = linha.texto.Substring(counter + 1, index - 1); if (token.Equals("&")) { break; } estadosFim.Add(token); for (int i = 0; i < estadosFim.Count; i++) { nome += "'"; } estado = new Estado(nome: "@" + nome, inicial: false, final: true); estados.Add(estado); break; } } if (!string.IsNullOrEmpty(token) && token != "&") { afnd.token = token; afnd.estadoOrigem.Add(linha.estadoOrigem); for (n = 0; n < TabelaAFND.Count && !Existe; n++) { for (int x = 0; x < TabelaAFND[n].estadoOrigem.Count; x++) { if ((TabelaAFND[n].token == afnd.token) && (TabelaAFND[n].estadoOrigem[x].nome == linha.estadoOrigem.nome)) { Existe = true; break; } } } if (Existe) { TabelaAFND[n - 1].estadoDestino.Add(estado); } else { afnd.estadoDestino.Add(estado); TabelaAFND.Add(afnd); } } if (!ExisteToken(token)) { tokens.Add(token); } } }
private void CriacaoNovoEstadoDeterministico() { AutomatoFinito afd = new AutomatoFinito(); AuxEstados.Add("S"); string estadoOrigem = ""; string estadoDestino = ""; bool Existe = false; for (int i = 0; i < TabelaAFND.Count; i++) { afd = new AutomatoFinito(); afd.estadoDestino = new List <Estado>(); afd.estadoOrigem = new List <Estado>(); TabelaAFND[i].estadoOrigem = TabelaAFND[i].estadoOrigem.OrderBy(l => l.nome).ToList();; TabelaAFND[i].estadoDestino = TabelaAFND[i].estadoDestino.OrderBy(l => l.nome).ToList();; if (TabelaAFND[i].estadoDestino.Count > 1) { afd.token = TabelaAFND[i].token; afd.estadoOrigem = TabelaAFND[i].estadoOrigem; afd.estadoDestino = TabelaAFND[i].estadoDestino; AutomatoFinito automato; for (int j = 0; j < tokens.Count; j++) { automato = new AutomatoFinito(); automato.estadoDestino = new List <Estado>(); automato.estadoOrigem = new List <Estado>(); for (int n = 0; n < afd.estadoDestino.Count; n++) { for (int x = 0; x < TabelaAFND.Count; x++) { estadoOrigem = ""; for (int b = 0; b < TabelaAFND[x].estadoOrigem.Count; b++) { estadoOrigem += TabelaAFND[x].estadoOrigem[b].nome; } if ((afd.estadoDestino[n].nome == estadoOrigem) && (tokens[j] == TabelaAFND[x].token)) { estadoDestino = ""; for (int a = 0; a < TabelaAFND[x].estadoDestino.Count; a++) { Existe = false; for (int z = 0; z < automato.estadoDestino.Count; z++) { if (automato.estadoDestino[z].nome == TabelaAFND[x].estadoDestino[a].nome) { Existe = true; break; } } if (Existe) { break; } automato.estadoDestino.Add(TabelaAFND[x].estadoDestino[a]); estadoDestino += TabelaAFND[x].estadoDestino[a].nome; automato.estadoDestino = automato.estadoDestino.OrderBy(l => l.nome).ToList(); } n++; x = -1; if (n == afd.estadoDestino.Count) { break; } } } } automato.token = tokens[j]; automato.estadoOrigem = afd.estadoDestino; Existe = false; string auxDestino, auxOrigem; estadoDestino = ""; estadoOrigem = ""; for (int b = 0; b < automato.estadoOrigem.Count; b++) { estadoOrigem += automato.estadoOrigem[b].nome; } for (int b = 0; b < automato.estadoDestino.Count; b++) { estadoDestino += automato.estadoDestino[b].nome; } if (automato.estadoDestino.Count != 0 && automato.estadoOrigem.Count != 0) { for (int y = 0; y < TabelaAFND.Count; y++) { auxDestino = ""; auxOrigem = ""; for (int b = 0; b < TabelaAFND[y].estadoOrigem.Count; b++) { auxOrigem += TabelaAFND[y].estadoOrigem[b].nome; } for (int b = 0; b < TabelaAFND[y].estadoDestino.Count; b++) { auxDestino += TabelaAFND[y].estadoDestino[b].nome; } if (automato.token == TabelaAFND[y].token && estadoDestino == auxDestino && estadoOrigem == auxOrigem) { Existe = true; break; } } if (!Existe) { TabelaAFND.Add(automato); } } else { continue; } estadoDestino = ""; for (int b = 0; b < automato.estadoDestino.Count; b++) { estadoDestino += automato.estadoDestino[b].nome; } if (estadoDestino.Length > 0) { AuxEstados.Add(estadoDestino); } } } else { AuxEstados.Add(TabelaAFND[i].estadoDestino[0].nome); } } }