-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyntactic.y
464 lines (400 loc) · 9.54 KB
/
syntactic.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
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "functions/symbol_table.c"
#include "print_list.h"
#define ELEMENT_SIZE 100
enum {REAL, INT} type_declaration;
int recur_count = 0;
int line_number=1;
char for_counter[20];
char for_expression[100] = "\0";
%}
%token EOL
%token NUMBER
%token POWER
%token PLUS MINUS TIMES DIVIDE
%token EQUAL
%token OPEN_PARENS CLOSE_PARENS
%token OPEN_BRACKET CLOSE_BRACKET
%token COMMENT
%token VAR_DEF_SEPARATOR
%token COMMA
%token SEMICOLON
%token IMPLICIT NONE PARAMETER
%token READ_COMMAND WRITE_COMMAND PRINT_COMMAND
%token PROGRAM_KEYWORD END_KEYWORD
%token INTEGER_KEYWORD REAL_KEYWORD
%token INT_NUM REAL_NUM
%token IDENTIFIER STRING
%token IF_KEYWORD ELSE_KEYWORD
%token EQUAL_KEYWORD
%token END_IF_KEYWORD
%token THEN_KEYWORD
%token BGE_KEYWORD BGT_KEYWORD BLE_KEYWORD BLT_KEYWORD BNE_KEYWORD BEQ_KEYWORD
%token AND_KEYWORD OR_KEYWORD
%token DO_KEYWORD
%token TRUE_KEYWORD
%token FALSE_KEYWORD
%token QUOTE
%token FMT_DGT FMT_TXT FMT_PT FMT_COMMA FMT_ANY FMT_BEG FMT_END
%left PLUS MINUS
%left TIMES DIVIDE
%left NEG
%right POWER
%%
CommandList:
/* Empty */
| CommandList Command SEMICOLON { line_number++; }
| CommandList Command EOL { line_number++; }
| CommandList Command COMMENT EOL {
line_number++;
printf("//%s\n", $3);
}
;
Command:
/* Empty */
| COMMENT {
printf("//%s\n", $1);
line_number++;
}
| BeginProg
| EndProg
| PrintText
| ReadStmt
| Declaration
| Assignment
| Conditional
| Repetition
;
Conditional:
IfStmt ConditionScope END_KEYWORD IF_KEYWORD
| IfStmt ElseStmt END_KEYWORD IF_KEYWORD
| IfStmt ElseIfStmt ElseStmt END_KEYWORD IF_KEYWORD
| IfStmt ElseIfStmt END_KEYWORD IF_KEYWORD
IfStmt:
IF_KEYWORD OPEN_PARENS {printf("if(");} ConditionStmt CLOSE_PARENS THEN_KEYWORD {
printf("){\n");
} ConditionScope {printf("}\n");}
ElseIfStmt:
ElseIfFormat
| ElseIfFormat ElseIfStmtRecur
;
ElseIfStmtRecur: ElseStmt
| ElseIfFormat ElseIfStmtRecur
;
ElseIfFormat:
ELSE_KEYWORD IF_KEYWORD OPEN_PARENS {printf("else if(");} ConditionStmt
CLOSE_PARENS THEN_KEYWORD {printf("){\n");} ConditionScope {printf("}\n");}
;
ElseStmt:
ELSE_KEYWORD {printf("else{\n");} ConditionScope {printf("}\n");}
;
ConditionStmt:
TRUE_KEYWORD {
$$ = strdup("1");
printf("1");
}
| FALSE_KEYWORD {
$$ = strdup("0");
printf("0\n");
}
| Expression Possible_Conditions Expression {
}
| Expression Possible_Conditions Expression AND_KEYWORD {printf(" && ");} ConditionStmt
| Expression Possible_Conditions Expression OR_KEYWORD {printf(" || ");} ConditionStmt
;
Possible_Conditions:
EQUAL_KEYWORD {printf(" == ");}
| BNE_KEYWORD {printf(" != ");}
| BGT_KEYWORD {printf(" > ");}
| BLT_KEYWORD {printf(" < ");}
| BGE_KEYWORD {printf(" >= ");}
| BLE_KEYWORD {printf(" <= ");}
;
ConditionScope:
ConditionScope EOL MultipleScope
| MultipleScope
;
MultipleScope:
| EOL COMMENT {printf("//%s\n", $2);} MultipleScope
| EOL MultipleScope
| EOL Assignment MultipleScope
| EOL Conditional MultipleScope
| EOL PrintText MultipleScope
| EOL ReadStmt MultipleScope
| EOL Repetition MultipleScope
;
Repetition:
RepetitionFormat ConditionScope END_KEYWORD DO_KEYWORD {printf("}\n");}
;
RepetitionFormat:
RepetitionPreamble {
printf(";%s <= %s; %s++){\n", for_counter, for_expression, for_counter);
for_expression[0] = '\0';
}
| RepetitionPreamble {
printf(";%s <= %s;", for_counter, for_expression);
*for_expression = 0;
} COMMA RepetitionExpression {
printf(" %s += %s ){\n", for_counter, for_expression);
for_expression[0] = '\0';
}
;
RepetitionPreamble:
FirstPartRepetitionFormat INT_NUM {printf("%s", $2);} COMMA RepetitionExpression
| FirstPartRepetitionFormat IDENTIFIER {printf("%s", $2);} COMMA RepetitionExpression
;
FirstPartRepetitionFormat:
DO_KEYWORD IDENTIFIER {
strcpy(for_counter, $2);
} EQUAL {
printf("for(%s = ", for_counter);
}
;
RepetitionExpression:
INT_NUM {
$$=$1;
strcat(for_expression, $1);
//printf("%s", $1);
}
| REAL_NUM{
$$=$1;
//printf("%s", $1);
}
| IDENTIFIER{
$$=$1;
strcat(for_expression, $1);
//printf("%s", $1);
}
| OPEN_PARENS {printf("(");} Expression CLOSE_PARENS {printf(")");}
| RepetitionExpression PLUS {strcat(for_expression, "+");} RepetitionExpression
| RepetitionExpression MINUS {strcat(for_expression, "-");} RepetitionExpression
| RepetitionExpression TIMES {strcat(for_expression, "*");} RepetitionExpression
| RepetitionExpression DIVIDE {strcat(for_expression, "/");} RepetitionExpression
| MINUS {printf(" - ");} RepetitionExpression %prec NEG
;
Declaration:
INTEGER_KEYWORD VAR_DEF_SEPARATOR IDENTIFIER {
char tmp[128];
sprintf(tmp, "%s", $3);
printf("int %s;\n", tmp);
add_var(&vars, "d", tmp);
type_declaration = INT;
}
| REAL_KEYWORD VAR_DEF_SEPARATOR IDENTIFIER {
char tmp[128];
sprintf(tmp, "%s", $3);
printf("double %s;\n", tmp);
add_var(&vars, "lf", tmp);
type_declaration = REAL;
}
| INTEGER_KEYWORD IDENTIFIER {
char tmp[128];
sprintf(tmp, "%s", $2);
printf("int %s;\n", tmp);
add_var(&vars, "d", tmp);
type_declaration = INT;
}
| REAL_KEYWORD IDENTIFIER {
char tmp[128];
sprintf(tmp, "%s", $2);
printf("double %s;\n", tmp);
add_var(&vars, "lf", tmp);
type_declaration = REAL;
}
| Declaration COMMA IDENTIFIER {
char tmp[128];
switch(type_declaration){
case INT:
sprintf(tmp, "%s", $3);
printf("int %s;\n", tmp);
add_var(&vars, "d", tmp);
break;
case REAL:
sprintf(tmp, "%s", $3);
printf("double %s;\n", tmp);
add_var(&vars, "lf", tmp);
type_declaration = REAL;
break;
default:
yyerror("Syntax error: erroneous variable declaration.\n");
}
}
;
Expression:
numbers_type {
$$=$1;
}
| IDENTIFIER {
$$=$1;
printf(" %s", $1);
}
| OPEN_PARENS {printf("(");} Expression CLOSE_PARENS {printf(")");}
| Expression PLUS {printf(" + ");} Expression
| Expression MINUS {printf(" - ");} Expression
| Expression TIMES {printf(" * ");} Expression
| Expression DIVIDE {printf(" / ");} Expression
| MINUS {printf(" - ");} Expression %prec NEG
;
Assignment:
| ExpressionAssign {
char tmp[512];
strcpy(tmp, $1);
tmp[strlen(tmp)-1] = '\0';
printf("\n");
}
| ExpressionAssign COMMENT{
char tmp[512] = " ";
strcat(tmp, " //");
strcat(tmp, $2);
tmp[strlen(tmp)] = '\0';
printf("%s\n", tmp);
}
;
ExpressionAssign:
IDENTIFIER EQUAL {printf("%s", $1);} Expression {printf(";");}
;
PrintText:
{ clear_all(); } PrintStmt { print_all(); }
| { clear_all(); } WriteStmt { print_all(); }
;
PrintStmt:
PRINT_COMMAND FormatPrint PrintPossibilities
| PrintStmt COMMA PrintPossibilities
;
FormatPrint:
TIMES COMMA
;
WriteStmt:
WRITE_COMMAND FormatWrite PrintPossibilities
| WriteStmt COMMA PrintPossibilities
;
FormatWrite:
FMT_ANY { default_format(); }
| FMT_BEG { custom_format(); } Format FMT_END
;
Format:
FMT_TXT { append_fmt($1); }
| INT_NUM { push_multiplier(atoi($1)); } OPEN_PARENS Format CLOSE_PARENS { pop_multiplier(); }
| Format COMMA Format
;
PrintPossibilities:
STRING {
char temp[128];
sprintf(temp, "\"%s\"", $1);
append_content(temp);
if (is_fmt_any())
append_fmt("a");
}
| REAL_NUM {
append_content($1);
if (is_fmt_any())
append_fmt("f");
}
| INT_NUM {
append_content($1);
if (is_fmt_any())
append_fmt("i");
}
| IDENTIFIER {
char type[4];
char var_name[128];
strcpy(var_name, $1);
if (get_var_type(&vars, var_name, type) < 0) {
fprintf(stderr, "Syntax error: couldn't find variable %s.\n", var_name);
exit(EXIT_FAILURE);
}
else {
if (!strcmp(type, "d")) strcpy(type, "i");
else if (!strcmp(type, "lf")) strcpy(type, "f");
else yyerror("Syntax error: unrecognized variable type.\n");
if (is_fmt_any())
append_fmt(type);
append_content(var_name);
}
}
;
ReadStmt:
READ_COMMAND FormatRead IDENTIFIER {
char type[4];
char var_name[128];
strcpy(var_name, $3);
if (get_var_type(&vars, var_name, type) < 0) {
//check_variable(var_name);
}
else
printf("scanf(\"%%%s\", &%s);\n", type, var_name);
}
| ReadStmt COMMA IDENTIFIER {
char type[4];
char var_name[128];
strcpy(var_name, $3);
if (get_var_type(&vars, var_name, type) < 0) {
//check_variable(var_name);
//fprintf(stderr, "Syntax error: couldn't find variable %s.\n", var_name);
exit(EXIT_FAILURE);
}
else
printf("scanf(\"%%%s\", &%s);\n", type, var_name);
}
;
numbers_type:
INT_NUM {
$$ = $1;
printf("%s", $1);
}
| REAL_NUM {
$$ = $1;
printf("%s", $1);
}
;
NumbersAssign:
IDENTIFIER EQUAL numbers_type
;
ReadStmt:
READ_COMMAND FormatRead VarList
;
FormatRead:
OPEN_PARENS TIMES COMMA TIMES CLOSE_PARENS
| TIMES COMMA
;
VarList:
IDENTIFIER {}
| VarList COMMA IDENTIFIER
;
BeginProg:
PROGRAM_KEYWORD IDENTIFIER EOL IMPLICIT NONE {printf("int main(void) {\n");}
;
EndProg:
END_KEYWORD PROGRAM_KEYWORD IDENTIFIER {printf("\nreturn 0;\n}\n");}
;
%%
int yyerror(char *error_message, char *variable){
printf("Error in line %d\n", line_number);
printf("%s %s\n\n", error_message, variable);
//printf("%s\n", s);
exit(EXIT_FAILURE);
}
int main(int argc, char *argv[]){
int debug = 0;
printf("#include <stdio.h>\n");
printf("#include <stdlib.h>\n");
printf("#include <string.h>\n");
printf("#include <math.h>\n\n");
yyparse();
if (argc > 1)
if (strcmp(argv[1], "-d") == 0)
debug = 1;
if (debug)
{
printf("------------------------------ Debugging ------------------------------\n");
printf("\n\nDebugging: variables\n");
struct var_node *cur;
printf("\n\nAll vars:\n");
for (cur = vars.head ; cur != NULL ; cur = cur->next)
printf("\tType: %s\n\tName: %s\n\n", cur->type, cur->name);
}
}