-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.c
More file actions
266 lines (252 loc) · 8.65 KB
/
parser.c
File metadata and controls
266 lines (252 loc) · 8.65 KB
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
#include "parser.h"
int main()
{
char* str = "x^2 + y^2 <= z^2 & z <= 10 & 0 <= z";
token* tokens;
int size = parseRule(str, &tokens);
printTokens(tokens, size);
tokenQueue queue;
initialize_queue(&queue);
convertToPolish(&queue, tokens, size);
printf("\n\n");
print_queue(&queue);
printf("\n\n");
free(tokens);
point p = {1 , 2 , 11};
double res = evalPolish(&queue, p);
printf("%g\n", res);
//Sleep(100000);
return 0;
}
void printTokens(token* tokens, int size)
{
int i = 0;
for(; i< size; i++)
{
switch(tokens[i].type)
{
case T_NUMBER:
printf("Type: 1, Value: %g\n" , tokens[i].value.num);
break;
case T_OPERATOR:
printf("Type: %d, Value: %c\n" , tokens[i].type, tokens[i].value.op->symbol);
break;
case T_VARIABLE:
printf("Type: %d, Value: %c\n" , tokens[i].type, tokens[i].value.var);
break;
}
}
}
//Impelemts Shunting yard algo with reverse polish notation
void convertToPolish(tokenQueue* queue, token* tokens, int size)
{
tokenStack opstack;
initialize_stack(&opstack);
token temp;
int i;
for(i = 0; i < size; i++)
{
switch(tokens[i].type)
{
case T_NUMBER: case T_VARIABLE:
enqueue(queue, tokens[i]);
break;
case T_OPERATOR:
// IF operator stack is empty, just push the operator OR IF token is '(', just push it
if(peek(&opstack, &temp) == NOT_OK || tokens[i].value.op->symbol == '(')
{
push(&opstack, tokens[i]);
}
// IF token is ')', pop all operators to the output queue and throw the '('
//
else if(tokens[i].value.op->symbol == ')')
{
while(peek(&opstack,&temp) == OK && temp.value.op->symbol != '(')
{
pop(&opstack,&temp);
enqueue(queue,temp);
}
pop(&opstack,&temp);//Throw "Open bracket" token
//IF first operator after brackets is unary, push it to the queue
if(peek(&opstack, &temp) == OK && temp.value.op->isUnary)
{
pop(&opstack,&temp);
enqueue(queue,temp);
}
}
// for all other operators w\o any special case
else
{
// WHILE the next operator is of lower precedence, pop all non '(' operators to output queue
while(peek(&opstack,&temp) == OK && temp.value.op->precedence > tokens[i].value.op->precedence && temp.value.op->symbol != '(')
{
pop(&opstack,&temp);
enqueue(queue, temp);
}
push(&opstack,tokens[i]);
}
break;
}
}
// after going through the whole token array; just pop all operators to the output queue
while(pop(&opstack, &temp) != NOT_OK)
{
enqueue(queue, temp);
}
free_stack(&opstack);
}
//Takes equation in str and tokenizes to numbers, operators, brackets, order signs, variables
int parseRule(char* rule, token** res)
{
token* tokens = (token*)malloc(sizeof(token)), *temp;
int index_t = 0, index_num = 0;
char temp_num[30];
while(*rule != '\0')
{
temp = (token*)realloc(tokens, (index_t + 1)*sizeof(token));
if(temp == NULL)
{
free(tokens);
printf("ERROR allocating memory");
exit(1);
}
tokens = temp;
while(*rule == ' ' || *rule == ',') rule++; //Ignores whitespace and commas
//If the char is a digit- must be part of a num so evaluates the decimal number
if(isdigit(*rule))
{
char* start_num = rule; //save start of num
do
{
index_num++;
rule++;
} while(isdigit(*rule) || *rule == '.'); //continue going through the string while chars represent a number
strncpy(temp_num, start_num, index_num); // copies only part of the string: the number
temp_num[index_num] = '\0'; // Null terminate the end
index_num = 0; //Reset for next num
tokens[index_t].type = T_NUMBER;
tokens[index_t].value.num = atof(temp_num);
}
//if the char is a single-char operator
else if(strchr("+/-*^()=&|",*rule) != NULL)
{
tokens[index_t].type = T_OPERATOR;
tokens[index_t].value.op = getop(*rule);
rule++;
}
//if the char is a variable
else if(strchr("xyz",*rule) != NULL)
{
tokens[index_t].type = T_VARIABLE;
tokens[index_t].value.var = *rule;
rule++;
}
//if the char is < , >
else if(strchr("><",*rule) != NULL)
{
if(*rule + 1 == '=')
{//if the operator is <= or >=
tokens[index_t].type = T_OPERATOR;
tokens[index_t].value.op = getop('.');
rule += 2;
}
else
{
tokens[index_t].type = T_OPERATOR;
tokens[index_t].value.op = getop(*rule);
rule++;
}
}
//if the char is a multi-char operator (CURRENTLY ONLY: abs, max, min)
else if(strstr(rule, "max") == &(*rule))
{//TODO THINK OF SOMETHING BETTER
tokens[index_t].type = T_OPERATOR;
tokens[index_t].value.op = getop('M');
rule = rule + 3;
}
else if(strstr(rule, "min") == &(*rule))
{
tokens[index_t].type = T_OPERATOR;
tokens[index_t].value.op = getop('m');
rule = rule + 3;
}
else if(strstr(rule, "abs") == &(*rule))
{
tokens[index_t].type = T_OPERATOR;
tokens[index_t].value.op = getop('a');
rule = rule + 3;
}
else
{//Edge case
printf("illegal char %c" , *rule);
exit(1);
}
index_t++;
}
*res = tokens;
return index_t;
}
//Takes Reverse polish equation and point p and evaluates the equation with respect to p's coords
double evalPolish(tokenQueue* queue, point p)
{//Does not overwrite tokenQueue* queue; so can be reused
tokenStack numstack;
initialize_stack(&numstack);
tokenNode *head = queue->head;
int size = queue->size ,res;
while(head != NULL)
{
switch(head->_token.type)
{
//If the token is just a number; push it to the numstack
case(T_NUMBER):
push(&numstack, head->_token);
break;
//If the token is a variable; convert it to num and push it to the numstack
case(T_VARIABLE):
switch(head->_token.value.var)
{
case 'x':
token x;
x.value.num = p.x;
x.type = T_NUMBER;
push(&numstack, x);
break;
case 'y':
token y;
y.value.num = p.y;
y.type = T_NUMBER;
push(&numstack, y);
break;
case 'z':
token z;
z.value.num = p.z;
z.type = T_NUMBER;
push(&numstack, z);
break;
}
break;
case(T_OPERATOR):
token L,R,result;
if(!(head->_token.value.op->isUnary))//NOT UNARY
{
pop(&numstack, &R);
pop(&numstack, &L);
result.value.num = head->_token.value.op->evalfunc(L.value.num, R.value.num);
result.type = T_NUMBER;
push(&numstack,result);
}
else //YES UNARY
{
pop(&numstack, &L);
result.value.num = head->_token.value.op->evalfunc(L.value.num, 0);
result.type = T_NUMBER;
push(&numstack,result);
}
break;
}
head = head->next;
}
res = numstack.tokenTop->_token.value.num;
free_stack(&numstack);
return res;
}