-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc.y
94 lines (80 loc) · 3.02 KB
/
calc.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
%{ /* Codigo C */
#include <stdio.h>
#include <math.h>
#include "diccionario.h"
DICCIONARIO diccionario; /* variable global para el diccionario */
%}
/* Declaraciones de BISON */
%union {
int valor_entero;
double valor_real;
char * texto;
}
%token <valor_real> CONSTANTE_REAL
%token <valor_entero> CONSTANTE_ENTERA
%token <texto> IDENTIFICADOR
%token <texto> SENO
%token <texto> COSENO
%token <texto> TANGENTE
%token <texto> MENORQUE
%token <texto> MAYORQUE
%token <texto> MENORIGUAL
%token <texto> MAYORIGUAL
%token <texto> O
%token <texto> Y
%token <texto> DIFERENTE
%token <texto> IGUAL
%token <texto> IF
%token <texto> THEN
%token <texto> ELSE
%left DIFERENTE MAYORQUE MENORQUE MAYORIGUAL MENORIGUAL Y O
%left '-' '+'
%left '*' '/'
%right '^'
%type <valor_real> expresion
%type <valor_entero> condicion
%%
/* Gramatica */
lineas: /* cadena vacia */
| lineas linea
;
linea: '\n'
| IDENTIFICADOR '=' expresion '\n' { insertar_diccionario(&diccionario, $1, $3); }
| expresion '\n' { printf ("resultado: %f\n", $1); }
| error '\n' { yyerrok;}
;
expresion: CONSTANTE_REAL { $$ = $1; }
| CONSTANTE_ENTERA { $$ = $1; }
| IDENTIFICADOR { ENTRADA * entrada = buscar_diccionario(&diccionario,$1);
if (entrada != NULL) { $$ = entrada->valor; }
else { printf("ERROR: variable %s no definida\n", $1); $$ = 0; } }
| '(' expresion ')' { $$ = $2; }
| expresion '+' expresion { $$ = $1 + $3; }
| expresion '-' expresion { $$ = $1 - $3; }
| expresion '*' expresion { $$ = $1 * $3; }
| expresion '/' expresion { $$ = $1 / $3; }
| expresion '^' expresion { $$ = pow($1, $3); }
| expresion MENORQUE expresion { if ($1 < $3) { $$ = 1.0; } else { $$ = 0.0; } }
| expresion MAYORQUE expresion { if ($1 > $3) { $$ = 1.0; } else { $$ = 0.0; } }
| expresion MAYORIGUAL expresion { if ($1 >= $3) { $$ = 1.0; } else { $$ = 0.0; } }
| expresion MENORIGUAL expresion { if ($1 <= $3) { $$ = 1.0; } else { $$ = 0.0; } }
| expresion DIFERENTE expresion { if ($1 != $3) { $$ = 1.0; } else { $$ = 0.0; } }
| expresion IGUAL expresion { if ($1 == $3) { $$ = 1.0; } else { $$ = 0.0; } }
| SENO '(' expresion ')' { $$ = sin($3); }
| COSENO '(' expresion ')' { $$ = cos($3); }
| TANGENTE '(' expresion ')' { $$ = tan($3); }
| '-' expresion { $$ = -$2; }
| condicion THEN expresion ELSE expresion { if($1) { $$ = $3; } else { $$ = $5; } }
;
condicion: expresion { if ($1) { $$ = 1.0; } else { $$ = 0.0; } }
| condicion Y condicion { if ($1 && $3) { $$ = 1.0; } else { $$ = 0.0; } }
| condicion O condicion { if ($1 || $3) { $$ = 1.0; } else { $$ = 0.0; } }
;
%%
int main(int argc, char** argv) {
inicializar_diccionario(&diccionario);
yyparse();
liberar_diccionario(&diccionario);
}
yyerror (char *s) { printf ("%s\n", s); }
int yywrap() { return 1; }