-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.h
303 lines (268 loc) · 7.82 KB
/
lexer.h
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#pragma once
#ifndef LEXER_H
#define LEXER_H
#include <vector>
#include <string>
using namespace std;
enum TOKENS_TYPE
{
DATA_TYPE,
DECISION,
LOOP,
READ,
WRITE,
FUNCTION,
ARITH_OPS,
RELATION_OPS,
INPUT_OP,
IDENTIFIER,
ASSIGNMENT,
RETURN,
CONST_LITERAL,
STRING,
NUMBER,
BRACKET,
PUNCTUATION,
COMMENTS,
INVALID
};
class Token
{
public:
static int curr_line;
static int curr_col;
static int tab_space;
static string::iterator file_begin;
static string::iterator file_end;
static string::iterator file_iter;
static Token look;
TOKENS_TYPE token;
string lexeme;
int line;
int col;
Token()
{
token = INVALID;
lexeme = "";
line = 0;
col = 0;
}
Token(TOKENS_TYPE type, string lex)
{
token = type;
lexeme = lex;
line = curr_line;
col = std::distance(Token::file_begin, Token::file_iter) - curr_col + Token::tab_space;
}
string to_string()
{
const string tokens_name[] = {"DATA_TYPE", "DECISION", "LOOP", "READ", "WRITE", "FUNCTION", "ARITH_OPS", "RELATION_OPS", "INPUT_OP", "IDENTIFIER", "ASSIGNMENT", "RETURN", "CONST_LITERAL", "STRING", "NUMBER", "BRACKET", "PUNCTUATION", "COMMENTS", "INVALID"};
return "{" + tokens_name[this->token] + ", " + this->lexeme + "}";
}
};
int Token::curr_line = 1;
int Token::curr_col = 1;
int Token::tab_space = 1;
Token Token::look = Token();
string::iterator Token::file_iter;
string::iterator Token::file_begin;
string::iterator Token::file_end;
bool is_dtype(const string &str)
{
const vector<string> keywords{"Integer", "char"};
for (const auto &keyword : keywords)
if (keyword == str)
return true;
return false;
}
bool is_decision(const string &str)
{
const vector<string> keywords{"if", "else", "elif"};
for (const auto &keyword : keywords)
if (keyword == str)
return true;
return false;
}
bool is_write(const string &str)
{
const vector<string> keywords{"print", "println"};
for (const auto &keyword : keywords)
if (keyword == str)
return true;
return false;
}
Token next_token(string::iterator &curr, const string::iterator end)
{
if (curr != Token::file_begin)
curr++;
// ignore white spaces in the source file
while (*curr == ' ' || *curr == '\n' || *curr == '\t')
{
if (*curr == '\n')
{
Token::curr_line++;
Token::curr_col = std::distance(Token::file_begin, curr); // num of chars from start to new line
Token::tab_space = 1; // reset tab spaces counter
}
if (*curr == '\t')
Token::tab_space += 3; // inc column location according to 4-spaces tabs
curr++;
}
// analyse tokens that start with an alphabet
if (isalpha(*curr))
{
string curr_tok(1, *curr);
while (curr + 1 != end && (isalpha(*(curr + 1)) || isdigit(*(curr + 1))))
{
curr++;
curr_tok += *curr;
}
// check if current word is any keyword or not
if (is_dtype(curr_tok))
return Token(DATA_TYPE, curr_tok);
else if (is_decision(curr_tok))
return Token(DECISION, curr_tok);
else if (is_write(curr_tok))
return Token(WRITE, curr_tok);
else if (curr_tok == "func")
return Token(FUNCTION, curr_tok);
else if (curr_tok == "In")
return Token(READ, curr_tok);
else if (curr_tok == "while")
return Token(LOOP, curr_tok);
else if (curr_tok == "ret")
return Token(RETURN, curr_tok);
else
return Token(IDENTIFIER, curr_tok); // it's an identifier if not a keyword
}
// analyse tokens that start with a digit
if (isdigit(*curr))
{
string curr_tok(1, *curr);
while (curr + 1 != end && isdigit(*(curr + 1)))
{
curr++;
curr_tok += *curr;
}
if (isalpha(*curr))
return Token(INVALID, "Incorect Number Token");
else
return Token(NUMBER, curr_tok);
}
// analyse tokens that start with symbols
switch (*curr)
{
case '(':
return Token(BRACKET, string("("));
case ')':
return Token(BRACKET, string(")"));
case '[':
return Token(BRACKET, string("["));
case ']':
return Token(BRACKET, string("]"));
case '{':
return Token(BRACKET, string("{"));
case '}':
return Token(BRACKET, string("}"));
case '>':
if (curr + 1 != end)
{
curr++;
if (*curr == '=')
return Token(RELATION_OPS, string(">="));
else if (*curr == '>')
return Token(INPUT_OP, string(">>"));
}
return Token(RELATION_OPS, ">");
case '<':
if (curr + 1 != end && *(curr + 1) == '=')
{
curr++;
return Token(RELATION_OPS, string("<="));
}
return Token(RELATION_OPS, string("<"));
case '=':
return Token(RELATION_OPS, string("="));
case ':':
if (curr + 1 != end && *(curr + 1) == '=')
{
curr++;
return Token(ASSIGNMENT, string(":="));
}
return Token(PUNCTUATION, string(":"));
case ';':
return Token(PUNCTUATION, string(";"));
case ',':
return Token(PUNCTUATION, string(","));
case '+':
return Token(ARITH_OPS, string("+"));
case '-':
return Token(ARITH_OPS, string("-"));
case '*':
return Token(ARITH_OPS, string("*"));
case '/':
if (curr + 1 != end)
{
string curr_tok(1, *curr);
char next_char = *(curr + 1);
switch (next_char)
{
case '=':
curr++;
return Token(RELATION_OPS, "/=");
case '*':
// skip the comments
curr++;
curr_tok += *curr;
while (curr + 1 != end && curr + 2 != end && !(*(curr + 1) == '*' && *(curr + 2) == '/'))
{
if (*curr == '\n')
Token::curr_line++;
curr++;
curr_tok += *curr;
}
curr++;
// if end token not found, invalid comment
if (curr + 1 == end)
return Token(INVALID, curr_tok + string(1, *curr));
curr++; // move pointer to ahead of comment
return next_token(curr, end);
}
}
return Token(ARITH_OPS, string("/"));
case '\'':
if (curr + 1 != end && isalpha(*(curr + 1)) && curr + 2 != end && *(curr + 2) == '\'')
{ // check for one letter constant literal
string ch(1, *(curr + 1));
curr += 2;
return Token(CONST_LITERAL, "\'" + ch + "\'");
}
else
return Token(INVALID, "Incorrect Constant Literal");
case '\"':
if (curr + 1 != end)
{ // check for n-length string literal
string curr_tok(1, *curr);
while (curr + 1 != end && *(curr + 1) != '\"')
{
curr++;
curr_tok += *curr;
}
if (curr + 1 == end)
return Token(INVALID, "Incorrect String Literal");
curr++;
return Token(STRING, curr_tok + "\"");
}
default: // incase no valid token found
return Token(INVALID, string(1, *curr));
}
}
// peek one token ahead current token
Token peek_token()
{
string::iterator temp = Token::file_iter;
Token ret_tok = next_token(Token::file_iter, Token::file_end);
Token::file_iter = temp;
return ret_tok;
}
#endif