/
parser.cpp
248 lines (207 loc) · 6.8 KB
/
parser.cpp
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
#include "parser.h"
std::ostream& operator<<(std::ostream& out, const Token& token) // output tokens
{
out << token.str;
return out;
}
std::deque<Token> expressToTokens(const std::string& str)
{
std::deque<Token> tokens;
// remove spaces from string
std::string newStr = str;
removeSpaces(newStr);
for (const auto* p = newStr.c_str(); *p; p++) {
if (isdigit(*p)) {
const auto* b = p;
while (isdigit(*p)) {
p++;
}
// add number to back of deque
const auto s = std::string(b, p);
tokens.push_back(Token{Token::Type::Number, s});
p--;
} else {
Token::Type t = Token::Type::Unknown;
int prec = -1;
bool ra = false;
switch(*p) {
default:
break;
case '(':
t = Token::Type::LeftParen;
break;
case ')':
t = Token::Type::RightParen;
break;
case '^':
t = Token::Type::Operator;
prec = 4;
ra = true;
break;
case '*':
t = Token::Type::Operator;
prec = 3;
break;
case '/':
t = Token::Type::Operator;
prec = 3;
break;
case '+':
t = Token::Type::Operator;
prec = 2;
break;
case '-':
t = Token::Type::Operator;
prec = 2;
break;
}
tokens.push_back(Token{t, std::string(1, *p), prec, ra});
}
}
return tokens;
}
std::deque<Token> shutingYard(const std::deque<Token>& tokens) {
std::vector<Token> stack;
std::deque<Token> output;
// while there are tokens to process
for (auto t : tokens) {
// read token
switch(t.type) {
case Token::Type::Number:
{
// if number, add to output queue
output.push_back(t);
break;
}
case Token::Type::Operator:
{
// if operator...
const auto o1 = t;
// while there are operators on the stack,
while (!stack.empty()) {
const auto o2 = stack.back();
// either o1 is left-associative and its precedence is less or equal to that of o2,
// or if o2 is right associative, and has precedence, less than 02
if ((!o1.rightAssociative && o1.precedence <= o2.precedence) ||
(o1.rightAssociative && o1.precedence < o2.precedence)) {
stack.pop_back();
output.push_back(o2);
continue;
}
break;
}
stack.push_back(o1);
break;
}
case Token::Type::LeftParen:
{
stack.push_back(t);
break;
}
case Token::Type::RightParen:
{
bool foundParen = false;
// until token at the top of the stack is a left a parenthesis
while (!stack.empty() && stack.back().type != Token::Type::LeftParen) {
// pop operators off stack on output queue
output.push_back(stack.back());
stack.pop_back();
foundParen = true;
}
// put left parenthesis on stack, not queue
stack.pop_back();
if (!foundParen && stack.empty()) {
std::cout << "missing left parenthesis (" << t.str.c_str() << ")" << std::endl;
return {}; // empty dequeue
}
break;
}
default:
{
std::cout << "invalid token" << std::endl;
return {};
break;
}
}
}
while (!stack.empty()) {
if (stack.back().type == Token::Type::LeftParen) {
std::cout << "missing right parenthesis" << std::endl;
}
// pop operator onto output queue
output.push_back(std::move(stack.back()));
stack.pop_back();
}
return output;
}
// do the calculations
float doCalculation(const std::string& exp)
{
const auto tokens = expressToTokens(exp);
auto output = shutingYard(tokens);
std::vector<float> stack;
while (!output.empty()) {
std::string op;
const auto token = output.front();
output.pop_front();
switch(token.type) {
case Token::Type::Number:
{
stack.push_back(std::stoi(token.str));
op = "Push " + token.str;
break;
}
case Token::Type::Operator:
{
const auto rhs = stack.back();
stack.pop_back();
const auto lhs = stack.back();
stack.pop_back();
switch(token.str[0]) { // do calculations
default:
{
std::cout << "error" << std::endl;
exit(0);
break;
}
case '^':
{
stack.push_back(static_cast<float>(pow(lhs, rhs)));
break;
}
case '*':
{
stack.push_back(lhs*rhs);
break;
}
case '/':
{
stack.push_back(lhs/rhs);
break;
}
case '+':
{
stack.push_back(lhs+rhs);
break;
}
case '-':
{
stack.push_back(lhs-rhs);
break;
}
}
op = "Push " + std::to_string(lhs) + " " + token.str + " " + std::to_string(rhs);
}
break;
default:
std::cout << "token error" << std::endl;
exit(0);
}
}
return stack.back();
}
void removeSpaces(std::string& str)
{
str.erase(std::remove(str.begin(), str.end(), '\t'), str.end());
str.erase(std::remove(str.begin(), str.end(), ' '), str.end());
}