-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyntaxAnalyzer.cpp
More file actions
executable file
·145 lines (123 loc) · 3.12 KB
/
Copy pathSyntaxAnalyzer.cpp
File metadata and controls
executable file
·145 lines (123 loc) · 3.12 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
#include "SyntaxAnalyzer.h"
SyntaxAnalyzer::SyntaxAnalyzer(const vector<token> &tokens)
{
raw_tokens = tokens;
init();
}
void SyntaxAnalyzer::init()
{
it = raw_tokens.begin();
pf_tokens.clear();
brackets_count = 0;
buf = "";
}
bool SyntaxAnalyzer::flush_buf(int pos)
{
if (!buf.empty()) {
if (pos == -1) {
pf_tokens.push_back(token {STR_T, buf});
} else {
pf_tokens.insert(pf_tokens.begin() + pos, token {STR_T, buf});
}
buf = "";
return true;
}
return false;
}
void SyntaxAnalyzer::E(bool last)
{
bool need_cat = false;
if (it >= raw_tokens.end()) {
throw invalid_argument("Not enough arguments for binary operation!");
}
int cur_pos = pf_tokens.size();
if (it->type != STR_T && it->type != CAT_T) {
flush_buf();
}
switch (it->type) {
case O_BR_T:
brackets_count++;
it++;
E();
if (it >= raw_tokens.end() || it->type != C_BR_T) {
throw invalid_argument("Brackets amount mismatch!");
}
brackets_count--;
flush_buf();
need_cat = true;
it++;
O(cur_pos);
break;
case C_BR_T:
if (brackets_count > 0) {
return;
} else {
throw invalid_argument("Wrong ')'!");
}
case STR_T: {
token tmp = *it;
it++;
if ((need_cat = O())) {
if (flush_buf(cur_pos)) {
pf_tokens.insert(pf_tokens.begin() + cur_pos, token {CAT_T, ""});
}
pf_tokens.push_back(tmp);
} else {
if (!tmp.lexeme.compare(".")) {
flush_buf();
pf_tokens.push_back(tmp);
} else {
buf += tmp.lexeme;
}
}
break;
}
default:
throw invalid_argument("Wrong operation syntax!");
}
if (it >= raw_tokens.end()) {
return;
}
if (it->type == C_BR_T && brackets_count <= 0) {
throw invalid_argument("Wrong ')'!");
}
if (it->type == STR_T || it->type == O_BR_T) {
if (it->type == O_BR_T || need_cat) {
flush_buf();
pf_tokens.insert(pf_tokens.begin() + cur_pos, token {CAT_T, ""});
}
E(true);
}
if (it->type == ENUM_T && !last) {
pf_tokens.insert(pf_tokens.begin() + cur_pos, *it);
flush_buf();
it++;
E();
}
}
bool SyntaxAnalyzer::O(int pos)
{
if (it < raw_tokens.end() && prior[it->type] > prior[STR_T]) {
if (pos >= 0) {
pf_tokens.insert(pf_tokens.begin() + pos, *it);
}
token tmp = *it;
it++;
O(pos);
if (pos == -1) {
pf_tokens.push_back(tmp);
}
return true;
}
return false;
}
vector<token> SyntaxAnalyzer::analyze()
{
init();
if (raw_tokens.size() == 0) {
return raw_tokens;
}
E();
flush_buf();
return pf_tokens;
}