Skip to content

Instantly share code, notes, and snippets.

@samirfor
Created September 20, 2014 18:39
Show Gist options
  • Save samirfor/fcee66df2330eb15470b to your computer and use it in GitHub Desktop.
Save samirfor/fcee66df2330eb15470b to your computer and use it in GitHub Desktop.
lexico.lex
%{
#define ARQUIVO "tabela_de_simbolos.txt"
#define TAMANHO_ID 200
#define ID 100
#define NUM 101
#define IF 102
#define ELSE 103
#define WHILE 104
#define PAR_A 105
#define PAR_F 106
#define OPER 107
#define AND 108
#define OR 109
#define NOT 110
#define ATR 113
#define TO_I 114
#define TO_F 115
#define TO_S 116
#define TO_B 117
#define END 118
#define TRUE 119
#define FALSE 120
#define PUT 121
#define GET 122
#define LENGTH 123
#define PVIRG 124
#define STRING 125
#define OP_REL 200
#define ERRO 400
%}
/*definições regulares*/
delim [ \t\n]
ws {delim}+
letra [A-Za-z]
digito [0-9]
id ({letra}|_)({letra}|{digito})*
numero {digito}+(\.{digito}+)?(eE[+|-]?{digito}+)?
carac_espec [^"\n]
string ((\"){carac_espec}*(\"))
c_e_c [^*/]
comentario ((\/)(\*){c_e_c}*(\*)(\/))
%%
{ws} {}
{comentario} {}
{string} {return (STRING);}
if {return (IF);}
while {return (WHILE);}
else {return (ELSE);}
end {return (END);}
to_i {return (TO_I);}
to_f {return (TO_F);}
to_s {return (TO_S);}
to_b {return (TO_B);}
put {return (PUT);}
get {return (GET);}
length {return (LENGTH);}
true {return (TRUE);}
false {return (FALSE);}
and {return (AND);}
or {return (OR);}
not {return (NOT);}
{id} {return (ID);}
{numero} {return (NUM);}
"=" {return (ATR);}
"<"|"<="|">"|">="|"!="|"==" {return (OP_REL);}
"^"|"+"|"-"|"*"|"/"|"%" {return (OPER);}
"(" {return (PAR_A);}
")" {return (PAR_F);}
";" {return (PVIRG);}
. {return (ERRO);}
%%
int existeID(char *str) {
int c;
char aux[TAMANHO_ID+4];
int pos = 0;
FILE *tabela = fopen(ARQUIVO, "r");
if (tabela) {
while ((c = getc(tabela)) != EOF){
if(c !=';' && c !='\n'){//monta a palavra
aux[pos] = c;
pos++;
}else{
aux[pos] = '\0';
pos = 0;
if(strcmp(aux,str) == 0){
fclose(tabela);
return 1;
}
}
}
fclose(tabela);
}
return 0;
}
void inserirID(char *str) {
if (!existeID(str)) {
char str2[TAMANHO_ID+4];
strcpy(str2, str);
strcat(str2, ";\n");
FILE *tabela;
if((tabela = fopen(ARQUIVO,"a+")) == NULL) {
printf("Erro ao abrir arquivo!!!\n\n");
exit(1);
}
fputs(str2,tabela);
fclose(tabela);
}
}
int main(int argc, char *argv[]){
remove(ARQUIVO);
int tk;
while (tk=yylex()){
if((tk==IF) || (tk==ELSE) || (tk == END) || (tk == FALSE) || (tk == TRUE)
|| (tk == TO_I) || (tk == TO_F) || (tk == TO_B) || (tk == TO_S)
|| (tk == LENGTH) || (tk == PUT) || (tk == GET) || (tk == WHILE)
|| (tk == AND) || (tk == OR) || (tk == NOT)){
printf("< palavra reservada ,%s >\n",yytext);
}else if(tk == ATR){
printf("< atribuição ,%s >\n",yytext);
}else if(tk == OP_REL){
printf("< operador relacional ,%s >\n",yytext);
}else if(tk == ID){
if(yyleng > TAMANHO_ID){
printf("Tamanho do identificador %d \nTamanho máximo permitido %d\n",yyleng,TAMANHO_ID);
exit(1);
}
printf("< identificador ,%s >\n",yytext);
inserirID(yytext);
}else if(tk == NUM){
printf("< numero ,%s >\n",yytext);
}else if(tk == PAR_A){
printf("< parentese abrindo ,%s >\n",yytext);
}else if(tk == PAR_F){
printf("< parentese fechando ,%s >\n",yytext);
}else if(tk == OPER){
printf("< operador ,%s >\n",yytext);
}else if(tk == STRING){
printf("< string ,%s >\n",yytext);
}else if(tk == ERRO){
printf("Caractere desconhecido [%c]\n",yytext[0]);
exit(1);
}else if(tk == PVIRG){
printf("< %s >\n",yytext);
}else{
printf("< %d,%s >\n",tk,yytext);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment