-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser2.y
More file actions
266 lines (216 loc) · 5.36 KB
/
parser2.y
File metadata and controls
266 lines (216 loc) · 5.36 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
%{
/*#define YYSTYPE double*/
#include <math.h>
#include <stdio.h>
int yylex (void);
void yyerror (char const *);
#define YYERROR_VERBOSE 1
#define YYLTYPE YYLTYPE
typedef struct YYLTYPE {
int first_line;
int first_column;
int last_line;
int last_column;
char *filename;
} YYLTYPE;
%}
%locations
/* Bison declarations. */
%token PLUS
%token MINUS
%token TIMES
%token SLASH
%token LPAREN
%token RPAREN
%token SEMICOLON
%token COMMA
%token PERIOD
%token BECOMES
%token LBRACKET
%token RBRACKET
%token LCORCHET
%token RCORCHET
%token ENTONCES
%token O
%token NO
%token SI
%token MIENTRAS
%token HACER
%token PARA
%token TAL
%token QUE
%token CON
%token CAMBIO
%token LEER
%token IMPRIMIR
%token RECIBE
%token Y
%token RETORNA
%token SEA
%token ESTRUCTURA
%token CONTIENE
%token UNION
%token VACIO
%token GUACARA
%token ENTERO
%token CARACTER
%token FLOTANTE
%token BOOLEANO
%token STRING
%token RETORNAR
%token VALOR
%token VERDADERO
%token FALSO
%token ES
%token A
%token MAYOR
%token MENOR
%token IGUAL
%token DISTINTO
%token DE
%token TIPO
%token NUMENTERO
%token NUMFLOTANTE
%token FUNCIONES
%token VARIABLES
%token GLOBALES
%token FUNCION
%token ARREGLO
%token ID
%token LA
%token POR
%token REFERENCIA
%token CONSTCARACTER
%left O
%left Y
%left EQ NE
%nonassoc LT GT LE GE
%left PLUS MINUS
%left TIMES SLASH
%left NEG
%start inicio
%% /* The grammar follows. */
inicio : varglobal
varglobal : funciones
| VARIABLES GLOBALES LBRACKET listaVariables RBRACKET funciones
;
listaVariables : decVariable
| decVariable SEMICOLON listaVariables
;
decVariable : SEA ID DE TIPO tipo
| SEA ID DE TIPO tipo CON VALOR expr
;
tipo : BOOLEANO
| ENTERO
| FLOTANTE
| CARACTER
| STRING
| VACIO
| ARREGLO DE tipo DE expr A expr
| LPAREN tipo RPAREN
;
funciones : programa
| FUNCIONES LBRACKET listaFunciones RBRACKET programa
;
listaFunciones : decFuncion
| decFuncion listaFunciones
;
decFuncion : SEA LA FUNCION ID QUE RECIBE listArg Y RETORNA tipo HACER bloque
| SEA LA FUNCION ID QUE RETORNA tipo HACER bloque
| SEA LA FUNCION ID QUE RECIBE listArg HACER bloque
;
listArg : tipo ID
| tipo POR REFERENCIA ID
| tipo POR REFERENCIA ID COMMA listArg
| tipo ID COMMA listArg
;
programa : GUACARA bloque
;
bloque : LBRACKET variables RBRACKET
;
variables : listaInstruccion
| VARIABLES LBRACKET listaVariables RBRACKET listaInstruccion
;
listaInstruccion : instruccion
| instruccion SEMICOLON listaInstruccion
;
elseif : else
| O SI expr ENTONCES bloque
| O SI expr ENTONCES bloque elseif
;
else : SI NO ENTONCES bloque
;
asignacion : ID BECOMES expr { printf("%d\n", $3); }
;
instruccion : LEER ID
| IMPRIMIR expr
| SI expr ENTONCES bloque
| SI expr ENTONCES bloque elseif
| asignacion
| MIENTRAS expr HACER bloque
| PARA asignacion TAL QUE expr CON CAMBIO asignacion HACER bloque
;
expr : LPAREN expr RPAREN { $$ = $2; }
| exprBooleana { $$ = $1; }
| exprAritmetica { $$ = $1; }
| terminal { $$ = $1; }
;
exprBooleana : expr MAYOR QUE expr %prec LT { $$ = $1 > $4; }
| expr MENOR QUE expr %prec GT { $$ = $1 < $4; }
| expr MAYOR O IGUAL QUE expr %prec GE { $$ = ($1 >= $4); }
| expr MENOR O IGUAL QUE expr %prec LE { $$ = ($1 <= $4); }
| expr IGUAL A expr %prec EQ { $$ = ($1 == $4); }
| expr DISTINTO A expr %prec NE { $$ = ($1 != $4); }
| expr Y expr { $$ = ($1 && $3); }
| expr O expr { $$ = ($1 || $3); }
;
exprAritmetica : expr PLUS expr { $$ = $1 + $3; }
| expr MINUS expr { $$ = $1 - $3; }
| expr TIMES expr { $$ = $1 * $3; }
| expr SLASH expr { $$ = $1 / $3; }
;
terminal : VERDADERO { $$ = 1; }
| FALSO { $$ = 0; }
| NUMENTERO { $$ = $1; }
| NUMFLOTANTE { $$ = $1; }
| CONSTCARACTER { $$ = $1; }
| ID { $$ = $1; }
| STRING { $$ = $1; } /* Hay q revisar esto */
;
/*
expr : LPAREN expr RPAREN
| exprBooleana
| exprAritmetica
| terminal
;
exprBooleana : expr MAYOR QUE expr %prec LT
| expr MENOR QUE expr %prec GT
| expr MAYOR O IGUAL QUE expr %prec GE
| expr MENOR O IGUAL QUE expr %prec LE
| expr IGUAL A expr %prec EQ
| expr DISTINTO A expr %prec NE
| expr Y expr
| expr O expr
;
exprAritmetica : expr PLUS expr
| expr MINUS expr
| expr TIMES expr
| expr SLASH expr
;
terminal : VERDADERO
| FALSO
| NUMENTERO
| NUMFLOTANTE
| CARACTER
| ID
;
*/
%%
void yyerror (char const *s)
{
fprintf (stderr, "Linea %d, Columna %d: %s\n", yylloc.first_line, yylloc.first_column, s);
}
void main (int argc,char **argv)
{
yyparse ();
}