-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.c
354 lines (306 loc) · 8.89 KB
/
parser.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
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
/*
* parser.c - recursive descent parser for a simple expression language.
* Most of the functions in this file model a Non-terminal in the
* grammar listed below
* Author: William Kreahling and Mark Holliday and Gajjan Jasani
* Date: Modified 9-29-08 and 3-25-15 and 4-12-2016
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include "parser.h"
/* Global declarations */
char *next_token; // hold next token of expression
extern char* get_token();
/*
* <bexpr> ::= <expr> ;
* <expr> ::= <term> <ttail>
* <ttail> ::= <add_sub_tok> <term> <ttail> | e
* <term> ::= <stmt> <stail>
* <stail> ::= <mult_div_tok> <stmt> <stail> | e
* <stmt> ::= <factor> <ftail>
* <ftail> ::= <compare_tok> <factor> <ftail> | e
* <factor> ::= <expp> ^ <factor> | <expp>
* <expp> ::= ( <expr> ) | <num>
* <add_sub_tok> ::= + | -
* <mul_div_tok> ::= * | /
* <compare_tok> ::= < | > | <= | >= | ! = | ==
* <num> ::= {0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9}+
*/
/*
* bexpr : Application of rule <bexpr> -> <expr>
* arg : token - token needing to be parsed
* Return: return value of expr function
*/
int bxper(char *token){
return expr(token);
}
/*
* expr : Application of rule <expr> -> <term> <ttail>
* arg : token - token needing to be parsed
* Return: return value of ttail function if not error
*/
int expr(char *token){
int subtotal = term(token);
if (subtotal == ERROR)
return subtotal;
else
return ttail(next_token, subtotal);
}
/*
* term : Application of rule <expr> -> <term> <ttail>
* arg : token - token needing to be parsed
* Return: return value of stail function if not error
*/
int term(char *token){
int subtotal = stmt(token);
if (subtotal == ERROR)
return subtotal;
else
return stail(next_token, subtotal);
}
/*
* stmt : Application of rule <stmt> -> <factor> <ftail>
* arg : token - token needing to be parsed
* Return: return value of ftail function if not error
*/
int stmt(char *token){
int subtotal = factor(token);
if (subtotal == ERROR)
return subtotal;
else
return ftail(next_token, subtotal);
}
/*
* factor: Application of rule <factor> -> <expp> ^ <factor> | <expp>
* arg : token - token needing to be parsed
* Return: return result of exponentiation/return val of expp() if not error
*/
int factor(char* token){
int subtotal = expp(token);
if (subtotal != ERROR){
if(!strncmp(next_token, "^", 1)){
expon_tok(token);
return ( exponentiation(subtotal, factor(next_token)));
} else {
return subtotal;
}
} else{
return ERROR;
}
}
/*
* expp : Application of rule <expp> -> ( <expr> ) | <num>
* arg : token - token needing to be parsed
* Return: return result of expression in ()/return val of num() if not error
*/
int expp(char *token){
int subtotal;
if(!strncmp(token, "(", 1)){
next_token = get_token();
subtotal = expr(next_token);
if(!strncmp(next_token, ")", 1)){
next_token = get_token();
return subtotal;
} else {
printf("===> ')' expected\n");
printf("Syntax Error");
exit(0);
}
} else {
return num(token);
}
return 0;
}
/*
* num : Application <num> -> {0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9}+
* arg : token - token needing to be parsed
* Return: return val of is_number function
*/
int num(char *token){
int subtotal = is_number(token);
return subtotal;
}
/*
* is_number : helper function for num()
* Checks if the token needing to be parsed is a number or not
* if it is a number, converts it to a number
* arg : token - token needing to be parsed
* Return: return token as an int if token is a number else error
*/
int is_number(char *token){
int subtotal;
if(isdigit(*token)){
subtotal = strtol(token, &token, 10);
next_token = get_token();
return subtotal;
} else {
return ERROR;
}
}
/*
* add_sub_tok : Application of rule <add_sub_tok> -> + | -
* Consumes + | - and updates next_token
* arg : token - token needing to be parsed
*/
void add_sub_tok(char *token){
next_token = get_token();
}
/*
* mul_div_tok : Application of rule <mul_div_tok> -> * | /
* Consumes * | / and updates next_token
* arg : token - token needing to be parsed
*/
void mul_div_tok(char *token){
next_token = get_token();
}
/*
* compare_tok : Application of <compare_tok> -> < | > | <= | >= | ! = | ==
* Consumes < | > | <= | >= | ! = | == and updates next_token
* arg : token - token needing to be parsed
*/
void compare_tok(char *token){
next_token = get_token();
}
/*
* expon_tok : Helper function for factor()
* Consumes ^ and updates next_token
* arg : token - token needing to be parsed
*/
void expon_tok(char *token){
next_token = get_token();
}
/*
* expon_tok : Helper function for factor()
* instead of using Math.Pow() this works with integers
* instead of just double type variables
* arg : base, exponent for exponentiation
* Return: result of exponentiation of base and exponent
*/
int exponentiation(int base, unsigned int exp){
int i, result = 1;
for (i = 0; i < exp; i++){
result *= base;
}
return result;
}
/*
* ttail : Application of rule <ttail> -> <add_sub_tok> <term> <ttail> | e
* arg : token - token needing to be parsed
* Return: return recursive result of itself
*/
int ttail(char *token, int subtotal){
int term_value;
if (!strncmp(token, "+", 1)){
add_sub_tok(token);
term_value = term(next_token);
// if term returned an error, give up otherwise call ttail
if (term_value == ERROR)
return term_value;
else
return ttail(next_token, (subtotal + term_value));
}
else if(!strncmp(token, "-", 1)){
add_sub_tok(token);
term_value = term(next_token);
// if term returned an error, give up otherwise call ttail
if (term_value == ERROR)
return term_value;
else
return ttail(next_token, (subtotal - term_value));
}
/* empty string */
else
return subtotal;
}
/*
* stail : Application of rule <stail> -> <mult_div_tok> <stmt> <stail> | e
* arg : token - token needing to be parsed
* Return: return recursive result of itself
*/
int stail(char *token, int subtotal){
int stmt_value;
if (!strncmp(token, "*", 1)){
mul_div_tok(token);
stmt_value = stmt(next_token);
// if term returned an error, give up otherwise call ttail
if (stmt_value == ERROR)
return stmt_value;
else
return stail(next_token, (subtotal * stmt_value));
}
else if(!strncmp(token, "/", 1)){
mul_div_tok(token);
stmt_value = stmt(next_token);
// if term returned an error, give up otherwise call ttail
if (stmt_value == ERROR)
return stmt_value;
else
return stail(next_token, (subtotal / stmt_value));
}
/* empty string */
else
return subtotal;
}
/*
* ftail : Application of rule <ftail> -> <compare_tok> <factor> <ftail> | e
* arg : token - token needing to be parsed
* Return: return recursive result of itself
*/
int ftail(char *token, int subtotal){
int factor_value;
if (!strncmp(token, "<", 1)){
compare_tok(token); // updates next_token to next one by get_token()
factor_value = factor(next_token); //using the updated next_token
// if term returned an error, give up otherwise call ttail
if (factor_value == ERROR)
return factor_value;
else
return ftail(next_token, (subtotal < factor_value));
}else if(!strncmp(token, ">", 1)){
compare_tok(token);
factor_value = factor(next_token);
// if term returned an error, give up otherwise call ttail
if (factor_value == ERROR)
return factor_value;
else
return ftail(next_token, (subtotal > factor_value));
}else if(!strncmp(token, "<=", 1)){
compare_tok(token);
factor_value = factor(next_token);
// if term returned an error, give up otherwise call ttail
if (factor_value == ERROR)
return factor_value;
else
return ftail(next_token, (subtotal <= factor_value));
}else if(!strncmp(token, ">=", 1)){
compare_tok(token);
factor_value = factor(next_token);
// if term returned an error, give up otherwise call ttail
if (factor_value == ERROR)
return factor_value;
else
return ftail(next_token, (subtotal >= factor_value));
}else if(!strncmp(token, "!=", 1)){
compare_tok(token);
factor_value = factor(next_token);
// if term returned an error, give up otherwise call ttail
if (factor_value == ERROR)
return factor_value;
else
return ftail(next_token, (subtotal != factor_value));
}else if(!strncmp(token, "==", 1)){
compare_tok(token);
factor_value = factor(next_token);
// if term returned an error, give up otherwise call ttail
if (factor_value == ERROR)
return factor_value;
else
return ftail(next_token, (subtotal == factor_value));
}
/* empty string */
else
return subtotal;
}