-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcalc.y
207 lines (167 loc) · 3.16 KB
/
calc.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
%{
/* Yacc program for calculator (using expression syntax tree) */
/*Node type constants*/
#define EXPR 11
#define PLUS 22
#define MINUS 33
#define MUL 44
#define DIV 55
#define REM 66
#define NEG 77
#define POW 88
#define NUM 99
/*Header files*/
#include<stdio.h>
#include<stdlib.h>
/*Data structure of a binary tree (which will be used to implement the expression syntax tree)*/
struct node
{
int type;
int num;
struct node *left;
struct node *right;
};
/*Function declarations : */
/*To report an error */
void yyerror(char *);
/*To calculate power*/
double power(int a, int b);
/*To make a leaf in the tree*/
struct node* makeLeaf(int type,int num);
/*To make a node in the tree*/
struct node* makenode(int type,struct node *left, struct node*right);
/*To recursively descend the tree and calculate the value of the expression*/
int calculate(struct node *t);
%}
%union
{
struct node *ptr;
int val;
};
%token <val> NUMBER
%token END
%left '+' '-'
%left '*' '/'
%left '%'
%nonassoc '^'
%nonassoc UMINUS
%type <ptr> expr
%start program
%%
program : expr END {
int ans = calculate($1);
printf("\nResult : %d\n",ans);
exit(1);
}
;
expr : expr '+' expr {$$=makenode(PLUS,$1,$3); }
| expr '-' expr {$$=makenode(MINUS,$1,$3); }
| expr '*' expr {$$=makenode(MUL,$1,$3); }
| expr '/' expr {$$=makenode(DIV,$1,$3); }
| expr '%' expr {$$=makenode(REM,$1,$3); }
| '(' expr ')' {$$=$2; }
| '-' expr %prec UMINUS {$$=makenode(NEG,$2,NULL); }
| expr '^' expr {$$=makenode(POW,$1,$3); }
| NUMBER {$$=makeLeaf(NUM,$1); }
;
%%
#include "lex.yy.c"
double power(int a,int b)
{
int i;
double c=1;
for(i=0;i<b;i++)
c=c*a;
return c;
}
struct node* makeLeaf(int type,int num)
{
struct node *ptr=malloc(sizeof(struct node));
ptr->type=type;
ptr->num=num;
ptr->left=NULL;
ptr->right=NULL;
return ptr;
}
struct node *makenode(int type,struct node *left, struct node*right)
{
struct node *ptr=malloc(sizeof(struct node));
ptr->type=type;
ptr->left=left;
ptr->right=right;
return ptr;
}
int calculate(struct node *t)
{
if(t!=NULL)
{
int ret;
if(t->type==PLUS)
{
int a = calculate(t->left);
int b = calculate(t->right);
ret = a+b;
}
else if(t->type==NEG)
{
int temp = calculate(t->left);
temp = temp * -1;
ret = temp;
}
else if(t->type==MINUS)
{
int a = calculate(t->left);
int b = calculate(t->right);
ret = a-b;
}
else if(t->type==MUL)
{
int a = calculate(t->left);
int b = calculate(t->right);
ret = a*b;
}
else if(t->type==DIV)
{
int a = calculate(t->left);
int b = calculate(t->right);
ret = a/b;
}
else if(t->type==REM)
{
int a = calculate(t->left);
int b = calculate(t->right);
ret = a%b;
}
else if(t->type==POW)
{
int a = calculate(t->left);
int b = calculate(t->right);
ret = (int)power(a,b);
}
else if(t->type==EXPR)
{
int a=calculate(t->left);
ret = a;
}
else if(t->type==NUM)
{
ret = t->num;
}
return ret;
}
else
return 0;
}
void yyerror(char *s)
{
fprintf(stderr, "%s\n", s);
}
int yywrap(void)
{
return 1;
}
int main()
{
yyparse();
return 0;
}