-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsyntaxAnalyzer.y
399 lines (327 loc) · 13 KB
/
syntaxAnalyzer.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
%{
#define __USE_C99_MATH
#include <stdio.h>
#include <stdlib.h>
#include "Doda.h"
#include <stdbool.h>
#include <stdarg.h>
#include "DodaSymbolTable.cpp"
using namespace std;
int yylex(void);
void yyerror(char *);
/* prototypes */
nodeType *opr(int oper, int nops, ...);
nodeType *id(string i);
nodeType *con(int value);
nodeType *conF(float value);
nodeType *conB(bool value);
nodeType *conC(char value);
nodeType *arg(string i);
void freeNode(nodeType *p);
int ex(nodeType *p);
extern int yylineno;
/*
Symbol Table
*/
// DodaSymbolTable symbolTable;
DodaSymbolTable symbolTable ;
%}
%code requires {
#include "./Doda.h"
#define YYDEBUG 1 // This is new
}
%union {
int intVal;
float floatVal;
bool boolVal;
char stringVal;
char* realStringVal;
nodeType *nPtr; /* node pointer */
};
%token <realStringVal>Identifiers
// TYPES
%token<intVal> INT
%token<intVal> FLOAT
%token<intVal> BOOL
%token<intVal> STRINGG
%token VOID
%token Constant // 5leha klma wa7da bs
//RELATIONAL
%token EQUAL
%token NOT_EQUAL
%token GREATER_THAN
%token GREATER_EQUAL
%token LESS_THAN
%token LESS_EQUAL
//BOOLEAN
%token AND
%token OR
%token NOT
//KEYWORDS
%token IF
%token ELSE
%token WHILE
%token DO
%token FOR
%token SWITCH
%token CASE
%token DEFAULT
%token BREAK
%token FUNC
%token RETURN
// VALUES
%token <floatVal> floatType
%token <intVal> intType
%token <stringVal>stringType
%token <boolVal> boolType
//statments tokens
%type <nPtr> statments block_statment statment while_statment if_statment value
%type <nPtr> matched_if unmatched_if for_statment for_begining do_while_statment switch_statment
%type <nPtr> case_statment cases_statment func_defintion_statment return_statment
%type <nPtr> func_return_statments func_def_arguments arguments func_call_statment
%type <nPtr> var_declare_statment expression_statment expression_statment_lv0 program
%type<intVal>data_type
//Precedence
// ref: https://en.cppreference.com/w/c/language/operator_precedence
%right '=' //
%left OR
%left AND
%left NOT_EQUAL EQUAL
%left GREATER_THAN GREATER_EQUAL LESS_THAN LESS_EQUAL
%left '-' '+'
%left '*' '/' '%'
%left '^'
%right NOT //hights precedence
%left ifpred
%nonassoc IF
%nonassoc ELSE
//%start program
%%
program: program statments { ex($2); }
|
;
statments:
statments statment { $$ = opr(';',2,$1,$2); }
| statment { $$ = $1;}
| statments block_statment { $$ = opr(';',2,$1,$2);}
| block_statment { $$ = $1;}
;
block_statment: {symbolTable.addBlock();}'{' statments '}' { $$ = $3; symbolTable.closeBlock(); }
;
statment: ';' {$$ = opr(';',2,NULL,NULL);}
| while_statment {$$ = $1; }
| if_statment {$$ = $1; }
| for_statment {$$ = $1;}
| do_while_statment ';' {$$ = $1;}
| switch_statment {$$ = $1;}
| func_defintion_statment {$$ = $1;}
| var_declare_statment ';' {$$ = $1; symbolTable.addRecord(); }
| expression_statment ';' {$$ = $1; }
| error ';' { $$ =opr(';',2,NULL,NULL); fprintf(stdout,"\t error near ; near line %d\n",yylineno); yyerrok; }
| error ')' { $$ =opr(';',2,NULL,NULL); fprintf(stdout,"\t error near ) near line %d\n",yylineno); yyerrok; }
| error '}' { $$ =opr(';',2,NULL,NULL); fprintf(stdout,"\t error near } near line %d\n",yylineno); yyerrok; }
;
while_statment: WHILE '(' expression_statment ')' block_statment {$$ = opr(WHILE,2,$3,$5);}
;
if_statment: matched_if { $$ = $1; }
| unmatched_if {$$ = $1; }
;
matched_if: IF '(' expression_statment ')' block_statment ELSE block_statment %prec ifpred {$$=opr(IF,3, $3, $5, $7); }
;
//matched_if: IF '(' expression_statment ')' '{' statments '}' ELSE block_statment {$$=opr(IF,3, $3, $6, $9); }
;
unmatched_if: IF '(' expression_statment ')' block_statment {$$=opr(IF,2,$3,$5);}
;
//matched_if: IF '(' expression_statment ')' '{' statments '}' ELSE '{' statments '}' %prec ifpred {$$=opr(IF,3,$3,$6,$10); }
// ;
//unmatched_if: IF '(' expression_statment ')' '{' statments '}' {$$=opr(IF,2,$3,$6);}
// | IF '(' expression_statment ')' '{' matched_if '}' ELSE '{' unmatched_if '}' {$$ = opr(IF,3,$3,$6,$10); }
// ;
for_statment: FOR '(' for_begining ';' expression_statment ';' expression_statment ')' block_statment {$$ = opr(FOR,4,$3,$5,$7,$9);}
;
for_begining: expression_statment {$$=$1;}
| {symbolTable.forFlag = true;} var_declare_statment {$$=$2;}
;
do_while_statment: DO block_statment WHILE '(' expression_statment ')' {$$ = opr(DO,2,$2,$5);}
;
switch_statment: SWITCH '(' Identifiers ')' cases_statment {$$=opr(SWITCH,2,id($3),$5);}
;
case_statment: {symbolTable.addBlock();} CASE intType ':' statment BREAK ';' {$$ =opr(CASE,2,con($3),$5); symbolTable.closeBlock();}
;
// force default statmentcases_statment
cases_statment: {symbolTable.addBlock();} DEFAULT ':' statment {$$ = opr(DEFAULT,1,$4);symbolTable.closeBlock();}
| case_statment cases_statment {$$ = opr(';',2,$1,$2);}
;
func_defintion_statment: FUNC VOID Identifiers '(' func_def_arguments ')' block_statment {$$=opr(FUNC,3,id($3),$5,$7);
symbolTable.currentRecord.type = "void";
symbolTable.currentRecord.name = $3;
symbolTable.currentRecord.kind = "func";
symbolTable.addRecord();}
| FUNC data_type Identifiers '(' func_def_arguments ')' func_return_statments {$$=opr(FUNC,3,id($3),$5,$7);
symbolTable.currentRecord.name = $3;
symbolTable.currentRecord.kind = "func";
symbolTable.addRecord();
}
;
return_statment: RETURN expression_statment ';' {$$ = opr(RETURN,1,$2);}
;
func_return_statments: {symbolTable.addBlock();} '{' return_statment '}' {$$=$3;symbolTable.closeBlock();}
| {symbolTable.addBlock();} '{' statments return_statment '}' {$$ = opr(';',2,$3,$4);symbolTable.closeBlock();}
;
data_type: INT {$$ = $1;symbolTable.currentRecord.type = "int";}
| FLOAT {$$ = $1;symbolTable.currentRecord.type = "float";}
| BOOL {$$ = $1;symbolTable.currentRecord.type = "bool";}
| STRINGG {$$ = $1;symbolTable.currentRecord.type = "char";}
;
func_def_arguments: {$$ = opr(';',2,NULL,NULL);}
| data_type Identifiers {$$ = arg($2);
symbolTable.currentRecord.name = $2;
symbolTable.currentRecord.kind = "par";
symbolTable.funcFlag = true;
symbolTable.addToFunc();}
| func_def_arguments ',' data_type Identifiers {$$ = opr(';',2,$1,arg($4));
symbolTable.currentRecord.name = $4;
symbolTable.currentRecord.kind = "par";
symbolTable.funcFlag = true;
symbolTable.addToFunc();}
;
arguments: {$$ = opr(';',2,NULL,NULL);}
| expression_statment {$$ = $1;} // test this
| expression_statment ',' arguments {$$ = opr(';',2,$3,$1);} //could be more than one in identifire check
;
func_call_statment: Identifiers '(' arguments ')' {$$ = opr(FUNC,2,id($1),$3);}
;
var_declare_statment: data_type Identifiers {$$ = opr(';',2,NULL,NULL);
symbolTable.currentRecord.kind = "Var";
symbolTable.currentRecord.name = $2;}
| data_type Identifiers '=' expression_statment {$$ = opr('=',2,arg($2),$4);
symbolTable.currentRecord.kind = "Var";
symbolTable.currentRecord.name = $2;}
| Constant data_type Identifiers '=' expression_statment {$$ = opr('=',2,arg($3),$5);
symbolTable.currentRecord.kind = "Const";
symbolTable.currentRecord.name = $3;}
;
value: intType {$$ = con($1);}
| floatType {$$ = conF($1);}
| boolType {$$ = conB($1); }
| stringType {$$ = conC($1);}
;
expression_statment: '(' expression_statment ')' {$$ = $2;}
| Identifiers {$$ =id($1);symbolTable.checkIdentifier($1,yylineno); symbolTable.addMatches($1) ; }
| value { $$ = $1; }
| func_call_statment { $$ = $1; }
| expression_statment_lv0 {$$ = $1; }
;
expression_statment_lv0: Identifiers '=' expression_statment {$$ = opr('=',2,arg($1),$3); symbolTable.checkIdentifier($1,yylineno) ; symbolTable.addMatches($1); symbolTable.checkIdentifiersType(yylineno);}
| expression_statment '+' expression_statment {$$ = opr('+',2,$1,$3); symbolTable.checkIdentifiersType(yylineno); }
| expression_statment '-' expression_statment {$$ = opr('-',2,$1,$3);symbolTable.checkIdentifiersType(yylineno);}
| expression_statment '*' expression_statment {$$ = opr('*',2,$1,$3);}
| expression_statment '/' expression_statment {$$ = opr('/',2,$1,$3);symbolTable.checkIdentifiersType(yylineno);}
| expression_statment '%' expression_statment {$$ = opr('%',2,$1,$3);}
| expression_statment '^' expression_statment {$$ = opr('^',2,$1,$3);}
| expression_statment AND expression_statment {$$ = opr(AND,2,$1,$3);}
| expression_statment OR expression_statment {$$ = opr(OR,2,$1,$3);}
| expression_statment GREATER_THAN expression_statment {$$ = opr(GREATER_THAN,2,$1,$3);}
| expression_statment GREATER_EQUAL expression_statment {$$ = opr(GREATER_EQUAL,2,$1,$3);}
| expression_statment LESS_THAN expression_statment {$$ = opr(LESS_THAN,2,$1,$3);}
| expression_statment LESS_EQUAL expression_statment {$$ = opr(LESS_EQUAL,2,$1,$3);}
| expression_statment EQUAL expression_statment {$$ = opr(EQUAL,2,$1,$3);}
| expression_statment NOT_EQUAL expression_statment {$$ = opr(NOT_EQUAL,2,$1,$3);}
| NOT expression_statment {$$ = opr(NOT,1,$2);}
;
%%
nodeType *con(int value) {
nodeType *p;
/* allocate node */
if ((p = (nodeType *)malloc(sizeof(nodeType))) == NULL)
yyerror((char *)"out of memory");
/* copy information */
p->type = typeCon;
p->con.value = value;
return p;
}
nodeType *conF(float value) {
nodeType *p;
/* allocate node */
if ((p = (nodeType *)malloc(sizeof(nodeType))) == NULL)
yyerror((char *)"out of memory");
/* copy information */
p->type = typeConFloat; // change type
p->conF.value = value; //change call
return p;
}
nodeType *conB(bool value) {
nodeType *p;
/* allocate node */
if ((p = (nodeType *)malloc(sizeof(nodeType))) == NULL)
yyerror((char *)"out of memory");
/* copy information */
p->type = typeConBool; // change type
p->conB.value = value; //change call
return p;
}
nodeType *conC(char value) {
nodeType *p;
/* allocate node */
if ((p = (nodeType *)malloc(sizeof(nodeType))) == NULL)
yyerror((char *)"out of memory");
/* copy information */
p->type = typeConChar; // change type
p->conC.value = value; //change call
return p;
}
nodeType *id(string i) {
nodeType *p;
/* allocate node */
if ((p = (nodeType *)malloc(sizeof(nodeType))) == NULL)
yyerror((char *)"out of memory");
/* copy information */
p->type = typeId;
p->id.i = i;
return p;
}
nodeType *arg(string i) {
nodeType *p;
/* allocate node */
if ((p = (nodeType *)malloc(sizeof(nodeType))) == NULL)
yyerror((char *)"out of memory");
/* copy information */
p->type = typeArg;
p->id.i = i;
return p;
}
nodeType *opr(int oper, int nops, ...) {
va_list ap;
nodeType *p;
int i;
/* allocate node, extending op array */
if ((p = (nodeType *)malloc(sizeof(nodeType) + (nops-1) * sizeof(nodeType *))) == NULL)
yyerror((char *)"out of memory");
/* copy information */
p->type = typeOpr;
p->opr.oper = oper;
p->opr.nops = nops;
va_start(ap, nops);
for (i = 0; i < nops; i++)
p->opr.op[i] = va_arg(ap, nodeType*);
va_end(ap);
return p;
}
void freeNode(nodeType *p) {
int i;
if (!p) return;
if (p->type == typeOpr) {
for (i = 0; i < p->opr.nops; i++)
freeNode(p->opr.op[i]);
}
free (p);
}
void yyerror(char *s) {
//fprintf(" %s near line %d ",s,yylineno);
//fprintf(stdout, "%s\n", s);
}
int main(void) {
// yydebug = 1; // This is new
yyparse();
return 0;
}