forked from AndersonFerreiraAlves/Tradutor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
b.y
432 lines (360 loc) · 9.14 KB
/
b.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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
%{
#include "classes.hpp"
#include <cstdio>
#include <map>
#include <iostream>
#include <math.h>
#include <string.h>
using namespace std;
extern "C" int yylex();
extern "C" int yyparse();
extern "C" FILE *yyin;
extern int numero_linhas;
void yyerror(const char *s);
%}
%token FUNCTION
%token RETURN
%token IF
%token ELSE
%token FOR
%token INT
%token MAIN
%token DOUBLE
%token PARENTESE_D
%token PARENTESE_E
%token CHAVE_D
%token CHAVE_E
%token IDENTIFICADOR
%token SOMA
%token SUBTRACAO
%token MULTIPLICACAO
%token DIVISAO
%token MAIOR
%token MENOR
%token EXCLAMACAO
%token E_COMECIAL
%token OU
%token NUMERO_INTEIRO
%token NUMERO_REAL
%token PONTO_VIRGULA
%token VIRGULA
%token IGUALDADE
%token DOIS_PONTOS
%token COLCHETE_E
%token COLCHETE_D
%token PRINT
%token VOID
%union {
int int_value;
double double_value;
char *id_value;
class VarDeclarations *varDeclarations;
class VarDeclaration *varDeclaration;
class NameList *nameList;
class Name *name;
class Names *names;
class NameID *nameID;
class NameAssignment *nameAssignment;
class Assignment *assignment;
class Commands *commands;
class Command *command;
class Print *print;
class Laco *laco;
class Inicializacoes *inicializacoes;
class Inicializacao *inicializacao;
class Incrementos *incrementos;
class Incremento *incremento;
class Condicional *condicional;
class Exp *exp;
class LValue *lValue;
class BinExpEqualDiff *binExpEqualDiff;
class BinExpLessGreater *binExpLessGreater;
class BinExpPlusMinus *binExpPlusMinus;
class BinExpAndOr *binExpAndOr;
class Factor *factor;
class UnExp *unExp;
class Value *value;
class BinExpPlus *binExpPlus;
class BinExpMinus *binExpMinus;
class BinExpLessThen *binExpLessThen;
class BinExpLessEqualThen *binExpLessEqualThen;
class BinExpGreaterThen *binExpGreaterThen;
class BinExpGreaterEqualThen *binExpGreaterEqualThen;
class BinExpEqual *binExpEqual;
class BinExpDiff *binExpDiff;
class BinExpAnd *binExpAnd;
class BinExpOr *binExpOr;
class FactorMul *factorMul;
class FactorDiv *factorDiv;
class UnExpPlus *unExpPlus;
class UnExpMinus *unExpMinus;
class LparExpRpar *lparExpRpar;
class Function *function;
class Functions *functions;
class Parametro *parametro;
class Parametros *parametros;
class ChamadaFuncao *c_funcao;
class ValorLiterals *vls;
class ValorLiteral *vl;
};
%type<int_value> Tipo;
%type<int_value> INT;
%type<int_value> DOUBLE;
%type<int_value> NUMERO_INTEIRO;
%type<double_value> NUMERO_REAL;
%type<id_value> IDENTIFICADOR;
%type<varDeclarations> VarDeclarations;
%type<varDeclaration> VarDeclaration;
%type<nameList> NameList;
%type<name> Name;
%type<names> Names;
%type<nameID> NameID;
%type<nameAssignment> NameAssignment;
%type<assignment> Assignment;
%type<commands> Comandos;
%type<command> Comando;
%type<print> Print;
%type<laco> Laco;
%type<inicializacoes> Inicializacoes;
%type<inicializacao> Inicializacao;
%type<incrementos> Incrementos;
%type<incremento> Incremento;
%type<condicional> Condicional;
%type<exp> Exp;
%type<lValue> LValue;
%type<binExpEqualDiff> BinExpEqualDiff;
%type<binExpLessGreater> BinExpLessGreater;
%type<binExpPlusMinus> BinExpPlusMinus;
%type<binExpAndOr> BinExpAndOr;
%type<factor> Factor;
%type<unExp> UnExp;
%type<value> Value;
%type<binExpPlus> BinExpPlus;
%type<binExpMinus> BinExpMinus;
%type<binExpLessThen> BinExpLessThen;
%type<binExpLessEqualThen> BinExpLessEqualThen;
%type<binExpGreaterThen> BinExpGreaterThen;
%type<binExpGreaterEqualThen> BinExpGreaterEqualThen;
%type<binExpEqual> BinExpEqual;
%type<binExpDiff> BinExpDiff;
%type<binExpAnd> BinExpAnd;
%type<binExpOr> BinExpOr;
%type<factorMul> FactorMul;
%type<factorDiv> FactorDiv;
%type<unExpPlus> UnExpPlus;
%type<unExpMinus> UnExpMinus;
%type<lparExpRpar> LparExpRpar;
%type<function> Funcao;
%type<functions> Funcoes;
%type<parametro> Parametro;
%type<parametros> Parametros;
%type <c_funcao> Chamada_Funcao;
%type <vls> ValorLiterals;
%type <vl> ValorLiteral;
%right IF ELSE
%error-verbose
%%
Funcoes: Funcoes Funcao {$$ = new Functions($1, $2);}
| Funcao {$$ = $1;}
;
Funcao: FUNCTION TIPO IDENTIFICADOR PARENTESE_E Parametros PARENTESE_D CHAVE_E VarDeclarations Comandos CHAVE_D {$$ = new Function($2,new IdValue(string($3)),$5,$8,$9);}
;
Parametros:
Parametros VIRGULA Parametro {$$ = new ParametroList($1,$3);}
| Parametro {$$ = $1}
;
Parametro:
TIPO IDENTIFICADOR {$$ = new Parametro($1,new IdValue(string($2)));}
;
Main:
VOID MAIN PARENTESE_E PARENTESE_D CHAVE_E VarDeclarations Comandos CHAVE_D {Programa *n = new Programa($6, $7);Contexto::getContexto().setPrograma(n);}
;
Tipo:
INT {$$ = $1;}
|DOUBLE {$$ = $1;}
;
VarDeclarations:
VarDeclarations VarDeclaration {$$ = new VarDeclarationList($1, $2);}
|VarDeclaration {$$ = $1;}
;
VarDeclaration:
Tipo NameList PONTO_VIRGULA {$$ = new VarDeclaration($1, $2);}
;
NameList:
Name {$$ = $1;}
|Names {$$ = $1;}
;
Names:
NameList VIRGULA Name {$$ = new Names($1, $3);}
;
Name:
NameID {$$ = $1;}
|NameAssignment {$$ = $1;}
;
NameID:
IDENTIFICADOR {$$ = new NameID (new IdValue($1));}
;
NameAssignment:
Assignment {$$ = new NameAssignment ($1);}
;
Comandos:
Comandos Comando {$$ = new CommandsList($1, $2);}
|Comando {$$ = $1;}
;
Comando:
Condicional {$$ = $1;}
|Laco {$$ = $1;}
|Print PONTO_VIRGULA {$$ = $1;}
|Exp PONTO_VIRGULA {$$ = $1;}
| Chamada_Funcao PONTO_VIRGULA {$$ = $1;}
;
Chamada_Funcao:
IDENTIFICADOR PARENTESE_E ValorLiterals PARENTESE_D {$$ = ChamadaFuncao(new IdValue(string($1)), $3); }
;
ValorLiterals:
ValorLiterals VIRGULA ValorLiteral {$$ = new ValorLiteralList($1,$3);}
| ValorLiteral {$$ = $1;}
;
ValorLiteral:
LValue {$$ = new ValorLiteral($1);}
;
Condicional:
IF PARENTESE_E LValue PARENTESE_D CHAVE_E Comandos CHAVE_D {$$ = new If($3, $6);}
|IF PARENTESE_E LValue PARENTESE_D CHAVE_E Comandos CHAVE_D ELSE CHAVE_E Comandos CHAVE_D{$$ = new IfElse($3, $6, $10);}
;
Laco:
FOR PARENTESE_E Inicializacoes PONTO_VIRGULA LValue PONTO_VIRGULA Incrementos PARENTESE_D CHAVE_E Comandos CHAVE_D{$$ = new Laco($3, $5, $7, $10);}
;
Print:
PRINT PARENTESE_E LValue PARENTESE_D{$$ = new Print($3);}
;
Inicializacoes:
Inicializacoes VIRGULA Inicializacao {$$ = new InicializacaoList($1, $3);}
|Inicializacao {$$ = $1;}
;
Inicializacao:
IDENTIFICADOR IGUALDADE LValue {$$ = new Inicializacao(new Assignment(new IdValue(string($1)),$3));}
;
Incrementos:
Incrementos VIRGULA Incremento {$$ = new IncrementoList($1, $3);}
|Incremento{$$ = $1;}
;
Incremento:
IDENTIFICADOR IGUALDADE LValue {$$ = new Incremento(new Assignment(new IdValue(string($1)),$3));}
;
Exp:
LValue {$$ = $1;}
|Assignment {$$ = $1;}
;
LValue:
BinExpEqualDiff {$$ = $1;}
|BinExpLessGreater {$$ = $1;}
|BinExpPlusMinus {$$ = $1;}
|BinExpAndOr {$$ = $1;}
|Factor {$$ = $1;}
;
BinExpEqualDiff:
BinExpEqual {$$ = $1;}
|BinExpDiff {$$ = $1;}
;
BinExpLessGreater:
BinExpLessThen {$$ = $1;}
|BinExpLessEqualThen {$$ = $1;}
|BinExpGreaterThen {$$ = $1;}
|BinExpGreaterEqualThen {$$ = $1;}
;
BinExpPlusMinus:
BinExpPlus {$$ = $1;}
|BinExpMinus {$$ = $1;}
;
BinExpAndOr:
BinExpAnd {$$ = $1;}
|BinExpOr {$$ = $1;}
;
BinExpPlus:
LValue SOMA Factor {$$ = new BinExpPlus($1,$3);}
;
BinExpMinus:
LValue SUBTRACAO Factor {$$ = new BinExpMinus($1, $3);}
;
BinExpLessThen:
LValue MENOR Factor {$$ = new BinExpLessThen($1, $3);}
;
BinExpLessEqualThen:
LValue MENOR IGUALDADE Factor {$$ = new BinExpLessEqualThen($1, $4);}
;
BinExpGreaterThen:
LValue MAIOR Factor {$$ = new BinExpGreaterThen($1, $3);}
;
BinExpGreaterEqualThen:
LValue MAIOR IGUALDADE Factor {$$ = new BinExpGreaterEqualThen($1, $4);}
;
BinExpEqual:
LValue IGUALDADE IGUALDADE Factor {$$ = new BinExpEqual($1, $4);}
;
BinExpDiff:
LValue EXCLAMACAO IGUALDADE Factor {$$ = new BinExpDiff($1, $4);}
;
BinExpAnd:
LValue E_COMECIAL E_COMECIAL Factor {$$ = new BinExpAnd($1, $4);}
;
BinExpOr:
LValue OU OU Factor {$$ = new BinExpOr($1, $4);}
;
Factor:
FactorMul {$$ = $1;}
|FactorDiv {$$ = $1;}
|UnExp {$$ = $1;}
;
FactorMul:
Factor MULTIPLICACAO UnExp {$$ = new FactorMul($1, $3);}
;
FactorDiv:
Factor DIVISAO UnExp {$$ = new FactorDiv($1, $3);}
;
UnExp:
UnExpPlus {$$ = $1;}
|UnExpMinus {$$ = $1;}
|LparExpRpar {$$ = $1;}
|Value {$$ = $1;}
;
UnExpPlus:
SOMA Value {$$ = new UnExpPlus($2);}
;
UnExpMinus:
SUBTRACAO Value {$$ = new UnExpMinus($2);}
;
Value:
NUMERO_INTEIRO {$$ = new IntValue($1);}
|NUMERO_REAL {$$ = new DoubleValue($1);}
|IDENTIFICADOR {$$ = new IdValue($1);}
;
LparExpRpar:
PARENTESE_E LValue PARENTESE_D {$$ = new LparExpRpar($2);}
;
Assignment:
IDENTIFICADOR IGUALDADE LValue {$$ = new Assignment(new IdValue(string($1)),$3);}
;
%%
int main(int, char**) {
yyin = fopen("entrada.txt", "r");
do {
yyparse();
} while (!feof(yyin));
Programa *prg = Contexto::getContexto().getPrograma();
prg->execute();
//map <string, Variavel> ola;
//Variavel::tipoVariavel c;
//c.a = 0;
//Variavel v;
//ola.insert(std::make_pair("moon", v));
//ola["ola"] = v;
//double d = 1 != 2;
//cout << "valor: " << d <<endl;
//cout << "valor: " << ola["ola"] << endl;
return 0;
}
void yyerror(const char *s) {
cout << "\nErro: " << s << "\nLinha do erro: " << numero_linhas + 1<< endl;
exit(-1);
}