-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.c
198 lines (194 loc) · 4.56 KB
/
parse.c
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
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
#include "stack.h"
#include "parse.h"
#include "handle.h"
// this function give the periority to each group of operators
int priority(char op)
{
switch (op)
{
case '+':
case '-': return 0;
case '*':
case '/':
case '%': return 1;
case '^': return 2;
default : return -1;
}
}
//this function tests which of two fuctions have high priority
int tpriority(char op1 , char op2 )
{
int p1 = priority(op1), p2 = priority(op2);
if(p1 == -1 || p2 == -1) return -1;
else if(p1>p2) return 1;
else if(p2>p1) return 2;
else return 0;
}
// this function converts string number of defined length
//into int intiger
int stoi(char *number, int len)
{
int i;
char temp[len];
for(i=0;i<len;i++) temp[i] = number[i];
return atoi(temp);
}
//this function takes 2 integers and operation and perform it
int operate (int y, int x, int z)
{
switch (z)
{
case '+': return x+y;
case '-': return x-y;
case '*': return x*y;
case '/': return x/y;
case '%': return x%y;
case '^': return pow(x,y);
default : return 0;
};
}
int parse( char *inst, int *sol )
{
//define the 2 stacks one for operators and one for operand
operation *op = NULL;
operand *num = NULL;
int len = strlen(inst), start = 0, end = 0;
// test for empy expression, or expression ends with operator,
// or begin with operator, this is only done once during parsing
if(priority(inst[len-1])!=-1)
{
printf("Error: Unexpected operator \'%c\'\n",inst[len-1]);
mkoperandempty(&num);
mkoperationemtpy(&op);
return 1;
}
if(priority(inst[0])>0)
{
printf("Error: Unexpected operator \'%c\'\n",inst[0]);
mkoperandempty(&num);
mkoperationemtpy(&op);
return 1;
}
if(isempty(inst)) return 1;
while(end<=len)
{
//handles brackets
if(inst[end] == ')')
{
puts("Error: Unbalanced or unexpected parenthesis or bracket");
mkoperandempty(&num);
mkoperationemtpy(&op);
return 1;
}
if(inst[end] =='(')
{
start=end+1;
int bracketcounter =1;
while(bracketcounter != 0)
{
end++;
if(inst[end] == '(') bracketcounter++;
else if(inst[end] == ')' ) bracketcounter--;
if(end ==len)
{
puts("Error: Unbalanced or unexpected parenthesis or bracket");
mkoperandempty(&num);
mkoperationemtpy(&op);
return 1;
}
}
inst[end] ='\0';
int temp;
if(parse(&inst[start],&temp)!=0)
{
mkoperandempty(&num);
mkoperationemtpy(&op);
return 1 ;
}
push_num( temp ,&num);
start=end;
end++;
continue;
}
//handles end of expression
if( endofexpression( inst, &num, &op, &end ) ==1 ) break;
//handle digits
else if( isdigit(inst[end]) )
{
start =end;
while( isdigit( inst[end] ) ) end++;
push_num( stoi(&inst[start] , end-start) , &num );
}
/***********************************************************************/
else
{
//check if the operator is invalid
if( priority(inst[end]) == -1 )
{
printf("Error: Undefined function or variable \'%c\'\n",inst[end] );
mkoperandempty(&num);
mkoperationemtpy(&op);
return 1;
}
// unary positive and plus overriding
if(inst[end] == '+' &&(end ==0 || priority(inst[end-1]) !=-1 ) )
{
end++;
start =end;
if( priority( inst[end] )!= -1 )
{
printf("Error: Unexpected operator \'%c\'\n",inst[end]);
mkoperandempty(&num);
mkoperationemtpy(&op);
return 1;
}
while( isdigit( inst[end] ) ) end++;
push_num( stoi(&inst[start] , end-start) , &num );
continue;
}
// unary negative and minus overriding
else if(inst[end] == '-' &&(end ==0 || priority(inst[end-1]) !=-1 ) )
{
end++;
start = end;
if( priority( inst[end] )!= -1 )
{
printf("Error: Unexpected operator \'%c\'\n",inst[end]);
mkoperandempty(&num);
mkoperationemtpy(&op);
return 1;
}
while( isdigit( inst[end] ) ) end++;
push_num( -1*stoi(&inst[start] , end-start) , &num );
continue;
}
else if( operationempty( op ) ) push_operation ( inst[end] , &op );
else
{
if( priority( inst[end-1] )!= -1 )
{
printf("Error: Unexpected operator \'%c\'\n",inst[end]);
mkoperandempty(&num);
mkoperationemtpy(&op);
return 1;
}
while(!operationempty( op ))
{
if( tpriority( op->operation, inst[end] )>1 ) break;
int a = pop_num(&num), b = pop_num(&num);
int y = operate( a, b , pop_operation(&op) );
push_num(y,&num);
}
push_operation(inst[end], &op);
}
end++;
}
}
*sol = pop_num(&num);
return 0;
}