forked from cparse/cparse
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshunting-yard.cpp
More file actions
180 lines (169 loc) · 5.8 KB
/
shunting-yard.cpp
File metadata and controls
180 lines (169 loc) · 5.8 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
// Source: http://www.daniweb.com/software-development/cpp/code/427500/calculator-using-shunting-yard-algorithm#
// Author: Jesse Brown
// Modifications: Brandon Amos
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include "shunting-yard.h"
#define isvariablechar(c) (isalpha(c) || c == '_')
TokenQueue_t calculator::toRPN(const char* expr,
std::map<std::string, double>* vars,
std::map<std::string, int> opPrecedence) {
TokenQueue_t rpnQueue; std::stack<std::string> operatorStack;
bool lastTokenWasOp = true;
// In one pass, ignore whitespace and parse the expression into RPN
// using Dijkstra's Shunting-yard algorithm.
while (*expr && isspace(*expr )) ++expr;
while (*expr ) {
if (isdigit(*expr )) {
// If the token is a number, add it to the output queue.
char* nextChar = 0;
double digit = strtod(expr , &nextChar);
# ifdef DEBUG
std::cout << digit << std::endl;
# endif
rpnQueue.push(new Token<double>(digit));
expr = nextChar;
lastTokenWasOp = false;
} else if (isvariablechar(*expr )) {
// If the function is a variable, resolve it and
// add the parsed number to the output queue.
if (!vars) {
throw std::domain_error(
"Detected variable, but the variable map is null.");
}
std::stringstream ss;
ss << *expr;
++expr;
while (isvariablechar(*expr )) {
ss << *expr;
++expr;
}
std::string key = ss.str();
std::map<std::string, double>::iterator it = vars->find(key);
if (it == vars->end()) {
throw std::domain_error(
"Unable to find the variable '" + key + "'.");
}
double val = vars->find(key)->second;
# ifdef DEBUG
std::cout << val << std::endl;
# endif
rpnQueue.push(new Token<double>(val));;
lastTokenWasOp = false;
} else {
// Otherwise, the variable is an operator or paranthesis.
switch (*expr) {
case '(':
operatorStack.push("(");
++expr;
break;
case ')':
while (operatorStack.top().compare("(")) {
rpnQueue.push(new Token<std::string>(operatorStack.top()));
operatorStack.pop();
}
operatorStack.pop();
++expr;
break;
default:
{
// The token is an operator.
//
// Let p(o) denote the precedence of an operator o.
//
// If the token is an operator, o1, then
// While there is an operator token, o2, at the top
// and p(o1) <= p(o2), then
// pop o2 off the stack onto the output queue.
// Push o1 on the stack.
std::stringstream ss;
ss << *expr;
++expr;
while (*expr && !isspace(*expr ) && !isdigit(*expr )
&& !isvariablechar(*expr) && *expr != '(' && *expr != ')') {
ss << *expr;
++expr;
}
ss.clear();
std::string str;
ss >> str;
# ifdef DEBUG
std::cout << str << std::endl;
# endif
if (lastTokenWasOp) {
// Convert unary operators to binary in the RPN.
if (!str.compare("-") || !str.compare("+")) {
rpnQueue.push(new Token<double>(0));
} else {
throw std::domain_error(
"Unrecognized unary operator: '" + str + "'.");
}
}
while (!operatorStack.empty() &&
opPrecedence[str] <= opPrecedence[operatorStack.top()]) {
rpnQueue.push(new Token<std::string>(operatorStack.top()));
operatorStack.pop();
}
operatorStack.push(str);
lastTokenWasOp = true;
}
}
}
while (*expr && isspace(*expr )) ++expr;
}
while (!operatorStack.empty()) {
rpnQueue.push(new Token<std::string>(operatorStack.top()));
operatorStack.pop();
}
return rpnQueue;
}
double calculator::calculate(const char* expr,
std::map<std::string, double>* vars) {
// 1. Create the operator precedence map.
std::map<std::string, int> opPrecedence;
opPrecedence["("] = -1;
opPrecedence["<<"] = 1; opPrecedence[">>"] = 1;
opPrecedence["+"] = 2; opPrecedence["-"] = 2;
opPrecedence["*"] = 3; opPrecedence["/"] = 3;
// 2. Convert to RPN with Dijkstra's Shunting-yard algorithm.
TokenQueue_t rpn = toRPN(expr, vars, opPrecedence);
// 3. Evaluate the expression in RPN form.
std::stack<double> evaluation;
while (!rpn.empty()) {
TokenBase* base = rpn.front();
rpn.pop();
Token<std::string>* strTok = dynamic_cast<Token<std::string>*>(base);
Token<double>* doubleTok = dynamic_cast<Token<double>*>(base);
if (strTok) {
std::string str = strTok->val;
if (evaluation.size() < 2) {
throw std::domain_error("Invalid equation.");
}
double right = evaluation.top(); evaluation.pop();
double left = evaluation.top(); evaluation.pop();
if (!str.compare("+")) {
evaluation.push(left + right);
} else if (!str.compare("*")) {
evaluation.push(left * right);
} else if (!str.compare("-")) {
evaluation.push(left - right);
} else if (!str.compare("/")) {
evaluation.push(left / right);
} else if (!str.compare("<<")) {
evaluation.push((int) left << (int) right);
} else if (!str.compare(">>")) {
evaluation.push((int) left >> (int) right);
} else {
throw std::domain_error("Unknown operator: '" + str + "'.");
}
} else if (doubleTok) {
evaluation.push(doubleTok->val);
} else {
throw std::domain_error("Invalid token.");
}
delete base;
}
return evaluation.top();
}