Skip to content
Snippets Groups Projects
Commit 6e8f2d45 authored by mandeca's avatar mandeca :speech_balloon:
Browse files

Ahora el lex funciona, pero

parent 4ed2ab4a
No related branches found
No related tags found
No related merge requests found
...@@ -47,31 +47,31 @@ LETTER ([_a-zA-Z]) ...@@ -47,31 +47,31 @@ LETTER ([_a-zA-Z])
DIGIT ([0-9]) DIGIT ([0-9])
DIGITS ({DIGIT}+) DIGITS ({DIGIT}+)
EXP ([eE][-+]?{DIGITS}) /* Exponente de una float */ EXP ([eE][-+]?{DIGITS})
FLOAT1 ({DIGITS}"."{DIGITS}) FLOAT1 ({DIGITS}"."{DIGITS})
FLOAT2 ({DIGITS}{EXP}) FLOAT2 ({DIGITS}{EXP})
FLOAT3 ({DIGITS}"."{DIGITS}{EXP}) FLOAT3 ({DIGITS}"."{DIGITS}{EXP})
IDENT ({LETTER}({LETTER}|{DIGIT})*) /* Identificador */ IDENT ({LETTER}({LETTER}|{DIGIT})*)
COMSEP ([\n]) /* Separador de "comandos" (sentencias) */ COMSEP ([\n])
WSPC ([ \t\f\r]) /* "Espacio en blanco", no se va a utilizar para nada */ WSPC ([ \t\f\r])
WSPCS ({WSPC}+) /* Muchos espacios en blanco */ WSPCS ({WSPC}+)
FLOAT ({FLOAT1}|{FLOAT2}|{FLOAT3}) FLOAT ({FLOAT1}|{FLOAT2}|{FLOAT3})
OP1 ([-+/*=<>?:()!^]) /* Operadores aritméticos */ OP1 ([-+/*=<>?:()!^])
OP2 ("=="|"!="|"<="|">="|"&&"|"||") /* Operadores lógicos */ OP2 ("=="|"!="|"<="|">="|"&&"|"||")
STRSTART (["]) /* Comienzo de cadena de caracteres: comillas dobles " /**/ STRSTART (["])
%% %%
WSPC ; /* nothing to do, white space */ {WSPC} ; /* nothing to do, white space */
IDENT { {IDENT} {
unsigned i = 0; unsigned i = 0;
int r=-1; int r=-1;
char *res; char *res;
...@@ -91,13 +91,13 @@ IDENT { ...@@ -91,13 +91,13 @@ IDENT {
return IDENT; /* Se devuelve que se ha leído un identificador */ return IDENT; /* Se devuelve que se ha leído un identificador */
} }
COMSEP { {COMSEP} {
/* Salto de línea: se aumenta el número de línea */ /* Salto de línea: se aumenta el número de línea */
++yylineno; /* yylineno se define aquí (en este fichero) */ ++yylineno; /* yylineno se define aquí (en este fichero) */
return yytext[0]; /* Se devuelve el salto de línea */ return yytext[0]; /* Se devuelve el salto de línea */
} }
DIGITS { {DIGITS} {
/* Se lee el número entero */ /* Se lee el número entero */
long int li; long int li;
sscanf(yytext,"%ld",&li); sscanf(yytext,"%ld",&li);
...@@ -108,7 +108,7 @@ DIGITS { ...@@ -108,7 +108,7 @@ DIGITS {
return FLOAT; return FLOAT;
} }
FLOAT { {FLOAT} {
/* Se lee el número decimal */ /* Se lee el número decimal */
sscanf(yytext,"%lf",&( yyFloat(yylval) )); sscanf(yytext,"%lf",&( yyFloat(yylval) ));
yyFlag(yylval) = fFLOAT; yyFlag(yylval) = fFLOAT;
...@@ -116,7 +116,7 @@ FLOAT { ...@@ -116,7 +116,7 @@ FLOAT {
return FLOAT; return FLOAT;
} }
STRSTART { {STRSTART} {
/* Se lee la String */ /* Se lee la String */
yyStr(yylval) = readStr(); yyStr(yylval) = readStr();
yyFlag(yylval) = fSTR; yyFlag(yylval) = fSTR;
...@@ -124,9 +124,9 @@ STRSTART { ...@@ -124,9 +124,9 @@ STRSTART {
return STR; return STR;
} }
OP1 return yytext[0]; /* Se lee el operador aritmético */ {OP1} return yytext[0]; /* Se lee el operador aritmético */
OP2 { {OP2} {
switch (yytext[0]) { /* Se lee el operador lógico */ switch (yytext[0]) { /* Se lee el operador lógico */
case '=': case '=':
return EQ; return EQ;
... ...
......
...@@ -361,31 +361,29 @@ struct yy_trans_info ...@@ -361,31 +361,29 @@ struct yy_trans_info
flex_int32_t yy_verify; flex_int32_t yy_verify;
flex_int32_t yy_nxt; flex_int32_t yy_nxt;
}; };
static const flex_int16_t yy_accept[45] = static const flex_int16_t yy_accept[29] =
{ 0, { 0,
0, 0, 11, 9, 10, 9, 9, 9, 9, 9, 0, 0, 11, 9, 1, 3, 7, 6, 9, 7,
9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 4, 7, 7, 7, 2, 9, 8, 0, 4, 0,
0, 0, 0, 7, 8, 0, 0, 0, 0, 0, 2, 5, 0, 5, 0, 0, 5, 0
0, 0, 1, 0, 0, 5, 2, 0, 3, 4,
0, 0, 6, 0
} ; } ;
static const YY_CHAR yy_ec[256] = static const YY_CHAR yy_ec[256] =
{ 0, { 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 3, 4,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 5, 1, 6, 7, 8, 9,
10, 1, 11, 1, 1, 12, 13, 14, 15, 16,
1, 17, 18, 19, 1, 1, 20, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 2, 4, 5, 1, 1, 1, 6, 1, 7,
7, 7, 8, 1, 8, 9, 7, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 7, 1, 11,
12, 13, 7, 1, 14, 14, 14, 14, 15, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
1, 1, 1, 7, 14, 1, 14, 14, 14, 14,
15, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 1, 16, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
...@@ -402,52 +400,44 @@ static const YY_CHAR yy_ec[256] = ...@@ -402,52 +400,44 @@ static const YY_CHAR yy_ec[256] =
1, 1, 1, 1, 1 1, 1, 1, 1, 1
} ; } ;
static const YY_CHAR yy_meta[21] = static const YY_CHAR yy_meta[17] =
{ 0, { 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1 1, 1, 1, 2, 2, 1
} ; } ;
static const flex_int16_t yy_base[46] = static const flex_int16_t yy_base[30] =
{ 0, { 0,
0, 19, 57, 58, 58, 41, 44, 42, 46, 36, 0, 0, 38, 39, 39, 39, 25, 39, 30, 39,
32, 32, 36, 38, 32, 38, 0, 28, 28, 25, 8, 23, 22, 21, 0, 16, 39, 21, 0, 11,
31, 36, 26, 58, 58, 20, 30, 27, 14, 13, 0, 10, 20, 19, 14, 18, 17, 39, 24
12, 10, 58, 11, 6, 58, 58, 18, 58, 58,
5, 0, 58, 58, 0
} ; } ;
static const flex_int16_t yy_def[46] = static const flex_int16_t yy_def[30] =
{ 0, { 0,
45, 45, 44, 44, 44, 44, 44, 44, 44, 44, 28, 1, 28, 28, 28, 28, 28, 28, 28, 28,
44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 28, 28, 28, 28, 29, 28, 28, 28, 11, 28,
44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 29, 28, 28, 28, 28, 28, 28, 0, 28
44, 44, 44, 44, 44, 44, 44, 44, 44, 44,
44, 44, 44, 0, 44
} ; } ;
static const flex_int16_t yy_nxt[79] = static const flex_int16_t yy_nxt[56] =
{ 0, { 0,
4, 5, 24, 25, 44, 6, 7, 44, 8, 44, 4, 5, 6, 7, 8, 9, 10, 10, 4, 11,
9, 44, 44, 44, 10, 44, 44, 11, 43, 12, 12, 13, 14, 15, 15, 16, 18, 19, 23, 22,
5, 42, 41, 40, 6, 7, 39, 8, 38, 9, 24, 26, 20, 27, 25, 21, 27, 27, 24, 24,
37, 36, 35, 10, 34, 33, 11, 32, 12, 31, 22, 17, 17, 17, 17, 17, 17, 28, 3, 28,
30, 29, 28, 27, 26, 23, 22, 21, 20, 19, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
18, 17, 16, 15, 14, 13, 44, 3, 44, 44, 28, 28, 28, 28, 28
44, 44, 44, 44, 44, 44, 44, 44, 44, 44,
44, 44, 44, 44, 44, 44, 44, 44
} ; } ;
static const flex_int16_t yy_chk[79] = static const flex_int16_t yy_chk[56] =
{ 0, { 0,
45, 1, 17, 17, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 1, 0, 0, 1, 42, 1, 1, 1, 1, 1, 1, 1, 11, 11, 20, 22,
2, 41, 38, 35, 2, 2, 34, 2, 32, 2, 20, 25, 11, 25, 22, 29, 27, 26, 24, 23,
31, 30, 29, 2, 28, 27, 2, 26, 2, 23, 18, 16, 14, 13, 12, 9, 7, 3, 28, 28,
22, 21, 20, 19, 18, 16, 15, 14, 13, 12, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
11, 10, 9, 8, 7, 6, 3, 44, 44, 44, 28, 28, 28, 28, 28
44, 44, 44, 44, 44, 44, 44, 44, 44, 44,
44, 44, 44, 44, 44, 44, 44, 44
} ; } ;
static yy_state_type yy_last_accepting_state; static yy_state_type yy_last_accepting_state;
...@@ -506,8 +496,8 @@ static void lower(char *s); /* Pasar todas las letras a minúsculas (definida ab ...@@ -506,8 +496,8 @@ static void lower(char *s); /* Pasar todas las letras a minúsculas (definida ab
static char *readStr(void); /* Leer y guardar String como "variable" (definida abajo) */ static char *readStr(void); /* Leer y guardar String como "variable" (definida abajo) */
int yywrap(void) { return 1; } /* Al terminar la lectura de un fichero, pista */ int yywrap(void) { return 1; } /* Al terminar la lectura de un fichero, pista */
#line 510 "inter05.lex.c" #line 500 "inter05.lex.c"
#line 511 "inter05.lex.c" #line 501 "inter05.lex.c"
#define INITIAL 0 #define INITIAL 0
...@@ -727,7 +717,7 @@ YY_DECL ...@@ -727,7 +717,7 @@ YY_DECL
#line 70 "inter05.l" #line 70 "inter05.l"
#line 731 "inter05.lex.c" #line 721 "inter05.lex.c"
while ( /*CONSTCOND*/1 ) /* loops until end-of-file is reached */ while ( /*CONSTCOND*/1 ) /* loops until end-of-file is reached */
{ {
...@@ -754,13 +744,13 @@ yy_match: ...@@ -754,13 +744,13 @@ yy_match:
while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )
{ {
yy_current_state = (int) yy_def[yy_current_state]; yy_current_state = (int) yy_def[yy_current_state];
if ( yy_current_state >= 45 ) if ( yy_current_state >= 29 )
yy_c = yy_meta[yy_c]; yy_c = yy_meta[yy_c];
} }
yy_current_state = yy_nxt[yy_base[yy_current_state] + yy_c]; yy_current_state = yy_nxt[yy_base[yy_current_state] + yy_c];
++yy_cp; ++yy_cp;
} }
while ( yy_base[yy_current_state] != 58 ); while ( yy_base[yy_current_state] != 39 );
yy_find_action: yy_find_action:
yy_act = yy_accept[yy_current_state]; yy_act = yy_accept[yy_current_state];
...@@ -813,6 +803,7 @@ YY_RULE_SETUP ...@@ -813,6 +803,7 @@ YY_RULE_SETUP
} }
YY_BREAK YY_BREAK
case 3: case 3:
/* rule 3 can match eol */
YY_RULE_SETUP YY_RULE_SETUP
#line 94 "inter05.l" #line 94 "inter05.l"
{ {
...@@ -895,7 +886,7 @@ YY_RULE_SETUP ...@@ -895,7 +886,7 @@ YY_RULE_SETUP
#line 151 "inter05.l" #line 151 "inter05.l"
ECHO; ECHO;
YY_BREAK YY_BREAK
#line 899 "inter05.lex.c" #line 890 "inter05.lex.c"
case YY_STATE_EOF(INITIAL): case YY_STATE_EOF(INITIAL):
yyterminate(); yyterminate();
...@@ -1192,7 +1183,7 @@ static int yy_get_next_buffer (void) ...@@ -1192,7 +1183,7 @@ static int yy_get_next_buffer (void)
while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )
{ {
yy_current_state = (int) yy_def[yy_current_state]; yy_current_state = (int) yy_def[yy_current_state];
if ( yy_current_state >= 45 ) if ( yy_current_state >= 29 )
yy_c = yy_meta[yy_c]; yy_c = yy_meta[yy_c];
} }
yy_current_state = yy_nxt[yy_base[yy_current_state] + yy_c]; yy_current_state = yy_nxt[yy_base[yy_current_state] + yy_c];
...@@ -1220,11 +1211,11 @@ static int yy_get_next_buffer (void) ...@@ -1220,11 +1211,11 @@ static int yy_get_next_buffer (void)
while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )
{ {
yy_current_state = (int) yy_def[yy_current_state]; yy_current_state = (int) yy_def[yy_current_state];
if ( yy_current_state >= 45 ) if ( yy_current_state >= 29 )
yy_c = yy_meta[yy_c]; yy_c = yy_meta[yy_c];
} }
yy_current_state = yy_nxt[yy_base[yy_current_state] + yy_c]; yy_current_state = yy_nxt[yy_base[yy_current_state] + yy_c];
yy_is_jam = (yy_current_state == 44); yy_is_jam = (yy_current_state == 28);
return yy_is_jam ? 0 : yy_current_state; return yy_is_jam ? 0 : yy_current_state;
} }
... ...
......
...@@ -17,10 +17,10 @@ ac.syn.c: $(SYN_DEP) ...@@ -17,10 +17,10 @@ ac.syn.c: $(SYN_DEP)
ac.syn.h: $(SYN_DEP) ac.syn.h: $(SYN_DEP)
$(LEXICAL): $(LEXICAL):
flex -o $(LEX_NAME) $? lex -o $(LEX_NAME) $?
$(SYNTAX): $(SYNTAX):
bison --defines -o $(SYN_NAME) $? yacc --defines -o $(SYN_NAME) $?
clean: clean:
rm ac ac.lex.c ac.syn.* rm ac ac.lex.c ac.syn.*
...@@ -45,58 +45,50 @@ extern char programName[]; ...@@ -45,58 +45,50 @@ extern char programName[];
static char *readStr(); static char *readStr();
static void addStr(char **s, unsigned long *len, char c); static void addStr(char **s, unsigned long *len, char c);
%} %}
/*Letras del alfabeto, minusculas y mayusculas*/
LETTER ([_a-zA-Z]) LETTER ([_a-zA-Z])
/*Digito y numeros*/
DIGIT ([0-9]) DIGIT ([0-9])
NUMBER ({DIGIT}+) NUMBER ({DIGIT}+)
/*Representacion tipo float*/ FLOAT (({NUMBER}"."{DIGIT}*)|({DIGIT}*"."{NUMBER}))
FLOAT (({NUMBER}"."{DIGIT}*)({DIGIT}*"."{NUMBER})
/*Identificador*/
ID ({LETTER}({LETTER}|{DIGIT})*) ID ({LETTER}({LETTER}|{DIGIT})*)
/*Delimitador de bloques*/ DEL_BL_A ([{{WSPC}*\n])
DEL_BL_A (["{"{WSPC}*"\n"]) DEL_BL_C ([}{WSPC}*\n])
DEL_BL_C (["}"{WSPC}*"\n"])
/* DEL_BL_C_2????? */
/*Espacios en blanco*/
WSPC ([ \t\f\r]) WSPC ([ \t\f\r])
WSCPS ({WSPC}+) WSCPS ({WSPC}+)
/*Operadores aritmeticos*/
OP_AR ([+*-/%=()^]) OP_AR ([+*-/%=()^])
/*Operadores logicos*/
OP_LOG ("=="|"!="|"<"|">") OP_LOG ("=="|"!="|"<"|">")
/*Comienzo de cadena de caracteres*/
STR_START (["]) STR_START (["])
/* Comentario de línea */ LINE_COMM ("//".*\n)
LINE_COMM ("//".*"\n")
/* Comentarios del programa */ COMM ("/*"[.|\n]*"*/")
COMM ("/*"[.|"\n"]*"*/")
%% %%
WSPC|LINE_COMM ; [\n] { /*Salto de linea*/
++yylineno;
return yytext[0];
}
{WSPC}|{LINE_COMM} ;
COMM { /*Cuenta las líneas que ocupa el comentario*/ {COMM} { /*Cuenta las líneas que ocupa el comentario*/
for (int i = 0; i < strlen(yytext); i++) for (int i = 0; i < strlen(yytext); i++)
{ {
if (yytext[0] == '\n') yylineno++; if (yytext[0] == '\n') yylineno++;
} }
} }
ID { {ID} {
unsigned i = 0; unsigned i = 0;
int r = -1; int r = -1;
char *res; char *res;
...@@ -114,13 +106,8 @@ ID { ...@@ -114,13 +106,8 @@ ID {
return ID; return ID;
} }
"\n" { /*Salto de linea*/ {NUMBER} { /*Se lee el numero entero y se castea a double*/
++yylineno; long li;
return yytext[0];
}
NUMBER { /*Se lee el numero entero y se castea a double*/
long int li;
sscanf(yytext, "%ld", &li); sscanf(yytext, "%ld", &li);
yylval.s.u.real_value = (double)li; yylval.s.u.real_value = (double)li;
yylval.s.type = REAL_id; yylval.s.type = REAL_id;
...@@ -128,7 +115,7 @@ NUMBER { /*Se lee el numero entero y se castea a double*/ ...@@ -128,7 +115,7 @@ NUMBER { /*Se lee el numero entero y se castea a double*/
return FLOAT; return FLOAT;
} }
FLOAT { /*Se lee el numero decimal*/ {FLOAT} { /*Se lee el numero decimal*/
sscanf(yytext, "%lf", &yylval.s.u.real_value); sscanf(yytext, "%lf", &yylval.s.u.real_value);
yylval.s.type = REAL_id; yylval.s.type = REAL_id;
...@@ -136,18 +123,18 @@ FLOAT { /*Se lee el numero decimal*/ ...@@ -136,18 +123,18 @@ FLOAT { /*Se lee el numero decimal*/
} }
STR_START { {STR_START} {
yylval.s.u.string = readStr(); yylval.s.u.string = readStr();
yylval.s.type = STR_id; yylval.s.type = STR_id;
return STR; return STR;
} }
OP_AR { {OP_AR} {
return yytext[0]; return yytext[0];
} }
OP_LOG { {OP_LOG} {
switch(yytext[0]) { switch(yytext[0]) {
case '=': case '=':
return EQ; return EQ;
... ...
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
int yylex(); int yylex();
int yyerror(char *error); int yyerror(char *error);
extern int yylineno; extern int yylineno;
extern FILE *yyin;
/* Árbol de sintaxis */ /* Árbol de sintaxis */
static ast_t *ast = NULL; static ast_t *ast = NULL;
...@@ -267,7 +268,7 @@ char programName[256] = ""; ...@@ -267,7 +268,7 @@ char programName[256] = "";
/* Gestión de errores */ /* Gestión de errores */
int yyerror(char *error) int yyerror(char *error)
{ {
printf("%s(%d): error -- %s\n", programName, yylineno, error); fprintf(stderr, "%s(%d): error -- %s\n", programName, yylineno, error);
return 1; return 1;
} }
...@@ -281,7 +282,7 @@ int main(int argc, char *argv[]) ...@@ -281,7 +282,7 @@ int main(int argc, char *argv[])
strcpy(programName, argv[1]); strcpy(programName, argv[1]);
FILE *yyin = fopen(programName, "rb"); yyin = fopen(programName, "rb");
if (yyin == NULL) if (yyin == NULL)
{ {
fprintf(stderr, "Error intentando abrir el fichero %s\n", programName); fprintf(stderr, "Error intentando abrir el fichero %s\n", programName);
... ...
......
read "Introduzca una variable" x
print "Ha introducido" x
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment