CODIFICACIÓN DE LA PANTALLA
(CODIGO FUENTE)
#include <conio.h>
#include
<dos.h>
#include
<ctype.h>
#include
<string.h>
#include
<stdio.h>
#include
<graphics.h>
#include
<stdlib.h>
#include
<stdarg.h>
int
MARGEN_IZQ =0;
int
MARGEN_DER =635;
int MARGEN_SUP
=25;
int
MARGEN_INF =439;
int
colfill =0;
int
x11=0,y11=0;
char text_entrada[35][70]={NULL};
void
Cursor(int xc,int yc,int colc_text,int colc_fill)
{
do
{
delay(30);
setcolor(colc_text);
outtextxy(xc,yc+2,"_");
delay(7);
setcolor(colc_fill);
outtextxy(xc,yc+2,"_");
}while(!kbhit());
}
void
Asigna_color(char car)
{
char
*po,*po1;
if(isalpha(car)){setcolor(2);}
else{setcolor(15);
po = strchr("()+-*/%;.{}<>=[]", car);
if (po)
{setcolor(14);}
po1 = strchr("0123456789",
car);
if (po1) {setcolor(7);}
}
}
void
crea_token()
{
int fit=0,fi=0,co=0,ux=0;
char cad_lin[70]={NULL};
for(int it=0; it<35 ;it++){for(int
itf=0; itf<70 ;itf++){pal_entrada[it][itf]=NULL;}}
while(fi<35)
{
while(fit<70)
{
if(text_entrada[fi][fit]=='\x0')
{
if (strcmpi(cad_lin," ")!=0 &&
strcmpi(cad_lin,"")!=0)
{
strcpy(pal_entrada[co++],cad_lin);
}
for(int cli=0; cli<70 ; cli++){ cad_lin[cli]=NULL;}
ux=0;break;
}
if (text_entrada[fi][fit]!=' ')
{
cad_lin[ux++]=text_entrada[fi][fit];
//strcpy(pal_entrada[co++],cad_lin);
}
else
{
if (strcmpi(cad_lin," ")!=0 &&
strcmpi(cad_lin,"")!=0)
{ strcpy(pal_entrada[co++],cad_lin);}
for(int cli=0; cli<70 ; cli++){ cad_lin[cli]=NULL;}
//strcpy(cad_lin,NULL);
ux=0;
}
fit++;
}
fit=0;
fi++;
}
/*
if (token==' ') { x11++; y11=0; }
else { pal_entrada[x11][y11]=token; y11++; }
*/
}
void
print_pantalla()
{
int y_graph=MARGEN_SUP,y_array=0,x_graph=MARGEN_IZQ,x_array=0;
char carac[2];
settextstyle(0,0,0);
while(strcmpi(text_entrada[y_array],NULL)!=0)
{
while(text_entrada[y_array][x_array]!=NULL)
// for(int x_array=0; x_array<
strlen(text_entrada[y_array]) ; x_array++)
{
sprintf(carac,"%c",text_entrada[y_array][x_array]);
Asigna_color(text_entrada[y_array][x_array]);
outtextxy(x_graph,y_graph,carac);
x_graph+=9;
x_array++;
}
x_array=0;
y_array++;
y_graph+=15;
x_graph=MARGEN_IZQ;
}
}
void
diseno_graph()
{
setbkcolor(1);
settextstyle(2,0,4);
setcolor(15);
setfillstyle(1,7);
bar3d(0,0,getmaxx(),18,1,0);
bar3d(0,462,getmaxx(),getmaxy(),1,0);
setcolor(4); outtextxy(8,4,"- COMPILADOR
[ ]");
outtextxy(8,465,"Ctrl+X");
outtextxy(128,465,"Ctrl+R");
outtextxy(280,465,"Ctrl+A");
outtextxy(400,465,"Ctrl+G");
outtextxy(520,465,"Ctrl+V");
setcolor(8); outtextxy(8,4," Anal. Lexico, Anal. Sintactico y Generador de codigo");
outtextxy(68,465,"Salir");
outtextxy(188,465,"Compilar");
outtextxy(340,465,"Abrir");
outtextxy(460,465,"Guardar");
outtextxy(570,465,"Ver Msg");
print_pantalla();
}
void
ver_msg()
{
int imp=0,xee=10,yee=315;
settextstyle(2,0,4);
setcolor(15);
setfillstyle(1,3);
bar3d(1,300,getmaxx()-1,461,1,0);
line(2,314,getmaxx()-2,314);
outtextxy(10,301,"Msg");
setcolor(4);
while(strcmpi(g_error[imp].tipi,NULL)!=0)
{
if(yee<450)
{
escribe(xee,yee+=10,"Error : %d,%d
%s",g_error[imp].linea,g_error[imp].columna,g_error[imp].tipi);
imp++;
}//else{ getch(); ver_msg();}
}
settextstyle(0,0,0);
do
{
delay(50);
setcolor(4);
outtextxy(450,303,"° ° ° ± ± ± ˛ ˛ ˛ Ű
Ű Ű");
delay(50);
setcolor(1);
outtextxy(450,303,"° ° ° ± ± ± ˛ ˛ ˛ Ű
Ű Ű");
}while(!kbhit());
//
getch();
}
void
compilar()
{
int
poi=0,xp=0,xn=70,xs=140,xopl=210,xopa=280,xid=350;
int yp=10,yn=10,ys=10,yopl=10,yopa=10,yid=10;
crea_token();
for(int ie=0; ie<25
;ie++){g_error[ie].linea=-1;g_error[ie].columna=-1; for(int jl=0; jl<40 ;
jl++){ g_error[ie].tipi[jl]=NULL;}}
count_err=0;
erro=0;
bandera=0;
setcolor(15);
Anal_Lex();
Anal_Sintact();
if(erro==0)
{
cleardevice();
setbkcolor(0);
settextstyle(2,0,4);
setcolor(14);
escribe(0,5,"PALCLA NUM SIM OPL OPA
ID");
setcolor(15);
for(int gf=0; gf<50 ; gf++)
{
if(strcmpi(_guarda[gf]._pc,NULL)!=0)
escribe(xp,yp+=9,"%s",_guarda[gf]._pc);
if(strcmpi(_guarda[gf]._n,NULL)!=0)
escribe(xn,yn+=9,"%s",_guarda[gf]._n);
if(_guarda[gf]._s!=NULL)
escribe(xs,ys+=9,"%c",_guarda[gf]._s);
if(_guarda[gf]._opl!=NULL) escribe(xopl,yopl+=9,"%c",_guarda[gf]._opl);
if(_guarda[gf]._opa!=NULL)
escribe(xopa,yopa+=9,"%c",_guarda[gf]._opa);
if(strcmpi(_guarda[gf]._id,NULL)!=0)
escribe(xid,yopa+=9,"%s",_guarda[gf]._id);
poi++;
}
getch();
cleardevice();
setbkcolor(0);
settextstyle(2,0,4);
Generar_Codigo();
do
{
setcolor(11);
escribe(390,458,"Presione cualquier tecla para
continuar...",save_polaca[0]);
delay(1);
setcolor(14);
escribe(390,458,"Presione cualquier tecla para
continuar...",save_polaca[0]);
}while(!kbhit());
}
else
{
ver_msg();
}
}
main()
{
int
modoi=DETECT,bu,lett_x=MARGEN_IZQ,lett_y=MARGEN_SUP,aux_x=0,l_text=0,c_text=0,setcol;
int n_line=0,mov_line=0;
char a11,text[2];
initgraph(&modoi,&bu,"");
cleardevice();
diseno_graph();
do
{
settextstyle(0,0,0);
Cursor(lett_x,lett_y,14,colfill);
a11=getch();
if (isascii(a11) && !iscntrl(a11)
&& a11!= 72 && a11!= 75 && a11!= 80 && a11!= 77
)
{
text_entrada[l_text][c_text]=a11;
//crea_token(a11);
for(int c_rel=c_text ; c_rel>=0 ;
c_rel--)
{
if(text_entrada[l_text][c_rel]=='\x0')
{
text_entrada[l_text][c_rel]=' ';
}
}
sprintf(text,"%c",a11);
setcolor(colfill);
outtextxy(lett_x,lett_y,"Ű");
Asigna_color(a11);
outtextxy(lett_x,lett_y,text);
lett_x+=9;
c_text++;
}
switch (a11)
{
case 25:
if (mov_line<n_line)
{
n_line--;
}
break;
case 13:
if(lett_y<MARGEN_INF)
{
for(int c_rel=c_text ; c_rel>=0 ; c_rel--)
{
if(text_entrada[l_text][c_rel]=='\x0')
{
text_entrada[l_text][c_rel]=' ';
}
}
outtextxy(lett_x,lett_y,"Ű");
text_entrada[l_text][c_text]='
';
lett_x=MARGEN_IZQ;
lett_y+=15;
l_text++;
c_text=0;
n_line++;
mov_line++;
}
break;
case 8:
if(lett_x>MARGEN_IZQ)
{
c_text--;
text_entrada[l_text][c_text]='\x0';
setcolor(colfill);
lett_x-=9;
outtextxy(lett_x,lett_y,"Ű");
}
break;
case 72:
if(lett_y>MARGEN_SUP) {
l_text--; lett_y-=15;
mov_line--;}
break;
case 80:
if(lett_y<MARGEN_INF && mov_line<n_line) {
l_text++; lett_y+=15; mov_line++;}
break;
case 75:
if(lett_x>MARGEN_IZQ) {
c_text--; lett_x-=9;}
break;
case 77:
if(lett_x<MARGEN_DER) {
c_text++; lett_x+=9; }
break;
case 18:
compilar();
cleardevice();
diseno_graph();
break;
case 22:
ver_msg();
cleardevice();
diseno_graph();
break;
}
if(lett_x>MARGEN_DER)
{
lett_x=MARGEN_IZQ;
lett_y+=15;
l_text++;
c_text=0;
}
}while(a11!=24);
closegraph();
return 0;
}
PANTALLAS COMPILADAS

VENTANA DE DEPURACIÓN

CODIFICACIÓN DEL ANALIZADOR SINTACTICO Y LÉXICO
struct
save_see
{
char _s ;
char
_opa;
char
_opl;
char
_n [5];
char
_pc [10];
char
_id [10];
}_guarda[50]={NULL};
struct errores
{
char
tipi[40];
int linea;
int columna;
}g_error[25]={NULL};
char
paren[5] ={'(',')','{','}'};
char
operadores_arit[6]={'+','-','*','/','%'};
char
operadores_logi[5]={'<','>','!'};
char
simbolos[3] ={';','='};
char
pal_cla[5][9] =
{"comienza","termina","while","if"};
char
pal_entrada[35][70]={NULL};//={"comienza","a=b3;","a=0;","a=(((0+3)));","b=0;","j=1;","xyz=a+b+c-p/q;","v=3;","v=3;","v=3;","v=3;","v=3*1+8+1*1;","a=xyz*(p+q);","p=a-xyz-p;","termina"};//,"while(a<5){","if(a!=0)}"};
int bandera=0,count_err=0,erro=0;
void Anal_Lex ();
void Anal_Sintact ();
void Error(int li,int co,char error[40]);
void escribe (int xx, int yy, char *cadena, ...)
{
va_list argptr;
char
strx [140];
va_start
(argptr, cadena);
vsprintf (strx, cadena, argptr);
outtextxy
(xx, yy, strx);
va_end (argptr);
}
void
Anal_Lex()
{
int
l_e=0,l_t=0,c_t=0,bander=0,cun=0,u=0,ban_lex=0,ban1_lex=0,ban2_lex=0,num=0;
int
g_s=0,g_opa=0,g_opl=0,g_pc=0,g_id=0,g_n=0;
char *sim,
*operad,*parente,*operad1,fi[40]={"Formato invalido "};
char
temp[50][25]={NULL};
while(strcmpi(pal_entrada[l_e],NULL)!=0)
{
for(int count=0 ;
count<strlen(pal_entrada[l_e]) ; count++)
{
sim=strchr(simbolos,pal_entrada[l_e][count]);
if(sim)
_guarda[g_s++]._s=pal_entrada[l_e][count]; //printf("Simbolo ,
%c\n",pal_entrada[l_e][count]);
else
{
operad=strchr(operadores_arit,pal_entrada[l_e][count]);
if(operad)
_guarda[g_opa++]._opa=pal_entrada[l_e][count];
// printf("Operador
Aritmetico, %c\n",pal_entrada[l_e][count]);
else
{
parente=strchr(paren,pal_entrada[l_e][count]);
if(parente)
_guarda[g_s++]._s=pal_entrada[l_e][count];//
printf("Simbolo , %c\n",pal_entrada[l_e][count]);
else
{
operad1=strchr(operadores_logi,pal_entrada[l_e][count]);
if(operad1)
_guarda[g_opl++]._opl=pal_entrada[l_e][count];//
printf("Operador Logico , %c\n",pal_entrada[l_e][count]);
else
{
temp[l_t][c_t]=pal_entrada[l_e][count];
c_t++;
bander=1;
}
}
}
}
if(bander==0 &&
pal_entrada[l_e][count+1] != '\x0')
{ l_t++; c_t=0;
}
bander=0;
}
l_e++;
l_t++;
c_t=0;
}
for(int c_tmp=0;c_tmp<50;c_tmp++)
{
if(strcmpi(temp[c_tmp],NULL)!=0)
{
while(strcmpi(pal_cla[u],NULL)!=0)
{
if(strcmpi(temp[c_tmp],pal_cla[u])==0)
{
strcpy(_guarda[g_pc++]._pc,temp[c_tmp]); //printf("Palabra clave ,
%s\n",temp[c_tmp]);
ban_lex=1;
break;
}
u++;
}
if (ban_lex==0)
{
for(int jm=0; jm<strlen(temp[c_tmp]);jm++)
{
if(isalpha(temp[c_tmp][jm]))
{ban1_lex=1;}
if(isdigit(temp[c_tmp][0])) {ban2_lex=1;}
if(ispunct(temp[c_tmp][jm])) {
Error(-1,-1,strcat(fi,temp[c_tmp]));
break;
//printf("Format invalid :
%s\n",temp[c_tmp]);
}
}
num=atoi(temp[c_tmp]);
if(ban1_lex==1 && ban2_lex==1)
{
Error(-1,-1,strcat(fi,temp[c_tmp]));
//printf("Format invalid ,
%s\n",temp[c_tmp]);
}else
{
if(num==0 &&
!ban1_lex==0 && !ban2_lex==1)
{
strcpy(_guarda[g_id++]._id,temp[c_tmp]);
cun++;
}else
strcpy(_guarda[g_n++]._n,temp[c_tmp]);//printf("Numero,
%s\n",temp[c_tmp]);
}
}
}
ban_lex=0;
ban1_lex=0;
ban2_lex=0;
u=0;
}//fin
while
}
void
Error(int li,int co,char error[40])
{
g_error[count_err].linea=li;
g_error[count_err].columna=co;
strcpy(g_error[count_err].tipi,error);
count_err++;
erro=1;
//printf("Lin[%d],Col[%d] :
%s",li,co,error);
}
void
is_ssm(char ch)
{
char
*ptr;
ptr=strchr(operadores_arit,ch);
if (ptr) {bandera=1;}
ptr=strchr(simbolos,ch);
if (ptr) {bandera=1;}
}
void Anal_Sintact()
{
int
l_e=1,poc,parente=0,c_aux=0,con_g=0,bader=0;
char
*comp,*comp1,aux_id[10]={NULL};
if(strcmpi(pal_entrada[0],"comienza")!=0) { Error(0+1,0,"Comienza no esta
definido");}
while(strcmpi(pal_entrada[l_e],NULL)!=0)
{
for( poc=0; poc < strlen(pal_entrada[l_e]) ; poc++)
{
switch (pal_entrada[l_e][poc])
{
case '=':
while(strcmpi(_guarda[con_g]._id,NULL)!=0)
{
if (strcmpi(_guarda[con_g]._id,aux_id)==0)
{bader=1;break;
}
con_g++;
}
if(atoi(aux_id)==0 && bader==1)
{
is_ssm(pal_entrada[l_e][poc-1]);
if (bandera==1)
{Error(l_e+1,poc-1,"Operacion no valida");}
is_ssm(pal_entrada[l_e][poc+1]);
if (bandera==1)
{Error(l_e+1,poc+1,"Operacion no valida");}
}else{Error(l_e+1,poc-1,"Asignacion no
valida");}
strcpy(aux_id,"
");
c_aux=0;
bader=0;
con_g=0;
break;
case ';':
is_ssm(pal_entrada[l_e][poc-1]);
if(bandera==1) {Error(l_e+1,poc-1,"Operacion no valida");}
if (pal_entrada[l_e][poc+1] != NULL||
pal_entrada[l_e][poc-1]=='(')
{Error(l_e+1,poc+1,"Operacion no
valida");}
strcpy(aux_id,"
");
c_aux=0;
break;
case '(':
if(pal_entrada[l_e][poc-1]==';'||
pal_entrada[l_e][poc-1]==')'||
pal_entrada[l_e][poc+1]==')'||
isalpha(pal_entrada[l_e][poc-1])||
isdigit(pal_entrada[l_e][poc-1]))
{Error(l_e+1,poc+1,"Operacion no
valida");}
is_ssm(pal_entrada[l_e][poc+1]);
if (bandera==1)
{Error(l_e+1,poc+1,"Operacion no valida");}
parente++;
strcpy(aux_id," ");
c_aux=0;
break;
case
')':
is_ssm(pal_entrada[l_e][poc-1]);
if (bandera==1)
{Error(l_e+1,poc-1,"Operacion no valida");}
if(pal_entrada[l_e][poc+1]=='=' ||
pal_entrada[l_e][poc+1]=='(' ||
pal_entrada[l_e][poc-1]=='(' ||
isalpha(pal_entrada[l_e][poc+1]) ||
isdigit(pal_entrada[l_e][poc+1]) )
{Error(l_e+1,poc+1,"Operacion no
valida");}
parente--;
strcpy(aux_id," ");
c_aux=0;
break;
case '{':
strcpy(aux_id," ");
c_aux=0;
break;
case '!':
strcpy(aux_id," ");
c_aux=0;
break;
case '<':
strcpy(aux_id," ");
c_aux=0;
break;
case '>':
strcpy(aux_id," ");
c_aux=0;
break;
case '}':
strcpy(aux_id," ");
c_aux=0;
break;
default:
if (pal_entrada[l_e][poc]== '+' ||
pal_entrada[l_e][poc]== '-' ||
pal_entrada[l_e][poc]== '*' ||
pal_entrada[l_e][poc]== '/' ||
pal_entrada[l_e][poc]== '%' )
{
is_ssm(pal_entrada[l_e][poc-1]);
if (bandera==1) {Error(l_e+1,poc-1,"Operacion no valida");}
is_ssm(pal_entrada[l_e][poc+1]);
if (bandera==1)
{Error(l_e+1,poc+1,"Operacion no valida");}
if(pal_entrada[l_e][poc-1]=='('||
pal_entrada[l_e][poc+1]==')')
{Error(l_e+1,poc+1,"Operacion no valida");}
strcpy(aux_id," ");
c_aux=0;
}
else
{
aux_id[c_aux]=pal_entrada[l_e][poc];
c_aux++;
}
break;
}
}
if(strcmpi(pal_entrada[l_e],"termina")!=0)
{
comp=strchr(pal_entrada[l_e],';');
if (!comp) Error(l_e+1,strlen(pal_entrada[l_e]),"Expresion no
valida, falta ';'");
comp1=strchr(pal_entrada[l_e],'=');
if (!comp1) Error(l_e+1,1,"Expresion no valida, falta '='");
}
if(parente!=0)
{Error(l_e+1,poc,"Los parentesis no estan balanceados");}
if
(strcmpi(pal_entrada[l_e+1],NULL)==0)
{ l_e++; break;}
parente=0;
l_e++;
}//fin while
if(strcmpi(pal_entrada[l_e-1],"termina")!=0) Error(l_e+1,0,"Termina no esta
definido");
}
void limpia_polaca()
{
int
recorre=0,ban_check=0,recorre1=0,bade=0,cs_t=0;
char
save_temp[50][15]={NULL},tok_aux[15]={NULL};
while(strcmpi(save_polaca[recorre],NULL)!=0)
{
if (save_polaca[recorre][0]=='#')
{
if(strcmpi(tok_aux,save_polaca[recorre])!=0)
{
strcpy(save_temp[recorre1],save_polaca[recorre]);
recorre1++;
}
}else
{
strcpy(save_temp[recorre1],save_polaca[recorre]);
recorre1++;
}
strcpy(tok_aux,save_temp[recorre1-1]);
recorre++;
}
for(int
kl=0; kl<50 ; kl++){ for(int kpi=0; kpi<15 ;
kpi++){save_polaca[kl][kpi]=NULL;}}
while(strcmpi(save_temp[cs_t],NULL)!=0)
{
strcpy(save_polaca[cs_t],save_temp[cs_t]);
cs_t++;
}
}
void Generar_Codigo()
{
int
c_pe=0,cuent=0,nv=1,lo=0,hi=0,px=0,py=0;
char
*checa,op1[15]={NULL},op2[15]={NULL},nom_var[4]={NULL},aux_var[4]={NULL};
mitote.inicia();
while
(strcmpi(pal_entrada[c_pe],NULL)!=0)
{
settextstyle(2,0,4);
polaca_inversa(pal_entrada[c_pe]);
// printf("\n;");
setcolor(3);
if(py>=430){py=0;px+=200;}
escribe (px, py, ";");
while(strcmpi(save_polaca[lo],NULL)!=0){ hi+=15;
escribe(px+hi,py,"%s",save_polaca[lo]); lo++; }
setcolor(15);
hi=0;
py+=3;
//
getch();
while(strcmpi(save_polaca[cuent],NULL)!=0)
{
checa=strchr(operadores_arit,save_polaca[cuent][0]);
if(checa)
{
itoa(nv,aux_var,10);
strcpy(nom_var,"#");
strcat(nom_var,aux_var);
strcpy(op1,save_polaca[cuent-1]);
strcpy(op2,save_polaca[cuent-2]);
if(py>=430){py=0;px+=200;}
escribe(px,py+=9,"Mov %s,
reg",op1);
escribe(px,py+=9,"Op %s,
reg",op2);
strcpy(save_polaca[cuent],nom_var);
if(save_polaca[cuent-1][0]!='#')
{
strcpy(save_polaca[cuent-1],nom_var);
}
if(save_polaca[cuent-2][0]!='#')
{
strcpy(save_polaca[cuent-2],nom_var);
}
strcpy(nom_var,"");
strcpy(aux_var,"");
limpia_polaca();
cuent=0;
nv++;
}
else{
if(save_polaca[cuent][0]=='=')
{
if(py>=430){py=0;px+=200;}
escribe(px,py+=9,"Mov %s,
Reg",save_polaca[cuent-1]);
escribe(px,py+=9,"Mov Reg, %s",save_polaca[0]);
}
}
cuent++;
}
nv=1;
cuent=0;
lo=0;
c_pe++;
py+=18;
}//fin while
}