-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsintatico.y
536 lines (466 loc) · 12.5 KB
/
sintatico.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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
%{
#include "global.h"
#include "symbolTable.h"
#include "auxFunctions.h"
#include "validations.h"
#include <stdlib.h> /* For malloc in symbol table */
#include <string.h> /* For strcmp in symbol table */
#include <stdio.h> /* For error messages */
#define YYDEBUG 1 /* For debugging */
/* interface to the lexer */
extern int yylineno;
extern int yyrestart();
int debugValue = 1;
// to use debugValues:
// debugValue = printDebugText(debugValue);
// {debugValue = printDebugText(debugValue);}
%}
%union { /* SEMANTIC RECORD */
char * strval;
type_values * all;
}
%token <all> T_INT_NUMBER;
%token <all> T_DOUBLE_NUMBER;
%token T_ATTRIBUTION;
%token T_SEMICOLON;
%token T_COLON;
%token T_QUOTATION_MARKS;
%token T_APOSTROPHE;
// structures
%token T_USES_STATEMENT;
%token T_IF_STATEMENT;
%token T_IF_THEN_STATEMENT;
%token T_ELSE_STATEMENT;
%token T_WHILE_STATEMENT;
%token T_DO_STATEMENT;
%token T_VAR_STATEMENT
%token T_CONST_STATEMENT
%token T_FOR_STATEMENT
%token T_TO_STATEMENT
%token T_DOWNTO_STATEMENT
%token T_REPEAT_STATEMENT
%token T_UNTIL_STATEMENT
// variable types
%token <strval> T_TYPE_CHAR
%token <strval> T_TYPE_STRING
%token <strval> T_TYPE_BOOLEAN
%token <strval> T_TYPE_SHORTINT
%token <strval> T_TYPE_SMALLINT
%token <strval> T_TYPE_LONGINT
%token <strval> T_TYPE_INT64
%token <strval> T_TYPE_INTEGER
%token <strval> T_TYPE_BYTE
%token <strval> T_TYPE_WORD
%token <strval> T_TYPE_LONGWORD
%token <strval> T_TYPE_QWORD
%token <strval> T_TYPE_CARDINAL
%token <strval> T_TYPE_REAL
%token <strval> T_TYPE_SINGLE
%token <strval> T_TYPE_DOUBLE
%token <strval> T_TYPE_EXTENDED
%token <strval> T_TYPE_COMP
%token <strval> T_TYPE_CURRENCY
%token <all> T_SOME_TEXT
%token <all> T_SOME_WORD
%token T_SOME_DIGIT
%token T_END_PROGRAM
%token T_PROGRAM
%token T_BEGIN_STATEMENT
%token T_END_STATEMENT
%token T_OR_STATEMENT
%token T_AND_STATEMENT
%token T_EQUAL
%token T_DIFFERENT
%token T_BIGGER
%token T_BIGGER_OR_EQUAL
%token T_MINOR
%token T_MINOR_OR_EQUAL
%token T_OPERATOR_PLUS
%token T_OPERATOR_MINUS
%token T_OPERATOR_MOD
%token T_OPERADOR_DIV
%token T_SLASH
%token T_ASTERISK
%token <all> T_SLASH_COMMENT
%token <all> T_BRACE_COMMENT
%token T_WRITE
%token T_WRITELN
%token T_READ
%token T_READLN
%token T_LEFT_PARENTHESIS
%token T_RIGHT_PARENTHESIS
%token T_CLRSCR
%type <strval> Type_Of_Variable
%type <all> Some_String
%type <all> Number
%type <all> Calc_Statments
%start Program_Begin
%left T_OPERATOR_PLUS T_OPERATOR_MINUS
%left T_SLASH T_ASTERISK
%%
Variable:
T_SOME_WORD
;
Some_String:
T_SOME_TEXT
| T_APOSTROPHE T_SOME_WORD T_APOSTROPHE {
$$ = $<all>2;
}
;
Number:
T_INT_NUMBER
| T_DOUBLE_NUMBER
;
Expression:
Variable
| Some_String
| Calc_Statments
;
Commands:
Command
| Commands Command
;
Command:
Attribuition
| If_Statement
| While_Statement
| For_Statement
| Write_Statements
| Read_Statement
| Repeat_Until_Statement
| Comment_Statement
| Clrscr_Statement
;
Program_Begin:
T_PROGRAM Variable T_SEMICOLON {
printIncludesOfProgram();
} Program_Begin_Complementation T_BEGIN_STATEMENT {
printBeginOfProgram();
incrementScope();
} Commands T_END_PROGRAM {
printEndOfProgram();
decrementScope();
}
;
Program_Begin_Complementation:
/* Nothing */
| Uses_Statement
| Declaration_Of_Variables
| Uses_Statement Declaration_Of_Variables
;
Uses_Statement:
T_USES_STATEMENT Variable T_SEMICOLON
;
Declaration_Of_Variables:
Declaration_Of_Variable
| Declaration_Of_Variables Declaration_Of_Variable
;
Declaration_Of_Variable:
T_VAR_STATEMENT Variable T_COLON Type_Of_Variable T_SEMICOLON {
if (validateDeclaration($4, $<all>2, yylineno, curfilename)) {
printDeclaration($4, $<all>2);
}
}
| Variable T_COLON Type_Of_Variable T_SEMICOLON {
if (validateDeclaration($3, $<all>1, yylineno, curfilename)) {
printDeclaration($3, $<all>1);
}
}
;
Attribuition:
Variable T_ATTRIBUTION Expression T_SEMICOLON {
if (validateAtribuition($<all>1->value, $<all>3, yylineno, curfilename)) {
printAtribuition($<all>1->value, $<all>3);
}
}
;
For_Statement:
T_FOR_STATEMENT {
printForDeclaration("begin");
} For_Complementation {
printForDeclaration("end");
} Statement_Complementation
;
For_Complementation:
For_Attribuition T_TO_STATEMENT Expression {
printForScope("<", $<all>3);
incrementScope();
} T_DO_STATEMENT
| For_Attribuition T_DOWNTO_STATEMENT Expression {
printForScope(">", $<all>3);
incrementScope();
} T_DO_STATEMENT
;
For_Attribuition:
Variable T_ATTRIBUTION Expression {
setVariableFor($<all>1->value);
printForAtribuition($<all>3);
}
;
If_Statement:
T_IF_STATEMENT {
printIfDeclaration("begin");
} Multiple_Conditions T_IF_THEN_STATEMENT {
printIfDeclaration("end");
incrementScope();
} If_Statement_Complementation
;
Else_Statement:
T_ELSE_STATEMENT {
printElseDeclaration("begin");
incrementScope();
} Statement_Complementation
;
If_Statement_Complementation:
Statement_Complementation
| T_BEGIN_STATEMENT Commands T_END_STATEMENT {
decrementScope();
printEndStatements();
} Else_Statement
;
While_Statement:
T_WHILE_STATEMENT {
printWhileDeclaration("begin");
} Multiple_Conditions T_DO_STATEMENT {
printWhileDeclaration("end");
incrementScope();
} Statement_Complementation
;
Repeat_Until_Statement:
T_REPEAT_STATEMENT {
printRepeatDeclaration("begin");
incrementScope();
} Commands T_UNTIL_STATEMENT {
decrementScope();
printRepeatDeclaration("before_end");
} Multiple_Conditions T_SEMICOLON {
printRepeatDeclaration("after_end");
}
;
Statement_Complementation:
T_BEGIN_STATEMENT Commands T_END_STATEMENT T_SEMICOLON {
decrementScope();
printEndStatements();
}
| Command {
decrementScope();
printEndStatements();
}
;
Write_Statements:
T_WRITE {
printWriteDeclaration("begin");
} Write_Statement_Complementation {
printWriteDeclaration("end");
}
| T_WRITELN {
printWriteDeclaration("begin");
} Write_Statement_Complementation {
printWriteDeclaration("endln");
}
;
Write_Statement_Complementation:
T_LEFT_PARENTHESIS Expression T_RIGHT_PARENTHESIS T_SEMICOLON {
printWriteDeclarationValues($<all>2);
}
;
Read_Statement:
T_READ Read_Statement_Complement
| T_READLN Read_Statement_Complement
;
Read_Statement_Complement:
T_LEFT_PARENTHESIS Variable T_RIGHT_PARENTHESIS T_SEMICOLON {
printReadDeclaration($<all>3->value);
}
;
Comment_Statement:
T_SLASH_COMMENT {
printComment("slash", $<all>1->value);
}
| T_BRACE_COMMENT {
printComment("brace_asterisk", $<all>1->value);
}
;
Calc_Statments:
Number
| Number T_OPERATOR_PLUS Calc_Statments {
type_values * returnCalc = validationCalculator($<all>1, $<all>3, "+", yylineno, curfilename);
$$ = returnCalc;
}
| Number T_OPERATOR_MINUS Calc_Statments {
type_values * returnCalc = validationCalculator($<all>1, $<all>3, "-", yylineno, curfilename);
$$ = returnCalc;
}
| Number T_SLASH Calc_Statments {
type_values * returnCalc = validationCalculator($<all>1, $<all>3, "/", yylineno, curfilename);
$$ = returnCalc;
}
| Number T_ASTERISK Calc_Statments {
type_values * returnCalc = validationCalculator($<all>1, $<all>3, "*", yylineno, curfilename);
$$ = returnCalc;
}
| Number T_OPERATOR_MOD Calc_Statments {
type_values * returnCalc = validationCalculator($<all>1, $<all>3, "mod", yylineno, curfilename);
$$ = returnCalc;
}
| Number T_OPERADOR_DIV Calc_Statments {
type_values * returnCalc = validationCalculator($<all>1, $<all>3, "div", yylineno, curfilename);
$$ = returnCalc;
}
;
Clrscr_Statement:
T_CLRSCR T_SEMICOLON {
printClrscrStatements();
}
;
Multiple_Conditions:
Conditions
| Conditions T_AND_STATEMENT {
printAndOrCondition("and");
} Conditions
| Conditions T_OR_STATEMENT {
printAndOrCondition("or");
} Conditions
;
Conditions:
Expression {
printConditionValue($<all>1);
}
| Expression T_EQUAL Expression {
printCondition($<all>1, $<all>3, "==");
}
| Expression T_DIFFERENT Expression {
printCondition($<all>1, $<all>3, "!=");
}
| Expression T_BIGGER Expression {
printCondition($<all>1, $<all>3, ">");
}
| Expression T_BIGGER_OR_EQUAL Expression {
printCondition($<all>1, $<all>3, ">=");
}
| Expression T_MINOR Expression {
printCondition($<all>1, $<all>3, "<");
}
| Expression T_MINOR_OR_EQUAL Expression {
printCondition($<all>1, $<all>3, "<=");
}
;
Type_Of_Variable:
T_TYPE_SHORTINT {
$$ = mallocNewString("signed char");
}
| T_TYPE_SMALLINT {
$$ = mallocNewString("short int");
}
| T_TYPE_LONGINT {
$$ = mallocNewString("int");
}
| T_TYPE_INT64 {
$$ = mallocNewString("long long");
}
| T_TYPE_BYTE {
$$ = mallocNewString("unsigned char");
}
| T_TYPE_WORD {
$$ = mallocNewString("unsigned short int");
}
| T_TYPE_LONGWORD {
$$ = mallocNewString("unsigned int");
}
| T_TYPE_QWORD {
$$ = mallocNewString("unsigned long long");
}
| T_TYPE_INTEGER {
$$ = mallocNewString("int");
}
| T_TYPE_CARDINAL {
$$ = mallocNewString("unsigned int");
}
| T_TYPE_CURRENCY
| T_TYPE_COMP
| T_TYPE_REAL
| T_TYPE_DOUBLE {
$$ = mallocNewString("double");
}
| T_TYPE_EXTENDED {
$$ = mallocNewString("long double");
}
| T_TYPE_SINGLE {
$$ = mallocNewString("float");
}
| T_TYPE_STRING {
$$ = mallocNewString("string");
}
| T_TYPE_CHAR {
$$ = mallocNewString("char");
}
| T_TYPE_BOOLEAN {
$$ = mallocNewString("bool");
}
;
%%
int main(int argc, char ** argv){
int i, k;
size_of_table = 0;
if (argc < 2) {
curfilename = "(stdin)";
yylineno = 1;
scope = 0;
// Start the analisis lexical
fileOut = fopen("out.c", "w");
printf("Esperando a entrada do código a ser compilado...\n");
yyparse();
fclose(fileOut);
printf("Leitura concluída e resultado compilado em %s.c\n", fileName);
} else {
for (i = 1; i < argc; i++) {
errors = 0;
FILE * f = fopen(argv[i], "r");
// Verified if the file is openned
if (!f) {
perror(argv[1]);
return (1);
} else {
curfilename = argv[i];
scope = 0;
fileName = malloc(sizeof(strlen(curfilename)));
for (k = 0; k < (int)strlen(curfilename)-4; k++) {
if (curfilename[k] != '.') {
fileName[k] = curfilename[k];
}
else break;
}
// Start the analisis lexical
yyrestart(f);
yylineno = 1;
printf("Iniciando leitura do arquivo %s...\n", curfilename);
printf("------------------------------------------\n");
char outfilename [10];
sprintf(outfilename, "%s.cpp", fileName);
fileOut = fopen(outfilename, "w");
yyparse();
fclose(fileOut);
printf("------------------------------------------\n");
if (errors != 0) {
remove(outfilename);
printf("%sFALHA:%s ", COLOR_RED, COLOR_RESET);
printf("Arquivo %s não compilado devido aos problemas.\n", curfilename);
} else {
printf("%sCONCLUIDO:%s ", COLOR_GRN, COLOR_RESET);
printf("Arquivo compilado e resultado em %s\n", outfilename);
}
printf("\n\n");
fclose(f);
}
}
}
// printf("\n\n----------------------\n");
// printf("Print of symbol table! \n\n");
// printSymbolTable();
// printf("----------------------\n");
return 1;
}
void yyerror(const char* errmsg) {
printf("\n*** Erro: %s na linha %d\n", errmsg, yylineno);
errors = errors + 1;
}