-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
647 lines (574 loc) · 20.7 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
#include <stdio.h>
#include <stdlib.h>
#include<ctype.h>
#include<string.h>
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_GREEN "\x1b[32m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_BLUE "\x1b[34m"
#define ANSI_COLOR_MAGENTA "\x1b[35m"
#define ANSI_COLOR_CYAN "\x1b[36m"
#define ANSI_COLOR_RESET "\x1b[0m"
//DELIMITADORES
char *variable_deli_keywords="<>=+-*/;( =):,\"";
char *operator_logicos_keywords="<>=";
char *operator_aritmetica_keywords="+-*/";
char keywordsRevers[13][12] = {"RETORNO", "INICIO", "FIN", "FUNCION", "IMPRIMIR", "RECOGER",
"BOOLEAN", "CADENA", "SINO", "HASTA", "SI", "ENTERO",
"PARA"};
//DELIMITADORES
int keyword_library(char temp[]);
int keyword_deli(char temp);
int variable_deli(char temp);
int automataIdenReser(char *temp);
int idenAritmetic(char temp[]); // para la identificación aritmetica
int idenOperator(char temp[]); //identifica cada operador logico
int operator_aritmetica(char temp);
int operator_logicos(char temp);
int operador_deli(char temp);
int operador_deliLogico(char temp);
int operador_deli_openClose(char temp, char automa);
char bufferW[100];
int numWorldRes=0;
int numWorldOperL=0;
int numWorldOperA=0;
int identificadores=0;
int numerTotal=0;
int numError=0;
char charError[1000];
int lineError=1;
int banner(){
printf("\n");
printf("\n");
printf(ANSI_COLOR_GREEN "\t █████╗ ███╗ ██╗ █████╗ ██╗ ██╗ ██╗███████╗███████╗██████╗ \n" ANSI_COLOR_RESET);
printf(ANSI_COLOR_GREEN "\t██╔══██╗████╗ ██║██╔══██╗██║ ╚██╗ ██╔╝╚══███╔╝██╔════╝██╔══██╗\n" ANSI_COLOR_RESET);
printf(ANSI_COLOR_GREEN "\t███████║██╔██╗ ██║███████║██║ ╚████╔╝ ███╔╝ █████╗ ██████╔╝\n" ANSI_COLOR_RESET);
printf(ANSI_COLOR_GREEN "\t██╔══██║██║╚██╗██║██╔══██║██║ ╚██╔╝ ███╔╝ ██╔══╝ ██╔══██╗\n" ANSI_COLOR_RESET);
printf(ANSI_COLOR_GREEN "\t██║ ██║██║ ╚████║██║ ██║███████╗██║ ███████╗███████╗██║ ██║\n" ANSI_COLOR_RESET);
printf(ANSI_COLOR_GREEN "\t╚═╝ ╚═╝╚═╝ ╚═══╝╚═╝ ╚═╝╚══════╝╚═╝ ╚══════╝╚══════╝╚═╝ ╚═╝\n" ANSI_COLOR_RESET);
printf(ANSI_COLOR_RED "\t\t\tby: Michael & Dickson"ANSI_COLOR_RESET "\n\n\n");
}
int analizerFile(char *dirFile){
FILE *file_pointer;
file_pointer = fopen(dirFile,"r");
if(file_pointer == NULL) {
printf("El archivo no pudo ser abierto.\n");
exit(0);
} else{
automataIdenReser(dirFile);
}
//getch();
return 0;
}
char * quitDeli(char temp[]){
int s, i, con=0;
s=strlen(temp);
char copyTemp[s];
for(i=0; i<s; i++){
copyTemp[i]=*temp;
if(isalpha(*temp)){
temp++;
} else{
con++;
}
}
s=s-con;
//char a[s];
//int* a = malloc(sizeof(int) * s);
char* a = (char *) malloc(sizeof(int) * s);
strncpy(a, copyTemp, s);
return a;
}
int fun(){
FILE* archivo = fopen("/home/by-default/Documents/gitProyects/automatasApp/archivo.txt","r");
char * buffer = NULL;
int i, c;
c = fgetc(archivo);
while(!feof(archivo))
{
buffer = (char*)realloc(NULL, sizeof(char));
i = 0;
while( c != '\n') {
char a=c;
buffer[i] = c;
i++;
buffer = (char*)realloc(buffer, (i+1)*sizeof(char));
c = fgetc(archivo);
}
/*Agrego el \n al buffer*/
buffer = (char*)realloc(buffer, (i+2)*sizeof(char));
buffer[i] = c;
buffer[i+1] = 0;
/*Trabajar con el buffer*/
printf("%s", buffer);
free(buffer);
c = fgetc(archivo);
}
fclose(archivo);
}
int automataIdenReser(char *dirFile){
FILE* archivo = fopen(dirFile,"r");
char * buffer = NULL;
int i, c;
char temporalChar;
c = fgetc(archivo);
char puntero;
int estado = 0, bandera;
char token = 0;
while(!feof(archivo)) {
buffer = (char*)realloc(NULL, sizeof(char));
i = 0;
while( c != '\n') {
puntero=c;
if(bandera==1){
free(buffer);
i=0;
buffer = (char*)realloc(NULL, sizeof(char));
bandera=0;
}
switch (estado) {
case 0:
if (isalpha(puntero) && islower(puntero)) {
estado = 1;
token = 1;
//c = fgetc(archivo);
} else if (isalpha(puntero) && isupper(puntero)) {
estado = 4;
} else if(isalnum(puntero)){
temporalChar=puntero;
estado=8;
c = fgetc(archivo);
}else if(operator_aritmetica(puntero)){
temporalChar=puntero;
estado=6;
c = fgetc(archivo);
} else if(puntero=='\t'){
estado=0;
c = fgetc(archivo);
}else if(operator_logicos(puntero)){
temporalChar=puntero;
estado=7;
c = fgetc(archivo);
}else if(puntero=='('){
temporalChar=puntero;
estado=9;
c = fgetc(archivo);
}else if(puntero==')'){
temporalChar=puntero;
estado=10;
c = fgetc(archivo);
} else if(puntero==';'){
estado=0;
c = fgetc(archivo);
}else {
estado = 0;
token = 0;
c = fgetc(archivo);
}
break;
case 1: //del 1 al 2 para delimitadores
if (isalpha(puntero) || isdigit(puntero)) {
buffer[i] = c;
i++;
buffer = (char*)realloc(buffer, (i+1)*sizeof(char));
estado = 1;
token = 1;
c = fgetc(archivo);
} else {
estado = 2;
token = 0;
}
break;
case 2:
//este if es para cuando dos variables estan unidas, si no esta este if se salta los delimitadores
if (isalpha(puntero) || isdigit(puntero)) {
c = fgetc(archivo);
}
if (variable_deli(puntero)) {
printf(buffer);
printf("\nToken: 3\n");
token = 3;
identificadores++;
buffer = (char*)realloc(buffer, (i+2)*sizeof(char));
buffer[i] = c;
buffer[i+1] = 0;
//free(buffer);
bandera=1;
estado=0;
} else {
estado = 0;
token = -1;
printf(ANSI_COLOR_RED "Error en la línea %d: %s se he encontrado un error.\n" ANSI_COLOR_RESET, lineError, buffer);
numError=numError+1;
c = fgetc(archivo);
}
break;
case 3:
if (isalpha(puntero) || isdigit(puntero)) {
estado = 3;
token = 1;
} else {
estado = 100;
token = 0;
}
c = fgetc(archivo);
break;
case 4: // desde el 4 al 5 para palabras reservadas
if (isupper(puntero)) {
estado = 4;
if(isupper(puntero)){
buffer[i] = c;
i++;
buffer = (char*)realloc(buffer, (i+1)*sizeof(char));
// bufferW[conForBuff++];
}
c = fgetc(archivo);
// }else if(puntero==';'){
} else {
estado = 5;
}
break;
case 5:
if (keyword_deli(puntero)) {
if (keyword_library(buffer)){
printf(buffer);
printf("\nToken: 1\n\n");
//free(buffer);
token = 1;
buffer = (char*)realloc(buffer, (i+2)*sizeof(char));
buffer[i] = c;
buffer[i+1] = 0;
bandera=1;
//free(buffer);
estado=0;
}else{
token=-1;
printf(ANSI_COLOR_RED "Error en la línea %d: %s No es palabra reservada.\n" ANSI_COLOR_RESET, lineError, buffer);
numError=numError+1;
estado=0;
}
//c = fgetc(archivo);
} else {
token = -1;
c = fgetc(archivo);
}
break;
case 6: //automara para aritmeticos
if (operador_deli(puntero)) {
printf("%c",temporalChar);
printf("\nToken: 10\n\n");
token=10;
estado=0;
//c = fgetc(archivo);
} else {
token = -1;
c = fgetc(archivo);
printf(ANSI_COLOR_RED "Error en la línea %d : %s verifique su operador aritmetico.\n" ANSI_COLOR_RESET,lineError, buffer);
numError=numError+1;
}
break;
case 7: //automata para logicos
if (operador_deliLogico(puntero)) {
printf("%c",temporalChar);
printf("\nToken: 11\n\n");
token=11;
estado=0;
//c = fgetc(archivo);
} else {
token = -1;
c = fgetc(archivo);
printf(ANSI_COLOR_RED "Error en la linea %d: mal escrito en el operador logico %s.\n" ANSI_COLOR_RESET, lineError, buffer);
numError=numError+1;
}
break;
case 8: //automata para numeros
if (isdigit(puntero)){
estado=8;
}
else if (variable_deli(puntero)) {
printf("%c",temporalChar);
printf("\nToken: 4\n\n");
token=4;
estado=0;
numerTotal++;
//c = fgetc(archivo);
} else {
token = -1;
printf(ANSI_COLOR_RED "Error linea %d: en la declaracion, verifique la expresion %s.\n" ANSI_COLOR_RESET, lineError, buffer);
numError=numError+1;
c = fgetc(archivo);
}
break;
case 9: //automata para abrir "("
if (operador_deli_openClose(puntero, '(')) {
printf("%c",temporalChar);
printf("\nToken: 14\n\n");
token=14;
estado=0;
//c = fgetc(archivo);
} else {
token = -1;
printf(ANSI_COLOR_RED "Error linea %d verifique la expresión.\n" ANSI_COLOR_RESET, lineError,buffer);
numError=numError+1;
c = fgetc(archivo);
}
break;
case 10: //automata para abrir ")"
if (operador_deli_openClose(puntero, ')')) {
printf("%c",temporalChar);
printf("\nToken: 15\n\n");
token=11;
estado=0;
//c = fgetc(archivo);
} else {
token = -1;
c = fgetc(archivo);
}
break;
default:
token = 0;
c = fgetc(archivo);
break;
}
}
lineError=lineError+1;
c = fgetc(archivo);
// switch (token) {
// case 0:
// printf("No es identificador\n");
// c = fgetc(archivo);
// break;
// case 1:
// printf("Es Palabra Reservada\n\n");
// c = fgetc(archivo);
// break;
// case 2:
// printf("Es Palabra Reservada\n");
// c = fgetc(archivo);
// break;
// case 4:
// printf("Es Numero\n");
// c = fgetc(archivo);
// break;
// case 96:
// printf("Reservad\n");
// c = fgetc(archivo);
// break;
// default:
// printf("Error:\n");
// break;
// }
}
}
int keyword_library(char temp[]) {
char *quitTemp;
quitTemp=quitDeli(temp);
int count = 0, flag = 0;
char keywords[13][12] = {"RETORNO", "INICIO", "FIN", "FUNCION", "IMPRIMIR", "RECOGER",
"BOOLEAN", "CADENA", "SINO", "HASTA", "SI", "ENTERO",
"PARA"};
while(count <= 12) {
if(strcmp(keywords[count], quitTemp) == 0) {
flag = 1;// entra es palabra reservada
numWorldRes++;
break;
}
count = count + 1;
}
return (flag);
}
int keyword_deli(char temp) {
char a[1];
a[1]=temp;
int count = 0, flag = 0;
if(isalpha(temp) && isupper(temp)){
flag=1;
}else{
//char keywords[3][1] = {";","("," "};
char *keywords=";( {}";
for (int i = 0; i < 5; ++i) {
if(*keywords== temp) {
flag = 1;
//numWorldOperA++;
break;
}
keywords++;
}
}
return (flag);
}
int operador_deli(char temp) {
char a[1];
a[1]=temp;
int count = 0, flag = 0;
if(isalpha(temp) || isdigit(temp)){
flag=1;
}else{
char keywords[1][1] = {" "};
while(count <= 1) {
if(strcmp(keywords[count], a) == 0) {
flag = 1;
break;
}
count = count + 1;
}
}
return (flag);
}
int operador_deli_openClose(char temp, char automa) {
char a[1];
a[1]=temp;
int count = 0, flag = 0;
char *keywords="";
if(automa=='('){
if(isalpha(temp) || isalnum(temp)){
flag=1;
}
keywords="\" ";
}else if(automa==')'){
if(isalpha(temp) || isalnum(temp)){
flag=0;
}
keywords="{ ;";
}
for (int i = 0; i < 2; ++i) {
if(*keywords== temp) {
flag = 1;
//numWorldOperA++;
break;
}
keywords++;
}
return (flag);
}
int operador_deliLogico(char temp) {
char a[1]; a[1]=temp;
int count = 0, flag = 0;
if(isalpha(temp) || isalnum(temp)){
flag=1;
}else{
//char keywords[1][1] = {" ","="};
char *keywords=" =";
for (int i = 0; i < 2; ++i) {
if(*keywords== temp) {
flag = 1;
//numWorldOperA++;
break;
} keywords++;
}
}
return (flag);
}
int operator_aritmetica(char temp) {
int flag = 0;
char *keywords=operator_aritmetica_keywords;
for (int i = 0; i < 4; ++i) {
if(*keywords== temp) {
flag = 1;
numWorldOperA++;
break;
}
keywords++;
}
return (flag);
}
int operator_logicos(char temp) {
char a[1];
a[1]=temp;
int count = 0, flag = 0;
if(isalpha(temp) && isupper(temp)){
flag=1;
}else{
char *keywords=operator_logicos_keywords;
for (int i = 0; i < 3; ++i) {
if(*keywords== temp) {
flag = 1;
numWorldOperL++;
break;
}
keywords++;
}
}
return (flag);
}
int variable_deli(char temp) {
char a[1];
a[1]=temp;
int count = 0, flag = 0;
//char keywords[12][1] = {"<", ">", "=","+", "-", "*", "/", ";", "(", " ", "=", ")", ":" };
char *keywords=variable_deli_keywords;
for (int i = 0; i < 15; ++i) {
if(*keywords== temp) {
flag = 1;
//numWorldOperA++;
break;
}
keywords++;
}
return (flag);
}
int isWorldR(int numero) {
int count = 0, flag = 0;
return (flag);
}
int resultadosFinales(){
printf(ANSI_COLOR_BLUE "\n\n##################################\n" ANSI_COLOR_RESET);
printf(ANSI_COLOR_YELLOW "\nTotal Palabras R.: %d\t" ANSI_COLOR_RESET,numWorldRes);
printf(ANSI_COLOR_YELLOW "\nTotal Operadores A.: %d\t" ANSI_COLOR_RESET,numWorldOperA);
printf(ANSI_COLOR_YELLOW "\nTotal Operadores L.: %d\t" ANSI_COLOR_RESET,numWorldOperL);
printf(ANSI_COLOR_YELLOW "\nTotal Identificadores: %d\t" ANSI_COLOR_RESET,identificadores);
printf(ANSI_COLOR_YELLOW "\nTotal Numeros: %d\t" ANSI_COLOR_RESET,numerTotal);
printf(ANSI_COLOR_RED "\nTotal Errores: %d\t" ANSI_COLOR_RESET,numError);
//printf(ANSI_COLOR_RED "\nUltma línea con error: %d\t" ANSI_COLOR_RESET,lineError);
printf(ANSI_COLOR_BLUE "\n\n##################################" ANSI_COLOR_RESET);
}
int presentarDatos(){
printf(ANSI_COLOR_YELLOW "\n\n\n\nDelimitadores para Identificadores.\n %s" ANSI_COLOR_RESET, variable_deli_keywords);
printf(ANSI_COLOR_YELLOW "\n\nOperadores Logicos.\n %s" ANSI_COLOR_RESET, operator_logicos_keywords);
printf(ANSI_COLOR_YELLOW "\n\nDelimitadores Aritmeticos.\n %s" ANSI_COLOR_RESET, operator_aritmetica_keywords);
printf(ANSI_COLOR_YELLOW "\n\nPalabras Rervadas. "ANSI_COLOR_RESET);
int count = 0, flag = 0;
char keywords[13][12] = {"RETORNO", "INICIO", "FIN", "FUNCION", "IMPRIMIR", "RECOGER",
"BOOLEAN", "CADENA", "SINO", "HASTA", "SI", "ENTERO",
"PARA"};
while(count <= 12) {
printf("\n%s\n",keywords[count]);
count = count + 1;
}
}
// FIN EDICION Michael
int main( int argc, char *argv[] ) {
banner();
int p=0;
int op;
char directorio[256];
char* texto;
while(op!=4) {
printf(ANSI_COLOR_CYAN "\n\n1.- Analizar Archivo\n" ANSI_COLOR_RESET);
printf(ANSI_COLOR_CYAN "2.- Analizar Texto (No desarrollado)\n" ANSI_COLOR_RESET);
printf(ANSI_COLOR_CYAN "3.- Datos de Analizador\n" ANSI_COLOR_RESET);
printf(ANSI_COLOR_CYAN "4.- Salir\n" ANSI_COLOR_RESET);
printf(ANSI_COLOR_YELLOW "Indica la opcion: " ANSI_COLOR_RESET);
scanf("%d", &op);
switch(op) {
case 1:
printf("Ingrese directorio completo del archivo: \n");
scanf("%s", &directorio);
analizerFile(directorio);
resultadosFinales();
break;
case 2:
printf("Ingrese directorio del archivo: \n");
scanf("%d", &texto);
printf(texto);
break;
case 3:
presentarDatos();
break;
case 4:
break;
}
}
return 0;
}
//autores: dickson y michaelju