-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.h
235 lines (219 loc) · 5.36 KB
/
parser.h
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
#ifndef PARSER_GUARD
#define PARSER_GUARD
#include "defines.h"
#include <string>
#include <map>
#include "errorhandling.h"
namespace ctb
{
class parser
{
public:
enum ttype{tWhite,tMod,tNum,tMul,tDiv,tAdd,tSub,tPB,tPE,tEof};
private:
typedef pair<ttype, int> token;
vector<token> stack;
ttype type(const token&);
int val(const token&);
token tokenize(const char *&);
const token& get(int i);
int process(const char*&);
void squash_mul_div();
void squash_add_sub();
static void diagnosis(const string& s, const string& e, int pos);
public:
static void self_test();
static int calculate(const string&);
};
void parser::self_test()
{
cout << "testing math parser" << endl;
assert(parser::calculate("1+1") == 2);
assert(parser::calculate("2+3*4+2") == 16);
assert(parser::calculate("(2+3)*(4-2)") == 10);
assert(parser::calculate("22-(2+3)*(4-2)") == 12);
assert(parser::calculate("(1 + (0/4))% (12/4)") == 1);
}
void parser::squash_mul_div()
{
while(stack.size() >= 3 && (type(get(1)) == tMul || type(get(1)) == tDiv || type(get(1)) == tMod))
{
if(type(get(0)) != tNum || type(get(2)) != tNum)
error("something wrong found where number expected");
int a = val(get(0));
int b = val(get(2));
ttype t = type(get(1));
stack.pop_back();
stack.pop_back();
stack.pop_back();
switch(t)
{
case tDiv:
stack.push_back(token(tNum,b/a));
break;
case tMul:
stack.push_back(token(tNum,b*a));
break;
case tMod:
stack.push_back(token(tNum,b%a));
break;
default:
error("this should have never happened");
}
}
}
void parser::squash_add_sub()
{
while(stack.size() >= 3 && (type(get(1)) == tAdd || type(get(1)) == tSub))
{
if(type(get(0)) != tNum || type(get(2)) != tNum)
error("something wrong found where number expected (note that we dont support unary minus!)");
int a = val(get(0));
int b = val(get(2));
ttype t = type(get(1));
stack.pop_back();
stack.pop_back();
stack.pop_back();
switch(t)
{
case tSub:
stack.push_back(token(tNum,b-a));
break;
case tAdd:
stack.push_back(token(tNum,a+b));
break;
default:
error("this should have never happened");
}
}
}
void parser::diagnosis(const string& s, const string& e, int pos)
{
error(string("at position ").append(to_string(pos)).append(" in expression ").append(s).append("\n ").append(e), false);
}
int parser::calculate(const string& s)
{
parser p;
const char* exp = s.c_str();
const char* start = s.c_str();
int r = 0;
try
{
r = p.process(exp);
}
catch(const error_struct& err)
{
diagnosis(s, err.first, exp-start);
}
catch(exception& err)
{
diagnosis(s, err.what(), exp-start);
}
return r;
}
const parser::token& parser::get(int i)
{
return stack[stack.size()-1-i];
}
int parser::val(const parser::token& t)
{
return t.second;
}
parser::ttype parser::type(const token& t)
{
return t.first;
}
parser::token parser::tokenize(const char *& ptr)
{
static map<char, ttype> hash = {
{'(',tPB},
{')',tPE},
{'*',tMul},
{'/',tDiv},
{'+',tAdd},
{'-',tSub},
{'%',tMod},
{'0',tNum},
{'1',tNum},
{'2',tNum},
{'3',tNum},
{'4',tNum},
{'5',tNum},
{'6',tNum},
{'7',tNum},
{'8',tNum},
{'9',tNum},
{'\0',tEof},
{' ',tWhite},
};
if(hash.find(*ptr) == hash.end())
error("unexpected character, cannot tokenize");
ttype t = hash.find(*ptr)->second;
if(t == tNum)
{
int i = 0;
while(isdigit(*ptr))
i = i*10+((*(ptr++))-48);
return token(t,i);
}
else
{
ptr++;
return token(t,0);
}
}
int parser::process(const char*& exp)
{
for(token t = tokenize(exp); true; t = tokenize(exp))
{
switch(type(t))
{
case tWhite:
break;
case tNum:
{
stack.push_back(t);
squash_mul_div();
}
break;
case tPE:
{
squash_add_sub();
if(stack.size() < 2)
error("misplaced parenthesis");
if(type(get(1)) != tPB)
error("misplaced parentheses");
token num = get(0);
stack.pop_back();
stack.pop_back();
stack.push_back(num);
}
break;
case tPB:
stack.push_back(t);
break;
case tMul:
case tDiv:
case tMod:
stack.push_back(t);
break;
case tAdd:
case tSub:
squash_add_sub();
stack.push_back(t);
break;
case tEof:
squash_mul_div();
squash_add_sub();
if(stack.size() != 1)
error(string(to_string(stack.size())).append(" tokens remained on stack"));
if(type(get(0)) != tNum)
error(string("wrong token type remained on stack "));
return val(get(0));
}
}
error("this place should never have been reached!");
return 0;
}
};
#endif