-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLexic.c
More file actions
533 lines (454 loc) · 16.7 KB
/
Copy pathLexic.c
File metadata and controls
533 lines (454 loc) · 16.7 KB
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
/*
*---------------------------------------------------------------------
*
* File : Lexic.c
* Created : 2017-05-09
* Modified: 2017-05-31
*
* Corpo do código principal
*
*---------------------------------------------------------------------
*/
/*
*---------------------------------------------------------------------
* INCLUDE FILES
*---------------------------------------------------------------------
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "headers/tokens.h"
#include "headers/erros.h"
#include "headers/tabSimb.h"
/*
*---------------------------------------------------------------------
* Data structures and variables
*---------------------------------------------------------------------
*/
FILE *arqEntrada;
/*
*---------------------------------------------------------------------
* Function prototypes
*---------------------------------------------------------------------
*/
char proximo_caractere();
void retrair(int);
void analisadorLexico();
/*
*---------------------------------------------------------------------
* Main
*---------------------------------------------------------------------
*/
int main(int argc, char const *argv[])
{
if(argc != 2){
printf("\nReferencie o arquivo. Ex: ./Portugol teste-01.ptg\n");
return 1;
}
if ((arqEntrada = fopen(argv[1], "r")) == NULL)
printf("Erro, arquivo nao encontrado\n");
else {
tTabelaDeSimbolos tabDeSimbolos; // Estrutura tabela de simbolos
inicializarTabelaDeSimbolos(&tabDeSimbolos); // Inicializa a tabela
inicializaTabelaToken(); // Inicializa a tabela de tokens
analisadorLexico(&tabDeSimbolos); // Faz a análise léxica
gerarArquivoTabelaDeSimbolos(&tabDeSimbolos,argv[1]); //Gera arquivo de simbolos
gerarArquivoTabelaDeTokens(head, argv[1], tabDeSimbolos.maiorLexema); // Gera arquivo de tokens
imprimeEntrada(argv[1]);
printf("Análise ocorreu com sucesso, arquivos gerados:\n\n%s.err\n%s.tbl\n%s.tok\n\n", argv[1], argv[1] , argv[1]);
}
fclose(arqEntrada);
return 0;
}
/*
*---------------------------------------------------------------------
* Function definitions
*---------------------------------------------------------------------
*/
void imprimeEntrada (const char *nomeArquivoEntrada) {
rewind(arqEntrada); // Volta pro inicio do arquivo para a análise
char *arquivoSaidaErro = (char *) malloc(4 + strlen(nomeArquivoEntrada)); // Alocação do arquivo de saida
sprintf(arquivoSaidaErro,"%s.err", nomeArquivoEntrada); // O arquivo de erro recebe o nome do arquivo de entrada + .err
arqErros = fopen(arquivoSaidaErro,"w"); // Tenta abrir o arquivo de erro para escrita
if(arqErros == NULL){ // Se não conseguir criar o arquivo
printf("Erro ao criar arquivo de erro!");
exit(0);
}
fprintf(arqErros, "Erros lexicos encontrados em \"%s\"\n\n", arquivoSaidaErro); // Primeira linha do arquivo
// Variáveis
int iErro; // Incrementador de erro
int linhaAtual = 1; // Linha atual
char linha[100]; // Array para receber a linha
tErrosNumalinha erros; // Estrutura que indica erros em uma linha
int errosJaImpressos = 0; // Erros já impressos
char strlinha[4]; // Variável para imprimir a linha
while ((fgets(linha, sizeof(linha), arqEntrada))!=NULL ) { // Faz a leitura de cada linha do arquivo de entrada
erros = errosNalinha(linhaAtual); // Analise de erros na linha
if (erros.quantosErros == 0){ // Se não houver erros na linha
sprintf(strlinha, "%3d", (int)linhaAtual); // strlinha recebe a linha atual
fprintf(arqErros,"[%s]%s",strlinha, linha); // Imprime a linha no arquivo
}
else for (iErro = 0; iErro < erros.quantosErros; iErro++) { // Se houver erros,
sprintf(strlinha, "%3d", (int)linhaAtual); //strlinha recebe a linha atual
fprintf(arqErros,"[%s]%s",strlinha, linha); // Imprime a linha no arquivo
imprimeErro(erros.indPrimErro + errosJaImpressos++); // Identifica a posição do erro
}
errosJaImpressos = 0; // Reseta os erros na linha
linhaAtual++; // Incrementa a linha atual
}
fprintf(arqErros, "\nTotal de erros encontrados: %d", totErrosEncontrados); // Imprime a quantidade de erros encontrados em todo o arquivo
free(arquivoSaidaErro);
fclose(arqErros);
}
char proximo_caractere(){ // Pega o próximo caracter
char getChar=getc(arqEntrada);
coluna++;
if(getChar=='\n'){ // Se o caracter for uma quebra de linha, incrementa a linha e reseta a coluna
linha++;
coluna=0;
}
return getChar;
}
void retrair(int n){ // Devolve caracter
fseek(arqEntrada, n*(-1), SEEK_CUR); // Volta uma coluna a partir da posição atual no arquivo
coluna-=n;
}
void analisadorLexico(tTabelaDeSimbolos *tabDeSimbolos){ // Autômato
while(verdade){ // Enquanto não chegar no final do arquivo
switch(state){ // Estados
case 0: // Estado inicial
c = proximo_caractere(); // Recebe próximo caracter
while (isspace(c)){
c = proximo_caractere();
state = 0;
}
// Verificações do estado 0
if(isalpha(c)){ state = 1;}
else if(isdigit(c)){ state = 7;}
else if (c == '.'){ state = 10;}
else if(c == '\"'){ state = 3;}
else if(c == '>'){ state = 15;}
else if(c == '<'){ state = 18;}
else if(c == '='){ state = 35;}
else if(c == EOF){ state = 22;}
else if(c == ':'){ state = 39;}
else if(c == ';'){ state = 38;}
else if(c == '('){ state = 42;}
else if(c == ')'){ state = 43;}
else if(c == '*'){ state = 40;}
else if(c == ','){ state = 41;}
else if(c == '+'){ state = 26;}
else if(c == '-'){ state = 23;}
else if(c == '/'){ state = 29;}
else {state = 38;}
break;
case 1://Estado Identificador
while(isalpha(c) || isdigit(c) || c == '_'){
addChar();
c=proximo_caractere();
}
state = 2;
break;
case 2://Estado final Identificador
state = 0;
retrair(1); // Devolve 1
instalaTabelaDeSimbolo(tabDeSimbolos,retornaNomeTokenReservado(lexeme)); // Insere o lexema na tabela de simbolos
instalaLexeme(retornaNomeToken(retornaNomeTokenReservado(lexeme)),retornaNomeTokenReservado(lexeme)); // Instala o nome do token e o token reservado
lexLen=0;
break;
case 3: //Estado Cadeia
addChar();
c=proximo_caractere();
while(isalpha(c) || isdigit(c)){
addChar();
c=proximo_caractere();
}
if(c == '\n'){ state = 5;}
else if(c == EOF){ state = 6;}
else if(c == '\"'){
state = 4;
addChar();
}
break;
case 4://Estado final Cadeia
state =0;
instalaTabelaDeSimbolo(tabDeSimbolos,tk_CADEIA);
instalaLexeme(retornaNomeToken(tk_CADEIA), tk_CADEIA);
lexLen=0;
break;
case 5://Estado final de erro na cadeia
case 6://Estado final de erro na cadeia
retrair(1); // Devolve 1
state=0;
instalaTabelaDeSimbolo(tabDeSimbolos,tk_CADEIA);
instalaLexeme(retornaNomeToken(tk_CADEIA), tk_CADEIA);
registraErroLex(linha,coluna,tErroLex_cadeiaNaoFechada);
break;
case 7://Estado Digito
while(isdigit(c)){
addChar();
c=proximo_caractere();
}
if(isalpha(c)){
state = 9;
break;
}
if(c =='.'){addChar(); state = 11;}
else state = 8;
break;
case 8://Estado final Digito Inteiro
state =0;
retrair(1); // Devolve 1
instalaTabelaDeSimbolo(tabDeSimbolos,tk_INT);
instalaLexeme(retornaNomeToken(tk_INT),tk_INT);
lexLen=0;
break;
case 9://Estado final erro Digito Inteiro
registraErroLex(linha,coluna,tErroLex_delimEsperado);
retrair(1); // Devolve 1
instalaTabelaDeSimbolo(tabDeSimbolos,tk_INT);
instalaLexeme(retornaNomeToken(tk_INT), tk_INT);
lexLen=0;
state = 0;
break;
case 10://Estado Decimal com ponto inicial
addChar();
c=proximo_caractere();
if(isdigit(c)){
state = 11;
addChar();
}else{
state=14;
}
break;
case 11://Estado Decimal com ponto na frente
c=proximo_caractere();
while(isdigit(c)){
addChar();
c=proximo_caractere();
}
if(isalpha(c)){ state=12;}
else state = 13;
break;
case 12://Estado final erro no Decimal
retrair(1); // Devolve 1
instalaTabelaDeSimbolo(tabDeSimbolos,tk_DEC);
instalaLexeme(retornaNomeToken(tk_DEC), tk_DEC);
registraErroLex(linha,coluna,tErroLex_delimEsperado);
lexLen=0;
state = 0;
break;
case 13://Estado final Decimal
state = 0;
retrair(1); // Devolve 1
instalaTabelaDeSimbolo(tabDeSimbolos,tk_DEC);
instalaLexeme(retornaNomeToken(tk_DEC), tk_DEC);
lexLen=0;
break;
case 14://Estado final de erro Ponto Isolado
state=0;
retrair(1); // Devolve 1
registraErroLex(linha,coluna,tErroLex_pontoIsolado);
break;
case 15://Estado Maior
addChar();
c=proximo_caractere();
if(c == '='){state = 16; addChar();}
else {state=17;}
break;
case 16://Estado final Maior Igual
state = 0;
retrair(1); // Devolve 1
instalaLexeme(retornaNomeToken(tk_maior_igual),tk_maior_igual);
lexLen=0;
break;
case 17://Estado final Maior
state = 0;
retrair(1); // Devolve 1
instalaLexeme(retornaNomeToken(tk_maior), tk_maior);
lexLen=0;
break;
case 18: //Estado Menor
addChar();
c=proximo_caractere();
if(c == '='){state = 19; addChar();}
else if(c == '>'){ state = 20; addChar();}
else state = 21;
break;
case 19://Estado final Menor Igual
state = 0;
retrair(1); // Devolve 1
instalaLexeme(retornaNomeToken(tk_menor_igual), tk_menor_igual);
lexLen=0;
break;
case 20://Estado final Diferente
state = 0;
retrair(1); // Devolve 1
instalaLexeme(retornaNomeToken(tk_diferente), tk_diferente);
lexLen=0;
break;
case 21://Estado final Menor
state = 0;
retrair(1); // Devolve 1
instalaLexeme(retornaNomeToken(tk_menor), tk_menor);
lexLen=0;
break;
case 22://Estado Final EOF
state = 0;
addChar();
instalaLexeme(retornaNomeToken(tk_EOF), tk_EOF);
verdade=0;
lexLen=0;
break;
case 23://Estado Menos
addChar();
c = proximo_caractere();
if(c == '-'){state = 24; addChar();}
else state = 25;
break;
case 24://Estado final Decremento
state = 0;
retrair(1); // Devolve 1
instalaLexeme(retornaNomeToken(tk_decr), tk_decr);
lexLen=0;
break;
case 25://Estado final Menos
retrair(1); // Devolve 1
instalaLexeme(retornaNomeToken(tk_menos), tk_menos);
state = 0;
lexLen=0;
break;
case 26://Estado Mais
addChar();
c = proximo_caractere();
if(c == '+'){state = 29; addChar();}
else state = 28;
break;
case 27://Estado final Incremento
state = 0;
retrair(1); // Devolve 1
instalaLexeme(retornaNomeToken(tk_incr),tk_incr);
lexLen=0;
break;
case 28://Estado final Mais
state = 0;
retrair(1); // Devolve 1
instalaLexeme(retornaNomeToken(tk_mais), tk_mais);
lexLen=0;
break;
case 29://Estado Dividido
addChar();
c=proximo_caractere();
if(c == '*'){ state = 31;}
else if(c == '/'){ state = 34;}
else state = 30;
break;
case 30://Estado final Dividido
state = 0;
retrair(1); // Devolve 1
instalaLexeme(retornaNomeToken(tk_dividido), tk_dividido);
lexLen=0;
break;
case 31:// Estado Comentario de Bloco
c=proximo_caractere();
while(isdigit(c) || isalpha(c)){
c=proximo_caractere();
}
if(c == '*'){ state = 32;}
else if(c == EOF){ state = 33;}
break;
case 32://Estado conteudo Comentario de Bloco
c=proximo_caractere();
while(isdigit(c) || isalpha(c)){
addChar();
c=proximo_caractere();
}
if(c == '/'){
state=0;
lexLen=0;
break;
}
else if(c == EOF){ state = 34;}
else{state = 31;}
break;
case 33: //Estado final erro Comentario de Bloco
state=0;
retrair(1); // Devolve 1
registraErroLex(linha,coluna,tErroLex_comentarioNaoFechado);
lexLen=0;
break;
case 34://Estado Comentario de linha
c=proximo_caractere();
while(isdigit(c) || isalpha(c)){
addChar();
c=proximo_caractere();
}
if(c == '\n'){
state=0;
lexLen=0;
}
else if(c == EOF){state = 22;}
break;
case 35://Estado Igual
addChar();
c=proximo_caractere();
if(c == '='){state = 36;}
else state = 37;
break;
case 36://Estado final Comparação
instalaLexeme(retornaNomeToken(tk_igual), tk_igual);
state = 0;
addChar();
lexLen=0;
break;
case 37:// Estado final Atribuição
instalaLexeme(retornaNomeToken(tk_atrib), tk_atrib);
state = 0;
addChar();
lexLen=0;
break;
case 38://Estado final Ponto e Virgula
instalaLexeme(retornaNomeToken(tk_pt_virg), tk_pt_virg);
state = 0;
addChar();
lexLen=0;
break;
case 39://Estado final Dois Pontos
instalaLexeme(retornaNomeToken(tk_dois_pts), tk_dois_pts);
state = 0;
addChar();
lexLen=0;
break;
case 40://Estado final Vezes
instalaLexeme(retornaNomeToken(tk_vezes), tk_vezes);
state = 0;
addChar();
lexLen=0;
break;
case 41://Estado final Virgula
instalaLexeme(retornaNomeToken(tk_virg), tk_virg);
state = 0;
addChar();
lexLen=0;
break;
case 42://Estado final Abre Parentesis
instalaLexeme(retornaNomeToken(tk_abre_par), tk_abre_par);
state = 0;
addChar();
lexLen=0;
break;
case 43://Estado final Fecha Parentesis
instalaLexeme(retornaNomeToken(tk_fecha_par), tk_fecha_par);
state = 0;
addChar();
lexLen=0;
break;
case 44://Caracter invalido
state = 0;
registraErroLex(linha, coluna, tErroLex_caractereInvalido);
lexLen = 0;
break;
}
}
}