fork download
  1. #include <iostream>
  2. #include <sstream>
  3. #include <vector>
  4. #include <map>
  5. #include <set>
  6. #include <cctype>
  7. #include <string>
  8. #include <algorithm>
  9.  
  10. using namespace std;
  11.  
  12. set<char> punc = { ';', ':', ',' };
  13.  
  14. set<char> br = { '(', ')', '{', '}', '[', ']' };
  15.  
  16. set<string> oper = { "+", "-", "*", "/", ">", ">=", "<", "<=", "==", "!=", "=", "&&", "||", "++", "--", "!" };
  17.  
  18. set<string> keywords = {"auto", "break", "case", "char", "const", "continue", "default", "do", "double", "else", "enum", "extern", "float", "for", "goto", "if", "int", "long", "register", "return", "short", "signed", "sizeof", "static", "struct", "switch", "typedef", "union", "unsigned", "void", "volatile", "while"};
  19.  
  20. bool isOp(const string& str) { return oper.find(str) != oper.end(); }
  21.  
  22. bool isBr(char ch) { return br.find(ch) != br.end(); }
  23.  
  24. bool isPunc(char ch) { return punc.find(ch) != punc.end(); }
  25.  
  26. bool isKe(const string& str) { return keywords.find(str) != keywords.end(); }
  27.  
  28. void proTok(const string& token, map<string, set<string>>& tok) {
  29. if (!token.empty()) {
  30. if (isKe(token))
  31. tok["Keyword"].insert(token);
  32. else if (isdigit(token[0]) || (token[0] == '-' && token.size() > 1 && isdigit(token[1])))
  33. tok["Constant"].insert(token);
  34. else
  35. tok["Identifier"].insert(token);
  36. }
  37. }
  38.  
  39. void tok(const string& li, map<string, set<string>>& tok) {
  40. string token;
  41. for (size_t i = 0; i < li.size(); ++i) {
  42. char curr = li[i];
  43. if (isspace(curr) || isPunc(curr) || isBr(curr)) {
  44. proTok(token, tok);
  45. token.clear();
  46. if (isPunc(curr))
  47. tok["Punctuation"].insert(string(1, curr));
  48. if (isBr(curr))
  49. tok["Bracket"].insert(string(1, curr));
  50. } else if (i + 1 < li.size() && isOp(string(1, curr) + string(1, li[i + 1]))) {
  51. proTok(token, tok);
  52. token.clear();
  53. tok["Operator"].insert(string(1, curr) + string(1, li[i + 1]));
  54. i++;
  55. } else if (isOp(string(1, curr))) {
  56. proTok(token, tok);
  57. token.clear();
  58. tok["Operator"].insert(string(1, curr));
  59. } else {
  60. token += curr;
  61. }
  62. }
  63. proTok(token, tok);
  64. }
  65.  
  66. int main() {
  67. map<string, set<string>> tokens; // Renamed from tok to tokens
  68. string li;
  69.  
  70. cout << "To finish input type 'end'!\n";
  71.  
  72. while (getline(cin, li)) {
  73. if (li == "end")
  74. break;
  75. tok(li, tokens); // Updated to use the renamed variable
  76. }
  77.  
  78. for (const auto& kv : tokens) {
  79. cout << kv.first << " <" << kv.second.size() << ">: ";
  80. bool first = true;
  81. for (const auto& token : kv.second) {
  82. cout << (first ? "" : ", ") << token;
  83. first = false;
  84. }
  85. cout << "\n";
  86. }
  87. }
  88.  
  89.  
  90.  
Success #stdin #stdout 0.01s 5272KB
stdin
Standard input is empty
stdout
To finish input type 'end'!