-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.y
96 lines (88 loc) · 2.54 KB
/
project.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
%{
#include <stdio.h>
#include <stdlib.h>
int yylex(void);
int yyerror(const char *s);
int flag=0;
float tmp[3];
float ans=0.0;
%}
%union
{
float number;
}
%token <number> NUM FLOAT
%type <number> A B C
%token KEYWORD PUNCTUATION OPERATOR
%%
S : A B C D { printf("===Entered Query is CORRECTLY WRITTEN!===\n");
if(flag==0){printf ("RESULT = %.3f\n",ans);} return 0;}
;
A : '+' NUM KEYWORD NUM PUNCTUATION { $$ = $2 + $4;
tmp[0]=$$;
printf("ENTERED FIRST EXPRESSION IS: %f + %f = %f \n",$2,$4,tmp[0]);
}
| '-' NUM KEYWORD NUM PUNCTUATION { $$ = $2 - $4;
tmp[0]=$$;
printf("ENTERED FIRST EXPRESSION IS: %f - %f = %f\n",$2,$4,tmp[0]);
}
| '*' NUM KEYWORD NUM PUNCTUATION { $$ = $2 * $4;
tmp[0]=$$;
printf("ENTERED FIRST EXPRESSION IS: %f * %f = %f\n",$2,$4,tmp[0]);
}
| '/' NUM KEYWORD NUM PUNCTUATION {
if($4==0){printf("===ERROR: DIVIDE BY ZERO \n CANNOT CALCULATE RESULT!==\n"); flag=1; }
else{
$$=$2/$4;
tmp[0]=$$;
printf("ENTERED FIRST EXPRESSION IS: %f / %f = %f\n",$2,$4,tmp[0]); }
}
;
B : KEYWORD '+' NUM KEYWORD NUM PUNCTUATION { $$ = $3 + $5;
tmp[1]=$$;
printf("ENTERED SECOND EXPRESSION IS: %f + %f = %f\n",$3,$5,tmp[1]);
}
| KEYWORD '-' NUM KEYWORD NUM PUNCTUATION { $$ = $3 - $5;
tmp[1]=$$;
printf("ENTERED SECOND EXPRESSION IS: %f - %f = %f\n",$3,$5,tmp[1]);
}
| KEYWORD '*' NUM KEYWORD NUM PUNCTUATION { $$ = $3 * $5;
tmp[1]=$$;
printf("ENTERED SECOND EXPRESSION IS: %f * %f = %f\n",$3,$5,tmp[1]);
}
| KEYWORD '/' NUM KEYWORD NUM PUNCTUATION { if($5==0){printf("===ERROR: DIVIDE BY ZERO \n CANNOT CALCULATE RESULT!==\n"); flag=1; }
else{
$$=$3/$5;
tmp[1]=$$;
printf("ENTERED SECOND EXPRESSION IS: %f / %f = %f\n",$3,$5,tmp[1]); }
}
;
C : KEYWORD KEYWORD '+' KEYWORD KEYWORD KEYWORD PUNCTUATION {
ans = tmp[0] + tmp[1];
printf("THIRD OPERATION PROVIDED IS + \n");
}
| KEYWORD KEYWORD '-' KEYWORD KEYWORD KEYWORD PUNCTUATION {
ans = tmp[0] - tmp[1];
printf("THIRD OPERATION PROVIDED IS - \n");
}
| KEYWORD KEYWORD '*' KEYWORD KEYWORD KEYWORD PUNCTUATION {
ans = tmp[0] * tmp[1];
printf("THIRD OPERATION PROVIDED IS * \n");
}
| KEYWORD KEYWORD '/' KEYWORD KEYWORD KEYWORD PUNCTUATION {
if(tmp[1]==0){printf("===ERROR: DIVIDE BY ZERO \n CANNOT CALCULATE RESULT!==\n"); flag=1; }
else{
ans = tmp[0]/tmp[1];
printf("THIRD OPERATION PROVIDED IS / /n"); }
}
;
D : KEYWORD KEYWORD KEYWORD PUNCTUATION
;
%%
int main()
{
printf("Enter Your Query : \n");
yyparse();
printf("\nSUCCESSFULLY EXECTUED PROGRAM! \n PROGRAM TERMINATING...\n");
return 0;
}