-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnrparser.c
More file actions
225 lines (206 loc) · 3.64 KB
/
nrparser.c
File metadata and controls
225 lines (206 loc) · 3.64 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
/*
* Non-recursive parser -- parses with right precedence
* expressions containing arithmetic operators and parentheses.
* Public Domain snippet at http://linuxc.proboards76.com
*/
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <ctype.h>
int top, n, c, i;
char stack [CHAR_MAX] [CHAR_MAX], token [CHAR_MAX], tok [CHAR_MAX],
optok [CHAR_MAX], buf [CHAR_MAX];
void error(char *s)
{
printf("%s\n", s);
exit(1);
}
void push(char *token)
{
if (top == CHAR_MAX) error("Stack full");
if (!(*token)) return;
sprintf(stack [top++], "%s", token);
}
void pop(char *token)
{
token [0] = 0;
if(top) sprintf (token, "%s", stack [--top]);
}
void next(char *token)
{
while (buf [c] == ' ') c++;
if (c == CHAR_MAX || !buf [c]) {
token [0] = 0;
} else if (!isdigit(buf [c])) {
sprintf(token, "%c", buf [c++]);
} else {
for (i = 0; c < CHAR_MAX && buf [c] != ' ' &&
buf [c] != '+' && buf [c] != '-' &&
buf [c] != '*' && buf [c] != '/' &&
buf [c] != '(' && buf [c] != ')' && buf [c]; i++)
token [i] = buf [c++];
token [i] = 0;
}
}
void reduceplus(void)
{
pop(tok);
if (*tok == '(') goto endf;
pop(optok);
if (*optok == '+') {
n = atoi(tok);
pop(tok);
if (*tok != '(')
n += atoi(tok);
else {
push(tok);
}
sprintf(tok, "%d", n);
} else {
push(optok);
}
endf: push(tok);
}
void reduceminus(void)
{
pop(tok);
if (*tok == '(') goto endf;
pop (optok);
if (*optok == '-') {
n = -atoi(tok);
pop(tok);
if (*tok != '(')
n += atoi(tok);
else
push(tok);
sprintf(tok, "%d", n);
} else {
push(optok);
}
endf: push(tok);
}
void reducemult(void)
{
pop(tok);
if (*tok == '(') goto endf;
pop(optok);
if (*optok == '*') {
n = atoi(tok);
pop(tok);
if (*tok == '(') error ("Using * as unary operator");
n *= atoi(tok);
sprintf(tok, "%d", n);
} else {
push(optok);
}
endf: push(tok);
}
void reducediv(void)
{
pop(tok);
if (*tok == '(') goto endf;
pop(optok);
if (*optok == '/') {
n = atoi(tok);
if (!n) error("divide by zero");
pop(tok);
if (*tok == '(') error("Using / as unary operator");
n = atoi(tok) / n;
sprintf(tok, "%d", n);
} else {
push(optok);
}
endf: push(tok);
}
void reduceparen(void)
{
pop(tok);
pop(optok);
if (*optok != '(')
error("Subexpression didn't reduce");
push(tok);
}
void shift(char *optok)
{
next(token);
if (!isdigit(*token) && *token != '(')
error("Number or ( expected");
push(optok);
push(token);
if (*token == '(')
pop(token);
else
next(token);
return;
}
int main(int argc, char **argv)
{
if (argc != 2) return 1;
sprintf(buf, "%s", argv [1]);
next(token);
if (!(*token)) return 1;
if (isdigit(*token)) {
push("+");
push(token);
next(token);
}
while (1) {
#if 1
for (i = 0; i < top; i++) printf("\"%s\" ", stack [i]);
printf("TOP\n");
#endif
switch (*token) {
case 0:
reducediv();
reducemult();
reduceminus();
reduceplus();
pop(token);
printf("%d\n", atoi (token));
exit(0);
case '+':
reducediv(); /* The order in which we */
reducemult(); /* reduce before shift */
reduceminus(); /* determines precedence */
reduceplus();
shift("+");
break;
case '-':
reducediv();
reducemult();
reduceminus();
shift("-");
break;
case '*':
reducediv();
reducemult();
shift("*");
break;
case '/':
reducediv();
shift("/");
break;
case '(':
next(token);
if (isdigit(*token)) {
push("(");
push(token);
next(token);
} else {
push("(");
}
break;
case ')':
reducediv();
reducemult();
reduceminus();
reduceplus();
reduceparen();
next(token);
break;
default:
error("Operator expected");
}
}
return 0;
}