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"
  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;
  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. // cout << newS << endl;
  51. if (cppOperators.find(newS) != cppOperators.end()) {
  52. tokens.push_back(newS);
  53. tmp = "";
  54. i++;
  55. return true;
  56. }
  57. }
  58. return false;
  59. }
  60.  
  61. void getToken(string s) {
  62. string tmp;
  63. int cnt = 0;
  64. for (int i = 0; i < s.size(); i++) {
  65. if (isNumber(s[i])) {
  66. string num;
  67. while (i < s.size() and (isNumber(s[i]) or (s[i] == '.'))) {
  68. num += s[i];
  69. i++;
  70. }
  71. tokens.push_back(num);
  72. }
  73.  
  74. if (isSymbol(s[i])) {
  75. if (
  76. i - 1 != -1 and
  77. i + 1 != s.size() and
  78. s[i - 1] != ' ' and
  79. s[i + 1] != ' '
  80. ) {
  81. if (tmp.size()) tokens.push_back(tmp);
  82. string str;
  83. str += s[i];
  84.  
  85. if (handle_operator(s, i, tmp)) continue;
  86.  
  87. tokens.push_back(str);
  88.  
  89. tmp = "";
  90.  
  91. continue;
  92. }
  93. else {
  94. string str;
  95. str += s[i];
  96.  
  97. if (tmp.size()) tokens.push_back(tmp);
  98.  
  99. if (handle_operator(s, i, tmp)) continue;
  100.  
  101. tokens.push_back(str);
  102.  
  103. tmp = "";
  104. continue;
  105. }
  106. }
  107. else if (s[i] == ' ' and !isSymbol(s[i - 1])) {
  108.  
  109. if (tmp.size()) tokens.push_back(tmp);
  110. tmp = "";
  111. }
  112.  
  113. if ((s[i] != ' ') and (static_cast<int>(s[i] != 9))) {
  114.  
  115. tmp += s[i];
  116. }
  117.  
  118. }
  119. }
  120.  
  121. vector < string > getKeywords() {
  122.  
  123. vector < string > keywords;
  124. for (auto el: tokens) {
  125.  
  126. if (cppKeywords.find(el) != cppKeywords.end())
  127. keywords.push_back(el);
  128.  
  129. }
  130.  
  131. return keywords;
  132.  
  133. }
  134.  
  135. vector < string > getIdentifier() {
  136.  
  137. vector < string > identifier;
  138.  
  139. for (auto el: tokens) {
  140. if (cppKeywords.find(el) == cppKeywords.end() and !isSymbol(el) and !isNumber(el) and !isFloat(el) and cppOperators.find(el) == cppOperators.end())
  141. identifier.push_back(el);
  142. }
  143.  
  144. return identifier;
  145. }
  146.  
  147. vector < string > getOperator() {
  148. vector < string > operators;
  149. for (auto el: tokens) {
  150. if (cppOperators.find(el) != cppOperators.end())
  151. operators.push_back(el);
  152. }
  153.  
  154. return operators;
  155. }
  156.  
  157. vector < string > getSymbols() {
  158. vector < string > symbols;
  159. for (auto el: tokens) {
  160. if (isSymbol(el) and (cppOperators.find(el) == cppOperators.end()))
  161. symbols.push_back(el);
  162. }
  163.  
  164. return symbols;
  165. }
  166.  
  167. vector < string > getNumbers() {
  168. vector < string > numbers;
  169. for (auto el: tokens) {
  170. if (isNumber(el))
  171. numbers.push_back(el);
  172. }
  173. return numbers;
  174. }
  175.  
  176. vector < string > getFloats() {
  177. vector < string > floats;
  178. for (auto el: tokens) {
  179. if (isFloat(el))
  180. floats.push_back(el);
  181. }
  182. return floats;
  183. }
  184.  
  185. int main() {
  186.  
  187. string s;
  188. int token = 0;
  189.  
  190. while (getline(cin, s)) {
  191.  
  192. getToken(s);
  193.  
  194. }
  195.  
  196. vector < string > keywords = getKeywords();
  197. vector < string > identifiers = getIdentifier();
  198. vector < string > operators = getOperator();
  199. vector < string > symbols = getSymbols();
  200. vector < string > numbers = getNumbers();
  201. vector < string > floats = getFloats();
  202.  
  203. cout << "Total Numbers of Tokens\t\t:" << tokens.size() << endl;
  204. cout << "Keywords\t\t\t\t\t:" << keywords.size() << " ";
  205. for (auto el: keywords) cout << el << " ";
  206. cout << endl;
  207. cout << "Identifiers\t\t\t\t\t:" << identifiers.size() << " ";
  208. for (auto el: identifiers) cout << el << " ";
  209. cout << endl;
  210. cout << "Operators\t\t\t\t\t:" << operators.size() << " ";
  211. for (auto el: operators) cout << el << " ";
  212. cout << endl;
  213. cout << "Symbols\t\t\t\t\t\t:" << symbols.size() << " ";
  214. for (auto el: symbols) cout << el << " ";
  215. cout << endl;
  216. cout << "Integer Constants\t\t\t:" << numbers.size() << " ";
  217. for (auto el: numbers) cout << el << " ";
  218. cout << endl;
  219. cout << "Floating Point Constants\t:" << floats.size() << " ";
  220. for (auto el: floats) cout << el << " ";
  221. cout << endl;
  222.  
  223. return 0;
  224. }
Success #stdin #stdout 0s 5288KB
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