-
Notifications
You must be signed in to change notification settings - Fork 1
/
oa.y
543 lines (484 loc) · 11.5 KB
/
oa.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
537
538
539
540
541
542
543
%{
#include <stdio.h>
#include <stdlib.h>
#include "oa.h"
#define YYDEBUG 1
%}
%union {
int number_int;
double number_double;
char type_char;
char *type_string;
char *type;
char *name;
struct TreeNode *node;
struct Expression *expression;
struct FormParam *formParam;
struct LeftValue *leftValue;
struct FactParam *factParam;
}
%token BOOLEAN BREAK CHAR CLASS CONTINUE DOUBLE ELIF ELSE EXTENDS FALSE FOR FOREACH IF IMPORT IN INT MY_NULL RETURN THIS TRUE VOID WHILE PART_LETTER DECIMAL_LITERAL DECIMAL_FLOATING_POINT_LITERAL TYPE_CHAR STRING LPAREN RPAREN LBRACE RBRACE LBRACKET RBRACKET SEMICOLON COMMA DOT ASSIGN LT GT BANG TILDE EQ LE GE NE SC_OR SC_AND PLUS MINUS STAR SLASH BIT_AND BIT_OR XOR REM NAME_SPACE CLASS_IDENTIFIER STRING_LITERAL
%token <type> INT DOUBLE CLASS_IDENTIFIER CHAR
%token <name> IDENTIFIER
%token <number_int> DECIMAL_LITERAL
%token <number_double> DECIMAL_FLOATING_POINT_LITERAL
%token <type_char> CHAR_LITERAL
%token <type_string> STRING_LITERAL
%type <type> type arrayType
%type <node> outerStatements outerStatement
functionStatements functionStatement
classStatements classStatement
varDeclareStatement varDefineStatement varAssignStatement
arrayDeclareStatement arrayDefineStatement arrayAssignStatement
ifStatement elifParts elifPart elsePart
whileStatement foreachStatement
classDefineStatement
functionDeclareStatement functionDefineStatement classMethodDefineStatement
breakStatement continueStatement returnStatement
%type <expression> expression compareExpression mathExpression term factor
%type <formParam> formParams formParamSet formParam
%type <leftValue> leftValue leftValueSet
%type <factParam> factParams factParamSet factParam
%%
outerStatements
:/*empty*/{
$$ = NULL;
}
|outerStatement outerStatements{
if($1){
$1->next = $2;
extern struct ParseTree *parseTree;
parseTree->root = $1;
}else{
$$ = $2;
}
}
;
outerStatement
:varDeclareStatement SEMICOLON
|arrayDeclareStatement SEMICOLON
|classDefineStatement
|functionDeclareStatement SEMICOLON
|functionDefineStatement
|classMethodDefineStatement
|returnStatement SEMICOLON
|SEMICOLON{
$$ = NULL;
}
;
functionStatements
:/*empty*/{
$$ = NULL;
}
|functionStatement functionStatements{
if($1){
$1->next = $2;
}else{
$$ = $2;
}
}
;
functionStatement
:varDeclareStatement SEMICOLON
|varDefineStatement SEMICOLON
|varAssignStatement SEMICOLON
|arrayDeclareStatement SEMICOLON
|arrayDefineStatement SEMICOLON
|arrayAssignStatement SEMICOLON
|ifStatement
|whileStatement
|foreachStatement
|breakStatement SEMICOLON
|continueStatement SEMICOLON
|returnStatement SEMICOLON
|SEMICOLON{
$$ = NULL;
}
;
classStatements
:/*empty*/{
$$ = NULL;
}
|classStatement classStatements{
if($1){
$1->next = $2;
}else{
$$ = $2;
}
}
;
classStatement
:varDeclareStatement SEMICOLON
|arrayDeclareStatement SEMICOLON
|functionDeclareStatement SEMICOLON
|SEMICOLON{
$$ = NULL;
}
;
/*--------------------variable part--------------------*/
varDeclareStatement
:type IDENTIFIER {
$$ = createVarDeclare($1, $2);
}
;
varDefineStatement
:type IDENTIFIER ASSIGN expression {
$$ = createVarDefine($1, $2, $4);
}
;
varAssignStatement /* containinig array[expression] assignment and not giving value expression using*/
:leftValue ASSIGN expression {
$$ = createVarAssign($1, $3, NULL);
}
|leftValue LBRACKET expression RBRACKET ASSIGN expression {
$$ = createVarAssign($1, $6, $3);
}
|expression{
$$ = createVarAssign(NULL, $1, NULL);
}
;
/*--------------------array part--------------------*/
arrayDeclareStatement
:arrayType IDENTIFIER{
$$ = createArrayDeclare($1, $2);
}
;
arrayDefineStatement
:arrayType IDENTIFIER ASSIGN type LBRACKET expression RBRACKET{
$$ = createArrayDefine($1, $2, $4, $6);
}
;
arrayAssignStatement
:leftValue ASSIGN type LBRACKET expression RBRACKET{
$$ = createArrayAssign($1, $3, $5);
}
|leftValue ASSIGN MY_NULL{
$$ = createArrayAssign($1, NULL, NULL);
}
;
/*--------------------if-elif-else part----------------*/
ifStatement
:IF LPAREN expression RPAREN LBRACE functionStatements RBRACE elifParts elsePart{
$$ = createIf($3, $6, $8, $9);
}
;
elifParts
:/*empty*/{
$$ = NULL;
}
|elifPart elifParts{
$1->next = $2;
$$ = $1;
}
;
elifPart
:ELIF LPAREN expression RPAREN LBRACE functionStatements RBRACE{
$$ = createElifPart($3, $6);
}
;
elsePart
:/*empty*/{
$$ = NULL;
}
|ELSE LBRACE functionStatements RBRACE{
$$ = createElsePart($3);
}
;
/*--------------------loop part----------------*/
whileStatement
:WHILE LPAREN expression RPAREN LBRACE functionStatements RBRACE{
$$ = createWhile($3, $6);
}
;
foreachStatement
:FOREACH LPAREN IDENTIFIER IN leftValue RPAREN LBRACE functionStatements RBRACE{
$$ = createForeach($3, $5, $8);
}
;
/*--------------------class part----------------*/
classDefineStatement
:CLASS CLASS_IDENTIFIER LBRACE classStatements RBRACE{
$$ = createClassDefine($2, NULL, $4);
}
|CLASS CLASS_IDENTIFIER EXTENDS CLASS_IDENTIFIER LBRACE classStatements RBRACE{
$$ = createClassDefine($2, $4, $6);
}
;
/*--------------------function part----------------*/
functionDeclareStatement
:type IDENTIFIER LPAREN formParams RPAREN{
$$ = createFunctionDeclare($1, $2, $4);
}
|arrayType IDENTIFIER LPAREN formParams RPAREN{
$$ = createFunctionDeclare($1, $2, $4);
}
|VOID IDENTIFIER LPAREN formParams RPAREN{
$$ = createFunctionDeclare(NULL, $2, $4);
}
;
functionDefineStatement
:type IDENTIFIER LPAREN formParams RPAREN LBRACE functionStatements RBRACE{
$$ = createFunctionDefine($1, $2, $4, $7);
}
|arrayType IDENTIFIER LPAREN formParams RPAREN LBRACE functionStatements RBRACE{
$$ = createFunctionDefine($1, $2, $4, $7);
}
|VOID IDENTIFIER LPAREN formParams RPAREN LBRACE functionStatements RBRACE{
$$ = createFunctionDefine(NULL, $2, $4, $7);
}
;
classMethodDefineStatement
:type CLASS_IDENTIFIER NAME_SPACE IDENTIFIER LPAREN formParams RPAREN LBRACE functionStatements RBRACE{
$$ = createClassMethodDefine($1, $4, $6, $9, $2);
}
|arrayType CLASS_IDENTIFIER NAME_SPACE IDENTIFIER LPAREN formParams RPAREN LBRACE functionStatements RBRACE{
$$ = createClassMethodDefine($1, $4, $6, $9, $2);
}
|VOID CLASS_IDENTIFIER NAME_SPACE IDENTIFIER LPAREN formParams RPAREN LBRACE functionStatements RBRACE{
$$ = createClassMethodDefine(NULL, $4, $6, $9, $2);
}
;
/*--------------------control part----------------*/
/*in loop*/
breakStatement
:BREAK{
$$ = createBreak();
}
;
continueStatement
:CONTINUE{
$$ = createContinue();
}
;
/*in function*/
returnStatement
:RETURN expression{
$$ = createReturn($2);
}
|RETURN{
$$ = createReturn(NULL);
}
;
/*--------------------parameter part----------------*/
formParams
:/*empty*/{
$$ = NULL;
}
|formParam formParamSet{
$1->next = $2;
$$ = $1;
}
;
formParamSet
:/*empty*/{
$$ = NULL;
}
|COMMA formParam formParamSet{
$2->next = $3;
$$ = $2;
}
;
formParam
:type IDENTIFIER{
$$ = createFormParam($1, $2);
}
|arrayType IDENTIFIER{
$$ = createFormParam($1, $2);
}
;
/*--------------------expression part----------------*/
expression
:compareExpression SC_AND expression{
$$ = createExpression($1, $3, OA_EXP_AND);
}
|compareExpression SC_OR expression{
$$ = createExpression($1, $3, OA_EXP_OR);
}
|BANG expression{
$$ = createExpression($2, NULL, OA_EXP_NOT);
}
|compareExpression
;
compareExpression
:mathExpression EQ mathExpression{
$$ = createExpression($1, $3, OA_EXP_EQ);
}
|mathExpression EQ MY_NULL{
$$ = createExpression($1, NULL, OA_EXP_EQ);
}
|mathExpression NE mathExpression{
$$ = createExpression($1, $3, OA_EXP_NE);
}
|mathExpression NE MY_NULL{
$$ = createExpression($1, NULL, OA_EXP_EQ);
}
|mathExpression GT mathExpression{
$$ = createExpression($1, $3, OA_EXP_GT);
}
|mathExpression GE mathExpression{
$$ = createExpression($1, $3, OA_EXP_GE);
}
|mathExpression LT mathExpression{
$$ = createExpression($1, $3, OA_EXP_LT);
}
|mathExpression LE mathExpression{
$$ = createExpression($1, $3, OA_EXP_LE);
}
|mathExpression
;
mathExpression
:mathExpression PLUS term{
$$ = createExpression($1, $3, OA_EXP_PLUS);
}
|mathExpression MINUS term{
$$ = createExpression($1, $3, OA_EXP_MINUS);
}
|term
;
term
:term STAR factor{
$$ = createExpression($1, $3, OA_EXP_MULTIPLE);
}
|term SLASH factor{
$$ = createExpression($1, $3, OA_EXP_DIVIDE);
}
|factor
;
factor
:DECIMAL_LITERAL{
$$ = createExpressionIntLeaf($1, 0);
}
|DECIMAL_FLOATING_POINT_LITERAL{
$$ = createExpressionDoubleLeaf($1, 0);
}
|CHAR_LITERAL{
$$ = createExpressionCharLeaf($1);
}
|STRING_LITERAL{
$$ = createExpressionStringLeaf($1);
}
|leftValue{
$$ = createExpressionLeftValueLeaf($1);
}
|leftValue LBRACKET expression RBRACKET{
$$ = createExpressionArrayValue($1, $3);
}
|leftValue LPAREN factParams RPAREN{
$$ = createExpressionFunctionValue($1, $3);
}
|LPAREN expression RPAREN{
$$ = $2;
}
|MINUS DECIMAL_LITERAL{
$$ = createExpressionIntLeaf($2, 1);
}
|MINUS DECIMAL_FLOATING_POINT_LITERAL{
$$ = createExpressionDoubleLeaf($2, 1);
}
;
leftValue
:IDENTIFIER leftValueSet{
$$ = createLeftValue($1);
$$->next = $2;
}
;
leftValueSet
:/*empty*/{
$$ = NULL;
}
|DOT IDENTIFIER leftValueSet{
$$ = createLeftValue($2);
$$->next = $3;
}
;
factParams
:/*empty*/{
$$ = NULL;
}
|factParam factParamSet{
$1->next = $2;
$$ = $1;
}
;
factParamSet
:/*empty*/{
$$ = NULL;
}
|COMMA factParam factParamSet{
$2->next = $3;
$$ = $2;
}
;
factParam
:expression{
$$ = createFactParam($1);
}
;
/*--------------------type part----------------*/
arrayType
:type LBRACKET RBRACKET{
$$ = createArrayType($1);
}
;
type
:INT
|DOUBLE
|CHAR
|CLASS_IDENTIFIER
;
%%
int yyerror(char* s){
extern int yylineno;
extern char *yytext;
fprintf(stderr, "%s ", s);
fprintf(stderr, "\nERROR! line %d: ", yylineno);
fprintf(stderr, "before \"%s\"\n", yytext);
return 0;
}
int executeParser(const char *filename)
{
extern FILE *tokenStream;
extern struct ParseTree *parseTree;
tokenStream = NULL;
tokenStream = fopen("tokenStream.txt", "w");
if(!tokenStream){
printf("cannot open token stream file\n");
return 1;
}
extern int yyparse(void);
extern FILE *yyin;
FILE* fp = NULL;
fp = fopen(filename, "r");
if(fp == NULL){
return 1;
}
parseTree = (struct ParseTree*)malloc(sizeof(struct ParseTree));
parseTree->root = NULL;
yyin = fp;
int result = yyparse();
fclose(fp);
fclose(tokenStream);
//printJason(parseTree->root);
return 0;
}
/*
int main(void)
{
extern int yyparse(void);
extern FILE *yyin;
parseTree = (struct ParseTree*)malloc(sizeof(struct ParseTree));
tokenStream = NULL;
tokenStream = fopen("tokenStream.txt", "w");
if(!tokenStream){
printf("cannot open token stream file\n");
return 1;
}
yyin = stdin;
if (yyparse()) {
fclose(tokenStream);
return 1;
}
printJason(parseTree->root);
fclose(tokenStream);
}
*/