Você está na página 1de 5

//PROGRAMA ANALIZADOR LEXICO

//ROSA IMELDA GARCIA CHI


#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
#include <ctype.h>
int matriz[9][14]={
{7,8,101,102,103,1,4,5,109,6,0,113,201},
{100,100,100,100,2,100,100,100,100,100,100,100,100},
{2,2,2,2,3,2,2,2,2,2,2,2,2},
{2,2,2,2,2,0,2,2,2,2,2,2,2},
{106,106,106,106,106,106,106,105,104,106,106,106,106},
{108,108,108,108,108,108,108,108,107,108,108,108,108},
{200,200,200,200,200,200,200,200,110,200,200,200,200},
{7,7,111,111,111,111,111,111,111,111,111,111,111},
{112,8,112,112,112,112,112,112,112,112,112,112,112}
};
FILE *fp;
typedef char string[15];
string ts[100];
int q, clase, tipo,pendiente, ultimo=19;
char c;
int x=5; int y=5;
string componente;
class lexico
{
public:
//constructor
lexico()
{
llena_reservada();
}
int trae_indice(char c)
{
int uno,otro=0;
switch (c)
{
case '+': uno=2;break;
case '-': uno=3;break;
case '*': uno=4;break;

case '/': uno=5;break;


case '<': uno=6;break;
case '>': uno=7;break;
case '=': uno=8;break;
case ':': uno=9;break;
case ' ':
case '\n':
case '\t': uno=10;break;
case ';': uno=11;break;
default: otro=1;
} /*switch*/
if (isdigit(c))
{uno=1; otro=0;}
if (isalpha(c))
{uno=0; otro=0;}
if (otro) uno=12;
return(uno);
}
void elimina_car(string componente)
{
int longitud;
longitud=strlen(componente);
componente[longitud-1]='\0';
}
void llena_reservada()
{
strcpy(ts[0],"program");
strcpy(ts[1],"array");
strcpy(ts[2],"var");
strcpy(ts[3],"const");
strcpy(ts[4],"begin");
strcpy(ts[5],"end");
strcpy(ts[6],"integer");
strcpy(ts[7],"if");
strcpy(ts[8],"then");
strcpy(ts[9],"real");
strcpy(ts[10],"step");
strcpy(ts[11],"case");
strcpy(ts[12],"else");
strcpy(ts[13],"char");
strcpy(ts[14],"for");
strcpy(ts[15],"to");
strcpy(ts[16],"do");
strcpy(ts[17],"while");
strcpy(ts[18],"repeat");

strcpy(ts[19],"until");
}
int guarda(string componente)
{
int i,encontro;
for (i=0;i<100;i++)
if (strcmpi(componente,ts[i])==0)
{ encontro=1;
ultimo=i;
break;
}
else
encontro=0;
if (!encontro)
{
ultimo=ultimo+1;
strcpy(ts[ultimo],componente);
}
return(ultimo);
}
void imprime()
{
gotoxy(x,y);
printf("%s
%i %i", componente, clase, tipo);
y=y+1;
if (y==22) {y=5;x=40;}
}
scan()
{ int r;
if (!pendiente)
fread(&c,sizeof(c),1,fp);
pendiente=0;
strcpy(componente,"");
q=0;
while(q<100)
{
int largo;
r=trae_indice(c);
q=matriz[q][r];
if (q!=0)
{
largo=strlen(componente);

componente[largo]=c;
componente[largo+1]='\x0';
}
if (q<100)
fread(&c,sizeof(c),1,fp);
}
switch (q)
{
case 100:elimina_car(componente);clase=4;tipo=4;pendiente=1;break;
case 101:clase=4;tipo=1;break;
case 102:clase=4;tipo=2;break;
case 103:clase=4;tipo=3;break;
case 104:clase=5;tipo=2;break;
case 105:clase=5;tipo=3;break;
case 106:elimina_car(componente);clase=5;tipo=1;pendiente=1;break;
case 107:clase=5;tipo=5;break;
case 108:elimina_car(componente);clase=5;tipo=4;pendiente=1;break;
case 109:clase=5;tipo=6;break;
case 110:clase=6;tipo=0;break;
case 113:clase=7;tipo=0;break;
case 111:
{elimina_car(componente);
tipo=guarda(componente);
if (tipo<20) //palabra reservada
clase=1;
else
clase=2; //identificador
pendiente=1;break;}
//constante entera
case 112: elimina_car(componente);clase=3;tipo=guarda(componente);
pendiente=1;break;
case 200: cprintf("ERROR - se esperaba un =");break;
case 201: cprintf("ERROR - caracter invalido");break;
}
return(componente, clase, tipo);
}
}; //clase
void main()
{
lexico lex;
clrscr();
if ((fp=fopen("edito.txt","r"))==NULL)
{ clrscr();
cprintf("NO PUEDE ABRIR EL ARCHIVO");
}
else

{
fread(&c,sizeof(c),1,fp);
pendiente=1;
while (!feof(fp))
{
lex.scan();
lex.imprime();
}
}
fclose(fp);
getche();
}

Você também pode gostar