Created
April 8, 2019 01:27
-
-
Save jeyziel/942b58fd5d7a46f347e707b662b7ec8a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# include <stdlib.h> | |
# include <stdio.h> | |
# include <ctype.h> | |
# include <string.h> | |
# include <stdbool.h> | |
# include "valor.h" | |
# include <sys/stat.h> | |
FILE *arquivo, *escrita; | |
int partida = 0; | |
int estado = 0; | |
int i = 0; | |
char string[25]; | |
char *keywords[] = {"char", "double", "else", "float", "if", "int", "main", "return", "static", "void", "while"}; | |
int posicao = 0; | |
void remove_file() { | |
remove("tabela_token.txt"); | |
} | |
bool is_keywords(char *lexema) | |
{ | |
int len = sizeof(keywords) / sizeof(keywords[0]); | |
for (int i = 0; i < len; i++) | |
{ | |
if (strcmp(lexema, keywords[i]) == 0) | |
{ | |
return true; | |
} | |
} | |
return false; | |
} | |
char* valor_token(char *lexema) | |
{ | |
if (strcmp(lexema, "if") == 0) return IF; | |
else if (strcmp(lexema, "char") == 0) return CHAR; | |
else if (strcmp(lexema, "else") == 0) return ELSE; | |
else if (strcmp(lexema, "float") == 0) return FLOAT; | |
else if (strcmp(lexema, "float") == 0) return INT; | |
else if (strcmp(lexema, "main") == 0) return MAIN; | |
else if (strcmp(lexema,"return") == 0)return RETURN; | |
else if (strcmp(lexema, "static") == 0) return STATIC; | |
else if (strcmp(lexema, "void") == 0) return VOID; | |
else if (strcmp(lexema, "while") == 0) return WHILE; | |
else if (lexema == ">=" || lexema == "<>" || lexema == ">" | |
|| lexema == "<" || lexema == "<=" || lexema == "==") return OP_REL; | |
else return "271"; | |
} | |
void tabela_token(char *lexema) | |
{ | |
FILE *token; | |
token = fopen("tabela_token.txt", "a"); | |
if(!is_keywords(lexema)) { | |
fprintf(token, "%s\t%d\n", valor_token(lexema), posicao); | |
}else{ | |
fprintf(token, "%s\t\n", valor_token(lexema)); | |
} | |
fclose(token); | |
} | |
/** int procurar_posicao(char *lexema) { | |
int result; | |
int id; | |
FILE *tabela_simbolos; | |
tabela_simbolos = fopen("tabela_simbolos.txt", "r"); | |
char palavra_tabela[20] ; | |
posicao = 0; | |
int achou = 0; | |
while (!feof(tabela_simbolos)) { | |
result = fscanf(tabela_simbolos, "%s%d", palavra_tabela, &id); | |
if (strcmp(lexema, palavra_tabela) == 0) | |
{ | |
achou = ++posicao; | |
} | |
posicao++; | |
} | |
fclose(tabela_simbolos); | |
return achou; | |
} **/ | |
void tabela_simbolos(char *lexema) | |
{ | |
FILE *tabela_simbolos; | |
bool achou = false; | |
int result; | |
char palavra_tabela[20]; | |
int id; | |
int linhas = 0; | |
posicao = 0; | |
tabela_simbolos = fopen("tabela_simbolos.txt", "a+"); | |
if (!is_keywords(lexema)) | |
{ | |
while (!feof(tabela_simbolos)) | |
{ | |
result = fscanf(tabela_simbolos, "%s%d", palavra_tabela, &id); | |
if (strcmp(lexema, palavra_tabela) == 0) | |
{ | |
achou = true; | |
posicao = (linhas++) + 1; | |
/**fprintf(tabela_simbolos, "%s\t%d\n", lexema, posicao); | |
achou = ++linhas; | |
fclose(tabela_simbolos);**/ | |
} | |
linhas++; | |
} | |
if (!achou) { | |
fprintf(tabela_simbolos, "\n%s\t%d", lexema, linhas+1); | |
posicao = linhas+1; | |
} | |
/** achou = procurar_posicao(lexema); | |
if (!achou) | |
{ | |
printf("%s\t%d\n", lexema, achou); | |
} */ | |
} | |
fclose(tabela_simbolos); | |
} | |
int falhar() { | |
switch (partida) | |
{ | |
case 0: | |
partida = 9; | |
break; | |
case 9: | |
partida = 12; | |
break; | |
case 12: | |
partida = 0; | |
default: | |
break; | |
} | |
return partida; | |
} | |
char next_char() | |
{ | |
return getc(arquivo); | |
} | |
int main(){ | |
arquivo = fopen("progama.txt", "r"); | |
escrita = fopen("escrita.txt", "w"); | |
int procurar; | |
char ch; | |
char *token; | |
remove_file(); | |
while (ch != EOF) | |
{ | |
switch(estado) | |
{ | |
case 0: ch = next_char(); | |
if (ch == ' ' || ch == '\t' || ch == '\n'){ | |
estado = 0; | |
} | |
else if (ch == '>') estado = 1; | |
else if (ch == '<') estado = 5; | |
else if (ch == '=') estado = 6; | |
else{ | |
estado = falhar(); | |
} | |
break; | |
case 1: ch = next_char(); | |
if (ch == '=') estado = 2; | |
if (ch == '<') estado = 3; | |
else estado=4; | |
break; | |
case 2: | |
estado = 0; | |
break; | |
case 3: | |
estado = 0; | |
break; | |
case 4: | |
estado = 0; | |
break; | |
case 5: | |
estado = 0; | |
case 6: | |
estado = 0; | |
break; | |
case 9: | |
if (isalpha(ch)){ | |
string[i++] = ch; | |
estado = 10; | |
}else{ | |
i = 0; | |
strcpy(string, ""); | |
estado = falhar(); | |
/* string[i] = '\0'; | |
fprintf(escrita, "%s\n", string); | |
printf("%s", string); | |
i = 0; | |
strcpy(string, "");*/ | |
} | |
break; | |
case 10: ch = next_char(); | |
if (isalpha(ch) || isdigit(ch)) { | |
estado = 10; | |
string[i++] = ch; | |
}else{ | |
estado = 11; | |
} | |
break; | |
case 11: | |
string[i] = '\0'; | |
tabela_simbolos(string); | |
tabela_token(string); | |
//fprintf(escrita, "%s\n", string); | |
i = 0; | |
strcpy(string, ""); | |
estado = falhar(); | |
break; | |
case 12: | |
if isdigit(ch) { | |
string[i++] = ch; | |
estado = 13; | |
}else{ | |
estado = falhar(); | |
} | |
break; | |
case 13: ch = next_char(); | |
if ( isdigit(ch)){ | |
string[i++] = ch; | |
estado = 13; | |
}else{ | |
estado = 14; | |
} | |
break; | |
case 14: | |
string[i] = '\0'; | |
fprintf(escrita, "%s\n", string); | |
i = 0; | |
strcpy(string, ""); | |
estado = falhar(); | |
break; | |
} | |
} | |
fclose(arquivo); | |
fclose(escrita); | |
//lexema = " "; | |
//putchar(ch); | |
//if (ch == '*') { | |
//printf("encontrei"); | |
//} | |
//ch = next_char(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment