-
Notifications
You must be signed in to change notification settings - Fork 1
/
bhash_latest.y
349 lines (299 loc) · 6.84 KB
/
bhash_latest.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
%{
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
FILE *yyin;
int intArr[100];
void yyerror(char*);
%}
%token ID
%token CONSTANT
%token FUNC
%token CHAR_CONSTANT
%token EQUALTO
%token INT
%token BLOCK
%token DOUBLE
%token DEFVAR
%token CHAR
%token STRING
%token LIST
%token BOOL
%token WHILE
%token IF
%token ELSE
%token ELIF
%token CASE
%token DEF
%token DO
%token FOR
%token BREAK
%token RETURN
%token CONTINUE
%token DEFAULT
%token BRIDGE
%token LABEL
%token TRUE_
%token FALSE_
%token INC
%token DEC
%token B_OP
%token RELOP
%token OR_B_RELOP
%token AND_B_RELOP
%token NOT_B_RELOP
%token MULOP
%token U_OP
%token BOOLEAN
%token UNARYOP
%token END
%token SWITCH
%token NUM
%token VALUE
%token TYPEDEF
%token PRINT
%token PRINTLN
%token INPUT
%token SUMOP
%token COMMENT
%left '+' '-' '^'
%left '*' '/' '%'
%%
program : declarationlist {printf("parsed successfully\n");}
;
declarationlist: declaration declarationlist
| declaration
;
declaration : function_definition
| variable_declaration
;
function_definition : FUNC ID '(' params ')' statement;
variable_declaration : variable_declaration datatype init_dec_list
| datatype init_dec_list
;
init_dec_list : init_dec ';'
|init_dec ',' init_dec_list ;
init_dec : ID EQUALTO initializer {intArr[$1] =$3;}
| ID {$$ = intArr[$1];}
| ID '[' NUM ']' {}
| ID '[' NUM ']' EQUALTO initializer
;
initializer : expression
| CONSTANT {$$ = $1;}
| list_init
| NUM {$$ = $1;}
|CHAR_CONSTANT {$$ = $1;}
;
list_init : '{' list_constant_list '}' ;
list_constant_list : list_constant_list ',' CONSTANT
| CONSTANT
;
list_constant_list : list_constant_list ',' NUM
| NUM
;
list_constant_list : list_constant_list ',' CHAR_CONSTANT
|CHAR_CONSTANT
;
params : paramlist | ;
paramlist: datatype ID
| paramlist ',' datatype ID
;
datatype : INT
| DOUBLE
| DEFVAR
| CHAR
| STRING
| BOOLEAN
| BLOCK
;
expression : ID EQUALTO simpleexpression {intArr[$1] = $3;}
| immutable B_OP simpleexpression {if($2 == 10)
{$1 = $1 + $3;}
else if($2 == 11)
{$1 = $1 - $3;}
else if($2 == 12)
{$1 = $1 * $3;}
else if($2 == 13)
{$1 = $1 / $3;}
else if($2 == 14)
{$1 = $1 % $3;}
}
| immutable inc_dec {
if ($2 == 16)
{
$1=$1+1;
intArr[$1] = $1;
}
else if($2 == 17)
intArr[$1]=intArr[$1]-1;
}
| simpleexpression {$$ = $1;} ;
inc_dec : INC {$$=$1;}
| DEC {$$=$1;};
simpleexpression : simpleexpression OR_B_RELOP andexpression {if($1 || $3)
$$ = true;
}
| andexpression{$$=$1;} ;
andexpression : andexpression AND_B_RELOP unaryrelexpression {if($1 && $3)
$$ = true;
}
| unaryrelexpression{$$=$1;} ;
unaryrelexpression : NOT_B_RELOP unaryrelexpression {if(!$2)
$$ = true;
}
| relexpression {$$=$1;} ;
relexpression : relexpression RELOP sumexpression { if($2 == 4)
{
if($1 != $3)
$$ = true;
}
else if($2 == 5)
{
if($1 >= $3)
$$ = true;
}
else if($2 == 6)
{
if($1 <= $3)
$$ = true;
}
else if($2 == 7)
{
if($1 > $3)
$$ = true;
}
else if($2 == 8)
{
if($1 < $3)
$$ = true;
}
else if($2 == 9)
{
if($1 == $3)
$$ = true;
}
}
| sumexpression {$$=$1;} ;
sumexpression : sumexpression SUMOP term {if($2 == 43)
{$$ = $1 + $3;}
else if($2 == 45)
{$$ = $1 - $3;}
}
| term {$$=$1;}
;
term : term MULOP unaryexpression {if($2 == 42)
{$$ = $1 * $3;}
else if($2 == 47)
{$$ = $1 / $3;}
else if($2 == 37)
{$$ = $1 % $3;}
}
| unaryexpression {$$=$1;}
;
unaryexpression : UNARYOP unaryexpression {$$=$2;}
| factor {$$=$1;}
;
factor : immutable {$$=$1;}
| mutable {$$=$1;}
;
immutable : ID {$$=intArr[$1];}
;
mutable : '(' expression ')' {$$=$2;}
| CONSTANT {$$=$1;}
| call
| NUM {$$=$1;}
| CHAR_CONSTANT {$$=$1;}
| TRUE_ {$$ = $1;}
| FALSE_ {$$ =$1;}
;
call : ID '(' args ')' {} ;
args : arglist
|
;
arglist : arglist ',' expression
| expression
;
statement : expressionstmt //{$$ = $1;
//printf("e--%d\n",$$);}
| iterationstmt //{$$ = $1;}
| selectionstmt {$$ = $1;}
| compoundstmt // {$$ = $1;}
| returnstmt {$$ = $1;}
| breakstmt {$$ = $1;}
| continuestmt {$$ = $1;}
| labeledstmt {$$ = $1;}
| typecaststatement {$$ = $1;}
| blockstmt {$$ = $1;}
| outputstatement {$$ = $1;}
| typedefstatement {$$ = $1;}
| inputstatement {$$ = $1;}
| local_declaration {$$ = $1;}
| funccallstmt {$$ = $1;}
;
funccallstmt : funcid_list EQUALTO call
|func_var_dec_list EQUALTO call ;
funcid_list : ID | funcid_list ',' ID ;
func_var_dec_list : func_var_dec_list ',' datatype ID
| datatype funcid_list ;
returnstmt : RETURN ';' | RETURN expression ';' | RETURN args ';'
;
breakstmt : BREAK ';'
| BREAK ID ';'
;
continuestmt : CONTINUE ';' ;
selectionstmt : IF '(' simpleexpression ')' statement
| IF '(' simpleexpression ')' statement ELSE statement
| IF '(' simpleexpression ')' statement ELIF '(' simpleexpression ')'statement ELSE statement
| SWITCH '(' simpleexpression ')' statement
;
labeledstmt : CASE '(' simpleexpression ')' statement labeledstmt
| DEFAULT statement
| CASE '(' simpleexpression ')' statement ;
compoundstmt : ':' statementlist END //{$$ = $2;
//printf("c--%d\n",$$);}
| ':' END;
local_declaration : datatype init_dec_list
;
typecaststatement : datatype ID EQUALTO
datatype '(' simpleexpression')' ';'
| ID EQUALTO datatype '(' simpleexpression ')' ';'
;
blockstmt : ID compoundstmt ;
statementlist : statementlist statement //{$$ = $2; printf("s--%d\n",$$);}
| statement //{$$ = $1;};
iterationstmt : WHILE '(' simpleexpression ')' statement
| DO statement WHILE '(' simpleexpression ')' ';'
| FOR '(' for_datatype_dec ID EQUALTO NUM ':'
NUM ':' NUM ')' statement
;
for_datatype_dec : datatype
|
;
expressionstmt : expression ';' {$$ = $1 ;}
| ';'
;
typedefstatement : STRING ID EQUALTO TYPEDEF '(' ID ')' ';'
| ID EQUALTO TYPEDEF '(' ID ')' ';' ;
outputstatement : PRINT '(' outvariable ')' ';' {printf("%d",$3);}
| PRINTLN '(' outvariable ')' ';' {printf("%d\n",$3);};
outvariable : outvariable ',' value |
value {$$=$1;}
| expression {$$=$1;}
;
value : ID {$$=$1;}
;
inputstatement : ID EQUALTO INPUT '(' input_value ')' ';'
input_value : CONSTANT {$$ = $1;}
|
;
%%
void yyerror(char *msg)
{
printf("invalid string \n");
exit(0);
}
int main()
{
yyin=fopen("code.c","r");
yyparse();
}