fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. set < string > cppKeywords = {
  5. "alignas", "alignof", "and", "and_eq", "asm", "atomic_cancel", "atomic_commit", "atomic_noexcept",
  6. "auto", "bitand", "bitor", "bool", "break", "case", "catch", "char", "char16_t", "char32_t", "class",
  7. "compl", "concept", "const", "consteval", "constexpr", "const_cast", "continue", "co_await", "co_return",
  8. "co_yield", "decltype", "default", "delete", "do", "double", "dynamic_cast", "else", "enum", "explicit",
  9. "export", "extern", "false", "float", "for", "friend", "goto", "if", "import", "inline", "int", "long",
  10. "mutable", "namespace", "new", "noexcept", "not", "not_eq", "nullptr", "operator", "or", "or_eq",
  11. "private", "protected", "public", "register", "reinterpret_cast", "requires", "return", "short", "signed",
  12. "sizeof", "static", "static_assert", "static_cast", "struct", "switch", "template", "this", "thread_local",
  13. "throw", "true", "try", "typedef", "typeid", "typename", "union", "unsigned", "using", "virtual", "void",
  14. "volatile", "wchar_t", "while", "xor", "xor_eq", "cout", "cin", "endl"
  15. };
  16.  
  17. set< string > cppOperators = {
  18. "+", "-", "*", "/", "%", "++", "--", "=", "+=", "-=", "*=", "/=", "%=",
  19. "==", "!=", "<", ">", "<=", ">=", "&&", "||", "!", "&", "|", "^", "~",
  20. "<<", ">>", ">>=", "<<=", "->", "&=", "|=", "^="
  21. };
  22.  
  23. vector < string > tokens;
  24.  
  25. bool isSymbol(char ch) {return !isalpha(ch) and !isdigit(ch) and (ch != ' ') and (static_cast<int>(ch) != 9);}
  26. bool isSymbol(string ch) {return (ch.size() > 1) ? false: isSymbol(ch[0]);}
  27. bool isNumber(char ch) {return ch >= '0' and ch <= '9';}
  28. bool isNumber(string s) {
  29. for (int i = 0; i < s.size(); i++) {
  30. if (s[i] < '0' or s[i] > '9')
  31. return false;
  32. }
  33. return true;
  34. }
  35.  
  36. bool isFloat(string s) {
  37. bool point = false;
  38. for (int i = 0; i < s.size(); i++) {
  39. if (s[i] == '.') point = true;
  40. else if (s[i] < '0' and s[i] > '9') return false;
  41. }
  42. return point and (s != ".");
  43. }
  44.  
  45. bool handle_operator(string s, int &i, string &tmp) {
  46. if ((i < s.size() - 1)) {
  47. string newS;
  48. newS += s[i];
  49. newS += s[i + 1];
  50. if (cppOperators.find(newS) != cppOperators.end()) {
  51.  
  52. if (i < s.size() - 2) {
  53. newS += s[i + 2];
  54. if (cppOperators.find(newS) != cppOperators.end()) {
  55. tokens.push_back(newS);
  56. tmp = "";
  57. i += 2;
  58. return true;
  59. }
  60. newS.pop_back();
  61. }
  62.  
  63. tokens.push_back(newS);
  64. tmp = "";
  65. i++;
  66. return true;
  67. }
  68. }
  69. return false;
  70. }
  71.  
  72. void getToken(string s) {
  73. string tmp;
  74. int cnt = 0;
  75. for (int i = 0; i < s.size(); i++) {
  76. if (isNumber(s[i]) or s[i] == '.') {
  77. string num;
  78. while (i < s.size() and (isNumber(s[i]) or (s[i] == '.'))) {
  79. num += s[i];
  80. i++;
  81. }
  82. if (num != ".") {
  83. tokens.push_back(num);
  84. }
  85. else i--;
  86. }
  87.  
  88. if (isSymbol(s[i])) {
  89. if (
  90. i - 1 != -1 and
  91. i + 1 != s.size() and
  92. s[i - 1] != ' ' and
  93. s[i + 1] != ' '
  94. ) {
  95. if (tmp.size()) tokens.push_back(tmp);
  96. string str;
  97. str += s[i];
  98.  
  99. if (handle_operator(s, i, tmp)) continue;
  100.  
  101. tokens.push_back(str);
  102.  
  103. tmp = "";
  104.  
  105. continue;
  106. }
  107. else {
  108. string str;
  109. str += s[i];
  110.  
  111. if (tmp.size()) tokens.push_back(tmp);
  112.  
  113. if (handle_operator(s, i, tmp)) continue;
  114.  
  115. tokens.push_back(str);
  116.  
  117. tmp = "";
  118. continue;
  119. }
  120. }
  121. else if (s[i] == ' ' and !isSymbol(s[i - 1])) {
  122.  
  123. if (tmp.size()) tokens.push_back(tmp);
  124. tmp = "";
  125. }
  126.  
  127. if ((s[i] != ' ') and (static_cast<int>(s[i] != 9))) {
  128.  
  129. tmp += s[i];
  130. }
  131.  
  132. }
  133. }
  134.  
  135. vector < string > getKeywords() {
  136.  
  137. vector < string > keywords;
  138. for (auto el: tokens) {
  139.  
  140. if (cppKeywords.find(el) != cppKeywords.end())
  141. keywords.push_back(el);
  142.  
  143. }
  144.  
  145. return keywords;
  146.  
  147. }
  148.  
  149. vector < string > getIdentifier() {
  150.  
  151. vector < string > identifier;
  152.  
  153. for (auto el: tokens) {
  154. if (cppKeywords.find(el) == cppKeywords.end() and !isSymbol(el) and !isNumber(el) and !isFloat(el) and cppOperators.find(el) == cppOperators.end())
  155. identifier.push_back(el);
  156. }
  157.  
  158. return identifier;
  159. }
  160.  
  161. vector < string > getOperator() {
  162. vector < string > operators;
  163.  
  164. for (auto el: tokens) {
  165. if (cppOperators.find(el) != cppOperators.end()) {
  166. operators.push_back(el);
  167. }
  168. }
  169.  
  170. return operators;
  171. }
  172.  
  173. vector < string > getSymbols() {
  174. vector < string > symbols;
  175. for (auto el: tokens) {
  176. if (isSymbol(el) and (cppOperators.find(el) == cppOperators.end()))
  177. symbols.push_back(el);
  178. }
  179.  
  180. return symbols;
  181. }
  182.  
  183. vector < string > getNumbers() {
  184. vector < string > numbers;
  185. for (auto el: tokens) {
  186. if (isNumber(el))
  187. numbers.push_back(el);
  188. }
  189. return numbers;
  190. }
  191.  
  192. vector < string > getFloats() {
  193. vector < string > floats;
  194. for (auto el: tokens) {
  195. if (isFloat(el))
  196. floats.push_back(el);
  197. }
  198. return floats;
  199. }
  200.  
  201. int main() {
  202.  
  203. string s;
  204. int token = 0;
  205.  
  206. while (getline(cin, s)) {
  207.  
  208. getToken(s);
  209.  
  210. }
  211.  
  212. vector < string > keywords = getKeywords();
  213. vector < string > identifiers = getIdentifier();
  214. vector < string > operators = getOperator();
  215. vector < string > symbols = getSymbols();
  216. vector < string > numbers = getNumbers();
  217. vector < string > floats = getFloats();
  218.  
  219. cout << "Total Numbers of Tokens\t\t:" << tokens.size() << endl;
  220. cout << "Keywords\t\t\t\t\t:" << keywords.size() << " ";
  221. for (auto el: keywords) cout << el << " ";
  222. cout << endl;
  223. cout << "Identifiers\t\t\t\t\t:" << identifiers.size() << " ";
  224. for (auto el: identifiers) cout << el << " ";
  225. cout << endl;
  226. cout << "Operators\t\t\t\t\t:" << operators.size() << " ";
  227. for (auto el: operators) cout << el << " ";
  228. cout << endl;
  229. cout << "Symbols\t\t\t\t\t\t:" << symbols.size() << " ";
  230. for (auto el: symbols) cout << el << " ";
  231. cout << endl;
  232. cout << "Integer Constants\t\t\t:" << numbers.size() << " ";
  233. for (auto el: numbers) cout << el << " ";
  234. cout << endl;
  235. cout << "Floating Point Constants\t:" << floats.size() << " ";
  236. for (auto el: floats) cout << el << " ";
  237. cout << endl;
  238.  
  239. return 0;
  240. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Total Numbers of Tokens		:0
Keywords					:0 
Identifiers					:0 
Operators					:0 
Symbols						:0 
Integer Constants			:0 
Floating Point Constants	:0