-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.cpp
194 lines (175 loc) · 5.02 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
#include "parser.hpp"
Expression *parse(const std::vector<Token*>& tokens)
{
std::vector<Expression*> exprStack;
std::vector<Operation> opStack;
Operation operation, nextOp;
int opSize;
std::unordered_map<Operation, Precedence> precedences ({
{PAR, PARENTHETICAL},
{ADD, ADDITIVE},
{SUB, ADDITIVE},
{MUL, MULTIPLICATIVE},
{DIV, MULTIPLICATIVE},
{MOD, MULTIPLICATIVE},
{EXP, EXPONENTIAL},
{NEG, NEGATIVE}
});
std::unordered_map<Operation, int> args ({
{PAR, 0},
{ADD, 1},
{SUB, 1},
{MUL, 1},
{DIV, 1},
{MOD, 1},
{EXP, 1},
{NEG, 0}
});
for (Token* token : tokens) {
switch (token->getType()) {
case INT_T:
{
IntToken *itoken = (IntToken*) token;
exprStack.push_back(new Const_E(itoken->getVal()));
//std::cout << "new constExpr" << std::endl;
break;
}
case PAREN_T:
{
ParenToken *ptoken = (ParenToken*) token;
if (ptoken->getSide() == LEFT) {
opStack.push_back(PAR);
break;
}
// RIGHT PARENTHESES
if (opStack.empty()) {
std::cerr << "[SYNTAX ERROR] : UNMATCHED RIGHT PARENTHESIS" << std::endl;
exit(EXIT_FAILURE);
}
// LOOP TO FIND RIGHT PARENTHESES
nextOp = opStack.back();
opStack.pop_back();
while (nextOp != PAR) {
pushOpExpr(nextOp, opStack, exprStack);
if (opStack.empty()) {
std::cerr << "[SYNTAX ERROR] : UNMATCHED RIGHT PARENTHESIS" << std::endl;
exit(EXIT_FAILURE);
}
nextOp = opStack.back();
opStack.pop_back();
}
break;
}
case BINOP_T:
{
BinaryOpToken *btoken = (BinaryOpToken*) token;
operation = btoken->getBinOp();
// LEADING NEGATIVE SIGN
if (opStack.empty()) {
if (operation == SUB && exprStack.empty()) {
opStack.push_back(NEG);
} else {
opStack.push_back(operation);
}
break;
}
// PUT OPERATION ONTO OPSTACK
nextOp = opStack.back();
while (precedences[nextOp] >= precedences[operation]) {
opSize = 0;
for (Operation op : opStack)
opSize += args[op];
if (exprStack.size() <= opSize) {
if (operation == SUB) {
operation = NEG;
break;
}
std::cerr << "[SYNTAX ERROR] : CANNOT USE TOKEN > " << token->getToken() << " < AS A PREFIX OPERATION" << std::endl;
exit(EXIT_FAILURE);
}
opStack.pop_back();
pushOpExpr(nextOp, opStack, exprStack);
if (opStack.empty())
break;
nextOp = opStack.back();
}
//std::cout << "pushing..." << binOpStack.size() << std::endl;
opStack.push_back(operation);
break;
}
default:
{
std::cerr << "[SYNTAX ERROR] : UNRECOGNIZED TOKEN > " << token->getToken() << " <" << std::endl;
exit(EXIT_FAILURE);
break;
}
}
delete token;
//std::cout << "del token" << std::endl;
}
// FLUSH THE OPSTACK
while (!opStack.empty()) {
//std::cout << "loop " << OpStack.size() << std::endl;
operation = opStack.back();
opStack.pop_back();
pushOpExpr(operation, opStack, exprStack);
}
// make sure the parser worked
if (exprStack.size() != 1) {
std::cerr << "[PARSING ERROR] : UNEXPECTED MULTIPLE SYNTAX TREES: " << exprStack.size() << std::endl;
Expression* e;
while (!exprStack.empty()) {
e = exprStack.back();
exprStack.pop_back();
std::cerr << "Tree: " << prettyPrint(e) << std::endl;
}
exit(EXIT_FAILURE);
}
return exprStack.back();
}
std::string binOpStr(Operation binOp)
{
switch (binOp) {
case ADD:
return "+";
case SUB:
return "-";
case MUL:
return "*";
case DIV:
return "/";
case EXP:
return "^";
case MOD:
return "%";
case NEG:
return "(-)";
default:
std::cerr << "Cannot convert binary operation " << binOp << " to string!" << std::endl;
exit(EXIT_FAILURE);
}
}
void pushOpExpr (Operation &operation, const std::vector<Operation> &opStack, std::vector<Expression*> &exprStack) {
Expression *lExpr, *rExpr;
if (operation == NEG) {
if (exprStack.size() < 1) {
std::cerr << "[SYNTAX ERROR] : NO EXPRESSION BOUND TO NEGATIVE SIGN" << std::endl;
exit(EXIT_FAILURE);
}
lExpr = exprStack.back();
exprStack.pop_back();
exprStack.push_back(new Neg_E(lExpr));
//std::cout << "new negExpr" << std::endl;
return;
}
if (exprStack.size() < 2) {
std::cerr << "[SYNTAX ERROR] : CANNOT USE TOKEN > " << binOpStr(operation) << " < AS A PREFIX OPERATION" << std::endl;
exit(EXIT_FAILURE);
}
rExpr = exprStack.back();
exprStack.pop_back();
lExpr = exprStack.back();
exprStack.pop_back();
exprStack.push_back(new BinaryOp_E(lExpr, rExpr, operation));
//std::cout << "new binOpExpr" << std::endl;
}