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

Código comentado.

parent 345acf5e
Branches
No related tags found
No related merge requests found
...@@ -101,10 +101,12 @@ BLOCK ...@@ -101,10 +101,12 @@ BLOCK
/* Grupo de sentencias */ /* Grupo de sentencias */
SENTENCE_GROUP SENTENCE_GROUP
/* Un único elemento de un programa */
: PROGRAM_ELEMENT : PROGRAM_ELEMENT
{ {
$$ = $1; $$ = $1;
} }
/* Un grupo de sentencias y un elemento del programa (recursivo) */
| SENTENCE_GROUP PROGRAM_ELEMENT | SENTENCE_GROUP PROGRAM_ELEMENT
{ {
$$.type = AST_NODE_id; $$.type = AST_NODE_id;
...@@ -154,92 +156,111 @@ SENTENCE ...@@ -154,92 +156,111 @@ SENTENCE
$$.type = AST_NODE_id; $$.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));
} }
/* Bucle while */
| WHILE '(' EXPR ')' SENTENCE | WHILE '(' EXPR ')' SENTENCE
{ {
$$.type = AST_NODE_id; $$.type = AST_NODE_id;
$$.u.node = newNode(WHILE, $3.u.node, $5.u.node); $$.u.node = newNode(WHILE, $3.u.node, $5.u.node);
} }
/* Condicional if-else */
| IF '(' EXPR ')' SENTENCE ELSE SENTENCE | IF '(' EXPR ')' SENTENCE ELSE SENTENCE
{ {
$$.type = AST_NODE_id; $$.type = AST_NODE_id;
$$.u.node = newNode('g', NULL, newRoot('r', newRoot('r', NULL, newNode(IF, $3.u.node, $5.u.node)), newNode(ELSE, $3.u.node, $7.u.node))); $$.u.node = newNode('g', NULL, newRoot('r', newRoot('r', NULL, newNode(IF, $3.u.node, $5.u.node)), newNode(ELSE, $3.u.node, $7.u.node)));
} }
/* Condicional if */
| IF '(' EXPR ')' SENTENCE | IF '(' EXPR ')' SENTENCE
{ {
$$.type = AST_NODE_id; $$.type = AST_NODE_id;
$$.u.node = newNode(IF, $3.u.node, $5.u.node); $$.u.node = newNode(IF, $3.u.node, $5.u.node);
}; };
/* Expresiones */
EXPR EXPR
/* Igualdad lógica */
: EXPR EQ EXPR : EXPR EQ EXPR
{ {
$$.type = AST_NODE_id; $$.type = AST_NODE_id;
$$.u.node = newNode(EQ, $1.u.node, $3.u.node); $$.u.node = newNode(EQ, $1.u.node, $3.u.node);
} }
/* Desigualdad lógica */
| EXPR NE EXPR | EXPR NE EXPR
{ {
$$.type = AST_NODE_id; $$.type = AST_NODE_id;
$$.u.node = newNode(NE, $1.u.node, $3.u.node); $$.u.node = newNode(NE, $1.u.node, $3.u.node);
} }
/* And lógico */
| EXPR LAND EXPR | EXPR LAND EXPR
{ {
$$.type = AST_NODE_id; $$.type = AST_NODE_id;
$$.u.node = newNode(LAND, $1.u.node, $3.u.node); $$.u.node = newNode(LAND, $1.u.node, $3.u.node);
} }
/* Or lógico */
| EXPR LOR EXPR | EXPR LOR EXPR
{ {
$$.type = AST_NODE_id; $$.type = AST_NODE_id;
$$.u.node = newNode(LOR, $1.u.node, $3.u.node); $$.u.node = newNode(LOR, $1.u.node, $3.u.node);
} }
/* Menor que */
| EXPR LT EXPR | EXPR LT EXPR
{ {
$$.type = AST_NODE_id; $$.type = AST_NODE_id;
$$.u.node = newNode(LT, $1.u.node, $3.u.node); $$.u.node = newNode(LT, $1.u.node, $3.u.node);
} }
/* Mayor que */
| EXPR GT EXPR | EXPR GT EXPR
{ {
$$.type = AST_NODE_id; $$.type = AST_NODE_id;
$$.u.node = newNode(GT, $1.u.node, $3.u.node); $$.u.node = newNode(GT, $1.u.node, $3.u.node);
} }
/* Suma aritmética */
| EXPR '+' EXPR | EXPR '+' EXPR
{ {
$$.type = AST_NODE_id; $$.type = AST_NODE_id;
$$.u.node = newNode('+', $1.u.node, $3.u.node); $$.u.node = newNode('+', $1.u.node, $3.u.node);
} }
/* Resta aritmética */
| EXPR '-' EXPR | EXPR '-' EXPR
{ {
$$.type = AST_NODE_id; $$.type = AST_NODE_id;
$$.u.node = newNode('-', $1.u.node, $3.u.node); $$.u.node = newNode('-', $1.u.node, $3.u.node);
} }
/* Multiplicación aritmética */
| EXPR '*' EXPR | EXPR '*' EXPR
{ {
$$.type = AST_NODE_id; $$.type = AST_NODE_id;
$$.u.node = newNode('*', $1.u.node, $3.u.node); $$.u.node = newNode('*', $1.u.node, $3.u.node);
} }
/* División aritmética */
| EXPR '/' EXPR | EXPR '/' EXPR
{ {
$$.type = AST_NODE_id; $$.type = AST_NODE_id;
$$.u.node = newNode('/', $1.u.node, $3.u.node); $$.u.node = newNode('/', $1.u.node, $3.u.node);
} }
/* Módulo */
| EXPR '%' EXPR | EXPR '%' EXPR
{ {
$$.type = AST_NODE_id; $$.type = AST_NODE_id;
$$.u.node = newNode('%', $1.u.node, $3.u.node); $$.u.node = newNode('%', $1.u.node, $3.u.node);
} }
/* Potencia */
| EXPR '^' EXPR | EXPR '^' EXPR
{ {
$$.type = AST_NODE_id; $$.type = AST_NODE_id;
$$.u.node = newNode('^', $1.u.node, $3.u.node); $$.u.node = newNode('^', $1.u.node, $3.u.node);
} }
/* Operación cociente */
| EXPR DIV EXPR | EXPR DIV EXPR
{ {
$$.type = AST_NODE_id; $$.type = AST_NODE_id;
$$.u.node = newNode(DIV, $1.u.node, $3.u.node); $$.u.node = newNode(DIV, $1.u.node, $3.u.node);
} }
/* Mantenimiento de signo */
| '+' EXPR %prec UNARY | '+' EXPR %prec UNARY
{ {
$$ = $2; $$ = $2;
} }
/* Cambio de signo */
| '-' EXPR %prec UNARY | '-' EXPR %prec UNARY
{ {
$$.type = AST_NODE_id; $$.type = AST_NODE_id;
...@@ -249,61 +270,73 @@ EXPR ...@@ -249,61 +270,73 @@ EXPR
{ {
$$ = $2; $$ = $2;
} }
/* Bit-and */
| EXPR BAND EXPR | EXPR BAND EXPR
{ {
$$.type = AST_NODE_id; $$.type = AST_NODE_id;
$$.u.node = newNode(BAND, $1.u.node, $3.u.node); $$.u.node = newNode(BAND, $1.u.node, $3.u.node);
} }
/* Bit-or */
| EXPR BOR EXPR | EXPR BOR EXPR
{ {
$$.type = AST_NODE_id; $$.type = AST_NODE_id;
$$.u.node = newNode(BOR, $1.u.node, $3.u.node); $$.u.node = newNode(BOR, $1.u.node, $3.u.node);
} }
/* Bit-not */
| BNOT EXPR | BNOT EXPR
{ {
$$.type = AST_NODE_id; $$.type = AST_NODE_id;
$$.u.node = newNode(BNOT, NULL, $2.u.node); $$.u.node = newNode(BNOT, NULL, $2.u.node);
} }
/* Shift left */
| EXPR SL EXPR | EXPR SL EXPR
{ {
$$.type = AST_NODE_id; $$.type = AST_NODE_id;
$$.u.node = newNode(SL, $1.u.node, $3.u.node); $$.u.node = newNode(SL, $1.u.node, $3.u.node);
} }
/* Shift right */
| EXPR SR EXPR | EXPR SR EXPR
{ {
$$.type = AST_NODE_id; $$.type = AST_NODE_id;
$$.u.node = newNode(SR, $1.u.node, $3.u.node); $$.u.node = newNode(SR, $1.u.node, $3.u.node);
} }
/* Número real */
| FLOAT | FLOAT
{ {
$$.type = AST_NODE_id; $$.type = AST_NODE_id;
$$.u.node = newLeafNum(FLOAT, $1.u.real_value); $$.u.node = newLeafNum(FLOAT, $1.u.real_value);
} }
/* Número entero */
| INT | INT
{ {
$$.type = AST_NODE_id; $$.type = AST_NODE_id;
$$.u.node = newLeafInt(INT, $1.u.int_value); $$.u.node = newLeafInt(INT, $1.u.int_value);
} }
/* Variable */
| ID | ID
{ {
$$.type = AST_NODE_id; $$.type = AST_NODE_id;
$$.u.node = newLeafString(ID, $1.u.string); $$.u.node = newLeafString(ID, $1.u.string);
} }
/* Seno */
| SIN '(' EXPR ')' | SIN '(' EXPR ')'
{ {
$$.type = AST_NODE_id; $$.type = AST_NODE_id;
$$.u.node = newNode(SIN, $3.u.node, NULL); $$.u.node = newNode(SIN, $3.u.node, NULL);
} }
/* Coseno */
| COS '(' EXPR ')' | COS '(' EXPR ')'
{ {
$$.type = AST_NODE_id; $$.type = AST_NODE_id;
$$.u.node = newNode(COS, $3.u.node, NULL); $$.u.node = newNode(COS, $3.u.node, NULL);
} }
/* Tangente */
| TAN '(' EXPR ')' | TAN '(' EXPR ')'
{ {
$$.type = AST_NODE_id; $$.type = AST_NODE_id;
$$.u.node = newNode(TAN, $3.u.node, NULL); $$.u.node = newNode(TAN, $3.u.node, NULL);
} }
/* Logaritmo neperiano */
| LN '(' EXPR ')' | LN '(' EXPR ')'
{ {
$$.type = AST_NODE_id; $$.type = AST_NODE_id;
...@@ -324,14 +357,17 @@ int yyerror(char *error) ...@@ -324,14 +357,17 @@ int yyerror(char *error)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
/* Comprobación de corrección de argumentos */
if (argc < 2 || argc > 3 || (argc == 3 && strcmp("-t", argv[2]) != 0)) if (argc < 2 || argc > 3 || (argc == 3 && strcmp("-t", argv[2]) != 0))
{ {
printf("Uso: ./ac <fichero> [-t]\n"); printf("Uso: ./ac <fichero> [-t]\n");
exit(FILE_ERROR); exit(FILE_ERROR);
} }
/* Nombre del fichero */
strcpy(programName, argv[1]); strcpy(programName, argv[1]);
/* Lectura del fichero */
yyin = fopen(programName, "rb"); yyin = fopen(programName, "rb");
if (yyin == NULL) if (yyin == NULL)
{ {
...@@ -339,6 +375,7 @@ int main(int argc, char *argv[]) ...@@ -339,6 +375,7 @@ int main(int argc, char *argv[])
exit(FILE_ERROR); exit(FILE_ERROR);
} }
/* Parseo */
if (yyparse() != PARSE_SUCCESS) if (yyparse() != PARSE_SUCCESS)
{ {
fclose(yyin); fclose(yyin);
...@@ -348,8 +385,10 @@ int main(int argc, char *argv[]) ...@@ -348,8 +385,10 @@ int main(int argc, char *argv[])
fclose(yyin); fclose(yyin);
/* Recorrido del árbol */
if (ast != NULL) if (ast != NULL)
{ {
/* Impresión, si se ha utilizado la opción -t */
if (argc == 3 && strcmp("-t", argv[2]) == 0) if (argc == 3 && strcmp("-t", argv[2]) == 0)
{ {
char *treeFilename; char *treeFilename;
...@@ -372,6 +411,7 @@ int main(int argc, char *argv[]) ...@@ -372,6 +411,7 @@ int main(int argc, char *argv[])
} }
fclose(treeFile); fclose(treeFile);
} }
/* Evaluación del árbol */
process(ast); process(ast);
} }
else else
... ...
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
extern int yylineno; extern int yylineno;
extern char programName[]; extern char programName[];
/* Creación de una hoja con valor de cadena de caracteres */
ast_t *newLeafString(unsigned tag, char *str) ast_t *newLeafString(unsigned tag, char *str)
{ {
ast_t *res; ast_t *res;
...@@ -27,7 +28,7 @@ ast_t *newLeafString(unsigned tag, char *str) ...@@ -27,7 +28,7 @@ ast_t *newLeafString(unsigned tag, char *str)
return res; return res;
} }
/* Creación de una hoja con valor de número real */
ast_t *newLeafNum(unsigned tag, double dval) ast_t *newLeafNum(unsigned tag, double dval)
{ {
ast_t *res; ast_t *res;
...@@ -38,7 +39,7 @@ ast_t *newLeafNum(unsigned tag, double dval) ...@@ -38,7 +39,7 @@ ast_t *newLeafNum(unsigned tag, double dval)
return res; return res;
} }
/* Creación de una hoja con valor de número entero */
ast_t *newLeafInt(unsigned tag, long val) ast_t *newLeafInt(unsigned tag, long val)
{ {
ast_t *res; ast_t *res;
...@@ -49,6 +50,7 @@ ast_t *newLeafInt(unsigned tag, long val) ...@@ -49,6 +50,7 @@ ast_t *newLeafInt(unsigned tag, long val)
return res; return res;
} }
/* Creación de un nodo no hoja */
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 *res;
...@@ -60,6 +62,7 @@ ast_t *newNode(unsigned tag, ast_t *l, ast_t *r) ...@@ -60,6 +62,7 @@ ast_t *newNode(unsigned tag, ast_t *l, ast_t *r)
return res; return res;
} }
/* Creación-actualización de la raíz del árbol */
ast_t *newRoot(unsigned tag, ast_t *lst, ast_t *nd) ast_t *newRoot(unsigned tag, ast_t *lst, ast_t *nd)
{ {
if (lst == NULL) { if (lst == NULL) {
...@@ -81,6 +84,7 @@ ast_t *newRoot(unsigned tag, ast_t *lst, ast_t *nd) ...@@ -81,6 +84,7 @@ ast_t *newRoot(unsigned tag, ast_t *lst, ast_t *nd)
return lst; return lst;
} }
/* Evaluar expresión del AST */
static symbol evaluateExpr(ast_t *node) static symbol evaluateExpr(ast_t *node)
{ {
symbol value; /* Valor de la expresión */ symbol value; /* Valor de la expresión */
...@@ -447,8 +451,7 @@ static symbol evaluateExpr(ast_t *node) ...@@ -447,8 +451,7 @@ static symbol evaluateExpr(ast_t *node)
} }
} }
/* Evaluar nodo-sentencia del AST */
static void evaluateNode(ast_t *node) static void evaluateNode(ast_t *node)
{ {
switch (node->tag) switch (node->tag)
...@@ -576,7 +579,7 @@ static void evaluateNode(ast_t *node) ...@@ -576,7 +579,7 @@ static void evaluateNode(ast_t *node)
} }
} }
/* Procesar un AST a partir de su raíz */
void process(ast_t *root) void process(ast_t *root)
{ {
while (root != NULL) while (root != NULL)
...@@ -587,6 +590,7 @@ void process(ast_t *root) ...@@ -587,6 +590,7 @@ void process(ast_t *root)
} }
} }
/* Transformar un nodo en una String para su impresión */
static char *translate(ast_t *node) static char *translate(ast_t *node)
{ {
char *leaf; char *leaf;
...@@ -637,6 +641,7 @@ static char *translate(ast_t *node) ...@@ -637,6 +641,7 @@ static char *translate(ast_t *node)
} }
} }
/* Imprimir recursivamente un árbol a partir del nodo actual */
void print_tree(FILE *f, ast_t *node, int space) void print_tree(FILE *f, ast_t *node, int space)
{ {
if (node->u.child.left != NULL && node->tag != STR && node->tag != INT && node->tag != FLOAT && node->tag != ID) if (node->u.child.left != NULL && node->tag != STR && node->tag != INT && node->tag != FLOAT && node->tag != ID)
... ...
......
...@@ -21,6 +21,7 @@ typedef struct ast_s { ...@@ -21,6 +21,7 @@ typedef struct ast_s {
} u; } u;
} ast_t; } ast_t;
/* Funciones de ast.c */
ast_t *newLeafString(unsigned tag, char *str); ast_t *newLeafString(unsigned tag, char *str);
ast_t *newLeafNum(unsigned tag, double dval); ast_t *newLeafNum(unsigned tag, double dval);
ast_t *newLeafInt(unsigned tag, long int); ast_t *newLeafInt(unsigned tag, long int);
... ...
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#define ILEGAL_OPERATION -6 #define ILEGAL_OPERATION -6
#define MEMORY_ERROR -7 #define MEMORY_ERROR -7
/* Malloc con comprobación automática de errores */
#define mallocCheck(ptr, size) { \ #define mallocCheck(ptr, size) { \
if ((ptr = malloc(size)) == NULL) \ if ((ptr = malloc(size)) == NULL) \
{ \ { \
... ...
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment