public void Analizador(String texto) { int estado = 0; //Identifica el estado actual del analizador String lexema = ""; Char c; bool hayPunto = false; //Bandera para verificar si la expresión es un número y si tiene un punto texto = texto + "$"; pila.push(new NoTerminal(0, "$")); listaSintactico.Add(dameListaPila()); //Inicia el automata del analizador for (int i = 0; i < texto.Length; i++) { c = texto[i]; switch (estado) { case INICIAL: if (Char.IsLetter(c) || c == '_') { //Verifica si es letra o empieza con un "_" estado = IDENTIFICADOR; lexema += c; } else if (Char.IsDigit(c)) { //Verifica si es digito estado = ENTERO; lexema += c; } else if (c == '"') { estado = CADENA; lexema += c; } else if (c == '+' || c == '-') { lexema += c; agregaToken(lexema, OPSUMA); AnalizadorSintactico(OPSUMA, lexema); estado = INICIAL; lexema = ""; } else if (c == '*' || c == '/') { lexema += c; agregaToken(lexema, OPMUL); AnalizadorSintactico(OPMUL, lexema); estado = INICIAL; lexema = ""; } else if (c == '<' || c == '>') { estado = OPRELAC; lexema += c; } else if (c == '|') { estado = OPOR; lexema += c; } else if (c == '&') { estado = OPAND; lexema += c; } else if (c == '=' || c == '!') { if (texto[i + 1] != '=') { if (c == '=') { lexema += c; agregaToken(lexema, IGUAL); AnalizadorSintactico(IGUAL, lexema); lexema = ""; estado = INICIAL; } else if (c == '!') { lexema += c; agregaToken(lexema, OPNOT); AnalizadorSintactico(OPNOT, lexema); estado = INICIAL; lexema = ""; } } else { estado = OPIGUALDAD; lexema += c; } } else if (c == ';') { lexema += c; agregaToken(lexema, PUNTOCOMA); AnalizadorSintactico(PUNTOCOMA, lexema); estado = INICIAL; lexema = ""; } else if (c == ',') { lexema += c; agregaToken(lexema, COMA); AnalizadorSintactico(COMA, lexema); estado = INICIAL; lexema = ""; } else if (c == '(') { lexema += c; agregaToken(lexema, ABREPARENTESIS); AnalizadorSintactico(ABREPARENTESIS, lexema); estado = INICIAL; lexema = ""; } else if (c == ')') { lexema += c; agregaToken(lexema, CIERRAPARENTESIS); AnalizadorSintactico(CIERRAPARENTESIS, lexema); estado = INICIAL; lexema = ""; } else if (c == '{') { lexema += c; agregaToken(lexema, ABRELLAVE); AnalizadorSintactico(ABRELLAVE, lexema); estado = INICIAL; lexema = ""; } else if (c == '}') { lexema += c; agregaToken(lexema, CIERRALLAVE); AnalizadorSintactico(CIERRALLAVE, lexema); estado = INICIAL; lexema = ""; } else if (c == '$') { lexema += c; agregaToken(lexema, SIGNOPESOS); AnalizadorSintactico(SIGNOPESOS, lexema); estado = INICIAL; lexema = ""; } break; case IDENTIFICADOR: if (Char.IsLetterOrDigit(c) || c == '_') { estado = IDENTIFICADOR; lexema += c; } else { if (esTipoDato(lexema)) { agregaToken(lexema, TIPO); AnalizadorSintactico(TIPO, lexema); estado = INICIAL; lexema = ""; } else if (esReservada(lexema)) { agregaToken(lexema, tipoReservada(lexema)); AnalizadorSintactico(tipoReservada(lexema), lexema); estado = INICIAL; lexema = ""; } else { agregaToken(lexema, IDENTIFICADOR); AnalizadorSintactico(IDENTIFICADOR, lexema); estado = INICIAL; lexema = ""; i--; } } break; case ENTERO: if (Char.IsDigit(c)) { estado = ENTERO; lexema += c; } else if (c == '.') { if (hayPunto == false) { estado = REAL; lexema += c; hayPunto = true; } else { lexema += c; agregaError(lexema); estado = INICIAL; lexema = ""; } } else { agregaToken(lexema, ENTERO); AnalizadorSintactico(ENTERO, lexema); estado = INICIAL; lexema = ""; i--; } break; case REAL: if (Char.IsDigit(c)) { estado = REAL; lexema += c; } else if (c == '.') { if (hayPunto) { lexema += c; agregaError(lexema); estado = INICIAL; lexema = ""; } } else { agregaToken(lexema, REAL); AnalizadorSintactico(REAL, lexema); estado = INICIAL; lexema = ""; } break; case CADENA: if (c == '"') { lexema += c; agregaToken(lexema, CADENA); AnalizadorSintactico(CADENA, lexema); estado = INICIAL; lexema = ""; } else { estado = CADENA; lexema += c; } break; case OPRELAC: if (c == '=') { lexema += c; agregaToken(lexema, OPRELAC); AnalizadorSintactico(OPRELAC, lexema); estado = INICIAL; lexema = ""; } else { agregaToken(lexema, OPRELAC); AnalizadorSintactico(OPRELAC, lexema); estado = INICIAL; lexema = ""; } break; case OPOR: if (c == '|') { lexema += c; agregaToken(lexema, OPOR); AnalizadorSintactico(OPOR, lexema); estado = INICIAL; lexema = ""; } else { agregaToken(lexema, OPOR); AnalizadorSintactico(OPOR, lexema); estado = INICIAL; lexema = ""; } break; case OPAND: if (c == '&') { lexema += c; agregaToken(lexema, OPAND); AnalizadorSintactico(OPAND, lexema); estado = INICIAL; lexema = ""; } else { agregaToken(lexema, OPAND); AnalizadorSintactico(OPAND, lexema); estado = INICIAL; lexema = ""; } break; case OPIGUALDAD: lexema += c; agregaToken(lexema, OPIGUALDAD); AnalizadorSintactico(OPIGUALDAD, lexema); estado = INICIAL; lexema = ""; break; default: break; } } //Termina el automata }