-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLexicalAnalyzer.cpp
More file actions
86 lines (79 loc) · 3.01 KB
/
Copy pathLexicalAnalyzer.cpp
File metadata and controls
86 lines (79 loc) · 3.01 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
#include "LexicalAnalyzer.h"
bool LexicalAnalyzer::isspecial(char ch)
{
return string(".?+{}*[-]|()\\").find(ch) != string::npos;
}
string LexicalAnalyzer::is_num(const string &s)
{
return to_string(stoi(s));
}
vector<token> LexicalAnalyzer::analyze(const string &expr)
{
vector<token> toks;
bool escaped = false;
for (auto it = expr.begin(); it < expr.end(); it++) {
if (!isgraph(*it)) {
throw invalid_argument("Invalid symbol in regex!");
}
if (!isspecial(*it) || escaped) {
if (!isspecial(*it) && escaped) {
throw invalid_argument("Invalid escape character!");
}
toks.push_back(token {STR_T, string(1, *it)});
escaped = false;
} else {
switch (*it) {
case '.':
toks.push_back(token {STR_T, "."});
break;
case '|':
toks.push_back(token {ENUM_T, "|"});
break;
case '+':
toks.push_back(token {ITER_OM_T, "+"});
break;
case '?':
toks.push_back(token {ITER_ZO_T, "?"});
break;
case '*':
toks.push_back(token {ITER_ZM_T, "*"});
break;
case '{': {
int pos = expr.find("}", it - expr.begin());
if (pos == string::npos) {
throw invalid_argument("Not enough '}'!");
} else {
string stok = expr.substr(it - expr.begin() + 1, pos - (it - expr.begin()) - 1);
it = expr.begin() + pos;
pos = stok.find(",");
if (pos == string::npos) {
toks.push_back(token {ITER_N_T, is_num(stok) + "," + is_num(stok)});
} else if (pos == 0) {
toks.push_back(token {ITER_N_T, "0," + is_num(stok.substr(1))});
} else if (pos == stok.size() - 1) {
toks.push_back(token {ITER_N_T, is_num(stok.substr(0, stok.size() - 1)) + ",-1"});
} else {
toks.push_back(token {ITER_N_T, is_num(stok.substr(0, pos)) + "," + is_num(stok.substr(pos + 1))});
}
}
break;
}
case '}':
throw invalid_argument("Unescaped '}'!");
break;
case '(':
toks.push_back(token {O_BR_T, "("});
break;
case ')':
toks.push_back(token {C_BR_T, ")"});
break;
case '\\':
escaped = true;
break;
default:
throw invalid_argument("Unescaped special symbol!");
}
}
}
return toks;
}