Ejemplo n.º 1
0
        public void lerTabelaLALR()
        {
            // le a tabela que o gold parser gera
            System.Xml.XmlNodeList xmlNodeList = xmlTabela.GetElementsByTagName("LALRState");

            tabelaLALR.estados = new int[xmlNodeList.Count];
            tabelaLALR.acoes   = new AcaoTabelaLALR[tabelaLALR.estados.Count(), tabelaLALR.simbolos.Count() + 1];

            foreach (System.Xml.XmlNode nodoEstado in xmlNodeList)
            {
                int indiceEstado = int.Parse(nodoEstado.Attributes["Index"].InnerText);
                tabelaLALR.estados[indiceEstado] = indiceEstado;

                foreach (System.Xml.XmlNode nodoAcao in nodoEstado.ChildNodes)
                {
                    int            indiceAcao = int.Parse(nodoAcao.Attributes["SymbolIndex"].InnerText);
                    AcaoTabelaLALR acao       = new AcaoTabelaLALR();
                    Enum.TryParse(nodoAcao.Attributes["Action"].InnerText, out acao.acao);
                    acao.valor = int.Parse(nodoAcao.Attributes["Value"].InnerText);
                    tabelaLALR.acoes[indiceEstado, indiceAcao] = acao;
                }
            }
        }
Ejemplo n.º 2
0
        public List <string> analisar(List <TabelaSimbolos> tokens)
        {
            List <string> listaRetorno = new List <string>();

            List <int> fita              = mapearFitaSaidaAnaliseSintatica(tokens);
            List <int> pilha             = new List <int>();
            int        indiceLeituraFita = 0;

            pilha.Add(tabelaLALR.estados[0]);

            while (true)
            {
                int estadoTopoPilha = pilha.Last();
                int tokenAtualFita  = fita[indiceLeituraFita];

                AcaoTabelaLALR acaoTabela = tabelaLALR.acoes[estadoTopoPilha, tokenAtualFita];

                if (acaoTabela == null)
                {
                    if (indiceLeituraFita == tokens.Count)
                    {
                        TabelaSimbolos token = tokens[indiceLeituraFita - 1];
                        listaRetorno.Add("Fim de arquivo inesperado. O token " + token.rotulo + ", na linha " + token.linha + " não um conclui uma expressão.");
                    }
                    else
                    {
                        TabelaSimbolos token = tokens[indiceLeituraFita];
                        listaRetorno.Add("Token " + token.rotulo + " inesperado na linha " + token.linha + ".");
                    }
                    break;
                }
                else if (acaoTabela.acao == Enumeradores.Acao.Aceitar)
                {
                    break;
                }
                else if (acaoTabela.acao == Enumeradores.Acao.Shift)
                {
                    pilha.Add(acaoTabela.valor);
                    indiceLeituraFita += 1;
                }
                else if (acaoTabela.acao == Enumeradores.Acao.Reduce)
                {
                    Producao producaoReducao = producoes[acaoTabela.valor];
                    if (producaoReducao != null)
                    {
                        int tamanhoProducao = producaoReducao.simbolosProducao.Count();
                        pilha.RemoveRange(pilha.Count() - tamanhoProducao, tamanhoProducao);
                        pilha.Add(tabelaLALR.acoes[pilha.Last(), producaoReducao.indiceNaoTerminal].valor);
                    }
                    else
                    {
                        listaRetorno.Add("Produção de redução não encontrada.");
                        break;
                    }
                }

                if (indiceLeituraFita == fita.Count())
                {
                    listaRetorno.Add("Fim da fita de análise... O código não foi reconhecido.");
                    break;
                }
            }

            return(listaRetorno);
        }