-
Notifications
You must be signed in to change notification settings - Fork 0
/
hwyacc.y
327 lines (295 loc) · 6.79 KB
/
hwyacc.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
%{
#include <stdio.h>
#include <string.h>
int yyparse();
int yylex();
extern FILE *yyin;
void yyerror(const char *str)
{
fprintf(stderr,"error: %s\n",str);
}
int yywrap()
{
return 1;
}
char canvas[200][200];
int main(int argc, char *argv[])
{
if(argc == 2){
yyin = fopen(argv[1], "r");
if(yyin == NULL){
printf("File not found.\n");
return 1;
}
for(int i = 0; i < 200; i++){
for(int j = 0; j < 200; j++){
canvas[i][j] = ' ';
}
}
yyparse();
printf("\n");
}
else{
printf("Invalid arguments, usage: ./interpreter <filename>\n");
return 1;
}
}
int canvasX, canvasY;
int cursorX, cursorY;
char *identifier;
char *message;
char *identifiers[100];
int idCount = 0;
int heights[100];
int widths[100];
%}
%token TOKPROGRAM TOKSEMICOLON TOKCANVASINITSECTION TOKCOLON TOKID TOKCANVASX TOKCONST TOKINTLIT TOKEQUAL TOKCANVASY TOKCOMMENT TOKCURSORX TOKCURSORY
%token TOKBRUSHDECLARATIONSECTION TOKBRUSH TOKCOMMA TOKDRAWINGSECTION TOKRENEWBRUSH TOKMESSAGE TOKPAINTCANVAS TOKEXHIBITCANVAS TOKMOVE TOKTO TOKPLUS TOKMINUS
%token TOKERROR
%%
peakasso:
TOKPROGRAM id TOKSEMICOLON canvas_init_section brush_declaration_section drawing_section
;
canvas_init_section:
TOKCANVASINITSECTION TOKCOLON canvas_size_init cursor_pos_init
;
canvas_size_init:
TOKCONST TOKCANVASX TOKEQUAL TOKINTLIT TOKSEMICOLON TOKCONST TOKCANVASY TOKEQUAL TOKINTLIT TOKSEMICOLON
{
if($4 >= 5 && $4 <= 200){
canvasX = $4;
} else{
printf("Invalid X value for Canvas, choosing 100 instead...\n");
canvasX = 100;
}
if($9 >= 5 && $9 <= 200){
canvasY = $9;
} else{
printf("Invalid Y value for Canvas, choosing 100 instead...\n");
canvasY = 100;
}
}
;
cursor_pos_init:
TOKCURSORX TOKEQUAL TOKINTLIT TOKSEMICOLON TOKCURSORY TOKEQUAL TOKINTLIT TOKSEMICOLON
{
cursorX = $3;
cursorY = $7;
}
;
brush_declaration_section:
TOKBRUSHDECLARATIONSECTION TOKCOLON
| TOKBRUSHDECLARATIONSECTION TOKCOLON variable_definition
;
variable_definition:
TOKBRUSH brush_list TOKSEMICOLON
;
brush_list:
brush_init_name
| brush_init_name TOKCOMMA brush_list
;
brush_init_name:
id
{
heights[idCount] = 1;
widths[idCount] = 1;
identifiers[idCount++] = strdup(identifier);
}
| id TOKEQUAL TOKINTLIT TOKINTLIT
{
if($3 > 0)
heights[idCount] = $3;
else{
heights[idCount] = 1;
printf("invalid height value (min=1), setting height to 1...");
}
if($4 > 0)
widths[idCount] = $4;
else{
widths[idCount] = 1;
printf("invalid width value (min=1), setting width to 1...");
}
identifiers[idCount++] = strdup(identifier);
}
;
brush_name:
id
{
int idFound = 0;
for(int i = 0; i < idCount; i++){
if(!strcmp(identifier, identifiers[i])){
idFound = 1;
break;
}
}
if(!idFound){
yyerror("Identifier not found");
return 1;
}
}
| id TOKEQUAL TOKINTLIT TOKINTLIT
{
int idFound = 0;
for(int i = 0; i < idCount; i++){
if(!strcmp(identifier, identifiers[i])){
idFound = 1;
heights[i] = $3;
widths[i] = $4;
break;
}
}
if(!idFound){
yyerror("Identifier not found");
return 1;
}
}
;
id:
TOKID
;
drawing_section:
TOKDRAWINGSECTION TOKCOLON statement_list
;
statement_list:
| statement TOKSEMICOLON
| statement TOKSEMICOLON statement_list
;
statement:
renew_statement
| paint_statement
| exhibit_statement
| cursor_move_statement
;
renew_statement:
TOKRENEWBRUSH TOKMESSAGE brush_name
{
printf("\n%s ", message);
FILE *temp = yyin;
yyin = NULL; // stdin;
int height, width;
scanf("%d", &height);
scanf("%d", &width);
int idFound = 0;
for(int i = 0; i < idCount; i++){
if(!strcmp(identifiers[i], identifier)){
if(height > 0)
heights[i] = height;
else
printf("invalid height value (min=1), not changing height...\n");
if(width > 0)
widths[i] = width;
else
printf("invalid width value (min=1), not changing width...\n");
idFound = 1;
break;
}
}
if(!idFound){
yyerror("Identifier not found");
return 1;
}
yyin = temp;
}
;
paint_statement:
TOKPAINTCANVAS brush_name
{
int height, width;
for(int i = 0; i < idCount; i++){
if(!strcmp(identifiers[i], identifier)){
height = heights[i];
width = widths[i];
break;
}
}
for(int i = 0; i < height; i++){
for(int j = 0; j < width; j++){
canvas[cursorY+i][cursorX+j] = '*';
}
}
}
;
exhibit_statement:
TOKEXHIBITCANVAS
{
for(int i = 0; i < canvasY; i++){
for(int j = 0; j < canvasX; j++){
printf("%c", canvas[i][j]);
}
printf("\n");
}
printf("\n");
}
;
cursor_move_statement:
TOKMOVE TOKCURSORX TOKTO expression
{
if($4 <= canvasX && $4 >= 0){
cursorX = $4;
} else{
printf("Trying to move out of canvas, operation neglected...");
}
}
| TOKMOVE TOKCURSORY TOKTO expression
{
if($4 <= canvasY && $4 >= 0){
cursorY = $4;
} else{
printf("Trying to move out of canvas, operation neglected...");
}
}
;
cursor:
TOKCURSORX
{
$$ = cursorX;
}
| TOKCURSORY
{
$$ = cursorY;
}
;
expression:
expression TOKPLUS term
{
$$ = $1 + $3;
}
| expression TOKMINUS term
{
$$ = $1 - $3;
}
|
term
{
$$ = $1;
}
;
term:
factor
{
$$ = $1;
}
;
factor:
TOKINTLIT
{
$$ = $1;
}
| cursor
{
$$ = $1;
}
| TOKCANVASX
{
$$ = canvasX;
}
| TOKCANVASY
{
$$ = canvasY;
}
| expression
{
$$ = $1;
}
;
%%