-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlexAnalizer.cpp
More file actions
134 lines (124 loc) · 4.41 KB
/
lexAnalizer.cpp
File metadata and controls
134 lines (124 loc) · 4.41 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
#include <iostream>
#include <string>
#include <unordered_map>
#include "lexAnalizer.h"
static const std::unordered_map<std::string, int> keywords = {
{"class", Class},
{"extends", Extends},
{"is", Is},
{"end", End},
{"var", Var},
{"method", Method},
{"this", This},
{"while", While},
{"loop", Loop},
{"if", If},
{"then", Then},
{"else", Else},
{"return", Return}
};
enum FsmStates {
StartSymbol,
FindColon,
NumericLiteral,
StringLexeme
};
static int getDelimiterCode(char c) {
switch (c) {
case '(':
return LeftRoundBracket;
case ')':
return RightRoundBracket;
case '[':
return LeftRectBracket;
case ']':
return RightRectBracket;
case ',':
return Comma;
case '.':
return Dot;
default:
return NotDelimiter;
}
}
std::vector<Tokens*> makeLexAnalysis(const char* str, const char* endStr) {
std::vector<Tokens*> all_tokens;
const char* lexemeStart;
const char* stringStart = str;
int delimiterCode;
int fsmState = StartSymbol;
int lineIndex = 0;
while (str != endStr) {
switch(fsmState) {
case StartSymbol:
delimiterCode = getDelimiterCode(*str);
if (delimiterCode != NotDelimiter){
all_tokens.push_back(new Tokens(std::string(1, *str), {lineIndex, str-stringStart}, delimiterCode));
str++;
}
else if (isspace(*str)){
if (*str == '\n') {
lineIndex++;
stringStart = str+1;
}
str++;
}
else if (*str == ':') {
str++;
fsmState = FindColon;
}
else if (isalpha(*str)) {
lexemeStart = str;
str++;
fsmState = StringLexeme;
}
else if (isdigit(*str)) {
lexemeStart = str;
str++;
fsmState = NumericLiteral;
}
break;
case FindColon:
if (*str == '=') {
all_tokens.push_back(new Tokens(":=", {lineIndex, str-stringStart-1}, Assignment));
str++;
}
else
all_tokens.push_back(new Tokens(":", {lineIndex, str-stringStart-1}, Colon));
fsmState = StartSymbol;
break;
case NumericLiteral:
if (isdigit(*str) || (*str == '.'))
str++;
else {
std::string lexeme = std::string(lexemeStart, str);
if (lexeme.find('.') != std::string::npos)
all_tokens.push_back(new Tokens(lexeme, {lineIndex, lexemeStart-stringStart}, RealLiteral));
else
all_tokens.push_back(new Tokens(lexeme, {lineIndex, lexemeStart-stringStart}, IntegerLiteral));
fsmState = StartSymbol;
}
break;
case StringLexeme:
if(isalnum(*str))
str++;
else {
std::string lexeme = std::string(lexemeStart, str);
auto keyIt = keywords.find(lexeme);
if (keyIt != keywords.end()) // найдено ключевое слово
all_tokens.push_back(new Tokens(lexeme, {lineIndex, lexemeStart-stringStart}, (*keyIt).second));
else if (lexeme == "true" || lexeme == "false") // найден литерал Boolean
all_tokens.push_back(new Tokens(lexeme, {lineIndex, lexemeStart-stringStart}, BooleanLiteral));
else
all_tokens.push_back(new Tokens(lexeme, {lineIndex, lexemeStart-stringStart}, Identifier));
fsmState = StartSymbol;
}
break;
}
}
return all_tokens;
}
void deleteVector(std::vector<Tokens*> & v) {
for (auto it = v.begin(); it != v.end(); ++it)
delete *it;
}