#include <iostream>
#include <string>
#include <map>
#include <cctype>
using namespace std;
struct Token {
string type;
string value;
};
const int MAX_TOKENS = 1000;
const int MAX_KEYWORDS = 50;
const int MAX_OPERATORS = 50;
const int MAX_SEPARATORS = 50;
int main() {
string code = R"(
#include <iostream>
using namespace std;
int main() {
int x = 10;
float y = 3.14;
string text = "Hello, World!";
if (x > 5 && y < 4.0) {
cout << text << endl;
for (int i = 0; i < x; i++) {
cout << "Iteration: " << i << endl;
}
}
return 0;
}
)";
cout << "Исходный код:\n";
cout << code << "\n";
cout << "---------------------\n";
string keywords[MAX_KEYWORDS] = {
"#include", "using", "namespace", "int", "float", "double",
"string", "if", "else", "for", "while", "return", "cout", "endl"
};
int keywords_count = 14;
string operators[MAX_OPERATORS] = {
"++", "--", "+=", "-=", "*=", "/=", "==", "!=", "<=", ">=",
"&&", "||", "<<", ">>", "+", "-", "*", "/", "=", ">", "<", "&"
};
int operators_count = 22;
char separators[MAX_SEPARATORS] = {
';', '(', ')', '{', '}', '[', ']', ',', '<', '>'
};
int separators_count = 10;
Token flow[MAX_TOKENS];
int flow_count = 0;
size_t i = 0;
int condition = 0;
while (i < code.length() && flow_count < MAX_TOKENS) {
char ch = code[i];
if (ch == ' ' || ch == '\n' || ch == '\t') {
i++;
continue;
}
// Поиск ключевых слов
bool found_keyword = false;
for (int j = 0; j < keywords_count; j++) {
string keyword = keywords[j];
if (code.compare(i, keyword.length(), keyword) == 0) {
flow[flow_count].type = "keyword";
flow[flow_count].value = keyword;
flow_count++;
i += keyword.length();
found_keyword = true;
condition = 1;
break;
}
}
if (found_keyword) continue;
// Поиск операторов
bool found_operator = false;
for (int j = 0; j < operators_count; j++) {
string op = operators[j];
if (code.compare(i, op.length(), op) == 0) {
flow[flow_count].type = "operator";
flow[flow_count].value = op;
flow_count++;
i += op.length();
found_operator = true;
condition = 4;
break;
}
}
if (found_operator) continue;
// Поиск разделителей
bool found_separator = false;
for (int j = 0; j < separators_count; j++) {
if (ch == separators[j]) {
flow[flow_count].type = "separator";
flow[flow_count].value = string(1, ch);
flow_count++;
i++;
found_separator = true;
condition = 5;
break;
}
}
if (found_separator) continue;
// Поиск чисел
if (isdigit(ch)) {
size_t start = i;
while (i < code.length() && (isdigit(code[i]) || code[i] == '.')) {
i++;
}
string number = code.substr(start, i - start);
flow[flow_count].type = "number";
flow[flow_count].value = number;
flow_count++;
condition = 7;
continue;
}
// Поиск строк
if (ch == '"') {
size_t start = i;
i++;
while (i < code.length() && code[i] != '"') {
i++;
}
if (i < code.length()) {
string str_content = code.substr(start, i - start + 1);
flow[flow_count].type = "string";
flow[flow_count].value = str_content;
flow_count++;
i++;
} else {
// Незакрытая строка
string str_content = code.substr(start);
flow[flow_count].type = "string";
flow[flow_count].value = str_content;
flow_count++;
}
condition = 6;
continue;
}
// Поиск идентификаторов
if (isalpha(ch) || ch == '_') {
size_t start = i;
while (i < code.length() && (isalnum(code[i]) || code[i] == '_')) {
i++;
}
string identifier = code.substr(start, i - start);
flow[flow_count].type = "identifier";
flow[flow_count].value = identifier;
flow_count++;
condition = 3;
continue;
}
i++;
}
condition = 8;
string token_codes[MAX_TOKENS];
int token_codes_count = 0;
int keyword_count = 1;
int identifier_count = 1;
int number_count = 1;
int string_count = 1;
int operator_count = 1;
int separator_count = 1;
map<string, string> keyword_map;
map<string, string> identifier_map;
map<string, string> number_map;
map<string, string> string_map;
map<string, string> operator_map;
map<string, string> separator_map;
for (int j = 0; j < flow_count; j++) {
string type = flow[j].type;
string value = flow[j].value;
if (type == "keyword") {
if (keyword_map.find(value) == keyword_map.end()) {
keyword_map[value] = "k" + to_string(keyword_count++);
}
token_codes[token_codes_count++] = keyword_map[value];
}
else if (type == "identifier") {
if (identifier_map.find(value) == identifier_map.end()) {
identifier_map[value] = "i" + to_string(identifier_count++);
}
token_codes[token_codes_count++] = identifier_map[value];
}
else if (type == "number") {
if (number_map.find(value) == number_map.end()) {
number_map[value] = "n" + to_string(number_count++);
}
token_codes[token_codes_count++] = number_map[value];
}
else if (type == "string") {
if (string_map.find(value) == string_map.end()) {
string_map[value] = "str" + to_string(string_count++);
}
token_codes[token_codes_count++] = string_map[value];
}
else if (type == "operator") {
if (operator_map.find(value) == operator_map.end()) {
operator_map[value] = "o" + to_string(operator_count++);
}
token_codes[token_codes_count++] = operator_map[value];
}
else if (type == "separator") {
if (separator_map.find(value) == separator_map.end()) {
separator_map[value] = "sep" + to_string(separator_count++);
}
token_codes[token_codes_count++] = separator_map[value];
}
}
cout << "Поток токенов:\n";
cout << "---------------------\n";
for (int j = 0; j < token_codes_count; j++) {
cout << token_codes[j] << " ";
}
cout << "\n---------------------\n";
cout << "Всего токенов: " << flow_count << "\n";
cout << "\nТаблица ключевых слов:\n";
for (const auto& entry : keyword_map) {
cout << entry.second << " : " << entry.first << "\n";
}
cout << "\nТаблица идентификаторов:\n";
for (const auto& entry : identifier_map) {
cout << entry.second << " : " << entry.first << "\n";
}
cout << "\nТаблица операторов:\n";
for (const auto& entry : operator_map) {
cout << entry.second << " : " << entry.first << "\n";
}
cout << "\nТаблица разделителей:\n";
for (const auto& entry : separator_map) {
cout << entry.second << " : " << entry.first << "\n";
}
cout << "\nТаблица чисел:\n";
for (const auto& entry : number_map) {
cout << entry.second << " : " << entry.first << "\n";
}
cout << "\nТаблица строк:\n";
for (const auto& entry : string_map) {
cout << entry.second << " : " << entry.first << "\n";
}
return 0;
}