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

Bloques de sentencias.

parent b52fc631
Branches
No related tags found
No related merge requests found
...@@ -48,33 +48,23 @@ LETTER ([_a-zA-Z]) ...@@ -48,33 +48,23 @@ LETTER ([_a-zA-Z])
DIGIT ([0-9]) DIGIT ([0-9])
NUMBER ({DIGIT}+) NUMBER ({DIGIT}+)
/*Exponente de float*/
EXP ([eE][-+]?{NUMBER})
/*Distintas formas de representacion de constantes tipo float*/
FLOAT_DOT ({NUMBER}"."{NUMBER})
FLOAT_EXP ({NUMBER}{EXP})
FLOAT_DE ({NUMBER}"."{NUMBER}{EXP})
/*Representacion tipo float*/ /*Representacion tipo float*/
FLOAT ({FLOAT_DOT}|{FLOAT_EXP}|{FLOAT_DE}) FLOAT (({NUMBER}"."{DIGIT}*)({DIGIT}*"."{NUMBER})
/*Identificador*/ /*Identificador*/
ID ({LETTER}({LETTER}|{DIGIT})*) ID ({LETTER}({LETTER}|{DIGIT})*)
/*Separador de sentencias*/
SEPAR ([\n])
/*Delimitador de bloques*/ /*Delimitador de bloques*/
DEL_BL_A ([{]) DEL_BL_A (["{"{WSPC}*"\n"])
DEL_BL_C ([}]) DEL_BL_C (["}"{WSPC}*"\n"])
/* DEL_BL_C_2????? */
/*Espacios en blanco*/ /*Espacios en blanco*/
WSPC ([ \t\f\r]) WSPC ([ \t\f\r])
WSCPS ({WSPC}+) WSCPS ({WSPC}+)
/*Operadores aritmeticos*/ /*Operadores aritmeticos*/
OP_AR ([-+/*%=()^]) OP_AR ([+*-/%=()^])
/*Operadores logicos*/ /*Operadores logicos*/
OP_LOG ("=="|"!="|"<"|">") OP_LOG ("=="|"!="|"<"|">")
...@@ -211,23 +201,3 @@ static char *readStr(void) ...@@ -211,23 +201,3 @@ static char *readStr(void)
} while(1); } while(1);
return buff; return buff;
} }
/*
______
< \
<. .\ O
| \______
<\O____/
/ \
> <
TODO
Donehre ASCII
*/
...@@ -45,39 +45,60 @@ static ast_t ast = NULL; ...@@ -45,39 +45,60 @@ static ast_t ast = NULL;
%term PRINT READ SIN COS TAN LN %term PRINT READ SIN COS TAN LN
%token <S> FLOAT %token <S> FLOAT
%token <s> STR %token <s> STR
%term WHILE IF ELSE
%term WHILE %type <s> PROGRAM PROGRAM_ELEMENT SENTENCE EXPR BLOCK
%type <s> PROGRAM PROGRAM_ELEMENT SENTENCE EXPR
%type <s> BLOCK
%% %%
/* Producciones de un programa */ /* Producciones de un programa */
PROGRAM PROGRAM
/* Un único elemento de programa */ /* Un único bloque de programa */
: PROGRAM_ELEMENT : BLOCK
{ {
ast = newRoot('l', ast, $1.u.node); ast = newRoot('r', ast, $1.u.node);
} }
/* Varios elementos de programa */ /* Varios bloques de programa */
| PROGRAM PROGRAM_ELEMENT | PROGRAM BLOCK
{ {
ast = newRoot('l', ast, $2.u.node); ast = newRoot('r', ast, $2.u.node);
}; };
/* Elementos de un programa */ /* Bloque de programa */
PROGRAM_ELEMENT BLOCK
/* Sentencia */ /* Una sentencia */
: SENTENCE '\n' : SENTENCE '\n'
{ {
$$ = $1; $$.type = AST_NODE_id;
$$.u.node = newNode('s', NULL, $1.u.node);
} }
/* Sentencia vacía */ /* Sentencia vacía */
| '\n' | '\n'
{ {
$$.type = AST_NODE_id; $$.type = AST_NODE_id;
$$.u.node = NULL; $$.u.node = NULL;
}; }
/* Grupo de sentencias entre corchetes */
| DEL_BL_A SENTENCE_GROUP DEL_BL_C
{
$$ = $2;
}
;
/* Grupo de sentencias */
SENTENCE_GROUP
/* Bloque de sentencias */
: BLOCK
{
$$ = $1;
}
/* Grupo de sentencias y una sentencia (recursivo) */
| SENTENCE_GROUP SENTECE '\n'
{
$$.type = AST_NODE_id;
$$.U.node = newNode('s', NULL, $2.u.node);
}
;
/* Sentencias */ /* Sentencias */
SENTENCE SENTENCE
...@@ -117,24 +138,23 @@ SENTENCE ...@@ -117,24 +138,23 @@ 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))
} }
| WHILE EXPR DEL_BL_A BLOCK DEL_BL_C '\n' | WHILE '(' EXPR ')' BLOCK
{ {
$$.type = AST_NODE_id; $$.type = AST_NODE_id;
$$.u.node = newNode(WHILE, $2.u.node, $4.u.node); $$.u.node = newNode(WHILE, $3.u.node, $5.u.node);
} }
| IF EXPR DEL_BL_A BLOCK DEL_BL_C ELSE DEL_BL_A BLOCK DEL_BL_C '\n' | IF '(' EXPR ')' BLOCK ELSE BLOCK
{ {
$$.type = AST_NODE_id; $$.type = AST_NODE_id;
$$.u.node = newNode(IF, $2.u.node, $4.u.node); $$.u.node = newNode(IF, $3.u.node, $5.u.node);
$$.u.node = newNode(ELSE, $2.u.node, $8.u.node); $$.u.node = newNode(ELSE, $3.u.node, $7.u.node);
} }
| IF EXPR DEL_BL_A BLOCK DEL_BL_C '\n' | IF '(' EXPR ')' BLOCK
{ {
$$.type = AST_NODE_id; $$.type = AST_NODE_id;
$$.u.node = newNode(IF, $2.u.node, $4.u.node); $$.u.node = newNode(IF, $3.u.node, $5.u.node);
}; };
EXPR EXPR
: EXPR EQ EXPR : EXPR EQ EXPR
{ {
...@@ -235,29 +255,6 @@ EXPR ...@@ -235,29 +255,6 @@ EXPR
$$.u.node = newNode(LN, $3.u.node, NULL); $$.u.node = newNode(LN, $3.u.node, NULL);
} }
; ;
BLOCK
: SENTENCE '\n'
{
$$.type = AST_NODE_id;
$$.u.node = newRoot(';', NULL, $1.u.ast);
}
| '\n'
{
$$.type = AST_NODE_id;
$$.u.node = NULL;
}
| BODY SENTENCE '\n'
{
$$.type = AST_NODE_id;
$$.u.node = newRoot(';', $1.u.ast, $2.u.ast);
}
| BODY '\n'
{
$$ = $1;
}
;
%% %%
...@@ -306,8 +303,3 @@ int main(int argc, char *argv[]) ...@@ -306,8 +303,3 @@ int main(int argc, char *argv[])
return 0; return 0;
} }
...@@ -228,25 +228,21 @@ static void proc(ast_t *root) ...@@ -228,25 +228,21 @@ static void proc(ast_t *root)
break; break;
case IF: case IF:
if(expr(left(root))) if(expr(left(root)))
{ {
evaluate(right(root)); evaluate(right(root));
} }
break; break;
case ELSE:
case ELSE:
if(!(expr(left(root)))) if(!(expr(left(root))))
{ {
evaluate(right(root)); evaluate(right(root));
} }
break; break;
default: default:
fprintf(stderr, "%s(%d): error -- Etiqueta desconocida en expresión AST %u\n", programName, lnum(root), tag(root));
fprintf(stderr, "%s(%d): error - Etiqueta desconocida en expresión AST %u\n", programName, lnum(root), tag(root));
break; break;
} }
} }
......
...@@ -4,11 +4,8 @@ ...@@ -4,11 +4,8 @@
* Curso 2019-2020 * Curso 2019-2020
*/ */
enum symbolType { VOID, REAL }; // TODO: tipos en función de los lexemas.
typedef enum symbolType symbol_t;
typedef struct symbol_s { typedef struct symbol_s {
symbol_t type; int type;
union { union {
double real; double real;
} value; } value;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment