-
Notifications
You must be signed in to change notification settings - Fork 0
/
PirateScript.y
378 lines (297 loc) · 8.82 KB
/
PirateScript.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
%{
#include<stdio.h>
#include <math.h>
int cnt=1,cntt=0,val,track=0;
typedef struct entry {
char *str;
int n;
}storage;
storage store[1000],sym[1000];
void insert (storage *p, char *s, int n);
int cnt2=1;
void insert2 (storage *p, char *s, int n);
#define pi 3.1416
%}
%union
{
int number;
char *string;
}
%token <number> NUM
%token <string> VAR
%token <string> IF Else_If ELSE Main_Function INT FLOAT DOUBLE CHAR LeftBracket RightBracket LeftSecondBracket RightSecondBracket COMMA Statement PLUS MINUS MULT DIV POW FACT ASSIGN FOR COL WHILE BREAK COLON DEFAULT CASE SWITCH inc dec not function LOGIC
%type <string> statement
%type <number> expression
%type <number> switch_expression
%nonassoc IFX
%nonassoc ELIFX
%nonassoc ELSE
%left LT GT
%left PLUS MINUS
%left MULT DIV
%left FACT
%left POW
%%
program: Main_Function LeftBracket RightBracket LeftSecondBracket cstatement RightSecondBracket { printf("\n-------------Successful compilation-----------------\n"); }
;
cstatement:
| cstatement statement
| cdeclaration
;
cdeclaration: TYPE ID1 Statement { printf("\nvalid declaration of variable\n"); }
;
TYPE : INT
| FLOAT
| CHAR
;
ID1 : ID1 COMMA VAR {
if(number_for_key($3))
{
printf("%s is already declared\n", $3 );
}
else
{
insert(&store[cnt],$3, cnt);
cnt++;
}
}
|VAR {
if(number_for_key($1))
{
printf("%s is already declared\n", $1 );
}
else
{
insert(&store[cnt],$1, cnt);
cnt++;
}
}
;
statement: Statement
| SWITCH LeftBracket switch_expression RightBracket LeftSecondBracket BASE RightSecondBracket {printf("Code_Book(SWITCH) and Pirate_Code(case).\n");}
| expression Statement { printf("\nvalue of expression: %d\n", ($1)); }
| VAR ASSIGN expression Statement {
if(number_for_key($1)){
int i = number_for_key2($1);
if (!i){
insert(&sym[cntt], $1, $3);
cntt++;
}
sym[i].n = $3;
printf("\n(%s) Value of the variable: %d\t\n",$1,$3);
}
else {
printf("%s not declared yet\n",$1);
}
}
| IF LeftBracket expression RightBracket LeftSecondBracket expression Statement RightSecondBracket %prec IFX {
if($3)
{
printf("\nvalue of expression in Parley (if): %d\n",($6));
}
else
{
printf("\ncondition value zero in Parley(if) block\n");
}
}
| IF LeftBracket expression RightBracket LeftSecondBracket expression Statement RightSecondBracket ELSE LeftSecondBracket expression Statement RightSecondBracket {
if($3)
{
printf("\nvalue of expression in Parley (if): %d\n",$6);
}
else
{
printf("\nvalue of expression in Aye (else): %d\n",$11);
}
}
| IF LeftBracket expression RightBracket LeftSecondBracket IF LeftBracket expression RightBracket LeftSecondBracket expression Statement RightSecondBracket ELSE LeftSecondBracket expression Statement RightSecondBracket expression Statement RightSecondBracket ELSE LeftSecondBracket expression Statement RightSecondBracket %prec IFX {
if($3)
{
if($8)
printf("\nvalue of expression middle Parley (if): %d\n",$11);
else
printf("\nvalue of expression middle Aye (else): %d\n",$16);
printf("\nvalue of expression in first Parley (if): %d\n",$19);
}
else
{
printf("\nvalue of expression in Aye (else): %d\n",$24);
}
}
| IF LeftBracket expression RightBracket LeftSecondBracket expression Statement RightSecondBracket Else_If LeftBracket expression RightBracket LeftSecondBracket expression Statement RightSecondBracket ELSE LeftSecondBracket expression Statement RightSecondBracket {
if($3)
{
printf("\nvalue of expression in Parley (if): %d\n",$6);
}
else if($11)
{
printf("\nvalue of expression in ElseWise (Else_If): %d\n",$14);
}
else
{
printf("\nvalue of expression in Aye (else): %d\n",$19);
}
}
| FOR LeftBracket NUM COL NUM RightBracket LeftSecondBracket expression RightSecondBracket {
int i=0;
for(i=$3;i<$5;i++){
printf("Shanty(for) loop statement\n");
}
}
| WHILE LeftBracket NUM GT NUM RightBracket LeftSecondBracket expression RightSecondBracket {
int i;
printf("Whale(While) Loop: ");
for(i=$3;i<=$5;i++)
{
printf("%d ",i);
}
printf("\n");
printf("value of the expression: %d\n",$8);
}
/*------function begin-----------*/
| function func
;
func : COL TYPE LeftBracket RightBracket LeftSecondBracket statement RightSecondBracket
{
printf("crew(function) is declared\n");
}
;
/*-------function end------------*/
BASE : Bas
| Bas Dflt
;
Bas : /*NULL*/
| Bas Cs
;
Cs : CASE NUM COL expression Statement {
if(val==$2){
track=1;
printf("\nPirate_Code(Case) No : %d and Result : %d\n",$2,$4);
}
}
;
Dflt : DEFAULT COL expression Statement {
if(track!=1){
printf("\nResult in default Value is : %d\n",$3);
}
track=0;
}
;
expression: NUM { $$ = $1; }
| VAR { $$ = number_for_key2($1); printf("Variable value: %d",$$);}
| expression PLUS expression { $$ = $1 + $3; }
| expression MINUS expression { $$ = $1 - $3; }
| expression MULT expression { $$ = $1 * $3; }
| expression DIV expression { if($3)
{
$$ = $1 / $3;
}
else
{
$$ = 0;
printf("\nWarning: division by zero\t");
}
}
| expression POW expression { $$ = pow($1,$3); }
| expression FACT {
int mult=1 ,i;
for(i=$1;i>0;i--)
{
mult=mult*i;
}
$$=mult;
}
| expression LT expression { $$ = $1 < $3; }
| expression GT expression { $$ = $1 > $3; }
| LeftBracket expression RightBracket { $$ = $2; }
| inc expression inc { $$=$2+1; printf("⚓️++ (increment operator): %d\n",$$);}
| dec expression dec { $$=$2-1; printf("⚓️-- (decrement operator): %d\n",$$);}
| not expression not {
if($2 != 0)
{
$$ = 0; printf("!⚓️ (not operator): %d\n",$$);
}
else{
$$ = 1 ; printf("aff: %d\n",$$);
}
}
;
switch_expression: NUM { $$ = $1; val = $$; }
| VAR { $$ = number_for_key2($1); val = $$;}
| switch_expression PLUS switch_expression { $$ = $1 + $3; val = $$; }
| switch_expression MINUS switch_expression { $$ = $1 - $3; val = $$; }
| switch_expression MULT switch_expression { $$ = $1 * $3; val = $$;}
| switch_expression DIV switch_expression { if($3)
{
$$ = $1 / $3; val = $$;
}
else
{
$$ = 0;
val = $$;
}
}
| switch_expression POW switch_expression { $$ = pow($1,$3); val = $$;}
| switch_expression FACT {
int mult=1 ,i;
for(i=$1;i>0;i--)
{
mult=mult*i;
}
$$=mult; val = $$;
}
| switch_expression LT switch_expression { $$ = $1 < $3; val = $$; }
| switch_expression GT switch_expression { $$ = $1 > $3; val = $$;}
| LeftBracket switch_expression RightBracket { $$ = $2; val = $$;}
| inc switch_expression inc { $$=$2+1; printf("⚓️++ (increment operator): %d\n",$$); val = $$;}
| dec switch_expression dec { $$=$2-1; printf("⚓️-- (decrement operator): %d\n",$$); val = $$;}
| not switch_expression not {
if($2 != 0)
{
$$ = 0; val = $$;
}
else{
$$ = 1 ; val = $$;
}
}
;
%%
void insert(storage *p, char *s, int n)
{
p->str = s;
p->n = n;
}
int number_for_key(char *key)
{
int i = 1;
char *name = store[i].str;
while (name) {
if (strcmp(name, key) == 0)
return store[i].n;
name = store[++i].str;
}
return 0;
}
void insert2 (storage *p, char *s, int n)
{
p->str = s;
p->n = n;
}
int number_for_key2(char *key)
{
int i = 1;
char *name = sym[i].str;
while (name) {
if (strcmp(name, key) == 0)
return i;
name = sym[++i].str;
}
return 0;
}
int yywrap()
{
return 1;
}
yyerror(char *s){
printf( "%s\n", s);
}