Skip to content

Instantly share code, notes, and snippets.

@yesidays
Created May 1, 2012 20:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yesidays/2571057 to your computer and use it in GitHub Desktop.
Save yesidays/2571057 to your computer and use it in GitHub Desktop.
Analizador
ES UN ANALIZADOR QUE RECONOCE CADENAS COMO:
*palabras reservadas(puedes agregar las que quieras).
*enteros(c/s signo)
*reales (c/s signo);
*exponenciales
*hexadecimales para procesador intel (digito{<letraHexa o digito>}h)
*operadores: logicos, relacionales, aritmeticos.
PRUEVALO Y DIME QUE TAL TE PARECE. SE ADMITEN RECOMENDACIONES.
#include <string.h>
#include <conio.h>
#include <stdio.h>
#include <io.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++)
{
gotoxy(i,1);cprintf("%c", 176);
gotoxy(i,24);cprintf("%c", 176);
}
for (i=1; i<=24; i++)
{
gotoxy(1,i);cprintf("%c", 176);
gotoxy(80,i);cprintf("%c", 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 '\0':
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{
clrscr();
margen();
gotoxy(29, 3);
textcolor(YELLOW);cprintf("== ANALIZADOR LEXICO ==");
gotoxy(3, 6);
textcolor(WHITE);cprintf("Cadena: ");
gotoxy(3, 7);gets(cad);
if (palabraReservada(cad))
{
textcolor(YELLOW);
gotoxy(10, 12);cprintf("SI ES UN TOKEN VALIDO DE C.");
gotoxy(10, 13);cprintf("ES UNA ");
textcolor(WHITE);cprintf("PALABRA RESERVADA.");
}
else
{
if (automata(cad, edo))
{
res=diccionario_edos(edo);
textcolor(YELLOW);
gotoxy(10, 12);cprintf("SI ES UN TOKEN VALIDO DE C.");
gotoxy(10, 13);cprintf("ES UN ");
textcolor(WHITE);
switch (res)
{
case identificador:
cprintf("IDENTIFICADOR.");
break;
case entero:
cprintf("NUMERO ENTERO.");
break;
case aritmeticoFinal:
cprintf("OPERADOR ARITMETICO.");
break;
case real:
cprintf("NUMERO REAL.");
break;
case relacionalFinal:
cprintf("OPERADOR RELACIONAL.");
break;
case logico:
cprintf("OPERADOR LOGICO.");
break;
case hexadecimal:
cprintf("NUMERO HEXADECIMAL PARA INTEL.");
break;
case exponencial:
cprintf("NUMERO EXPONENCIAL.");
break;
}
}
else
{
textcolor(YELLOW);
gotoxy(10, 9);cprintf("NO ES UN TOKEN VALIDO DE C.");
}
}
gotoxy(20, 20);
textcolor(GREEN);cprintf(">> [ENTER] PARA REPETIR.");
gotoxy(20, 21);cprintf(">> [ESC] PARA SALIR.");
rep=getch();
}while(rep==13);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment