fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <map>
  4. #include <cctype>
  5.  
  6. using namespace std;
  7.  
  8. struct Token {
  9. string type;
  10. string value;
  11. };
  12.  
  13. const int MAX_TOKENS = 1000;
  14. const int MAX_KEYWORDS = 50;
  15. const int MAX_OPERATORS = 50;
  16. const int MAX_SEPARATORS = 50;
  17.  
  18. int main() {
  19. string code = R"(
  20. #include <iostream>
  21. using namespace std;
  22.  
  23. int main() {
  24. int x = 10;
  25. float y = 3.14;
  26. string text = "Hello, World!";
  27.  
  28. if (x > 5 && y < 4.0) {
  29. cout << text << endl;
  30. for (int i = 0; i < x; i++) {
  31. cout << "Iteration: " << i << endl;
  32. }
  33. }
  34.  
  35. return 0;
  36. }
  37. )";
  38.  
  39. cout << "Исходный код:\n";
  40. cout << code << "\n";
  41. cout << "---------------------\n";
  42.  
  43. string keywords[MAX_KEYWORDS] = {
  44. "#include", "using", "namespace", "int", "float", "double",
  45. "string", "if", "else", "for", "while", "return", "cout", "endl"
  46. };
  47. int keywords_count = 14;
  48.  
  49. string operators[MAX_OPERATORS] = {
  50. "++", "--", "+=", "-=", "*=", "/=", "==", "!=", "<=", ">=",
  51. "&&", "||", "<<", ">>", "+", "-", "*", "/", "=", ">", "<", "&"
  52. };
  53. int operators_count = 22;
  54.  
  55. char separators[MAX_SEPARATORS] = {
  56. ';', '(', ')', '{', '}', '[', ']', ',', '<', '>'
  57. };
  58. int separators_count = 10;
  59.  
  60. Token flow[MAX_TOKENS];
  61. int flow_count = 0;
  62. size_t i = 0;
  63. int condition = 0;
  64.  
  65. while (i < code.length() && flow_count < MAX_TOKENS) {
  66. char ch = code[i];
  67.  
  68. if (ch == ' ' || ch == '\n' || ch == '\t') {
  69. i++;
  70. continue;
  71. }
  72.  
  73. // Поиск ключевых слов
  74. bool found_keyword = false;
  75. for (int j = 0; j < keywords_count; j++) {
  76. string keyword = keywords[j];
  77. if (code.compare(i, keyword.length(), keyword) == 0) {
  78. flow[flow_count].type = "keyword";
  79. flow[flow_count].value = keyword;
  80. flow_count++;
  81. i += keyword.length();
  82. found_keyword = true;
  83. condition = 1;
  84. break;
  85. }
  86. }
  87. if (found_keyword) continue;
  88.  
  89. // Поиск операторов
  90. bool found_operator = false;
  91. for (int j = 0; j < operators_count; j++) {
  92. string op = operators[j];
  93. if (code.compare(i, op.length(), op) == 0) {
  94. flow[flow_count].type = "operator";
  95. flow[flow_count].value = op;
  96. flow_count++;
  97. i += op.length();
  98. found_operator = true;
  99. condition = 4;
  100. break;
  101. }
  102. }
  103. if (found_operator) continue;
  104.  
  105. // Поиск разделителей
  106. bool found_separator = false;
  107. for (int j = 0; j < separators_count; j++) {
  108. if (ch == separators[j]) {
  109. flow[flow_count].type = "separator";
  110. flow[flow_count].value = string(1, ch);
  111. flow_count++;
  112. i++;
  113. found_separator = true;
  114. condition = 5;
  115. break;
  116. }
  117. }
  118. if (found_separator) continue;
  119.  
  120. // Поиск чисел
  121. if (isdigit(ch)) {
  122. size_t start = i;
  123. while (i < code.length() && (isdigit(code[i]) || code[i] == '.')) {
  124. i++;
  125. }
  126. string number = code.substr(start, i - start);
  127. flow[flow_count].type = "number";
  128. flow[flow_count].value = number;
  129. flow_count++;
  130. condition = 7;
  131. continue;
  132. }
  133.  
  134. // Поиск строк
  135. if (ch == '"') {
  136. size_t start = i;
  137. i++;
  138. while (i < code.length() && code[i] != '"') {
  139. i++;
  140. }
  141. if (i < code.length()) {
  142. string str_content = code.substr(start, i - start + 1);
  143. flow[flow_count].type = "string";
  144. flow[flow_count].value = str_content;
  145. flow_count++;
  146. i++;
  147. } else {
  148. // Незакрытая строка
  149. string str_content = code.substr(start);
  150. flow[flow_count].type = "string";
  151. flow[flow_count].value = str_content;
  152. flow_count++;
  153. }
  154. condition = 6;
  155. continue;
  156. }
  157.  
  158. // Поиск идентификаторов
  159. if (isalpha(ch) || ch == '_') {
  160. size_t start = i;
  161. while (i < code.length() && (isalnum(code[i]) || code[i] == '_')) {
  162. i++;
  163. }
  164. string identifier = code.substr(start, i - start);
  165. flow[flow_count].type = "identifier";
  166. flow[flow_count].value = identifier;
  167. flow_count++;
  168. condition = 3;
  169. continue;
  170. }
  171.  
  172. i++;
  173. }
  174. condition = 8;
  175.  
  176. string token_codes[MAX_TOKENS];
  177. int token_codes_count = 0;
  178.  
  179. int keyword_count = 1;
  180. int identifier_count = 1;
  181. int number_count = 1;
  182. int string_count = 1;
  183. int operator_count = 1;
  184. int separator_count = 1;
  185.  
  186. map<string, string> keyword_map;
  187. map<string, string> identifier_map;
  188. map<string, string> number_map;
  189. map<string, string> string_map;
  190. map<string, string> operator_map;
  191. map<string, string> separator_map;
  192.  
  193. for (int j = 0; j < flow_count; j++) {
  194. string type = flow[j].type;
  195. string value = flow[j].value;
  196.  
  197. if (type == "keyword") {
  198. if (keyword_map.find(value) == keyword_map.end()) {
  199. keyword_map[value] = "k" + to_string(keyword_count++);
  200. }
  201. token_codes[token_codes_count++] = keyword_map[value];
  202. }
  203. else if (type == "identifier") {
  204. if (identifier_map.find(value) == identifier_map.end()) {
  205. identifier_map[value] = "i" + to_string(identifier_count++);
  206. }
  207. token_codes[token_codes_count++] = identifier_map[value];
  208. }
  209. else if (type == "number") {
  210. if (number_map.find(value) == number_map.end()) {
  211. number_map[value] = "n" + to_string(number_count++);
  212. }
  213. token_codes[token_codes_count++] = number_map[value];
  214. }
  215. else if (type == "string") {
  216. if (string_map.find(value) == string_map.end()) {
  217. string_map[value] = "str" + to_string(string_count++);
  218. }
  219. token_codes[token_codes_count++] = string_map[value];
  220. }
  221. else if (type == "operator") {
  222. if (operator_map.find(value) == operator_map.end()) {
  223. operator_map[value] = "o" + to_string(operator_count++);
  224. }
  225. token_codes[token_codes_count++] = operator_map[value];
  226. }
  227. else if (type == "separator") {
  228. if (separator_map.find(value) == separator_map.end()) {
  229. separator_map[value] = "sep" + to_string(separator_count++);
  230. }
  231. token_codes[token_codes_count++] = separator_map[value];
  232. }
  233. }
  234.  
  235. cout << "Поток токенов:\n";
  236. cout << "---------------------\n";
  237.  
  238. for (int j = 0; j < token_codes_count; j++) {
  239. cout << token_codes[j] << " ";
  240. }
  241.  
  242. cout << "\n---------------------\n";
  243. cout << "Всего токенов: " << flow_count << "\n";
  244.  
  245. cout << "\nТаблица ключевых слов:\n";
  246. for (const auto& entry : keyword_map) {
  247. cout << entry.second << " : " << entry.first << "\n";
  248. }
  249.  
  250. cout << "\nТаблица идентификаторов:\n";
  251. for (const auto& entry : identifier_map) {
  252. cout << entry.second << " : " << entry.first << "\n";
  253. }
  254.  
  255. cout << "\nТаблица операторов:\n";
  256. for (const auto& entry : operator_map) {
  257. cout << entry.second << " : " << entry.first << "\n";
  258. }
  259.  
  260. cout << "\nТаблица разделителей:\n";
  261. for (const auto& entry : separator_map) {
  262. cout << entry.second << " : " << entry.first << "\n";
  263. }
  264.  
  265. cout << "\nТаблица чисел:\n";
  266. for (const auto& entry : number_map) {
  267. cout << entry.second << " : " << entry.first << "\n";
  268. }
  269.  
  270. cout << "\nТаблица строк:\n";
  271. for (const auto& entry : string_map) {
  272. cout << entry.second << " : " << entry.first << "\n";
  273. }
  274.  
  275. return 0;
  276. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Исходный код:

#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;
}

---------------------
Поток токенов:
---------------------
k1 o1 i1 o2 k2 k3 i2 sep1 k4 i3 sep2 sep3 sep4 k4 i4 o3 n1 sep1 k5 i5 o3 n2 sep1 k6 i6 o3 str1 sep1 k7 sep2 i4 o2 n3 o4 i5 o1 n4 sep3 sep4 k8 o5 i6 o5 k9 sep1 k10 sep2 k4 i7 o3 n5 sep1 i7 o1 i4 sep1 i7 o6 sep3 sep4 k8 o5 str2 o5 i7 o5 k9 sep1 sep5 sep5 k11 n5 sep1 sep5 
---------------------
Всего токенов: 74

Таблица ключевых слов:
k1 : #include
k8 : cout
k9 : endl
k5 : float
k10 : for
k7 : if
k4 : int
k3 : namespace
k11 : return
k6 : string
k2 : using

Таблица идентификаторов:
i7 : i
i1 : iostream
i3 : main
i2 : std
i6 : text
i4 : x
i5 : y

Таблица операторов:
o4 : &&
o6 : ++
o1 : <
o5 : <<
o3 : =
o2 : >

Таблица разделителей:
sep2 : (
sep3 : )
sep1 : ;
sep4 : {
sep5 : }

Таблица чисел:
n5 : 0
n1 : 10
n2 : 3.14
n4 : 4.0
n3 : 5

Таблица строк:
str1 : "Hello, World!"
str2 : "Iteration: "