-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlex.cpp
More file actions
221 lines (193 loc) · 5.49 KB
/
lex.cpp
File metadata and controls
221 lines (193 loc) · 5.49 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include "lex.h"
#include <set>
#include <iostream>
using namespace std;
set<string> key_words {"if", "then", "goto", "print", "buy", "sell", "prod",
"build", "endturn", "begin", "end", "end."}; /* all possible key words */
Lexeme::Lexeme(LexType t, const string& s, int i): type(t), name(s), line(i) {}
int Lexeme::get_line() const {
return line;
}
const string& Lexeme::get_name() const {
return name;
}
bool Lexeme::operator==(const Lexeme& l) const {
if (name != l.name)
return false;
if (type != l.type)
return false;
return true; /* don't compare lines */
}
bool Lexeme::operator!=(const Lexeme& l) const {
return !(*this == l);
}
bool Lexeme::operator==(LexType l) const {
return type == l;
}
bool Lexeme::operator!=(LexType l) const {
return type != l;
}
bool Lexeme::operator==(const string& s) const {
return name == s;
}
bool Lexeme::operator!=(const string& s) const {
return name != s;
}
ostream& operator<<(ostream& out, const Lexeme& l) {
switch (l.type) {
case (NUMBER): out << "NUMBER "; break;
case (MARK): out << "MARK "; break;
case (FUNCTION): out << "FUNCTION "; break;
case (KEY): out << "KEY "; break;
case (IDENTIFIER): out << "IDENTIFIER "; break;
case (STRING): out << "STRING "; break;
case (SEPARATOR): out << "SEPARATOR "; break;
case (COMPARE): out << "COMPARE "; break;
case (ADDITION): out << "ADDITION "; break;
case (MULTIPLICATION): out << "MULTIPLICATION "; break;
case (NOT): out << "NOT "; break;
}
out << l.name << ' ' << l.line;
return out;
}
bool LexAnalyser::getc() {
input.get(c);
if (input.eof()) /* check for file end */
return false;
return true;
}
void LexAnalyser::ungetc() {
input.putback(c); /* put symbol back to stream */
}
bool LexAnalyser::sep(char s) {
/* all symbols that may not be highlighted by spaces */
if ((s == ' ') || (s == '\t') || (s == '\n'))
return true;
if ((s == '+') || (s == '-') || (s == '*') || (s == '/') || (s == '%'))
return true;
if ((s == '<') || (s == '>') || (s == '=') || (s == '!'))
return true;
if ((s == ';') || (s == ','))
return true;
if ((s == '(') || (s == ')'))
return true;
return false;
}
void LexAnalyser::H() {
if (!buffer.empty()) /* other functions save lexemes by themself */
buffer.clear();
if (!getc()) /* check for file end */
return;
if ((c == ' ') || (c == '\t') || (c == '\n')) { /* skip spaces */
if (c == '\n') /* new line in origin */
line++;
H();
return;
}
buffer.push_back(c); /* beginning of new lexeme */
if ((c >= '0') && (c <= '9')) /* beginning of integer */
N();
else if (((c >='a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')))
/* beginning of identifier */
I();
else if (c == '"') /* beginning of string */
S();
/* all single operators and separators */
else if ((c == '+') || (c == '-')) {
text.push_back(Lexeme(ADDITION, buffer, line));
H();
} else if ((c == '*') || (c == '/') || (c == '%')) {
text.push_back(Lexeme(MULTIPLICATION, buffer, line));
H();
} else if ((c == '(') || (c == ')') || (c == ';') || (c == ',')) {
text.push_back(Lexeme(SEPARATOR, buffer, line));
H();
/* compare operators (may be double) */
} else if ((c == '<') || (c == '>') || (c == '=') || (c == '!'))
C1();
else /* unknown symbol */
throw LexError(c, line); /* unexpected symbol */
}
void LexAnalyser::N() {
if (!getc()) /* check for file end */
return;
if ((c >= '0') && (c <= '9')) { /* integer continues */
buffer.push_back(c);
N();
} else if (sep(c)) { /* end of integer */
ungetc(); /* last symbol is the beginning of next lexeme */
text.push_back(Lexeme(NUMBER, buffer, line)); /* save lexeme */
H(); /* return to starting vertex */
} else /* unexpected symbol */
throw LexError(c, line);
}
void LexAnalyser::I() {
if (!getc()) /* check for file end */
return;
if (((c >= '0') && (c <= '9')) || ((c >= 'a') && (c <= 'z')) ||
((c >= 'A') && (c >= 'Z'))) { /* identifier continues */
buffer.push_back(c);
I();
} else if (c == ':') { /* mark */
text.push_back(Lexeme(MARK, buffer, line));
H();
} else if (c == '(') { /* function */
ungetc(); /* '(' is separate lexeme */
text.push_back(Lexeme(FUNCTION, buffer, line));
H();
} else if (c == '.') { /* end. */
if (buffer == "end")
buffer.push_back(c);
text.push_back(Lexeme(KEY, buffer, line));
H();
} else if (sep(c)) { /* end of identifier */
ungetc(); /* last symbol is the beginning of next lexeme */
if (key_words.find(buffer) != key_words.end()) /* check for key word */
text.push_back(Lexeme(KEY, buffer, line));
else
text.push_back(Lexeme(IDENTIFIER, buffer, line)); /*usual identifier*/
H();
} else /* unexpected symbol */
throw LexError(c, line);
}
void LexAnalyser::S() {
if (!getc()) /* check for file end */
return;
if (c != '"') { /* string continues */
buffer.push_back(c);
S();
} else {
buffer.push_back(c);
text.push_back(Lexeme(STRING, buffer, line));
H();
}
}
void LexAnalyser::C1() {
char tmp = c;
if (!getc()) /* check for file end */
return;
if (c != '=') { /* it's single operator */
ungetc(); /* last symbol is the beginning of next lexeme */
if (tmp == '!') {
text.push_back(Lexeme(NOT, buffer, line));
H();
} else {
text.push_back(Lexeme(COMPARE, buffer, line));
H();
}
} else { /* it's double operator */
buffer.push_back(c);
text.push_back(Lexeme(COMPARE, buffer, line));
H();
}
}
LexAnalyser::LexAnalyser(istream& i): input(i) {
line = 1;
}
bool LexAnalyser::run() {
/* clean given containers before filling them */
buffer.clear();
text.clear();
H();
return true;
}