From 0d530f0e84c15387cd22ae1608c284befff8e87f Mon Sep 17 00:00:00 2001 From: leo Date: Tue, 16 Apr 2024 22:12:29 -0300 Subject: so falta os comentarios e salvar em um arquivo --- include/lexical.h | 2 +- lexical.c | 9 ++- main | Bin 30632 -> 30352 bytes main.c | 163 ++++++++++++++++++++++++++---------------------------- map.c | 2 +- txt.txt | 11 ++-- 6 files changed, 94 insertions(+), 93 deletions(-) diff --git a/include/lexical.h b/include/lexical.h index 6898d6c..a3168ef 100644 --- a/include/lexical.h +++ b/include/lexical.h @@ -29,6 +29,6 @@ int is_numero(char tmp); void PROXIMO(FILE *fp); char* CODIGO(char *cod); -void ERRO(); +void ERRO(int error_code); #endif diff --git a/lexical.c b/lexical.c index 46ffffb..22d219b 100644 --- a/lexical.c +++ b/lexical.c @@ -80,14 +80,17 @@ void init_all_map(){ void PROXIMO(FILE *fp){ + if(is_EOF) return; + if(fscanf(fp, "%c", &proximo) == EOF){ is_EOF = 1; return ; } - printf("%c", proximo); if(proximo == '\n'){ + printf("\n"); PROXIMO(fp); linha++; + printf("linha %d: ", linha); } return ; @@ -114,7 +117,9 @@ char* CODIGO(char *cod){ return NULL; } -void ERRO(){ +void ERRO(int error_code){ fprintf(stderr, "Erro na linha %d\n", linha); + if(error_code == 1) fprintf(stderr, "primerio erro\n"); + else fprintf(stderr, "segundo erro\n"); exit(1); } diff --git a/main b/main index 6032534..6eba4ba 100755 Binary files a/main and b/main differ diff --git a/main.c b/main.c index e0663a4..50f5354 100644 --- a/main.c +++ b/main.c @@ -15,21 +15,10 @@ extern int is_EOF; int main(int argc, char *argv[]){ /* - init_special_char(); - printf("%s\n", test); - init_simbolos_especiais(); - test = get_value("d", special_char); - printf("%s\n", test); - test = get_value("d", special_char); - printf("%s\n", test); - test = get_value("aa", special_char); - printf("%s\n", test); - */ - /* - if(argc != 2){ + if(argc != 2){ fprintf(stderr, "passe o nome do arquivo\n"); exit(EXT_ERR_PARAMS); - }*/ + }*/ init_all_map(); char *file_path = "txt.txt";//argv[1]; @@ -40,89 +29,93 @@ int main(int argc, char *argv[]){ char atomo[4096]; PROXIMO(fp); - while(proximo == '\n'){ - PROXIMO(fp); - } - - char tmp[2] = "\0"; tmp[0] = proximo; - if(get_value(tmp, simbolos_especiais) != NULL){ - char s[3] = "\0"; s[0] = proximo; - PROXIMO(fp); - - if(s[0] == ':' && proximo == '='){ - s[1] = '='; - } - simbolo = CODIGO(s); - }else if(is_caracter(proximo)){ - int indice = 0; - while(is_caracter(proximo) || is_numero(proximo)){ - atomo[indice++] = proximo; + + printf("linha 1: "); + while(is_EOF == 0){ + while(proximo == ' ' || proximo == '\t' || proximo == '\r') PROXIMO(fp); + while(proximo == '\n' && is_EOF != 1){ + printf("\n"); PROXIMO(fp); } - atomo[indice] = '\0'; - printf("atomo: %s\n", atomo); - - //se atomo pertence a palavra reservada - if(get_value(atomo, simbolos_especiais_composto_negrito) != NULL || - get_value(atomo, simbolos_identificadores) != NULL){ - simbolo = CODIGO(atomo); - }else{ - /* criar codigo identificador */ - char *ci = malloc(sizeof(char) * 256); - ci[0] = 'i'; ci[1] = 'd'; - - int _i = 2; - extern int identificador_num; - int aux = identificador_num++; - - while(aux > 0){ - int n = aux%10; - aux /= 10; - ci[_i++] = '0' + n; - } - ci[_i] = '\0'; + if(is_EOF) break; - simbolo = ci; - // printf("atomo: %s\t ci: %s\n", atomo, ci); - insert_map(atomo, ci, simbolos_identificadores); - } - }else if(is_numero(proximo)){ - int indice = 0; - while(is_numero(proximo)){ - atomo[indice++] = proximo; + char tmp[2] = "\0";/* "\0\0" */ tmp[0] = proximo; + if(get_value(tmp, simbolos_especiais) != NULL){ + char s[3] = "\0"; s[0] = proximo; PROXIMO(fp); - } - atomo[indice] = '\0'; - if(is_caracter(proximo)){ - ERRO(); - } + if(s[0] == ':' && proximo == '='){ + s[1] = '='; + PROXIMO(fp); + } + simbolo = CODIGO(s); + }else if(is_caracter(proximo)){ + int indice = 0; + while(is_caracter(proximo) || is_numero(proximo)){ + atomo[indice++] = proximo; + PROXIMO(fp); + } + atomo[indice] = '\0'; + // printf("atomo: %s\n", atomo); + + //se atomo pertence a palavra reservada + if(get_value(atomo, simbolos_especiais_composto_negrito) != NULL || + get_value(atomo, simbolos_identificadores) != NULL){ + simbolo = CODIGO(atomo); + }else{ + /* criar codigo identificador */ + char *ci = malloc(sizeof(char) * 256); + ci[0] = 'i'; ci[1] = 'd'; + + int _i = 2; + extern int identificador_num; + int aux = identificador_num++; + + while(aux > 0){ + int n = aux%10; + aux /= 10; + ci[_i++] = '0' + n; + } + ci[_i] = '\0'; + + simbolo = ci; + // printf("atomo: %s\t ci: %s\n", atomo, ci); + insert_map(atomo, ci, simbolos_identificadores); + } + }else if(is_numero(proximo)){ + int indice = 0; + while(is_numero(proximo)){ + atomo[indice++] = proximo; + PROXIMO(fp); + } + atomo[indice] = '\0'; - char *tmpp; - if((tmpp = get_value(atomo, simbolos_numeros)) != NULL){ - simbolo = tmpp; - }else{ - char *ni = malloc(sizeof(char) * 128); - ni[0] = 'n'; ni[1] = 'u'; ni[2] = 'm'; - for(int i=0; i<=strlen(atomo); ++i){ - ni[i+3] = atomo[i]; + if(is_caracter(proximo)){ + ERRO(1); + } + + char *tmpp; + if((tmpp = get_value(atomo, simbolos_numeros)) != NULL){ + simbolo = tmpp; + }else{ + char *ni = malloc(sizeof(char) * 128); + ni[0] = 'n'; ni[1] = 'u'; ni[2] = 'm'; + for(int i=0; i<=strlen(atomo); ++i){ + ni[i+3] = atomo[i]; + } + simbolo = ni; + insert_map(atomo, ni, simbolos_numeros); + // printf("ni: %s\n", ni); } - simbolo = ni; - insert_map(atomo, ni, simbolos_numeros); -// printf("ni: %s\n", ni); + }else{ + ERRO(2); } - }else{ - ERRO(); + printf("%s ", simbolo); } + // printf("nu: %s\n", get_value("123123", simbolos_numeros)); -// printf("nu: %s\n", get_value("123123", simbolos_numeros)); - -// printf("linha: %d\n", linha); - /*while(is_EOF != 1){ - PROXIMO(fp); - printf("%c", proximo); - }*/ + // printf("linha: %d\n", linha); fclose(fp); diff --git a/map.c b/map.c index 58ed548..39272fd 100644 --- a/map.c +++ b/map.c @@ -72,6 +72,6 @@ char *get_value(char *key, string_map map){ head = head->next; } - fprintf(stderr, "VALOR NAO ESTA NO HASH TABLE\n"); + // fprintf(stderr, "VALOR NAO ESTA NO HASH TABLE\n"); return NULL; } diff --git a/txt.txt b/txt.txt index afd934b..48b0fbc 100644 --- a/txt.txt +++ b/txt.txt @@ -1,4 +1,7 @@ + + program exemplo1 + var n, s, i: integer; procedure soma; @@ -17,8 +20,8 @@ begin begin soma; write (s); - i:=i+1; + i:=i+1; end -end. - - \ No newline at end of file +end. + + -- cgit v1.2.3