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

Cabecera autils.h.

parent 0903fb27
No related branches found
No related tags found
No related merge requests found
...@@ -100,7 +100,7 @@ ID { ...@@ -100,7 +100,7 @@ ID {
} }
/*Si no es una palabra clave y lo devuelve como identificador*/ /*Si no es una palabra clave y lo devuelve como identificador*/
yystr(yylval) = sdup(yytext); yystr(yylval) = sdup(yytext);
yyFlag(yylval) = fIDENT; yyFlag(yylval) = ID_id;
return ID; return ID;
} }
...@@ -114,14 +114,14 @@ NUMBER { /*Se lee el numero entero y se castea a double*/ ...@@ -114,14 +114,14 @@ NUMBER { /*Se lee el numero entero y se castea a double*/
long int li; long int li;
sscanf(yytext, "%d", &li); sscanf(yytext, "%d", &li);
yyFloat(yylval) = (double)li; yyFloat(yylval) = (double)li;
yyFlag(yylval) = fFLOAT; yyFlag(yylval) = REAL_id;
return FLOAT; return FLOAT;
} }
FLOAT { /*Se lee el numero decimal*/ FLOAT { /*Se lee el numero decimal*/
sscanf(yytext, "%lf", &(yyFloat(yylval))); sscanf(yytext, "%lf", &(yyFloat(yylval)));
yyFlag(yylval) = fFLOAT; yyFlag(yylval) = REAL_id;
return FLOAT; return FLOAT;
...@@ -129,7 +129,7 @@ FLOAT { /*Se lee el numero decimal*/ ...@@ -129,7 +129,7 @@ FLOAT { /*Se lee el numero decimal*/
STR_START { STR_START {
yyStr(yylval) = readStr(); yyStr(yylval) = readStr();
yyFlag(yylval) = fSTR; yyFlag(yylval) = STR_id;
return STR; return STR;
} }
......
...@@ -73,7 +73,7 @@ PROGRAM_ELEMENT ...@@ -73,7 +73,7 @@ PROGRAM_ELEMENT
/* Sentencia vacía */ /* Sentencia vacía */
| '\n' | '\n'
{ {
$$.type = AST_NODE; $$.type = AST_NODE_id;
$$.u.node = NULL; $$.u.node = NULL;
}; };
...@@ -82,94 +82,94 @@ SENTENCE ...@@ -82,94 +82,94 @@ SENTENCE
/* Asignación */ /* Asignación */
: ID '=' EXPR : ID '=' EXPR
{ {
$$.type = AST_NODE; $$.type = AST_NODE_id;
$$.u.node = newNode(ASSIG, newLeafString(ID, $1.u.string), $3.u.node); $$.u.node = newNode(ASSIG, newLeafString(ID, $1.u.string), $3.u.node);
} }
/* Impresión de un valor */ /* Impresión de un valor */
| PRINT EXPR | PRINT EXPR
{ {
$$.type = AST_NODE; $$.type = AST_NODE_id;
$$.u.node = newNode(PRINT, NULL, $2.u.node); $$.u.node = newNode(PRINT, NULL, $2.u.node);
} }
/* Impresión de una string */ /* Impresión de una string */
| PRINT STR | PRINT STR
{ {
$$.type = AST_NODE; $$.type = AST_NODE_id;
$$.u.node = newNode(PRINT, newLeafString(STR, $2.u.string), NULL); $$.u.node = newNode(PRINT, newLeafString(STR, $2.u.string), NULL);
} }
/* Impresión de una string y un valor */ /* Impresión de una string y un valor */
| PRINT STR EXPR | PRINT STR EXPR
{ {
$$.type = AST_NODE; $$.type = AST_NODE_id;
$$.u.node = newNode(PRINT, newLeafString(STR, $2.u.string), $3.u.node); $$.u.node = newNode(PRINT, newLeafString(STR, $2.u.string), $3.u.node);
} }
/* Lectura de un valor */ /* Lectura de un valor */
| READ ID | READ ID
{ {
$$.type = AST_NODE; $$.type = AST_NODE_id;
$$.u.node = newNode(READ, NULL, newLeafString(ID, $2.u.string)); $$.u.node = newNode(READ, NULL, newLeafString(ID, $2.u.string));
} }
/* Lectura de un valor con mensaje de aviso */ /* Lectura de un valor con mensaje de aviso */
| READ STR ID | READ STR ID
{ {
$$.type = AST_NODE; $$.type = AST_NODE_id;
$$.u.node = newNode(READ, newLeafString(STR, $2.u.string), newLeafString(ID, $3.u.string)) $$.u.node = newNode(READ, newLeafString(STR, $2.u.string), newLeafString(ID, $3.u.string))
}; };
EXPR EXPR
: EXPR EQ EXPR : EXPR EQ EXPR
{ {
$$.type = AST_NODE; $$.type = AST_NODE_id;
$$.u.node = newNode(EQ, $1.u.node, $3.u.node); $$.u.node = newNode(EQ, $1.u.node, $3.u.node);
} }
| EXPR NE EXPR | EXPR NE EXPR
{ {
$$.type = AST_NODE; $$.type = AST_NODE_id;
$$.u.node = newNode(NE, $1.u.node, $3.u.node); $$.u.node = newNode(NE, $1.u.node, $3.u.node);
} }
| EXPR LT EXPR | EXPR LT EXPR
{ {
$$.type = AST_NODE; $$.type = AST_NODE_id;
$$.u.node = newNode(LT, $1.u.node, $3.u.node); $$.u.node = newNode(LT, $1.u.node, $3.u.node);
} }
| EXPR GT EXPR | EXPR GT EXPR
{ {
$$.type = AST_NODE; $$.type = AST_NODE_id;
$$.u.node = newNode(GT, $1.u.node, $3.u.node); $$.u.node = newNode(GT, $1.u.node, $3.u.node);
} }
| EXPR '+' EXPR | EXPR '+' EXPR
{ {
$$.type = AST_NODE; $$.type = AST_NODE_id;
$$.u.node = newNode('+', $1.u.node, $3.u.node); $$.u.node = newNode('+', $1.u.node, $3.u.node);
} }
| EXPR '-' EXPR | EXPR '-' EXPR
{ {
$$.type = AST_NODE; $$.type = AST_NODE_id;
$$.u.node = newNode('-', $1.u.node, $3.u.node); $$.u.node = newNode('-', $1.u.node, $3.u.node);
} }
| EXPR '*' EXPR | EXPR '*' EXPR
{ {
$$.type = AST_NODE; $$.type = AST_NODE_id;
$$.u.node = newNode('*', $1.u.node, $3.u.node); $$.u.node = newNode('*', $1.u.node, $3.u.node);
} }
| EXPR '/' EXPR | EXPR '/' EXPR
{ {
$$.type = AST_NODE; $$.type = AST_NODE_id;
$$.u.node = newNode('/', $1.u.node, $3.u.node); $$.u.node = newNode('/', $1.u.node, $3.u.node);
} }
| EXPR '%' EXPR | EXPR '%' EXPR
{ {
$$.type = AST_NODE; $$.type = AST_NODE_id;
$$.u.node = newNode('%', $1.u.node, $3.u.node); $$.u.node = newNode('%', $1.u.node, $3.u.node);
} }
| EXPR '^' EXPR | EXPR '^' EXPR
{ {
$$.type = AST_NODE; $$.type = AST_NODE_id;
$$.u.node = newNode('^', $1.u.node, $3.u.node); $$.u.node = newNode('^', $1.u.node, $3.u.node);
} }
| EXPR DIV EXPR | EXPR DIV EXPR
{ {
$$.type = AST_NODE; $$.type = AST_NODE_id;
$$.u.node = newNode(DIV, $1.u.node, $3.u.node); $$.u.node = newNode(DIV, $1.u.node, $3.u.node);
} }
| '+' EXPR %prec UNARY | '+' EXPR %prec UNARY
...@@ -178,7 +178,7 @@ EXPR ...@@ -178,7 +178,7 @@ EXPR
} }
| '-' EXPR %prec UNARY | '-' EXPR %prec UNARY
{ {
$$.type = AST_NODE; $$.type = AST_NODE_id;
$$.u.node = newNode('-', NULL, $2.u.node); $$.u.node = newNode('-', NULL, $2.u.node);
} }
| '(' EXPR ')' | '(' EXPR ')'
...@@ -187,32 +187,32 @@ EXPR ...@@ -187,32 +187,32 @@ EXPR
} }
| FLOAT | FLOAT
{ {
$$.type = AST_NODE; $$.type = AST_NODE_id;
$$.u.node = newLeafNum(FLOAT,$1.u.real_value); $$.u.node = newLeafNum(FLOAT,$1.u.real_value);
} }
| ID | ID
{ {
$$.type = AST_NODE; $$.type = AST_NODE_id;
$$.u.node = newLeafString(ID,$1.u.string); $$.u.node = newLeafString(ID,$1.u.string);
} }
| SIN '(' EXPR ')' | SIN '(' EXPR ')'
{ {
$$.type = AST_NODE; $$.type = AST_NODE_id;
$$.u.node = newNode(SIN, $3.u.node, NULL); $$.u.node = newNode(SIN, $3.u.node, NULL);
} }
| COS '(' EXPR ')' | COS '(' EXPR ')'
{ {
$$.type = AST_NODE; $$.type = AST_NODE_id;
$$.u.node = newNode(COS, $3.u.node, NULL); $$.u.node = newNode(COS, $3.u.node, NULL);
} }
| TAN '(' EXPR ')' | TAN '(' EXPR ')'
{ {
$$.type = AST_NODE; $$.type = AST_NODE_id;
$$.u.node = newNode(TAN, $3.u.node, NULL); $$.u.node = newNode(TAN, $3.u.node, NULL);
} }
| LN '(' EXPR ')' | LN '(' EXPR ')'
{ {
$$.type = AST_NODE; $$.type = AST_NODE_id;
$$.u.node = newNode(LN, $3.u.node, NULL); $$.u.node = newNode(LN, $3.u.node, NULL);
}; };
......
...@@ -15,7 +15,7 @@ extern char programName[]; ...@@ -15,7 +15,7 @@ extern char programName[];
ast_t *newLeafString(unsigned tag, char *str) ast_t *newLeafString(unsigned tag, char *str)
{ {
ast_t *res = (ast_t *)malloc(sizeof(ast_t)); mallocCheck(ast_t *res, sizeof(ast_t));
lnum(res) = (unsigned)lineNum; lnum(res) = (unsigned)lineNum;
tag(res) = tag; tag(res) = tag;
sv(res) = str; sv(res) = str;
...@@ -25,7 +25,7 @@ ast_t *newLeafString(unsigned tag, char *str) ...@@ -25,7 +25,7 @@ ast_t *newLeafString(unsigned tag, char *str)
ast_t *newLeafNum(unsigned tag, double dval) ast_t *newLeafNum(unsigned tag, double dval)
{ {
ast_t *res = (ast_t *)malloc(sizeof(ast_t)); mallocCheck(ast_t *res, sizeof(ast_t));
lnum(res) = (unsigned)lineNum; lnum(res) = (unsigned)lineNum;
tag(res) = tag; tag(res) = tag;
dv(res) = dval; dv(res) = dval;
...@@ -34,7 +34,7 @@ ast_t *newLeafNum(unsigned tag, double dval) ...@@ -34,7 +34,7 @@ ast_t *newLeafNum(unsigned tag, double dval)
ast_t *newNode(unsigned tag, ast_t *l, ast_t *r) ast_t *newNode(unsigned tag, ast_t *l, ast_t *r)
{ {
ast_t *res = (ast_t *)malloc(sizeof(ast_t)); mallocCheck(ast_t *res, sizeof(ast_t));
lnum(res) = (unsigned)lineNum; lnum(res) = (unsigned)lineNum;
tag(res) = tag; tag(res) = tag;
left(res) = l; left(res) = l;
......
autils.h 0 → 100644
/*
* CÓDIGOS DE SALIDA PARA EL INTÉRPRETE.
*
*/
/* Identificadores de tokens */
#define ID_id 1
#define INT_id 2
#define REAL_id 3
#define STR_id 4
#define AST_NODE_id 5
/* Salidas del programa. */
#define PARSE_SUCCESS 0
#define LEXICAL_ERROR -1
#define SYNTAX_ERROR -2
#define FILE_ERROR -3
#define SYMTAB_NOT_FOUND -4
#define MEMORY_ERROR -5
#define mallocCheck(ptr, size) { \
if ((ptr = malloc(size)) == NULL) \
{ \
fprintf(stderr, "Fallo de alocación de memoria.\n"); \
exit(MEMORY_ERROR); \
} \
}
/*
* CÓDIGOS DE SALIDA PARA EL INTÉRPRETE.
*
*/
/* Salidas del programa. */
#define PARSE_SUCCESS 0
#define LEXICAL_ERROR -1
#define SYNTAX_ERROR -2
#define FILE_ERROR -3
#define SYMTAB_NOT_FOUND -4
\ No newline at end of file
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
*/ */
#include "symtab.h" #include "symtab.h"
#include "exits.h" #include "autils.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
...@@ -33,7 +33,7 @@ static symbol *symTab = NULL; ...@@ -33,7 +33,7 @@ static symbol *symTab = NULL;
/* Rellenar la tabla con entradas vacías */ /* Rellenar la tabla con entradas vacías */
static void init() static void init()
{ {
symTab = (symbol *)malloc(sizeof(symbol) * size); mallocCheck(symTab, sizeof(symbol) * size);
memset(symTab, 0, sizeof(symbol) * (size_t)size); memset(symTab, 0, sizeof(symbol) * (size_t)size);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment