Exemplo n.º 1
0
        // Concatena 2 autômatos
        public Automato Interseccao(Automato a2)
        {
            Automato r = new Automato(ID * a2.ID, this);

            r.simbolos.UnionWith(a2.simbolos);
            r.estados.UnionWith(a2.estados);

            foreach (KeyValuePair <KeyTransicao, Transicao> tra in a2.transicoes)
            {
                r.addTransicao(tra.Value);
            }

            HashSet <string> temp = new HashSet <string>
            {
                a2.estadoInicial
            };

            foreach (string ef in r.estadosFinais)
            {
                r.addTransicao(ef, "&", temp);
            }

            r.estadosFinais.Clear();
            r.estadosFinais.UnionWith(a2.estadosFinais);

            return(r);
        }
Exemplo n.º 2
0
        //Gera os estados do Automato de acordo com a tabela posicao_seguinte no algoritmo Aho
        private Automato geraEstados(Automato automato, Stack <HashSet <int> > stack)
        {
            HashSet <int> estado1, estado2 = new HashSet <int>();

            while (stack.Count() > 0)
            {
                estado1 = stack.Pop();
                string nomeEstado1 = geraNomeDoEstado(estado1);
                estado2 = new HashSet <int>();
                foreach (var simbol in arvore.simbolos.ToList())
                {
                    estado2 = new HashSet <int>();
                    foreach (var i in estado1.ToList())
                    {
                        if ((string)arvore.folhas[i] == simbol)
                        {
                            if (arvore.posicao_seguinte.ContainsKey(i))
                            {
                                estado2.UnionWith((HashSet <int>)arvore.posicao_seguinte[i]);
                            }
                        }
                    }
                    if (estado2.Count > 0)
                    {
                        string nomeEstado2 = geraNomeDoEstado(estado2);

                        if (estado1.SetEquals(estado2))
                        {
                            automato.addTransicao(nomeEstado1, simbol, nomeEstado1);
                        }
                        else
                        {
                            bool contemTransacao = false;
                            var  t = automato.GetTransicao(nomeEstado1, simbol);
                            foreach (var e in t.estado2)
                            {
                                if (e == nomeEstado2)
                                {
                                    contemTransacao = true;
                                }
                            }
                            //foreach (var k in automato.transicoes.Keys)
                            //    if (k[0] == nomeEstado1 && k[1] == simbol)
                            //        contemTransacao = true;
                            if (!contemTransacao)
                            {
                                if (!automato.estados.Contains(nomeEstado2))
                                {
                                    stack.Push(estado2);
                                    automato.addEstado(nomeEstado2);
                                }
                                automato.addTransicao(nomeEstado1, simbol, nomeEstado2);
                                //estado2.Clear();
                            }
                        }
                    }
                }
            }
            return(automato);
        }
Exemplo n.º 3
0
        // Faz a união de 2 autômatos
        public Automato Uniao(Automato a2)
        {
            Automato r = new Automato(ID * a2.ID, this);

            r.estadosFinais.UnionWith(a2.estadosFinais);
            r.simbolos.UnionWith(a2.simbolos);
            r.simbolos.Add("&");

            foreach (KeyValuePair <KeyTransicao, Transicao> tra in a2.transicoes)
            {
                r.addTransicao(tra.Value);
            }

            r.addEstado("+Uniao");
            HashSet <string> temp = new HashSet <string>
            {
                a2.estadoInicial,
                estadoInicial
            };

            r.addTransicao("+Uniao", "&", temp);
            return(r);
        }
Exemplo n.º 4
0
        public Automato eliminaEstadosInalcancaveis(Automato automato, Automato miniAuto, string estado)
        {
            foreach (string s in automato.simbolos)
            {
                HashSet <string> temp = automato.GetTransicao(estado, s).estado2;
                foreach (string e in temp)
                {
                    bool exists = miniAuto.estados.Contains(e);
                    if (!exists)
                    {
                        miniAuto.addEstado(e);
                    }

                    miniAuto.addTransicao(estado, s, e);
                    if (!exists /*&& !miniAuto.estadoInicial.Contains(e)*/)
                    {
                        eliminaEstadosInalcancaveis(automato, miniAuto, e);
                    }
                }
            }
            return(miniAuto);
        }
Exemplo n.º 5
0
        // Gera um autômato a partir dessa Gramática
        public Automato GetAutomato()
        {
            Automato ret  = new Automato();
            Random   rand = new Random();

            string novo = rand.Next(100).ToString();

            foreach (char NT in SimbolosNaoTerminais)
            {
                ret.addEstado(NT.ToString());
            }
            ret.addEstado(novo);

            ret.estadoInicial = Inicial.ToString();

            foreach (char T in SimbolosTerminais)
            {
                ret.simbolos.Add(T.ToString());
            }

            ret.estadosFinais.Add(novo);



            foreach (KeyValuePair <char, Regular> Pr in Producoes)
            {
                foreach (string P in Pr.Value.Proximos)
                {
                    string nt = "";
                    string t  = "";

                    foreach (char NT in SimbolosNaoTerminais)
                    {
                        if (P.Contains(NT))
                        {
                            nt += NT;
                        }
                    }
                    foreach (char T in SimbolosTerminais)
                    {
                        if (P.Contains(T))
                        {
                            t += T;
                        }
                    }

                    if (nt.Length != 0 && t.Length != 0)
                    {
                        foreach (char c in t)
                        {
                            ret.addTransicao(Pr.Key.ToString(), c.ToString(), nt);
                        }
                    }
                    else if (nt.Length == 0)
                    {
                        foreach (char c in t)
                        {
                            ret.addTransicao(Pr.Key.ToString(), c.ToString(), novo);
                        }
                    }
                }
            }

            return(ret);
        }