-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.y
More file actions
342 lines (336 loc) · 8.79 KB
/
Copy pathparser.y
File metadata and controls
342 lines (336 loc) · 8.79 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
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
%{
/*
* parser.y - yacc source for the MiniC
* progrmmer – SHIN-JUNGHWA(2271035), KIM-YEJI(2176082), SONG-CHAEWON(2076216), YOON-HAYEONG(2071033)
* date - 2024-05-30
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <malloc.h>
#include "glob.h"
int returntp = 0; // 0:void, 1:int
int type = 0; // 0:scalar, 1:array, 2:function
int paramidx = 0; // index of parameter array
extern printError(ERRORtypes err);
extern yylex();
extern yyerror(char* s);
%}
%token TIDENT TNUMBER TRNUMBER TCONST TELSE TIF TINT TFLOAT TRETURN TVOID TWHILE
%token TADD TSUB TMUL TDIV TMOD
%token TASSIGN TADDASSIGN TSUBASSIGN TMULASSIGN TDIVASSIGN TMODASSIGN
%token TNOT TOR TAND TEQUAL TNOTEQU TGREAT TLESS TGREATE TLESSE
%token TINC TDEC TCOMMA TSEMICOLON
%token TLPAREN TRPAREN TLBRACE TRBRACE TLSQUARE TRSQUARE
%token TTOOLONG TSTARTWD TILLCH
%nonassoc TLOWERTHANELSE
%nonassoc TELSE
%%
mini_c : translation_unit
;
translation_unit : external_dcl
| translation_unit external_dcl
| TILLCH
{
yyerrok;
printError(illch); /* error - Lillegal character */
}
;
external_dcl : function_def
| declaration
| TIDENT error
{
yyerrok;
printError(wrong_st); /* error - Wrong Statement */
}
| TRBRACE
{
yyerrok;
printError(nobrace); /* error - Missing brace */
}
;
function_def : function_header compound_st
| function_header TSEMICOLON
| function_header error
{
yyerrok;
printError(wrong_funcdef); /* error - Wrong function definition */
}
;
function_header : dcl_spec function_name formal_param
;
dcl_spec : dcl_specifiers
;
dcl_specifiers : dcl_specifier
| dcl_specifiers dcl_specifier
;
dcl_specifier : type_qualifier
| type_specifier
;
type_qualifier : TCONST
;
type_specifier : TINT {returntp = 1;} /* type: integer */
| TFLOAT {returntp = 2;} /* type: float */
| TVOID {returntp = 0;} /* type: void */
;
function_name : identifier
{
curid->tp = 4;
curid->rtp = returntp;
preid = curid;
paramidx = 0;
}
;
formal_param : TLPAREN opt_formal_param TRPAREN
| TLPAREN opt_formal_param error
{
yyerrok;
printError(noparen); /* error - Missing paren */
}
;
opt_formal_param : formal_param_list
|
;
formal_param_list : param_dcl
| formal_param_list TCOMMA param_dcl
;
param_dcl : dcl_spec declarator
{
curid->tp = type;
preid->param[paramidx++] = curid->index;
preid->paramnum++;
}
;
compound_st : TLBRACE opt_dcl_list opt_stat_list TRBRACE
| TLBRACE opt_dcl_list opt_stat_list error
{
yyerrok;
printError(nobrace); /* error - Missing brace */
}
;
opt_dcl_list : declaration_list
|
;
declaration_list : declaration
| declaration_list declaration
;
declaration : dcl_spec init_dcl_list TSEMICOLON
| dcl_spec init_dcl_list error
{
yyerrok;
printError(nosemi); /* error - Missing semicolon */
}
| dcl_spec error
{
yyerrok;
printError(wrong_dcl); /* error - wrong declaration */
}
;
init_dcl_list : init_declarator
| init_dcl_list TCOMMA init_declarator
;
init_declarator : declarator
| declarator TASSIGN TNUMBER
| declarator TASSIGN TRNUMBER
;
declarator : identifier
{
if (returntp == 1) {type = 0; curid->tp = 0;}
else if (returntp == 2) {type = 2; curid->tp = 2;}
}
| identifier TLSQUARE opt_number TRSQUARE
{
if (returntp == 1) {type = 1; curid->tp = 1; returntp = type = -1;}
else if (returntp == 2) {type = 3; curid->tp = 3; returntp = type = -1;}
}
| identifier TLSQUARE opt_number error
{
yyerrok;
printError(nosquare); /* error - Missing square */
}
;
opt_number : TNUMBER
|
;
opt_stat_list : statement_list
|
;
statement_list : statement
| statement_list statement
;
statement : compound_st
| expression_st
| if_st
| while_st
| return_st
;
expression_st : opt_expression TSEMICOLON
| expression error
{
yyerrok;
printError(nosemi); /* error - Missing semicolon */
}
;
opt_expression : expression
|
;
if_st : TIF TLPAREN expression TRPAREN statement %prec TLOWERTHANELSE
| TIF TLPAREN expression TRPAREN statement TELSE statement
;
while_st : TWHILE TLPAREN expression TRPAREN statement
;
return_st : TRETURN opt_expression TSEMICOLON
| TRETURN opt_expression error
{
yyerrok;
printError(nosemi); /* error - Missing semicolon */
}
;
expression : assignment_exp
;
assignment_exp : logical_or_exp
| unary_exp TASSIGN assignment_exp
| unary_exp TASSIGN error
{
yyerrok;
printError(wrong_asgn); /* error - Wrong assignment */
}
| unary_exp TADDASSIGN assignment_exp
| unary_exp TADDASSIGN error
{
yyerrok;
printError(wrong_asgn); /* error - Wrong assignment */
}
| unary_exp TSUBASSIGN assignment_exp
| unary_exp TSUBASSIGN error
{
yyerrok;
printError(wrong_asgn); /* error - Wrong assignment */
}
| unary_exp TMULASSIGN assignment_exp
| unary_exp TMULASSIGN error
{
yyerrok;
printError(wrong_asgn); /* error - Wrong assignment */
}
| unary_exp TDIVASSIGN assignment_exp
| unary_exp TDIVASSIGN error
{
yyerrok;
printError(wrong_asgn); /* error - Wrong assignment */
}
| unary_exp TMODASSIGN assignment_exp
| unary_exp TMODASSIGN error
{
yyerrok;
printError(wrong_asgn); /* error - Wrong assignment */
}
;
logical_or_exp : logical_and_exp
| logical_or_exp TOR logical_and_exp
| logical_or_exp TOR error
{
yyerrok;
printError(wrong_op); /* error - Wrong operation */
}
;
logical_and_exp : equality_exp
| logical_and_exp TAND equality_exp
| logical_and_exp TAND error
{
yyerrok;
printError(wrong_op); /* error - Wrong operation */
}
;
equality_exp : relational_exp
| equality_exp TEQUAL relational_exp
| equality_exp TEQUAL error
{
yyerrok;
printError(wrong_op); /* error - Wrong operation */
}
| equality_exp TNOTEQU relational_exp
| equality_exp TNOTEQU error
{
yyerrok;
printError(wrong_op); /* error - Wrong operation */
}
;
relational_exp : additive_exp
| relational_exp TGREAT additive_exp
| relational_exp TLESS additive_exp
| relational_exp TGREATE additive_exp
| relational_exp TLESSE additive_exp
;
additive_exp : multiplicative_exp
| additive_exp TADD multiplicative_exp
| additive_exp TADD error
{
yyerrok;
printError(wrong_op); /* error - Wrong operation */
}
| additive_exp TSUB multiplicative_exp
| additive_exp TSUB error
{
yyerrok;
printError(wrong_op); /* error - Wrong operation */
}
;
multiplicative_exp : unary_exp
| multiplicative_exp TMUL unary_exp
| multiplicative_exp TMUL error
{
yyerrok;
printError(wrong_op); /* error - Wrong operation */
}
| multiplicative_exp TDIV unary_exp
| multiplicative_exp TDIV error
{
yyerrok;
printError(wrong_op); /* error - Wrong operation */
}
| multiplicative_exp TMOD unary_exp
| multiplicative_exp TMOD error
{
yyerrok;
printError(wrong_op); /* error - Wrong operation */
}
;
unary_exp : postfix_exp
| TSUB unary_exp
| TNOT unary_exp
| TINC unary_exp
| TDEC unary_exp
;
postfix_exp : primary_exp
| postfix_exp TLSQUARE expression TRSQUARE
| postfix_exp TLPAREN opt_actual_param TRPAREN
| postfix_exp TINC
| postfix_exp TDEC
;
opt_actual_param : actual_param
|
;
actual_param : actual_param_list
;
actual_param_list : assignment_exp
| actual_param_list TCOMMA assignment_exp
;
primary_exp : identifier
| TNUMBER
| TRNUMBER
| TLPAREN expression TRPAREN
;
identifier : TIDENT
| TTOOLONG
{
yyerrok;
printError(toolong); /* error - too long identifier */
}
| TSTARTWD
{
yyerrok;
printError(startwd); /* error - Start with digit identifier */
}
;
%%