-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.cpp
110 lines (86 loc) · 2.16 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
#include "Parser.h"
Parser::Parser()
{
}
Parser::~Parser()
{
}
int Parser::getClosedBracketPos(string s, int pos){
int brCount = 1;
int i = pos + 1;
while (i < s.size() && brCount > 0){
if (s[i] == '(')
brCount++;
else if (s[i] == ')')
brCount--;
i++;
}
return i - 1;
}
string Parser::unwrap(string s){
while (s[0] == '(' && getClosedBracketPos(s, 0) == s.size() - 1)
s = s.substr(1, s.size() - 2);
return s;
}
int Parser::getOperator(string expr, int startPos){
for (int i = 0; i < tokens.size(); i++){
string cmpstr = expr.substr(startPos, tokens[i].size());
if (tokens[i].compare(cmpstr) == 0)
return i;
}
return -1;
}
ParseResult* Parser::parse(string expr){
if (expr.size() == 0)
return nullptr;
int lowPriorityToken = tokens.size(); // Track the minimum priority
int lowPrPos = -1; // operator token during parsing
// Remove unwanted chars
expr = removeChar(expr, ' ');
expr = unwrap(expr);
transform(expr.begin(), expr.end(), expr.begin(), ::tolower);
// Add 0 to let constants have negative sign
if (expr[0] == '-')
expr = '0' + expr;
for (int i = 0; i < expr.size(); i++){
char currChar = expr[i];
if (currChar == '('){
i = getClosedBracketPos(expr, i);
continue;
}
int op = getOperator(expr, i);
if (op >= 0 && op < lowPriorityToken){
lowPriorityToken = op;
lowPrPos = i;
}
}
if (lowPrPos >= 0){
int opEndPos = lowPrPos + tokens[lowPriorityToken].size();
ParseResult* result = new ParseResult;
result->id = static_cast<TokenId>(lowPriorityToken);
result->lExpr = expr.substr(0, lowPrPos);
result->rExpr = expr.substr(opEndPos, expr.size() - opEndPos);
return result;
}
else if (lowPriorityToken == VARX || lowPriorityToken == VARX)
{
ParseResult* result = new ParseResult;
result->id = static_cast<TokenId>(lowPriorityToken);
result->lExpr = "";
result->rExpr = "";
return result;
}
ParseResult* result = new ParseResult;
result->id = CONST;
result->lExpr = expr;
result->rExpr = "";
return result;
}
string Parser::removeChar(string str, char c){
string newstr;
for(auto i=str.begin(); i != str.end(); i++) {
if (*i != c)
newstr += *i;
}
return newstr;
}