Advertisement
AlejandroGY

Codigo feo

Oct 7th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.43 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <string>
  4. #include <sstream>
  5. #include <unordered_map>
  6.  
  7. std::unordered_map<std::string, std::string> agrupacion;
  8.  
  9. int esNumero(std::string& s, int i, bool anterior) {
  10.    int idx = i;
  11.    if (isdigit(s[i]) || (s[i] == '-' && isdigit(s[i + 1])) /*|| (s[i] == '+' && isdigit(s[i + 1]))*/) {
  12.       idx++;
  13.       while (isdigit(s[idx])) idx++;
  14.  
  15.       if (s[idx] == '.') {
  16.          idx++;
  17.          while (isdigit(s[idx])) idx++;
  18.  
  19.          if (s[idx] == 'e') {
  20.             if (s[idx + 1] == '-' && isdigit(s[idx + 2])) {
  21.                idx += 2;
  22.                while (isdigit(s[idx])) idx++;
  23.             } else if (isdigit(s[idx + 1])) {
  24.                idx++;
  25.                while (isdigit(s[idx])) idx++;
  26.             }
  27.          }
  28.       } else if (s[idx] == 'e') {
  29.          if (s[idx + 1] == '-' && isdigit(s[idx + 2])) {
  30.             idx += 2;
  31.             while (isdigit(s[idx])) idx++;
  32.          } else if (isdigit(s[idx + 1])) {
  33.             idx++;
  34.             while (isdigit(s[idx])) idx++;
  35.          }
  36.       }
  37.    }
  38.    
  39.     if (idx != i && anterior == true && s[i] == '-') {
  40.         std::cout << "ARITHOP ";
  41.     }
  42.    return idx;
  43. }
  44.  
  45. int esIdentificador(std::string& s, int i) {
  46.    int idx = i;
  47.    std::string temp = "";
  48.    if (isalpha(s[i]) || s[i] == '_') {
  49.       while (isalpha(s[idx]) || s[idx] == '_' || isdigit(s[idx])) temp += s[idx++];
  50.       auto it = agrupacion.find(temp);
  51.       if (it != agrupacion.end( )) {
  52.          std::cout << it -> second << " ";
  53.       } else {
  54.          std::cout << "ID ";
  55.       }
  56.       return idx;
  57.    }
  58.    return i;
  59. }
  60.  
  61. int esRelacional(std::string& s, int i) {
  62.    int idx = i;
  63.    if (s[idx] == '<' || s[idx] == '>') {
  64.       if (s[idx + 1] == '=') {
  65.          idx += 1;
  66.       }
  67.       return idx + 1;
  68.    } else if (s[idx] == '=' && s[idx + 1] == '=') {
  69.       return idx + 2;
  70.    }
  71.    return i;
  72. }
  73.  
  74. int esAritmetico(std::string& s, int i) {
  75.    return i + (s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/' || s[i] == '%');
  76. }
  77.  
  78. int esLogico(std::string& s, int i) {
  79.    if ((s[i] == '&' && s[i + 1] == '&') || (s[i] == '|' && s[i + 1] == '|')) {
  80.       return i + 2;
  81.    }
  82.    return i;
  83. }
  84.  
  85. int esAsignacion(std::string& s, int i) {
  86.    return i + (s[i] == '=');
  87. }
  88.  
  89. int esAgrupacion(std::string& s, int i) {
  90.    std::string temp = "";
  91.    temp += s[i];
  92.  
  93.    auto it = agrupacion.find(temp);
  94.    if (it != agrupacion.end( )) {
  95.       std::cout << it -> second << " ";
  96.       return i + 1;
  97.    }
  98.    return i;
  99. }
  100.  
  101. int esPuntuacion(std::string& s, int i) {
  102.    return i + (s[i] == ';');
  103. }
  104.  
  105. int main( ) {
  106.    std::ios_base::sync_with_stdio(false);
  107.    std::cin.tie(nullptr);
  108.    std::cout.tie(nullptr);
  109.  
  110.    //std::freopen("in.txt", "r", stdin);
  111.  
  112.    std::string line;
  113.  
  114.    agrupacion["("] = "LPAR";
  115.    agrupacion[")"] = "RPAR";
  116.    agrupacion["["] = "LBRACKET";
  117.    agrupacion["]"] = "RBRACKET";
  118.    agrupacion["{"] = "LCURLY";
  119.    agrupacion["}"] = "RCURLY";
  120.  
  121.    agrupacion["if"] = "IF";
  122.    agrupacion["else"] = "ELSE";
  123.    agrupacion["do"] = "DO";
  124.    agrupacion["while"] = "WHILE";
  125.    agrupacion["break"] = "BREAK";
  126.  
  127.    agrupacion["<"] = "RELOP";
  128.    agrupacion[">"] = "RELOP";
  129.    agrupacion["<="] = "RELOP";
  130.    agrupacion[">="] = "RELOP";
  131.    agrupacion["=="] = "RELOP";
  132.  
  133.    agrupacion["+"] = "ARITHOP";
  134.    agrupacion["-"] = "ARITHOP";
  135.    agrupacion["*"] = "ARITHOP";
  136.    agrupacion["/"] = "ARITHOP";
  137.    agrupacion["%"] = "ARITHOP";
  138.  
  139.    agrupacion["bool"] = "BOOL";
  140.    agrupacion["true"] = "TRUE";
  141.    agrupacion["false"] = "FALSE";
  142.  
  143.    agrupacion["num"] = "NUM";
  144.    agrupacion["="] = "ASSIGN";
  145.  
  146.    int T = 1;
  147.    for (; std::getline(std::cin, line); ++T) {
  148.       std::cout << T << " ";
  149.       std::stringstream ss(line);
  150.  
  151.       std::string act;
  152.       while (ss >> act) {
  153.             bool antID = false;
  154.          auto it = agrupacion.find(act);
  155.          if (it != agrupacion.end( )) {
  156.             std::cout << it->second << " ";
  157.          } else {
  158.             for (int i = 0; i < act.size( ); ++i) {
  159.                int idx = i;
  160.                idx = esNumero(act, i, antID);
  161.                if (idx != i) {
  162.                   std::cout << "LNUM ";
  163.                         antID = true;
  164.                   i += (idx - i - 1);
  165.                } else {
  166.                   idx = esIdentificador(act, i);
  167.                   if (idx != i) {
  168.                      i += (idx - i - 1);
  169.                             antID = true;
  170.                   } else {
  171.                      idx = esRelacional(act, i);
  172.                      if (idx != i) {
  173.                         i += (idx - i - 1);
  174.                         std::cout << "RELOP ";
  175.                                 antID = false;
  176.                      } else {
  177.                         idx = esAritmetico(act, i);
  178.                         if (idx != i) {
  179.                            i += (idx - i - 1);
  180.                            std::cout << "ARITHOP ";
  181.                                     antID = false;
  182.                         } else {
  183.                            idx = esLogico(act, i);
  184.                            if (idx != i) {
  185.                               i += (idx - i - 1);
  186.                               std::cout << "LOGOP ";
  187.                                         antID = false;
  188.                            } else {
  189.                               idx = esAsignacion(act, i);
  190.                               if (idx != i) {
  191.                                  i += (idx - i - 1);
  192.                                  std::cout << "ASSIGN ";
  193.                                             antID = false;
  194.                               } else {
  195.                                  idx = esAgrupacion(act, i);
  196.                                  if (idx != i) {
  197.                                     i += (idx - i - 1);
  198.                                                 antID = false;
  199.                                  } else {
  200.                                     idx = esPuntuacion(act, i);
  201.                                     if (idx != i) {
  202.                                        i += (idx - i - 1);
  203.                                        std::cout << "SEMICOLON ";
  204.                                                     antID = false;
  205.                                     } else { // ERROR
  206.                                        std::cout << "\nERROR LINE " << T << "\n";
  207.                                        return 0;
  208.                                     }
  209.                                  }
  210.                               }
  211.                            }
  212.                         }
  213.                      }
  214.                   }
  215.                }
  216.             }
  217.          }
  218.       }
  219.       std::cout << "\n";
  220.       //std::getline(std::cin, line);
  221.    }
  222.    std::cout << T << "\n";
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement