YA TAMBIEN TENGO UNA ANALIZADOR
QUE TE PARECE SI INTERCAMBIAMOS
ESTE ES EL CODIGO DEL MIO
ESPERO EL TUYO O EN MI DIRECCION DE CORREO ELECTRONICO
QUE ES:
[email protected]
confio en ti
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
typedef char cadena[30];
typedef enum
{
FALSE, TRUE
} booleano;
typedef enum
{
letra, guionBajo, digito, amperson, bype, aritmetico,
igual, relacional, menos, mas, negacion, letraHexa, e, punto,
h, FDC, otros, circunflejo
} entradas;
typedef enum
{
x=-2, bien, q0, q1, q2, q3, q4, q5, q6, q7, q8, q9,
q10, q11, q12, q13, q14, q15, q16, q17, q18, q19,
q20, q21, q22, q23, q24
} estados;
typedef enum
{
identificador, entero, aritmeticoFinal, real, relacionalFinal,
logico, hexadecimal, exponencial
} resultados;
//////// AUTOMATA EN FORMA DE TABLA //////////
estados tabla[25][18]=
{
{q1, q1, q15, q12, q13, q6, q7, q9, q3, q11, q19, q1, q1, q4, q1, x, x, q6},
{q1, q1, q1, x, x, x, x, x, x, x, x, q1, q1, x, q1, bien, x, x},
{x, x, q2, x, x, x, x, x, x, x, x, x, q20, q4, x, bien, x, x},
{x, x, q2, x, x, x, q8, x, q8, x, x, x, x, q4, x, bien, x, x},
{x, x, q5, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x},
{x, x, q5, x, x, x, x, x, x, x, x, x, q20, x, x, bien, x, x},
{x, x, x, x, x, x, q8, x, x, x, x, x, x, x, x, bien, x, x},
{x, x, x, x, x, x, q10, x, x, x, x, x, x, x, x, bien, x, x},
{x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, bien, x},
{x, x, x, x, x, x, q10, x, x, x, x, x, x, x, x, bien, x, x},
{x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, bien, x, x},
{x, x, q2, x, x, x, q8, x, x, q8, x, x, x, q4, x, bien, x, x},
{x, x, x, q14, x, x, x, x, x, x, x, x, x, x, x, x, x, x},
{x, x, x, x, q14, x, x, x, x, x, x, x, x, x, x, x, x, x},
{x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, bien, x, x},
{x, x, q15, x, x, x, x, x, x, x, x, q17, q16, q4, q18, bien, x, x},
{x, x, q22, x, x, x, x, x, q23, q23, x, q17, q17, x, q18, x, x, x},
{x, x, q17, x, x, x, x, x, x, x, x, q17, q17, x, q18, x, x, x},
{x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, bien, x, x},
{x, x, x, x, x, x, q10, x, x, x, x, x, x, x, x, bien, x, x},
{x, x, q21, x, x, x, x, x, q23, q23, x, x, x, x, x, x, x, x},
{x, x, q21, x, x, x, x, x, x, x, x, x, x, x, x, bien, x, x},
{x, x, q22, x, x, x, x, x, x, x, x, q17, q17, x, q18, bien, x, x},
{x, x, q24, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x},
{x, x, q24, x, x, x, x, x, x, x, x, x, x, x, x, bien, x, x}
};
/**********MARGEN********/
/*void margen(void)
{
int i, j;
//textbackground(BLUE);
//textcolor(WHITE);
for (i=1; i<=80; i++)
{
cout<<"t 176";
cout<<"t 176";
}
for (i=1; i<=24; i++)
{
cout<<"t 176";
cout<<"t 176";
}
}*/
/*********PALABRAS RESERVADAS*******/
booleano palabraReservada(cadena cad)
{
int i;
cadena lista[]={"int", "float", "char", "enum", "unsigned", "short",
"long", "double", "class", "public", "static", "void", "main",
"protected", "for", "if", "while", "do", "switch", "case", "default",
"{", "}", "printf", "goto", "scanf", "gotoxy", "typedef", "struct",
"union", "auto", "return", "include", "define", "malloc", "new",
"delete", "continue", "free", "else"};
for (i=0; i<40; i++)
if (strcmp(lista[i], cad)==0)
return TRUE;
return FALSE;
}
/****************DICCIONARIO DE ENTRADAS*************/
entradas diccionario(char caracter)
{
switch (caracter)
{
case 'g':case 'i':case 'j':case 'k':case 'l':case 'm':
case 'n':case 'o':case 'p':case 'q':case 'r':case 's':
case 't':case 'u':case 'v':case 'w':case 'x':case 'y':
case 'z':case 'G':case 'I':case 'J':case 'K':case 'L':
case 'M':case 'N':case 'O':case 'P':case 'Q':case 'R':
case 'S':case 'T':case 'U':case 'V':case 'W':case 'X':
case 'Y':case 'Z':
return letra;
case '_':
return guionBajo;
case '0':case '1':case '2':case '3':case '4':case '5':
case '6':case '7':case '8':case '9':
return digito;
case '&':
return amperson;
case '|':
return bype;
case '%':case '/':case '*':
return aritmetico;
case '=':
return igual;
case '<':case '>':
return relacional;
case '-':
return menos;
case '+':
return mas;
case '!':
return negacion;
case 'a':case 'b':case 'c':case 'd':case 'f':
case 'A':case 'B':case 'C':case 'D':case 'F':
return letraHexa;
case 'e':case 'E':
return e;
case '.':
return punto;
case 'h':case 'H':
return h;
case '^':
return circunflejo;
case ' ':
return FDC;
default:
return otros;
}
}
/*******DICCIONARIO DE ESTADOS*********/
resultados diccionario_edos(estados edo)
{
switch (edo)
{
case q1:
return identificador;
case q2:case q15:
return entero;
case q3:case q6:case q7:case q8:case q11:
return aritmeticoFinal;
case q5:
return real;
case q9:case q10:
return relacionalFinal;
case q14:case q19:
return logico;
case q18:
return hexadecimal;
case q21:case q22:case q24:
return exponencial;
}
return entero;
}
/***********AUTOMATA DEL ANALISADOR**********/
booleano automata(cadena cad, estados &edo)
{
int i=0;
estados edoAnterior;
edo=q0;
entradas in;
do
{
in=diccionario(cad[i]);
edoAnterior=edo;
edo=tabla[edo][in];
if (edo==x)
return FALSE;
i++;
}while(edo!=bien);
edo=edoAnterior;
return TRUE;
}
/**********MAIN***********/
void main(void)
{
cadena cad;
estados edo;
resultados res;
char rep;
do
{
system("cls");
//margen();
//gotoxy(29, 3);
cout<<"t== ANALIZADOR LEXICO ==";
//gotoxy(3, 6);
cout<<"tCadena: ";
cout<<"nt";
gets(cad);
if (palabraReservada(cad))
{
//textcolor(YELLOW);
cout<<"n";
cout<<"tSI ES UN TOKEN VALIDO DE C.";
cout<<"ntES UNA PALABRA RESERVADA.";
}
else
{
if (automata(cad, edo))
{
res=diccionario_edos(edo);
//textcolor(YELLOW);
cout<<"n";
cout<<"tSI ES UN TOKEN VALIDO DE C.";
cout<<"ntES UN ";
//textcolor(WHITE);
switch (res)
{
case identificador:
cout<<"ntIDENTIFICADOR.";
break;
case entero:
cout<<"ntNUMERO ENTERO.";
break;
case aritmeticoFinal:
cout<<"ntOPERADOR ARITMETICO.";
break;
case real:
cout<<"ntNUMERO REAL.";
break;
case relacionalFinal:
cout<<"ntOPERADOR RELACIONAL.";
break;
case logico:
cout<<"ntOPERADOR LOGICO.";
break;
case hexadecimal:
cout<<"ntNUMERO HEXADECIMAL PARA INTEL.";
break;
case exponencial:
cout<<"ntNUMERO EXPONENCIAL.";
break;
}
}
else
{
//textcolor(YELLOW);
cout<<"ntNO ES UN TOKEN VALIDO DE C.";
}
}
//gotoxy(20, 20);
cout<<"nt>> [r] PARA REPETIR.";
cout<<"nt>> [s] PARA SALIR.";
//rep=getch();
//system("pause");
cin>>rep;
}while(rep=='r');
}