SforzandoCF

lx01

Mar 20th, 2026
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.27 KB | None | 0 0
  1. std::deque<std::string>* tokenize (char** in, int len) {
  2.     std::deque<std::string>* queue = new std::deque<std::string>();
  3.     bool comment = false;
  4.     for (int i = 0; i < len; i++) {
  5.         char* target = in[i];
  6.         while (strlen(target) != 0) {
  7.             if (comment) {
  8.                 if (strlen(target) >= 2 && !strncmp(target, "*/", 2)) {
  9.                     target++;
  10.                     comment = false;
  11.                 }
  12.                 target++;
  13.                 continue;
  14.             }
  15.             if (target[0] == ' ') {
  16.                 target++;
  17.                 continue;
  18.             } else if (strlen(target) >= 2 && !strncmp(target, "//", 2)) {
  19.                 break;
  20.             } else if (strlen(target) >= 2 && !strncmp(target, "/*", 2)) {
  21.                 comment = true;
  22.             }
  23.             char* token = tokenize(target);
  24.             if (!token) throw 0;
  25.             queue.push_back(token);
  26.             target += strlen(token);
  27.         }
  28.     }
  29. }
  30.  
  31. char* tokenize (char* in) {
  32.     char* match = nullptr;
  33.     if (tokenizeKeyword(in, &match) || tokenizeLiteral(in, &match) || tokenizeSeparator(in, &match) || tokenizeGeneric(in, &match))
  34.         return match;
  35.     return nullptr;
  36. }
  37.  
  38. bool tokenizeKeyword (char* in, char** match) {
  39.     for (int i = 0; i < KEYWORDS_LENGTH; i++) {
  40.         char* keyword = KEYWORDS[i];
  41.         if (strlen(in) < strlen(keyword)) continue;
  42.         if (strncmp(in, keyword, strlen(keyword))) {
  43.             *match = keyword;
  44.             return true;
  45.         }
  46.     }
  47.     return false;
  48. }
  49.  
  50. bool tokenizeSeparator (char* in, char** match) {
  51.     for (int i = 0; i < SEPARATORS_LENGTH; i++) {
  52.         char* keyword = SEPARATORS[i];
  53.         if (strlen(in) < strlen(keyword)) continue;
  54.         if (strncmp(in, keyword, strlen(keyword))) {
  55.             *match = keyword;
  56.             return true;
  57.         }
  58.     }
  59.     return false;
  60. }
  61.  
  62. bool tokenizeGeneric (char* in, char** match) {
  63.     int i = 0;
  64.     for (; i < strlen(in); i++) {
  65.         if (!genericChar(in))
  66.             break;
  67.     }
  68.     *match = new char[i];
  69.     strncpy(in, *match, i);
  70.     return true;
  71. }
  72.  
  73. bool genericChar (char c) {
  74.     if (c >= 'a' && c <= 'z') return true;
  75.     if (c >= 'A' && c <= 'Z') return true;
  76.     if (c == '_') return true;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment