Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <string>
- #include <sstream>
- #include <unordered_map>
- std::unordered_map<std::string, std::string> agrupacion;
- int esNumero(std::string& s, int i, bool anterior) {
- int idx = i;
- if (isdigit(s[i]) || (s[i] == '-' && isdigit(s[i + 1])) /*|| (s[i] == '+' && isdigit(s[i + 1]))*/) {
- idx++;
- while (isdigit(s[idx])) idx++;
- if (s[idx] == '.') {
- idx++;
- while (isdigit(s[idx])) idx++;
- if (s[idx] == 'e') {
- if (s[idx + 1] == '-' && isdigit(s[idx + 2])) {
- idx += 2;
- while (isdigit(s[idx])) idx++;
- } else if (isdigit(s[idx + 1])) {
- idx++;
- while (isdigit(s[idx])) idx++;
- }
- }
- } else if (s[idx] == 'e') {
- if (s[idx + 1] == '-' && isdigit(s[idx + 2])) {
- idx += 2;
- while (isdigit(s[idx])) idx++;
- } else if (isdigit(s[idx + 1])) {
- idx++;
- while (isdigit(s[idx])) idx++;
- }
- }
- }
- if (idx != i && anterior == true && s[i] == '-') {
- std::cout << "ARITHOP ";
- }
- return idx;
- }
- int esIdentificador(std::string& s, int i) {
- int idx = i;
- std::string temp = "";
- if (isalpha(s[i]) || s[i] == '_') {
- while (isalpha(s[idx]) || s[idx] == '_' || isdigit(s[idx])) temp += s[idx++];
- auto it = agrupacion.find(temp);
- if (it != agrupacion.end( )) {
- std::cout << it -> second << " ";
- } else {
- std::cout << "ID ";
- }
- return idx;
- }
- return i;
- }
- int esRelacional(std::string& s, int i) {
- int idx = i;
- if (s[idx] == '<' || s[idx] == '>') {
- if (s[idx + 1] == '=') {
- idx += 1;
- }
- return idx + 1;
- } else if (s[idx] == '=' && s[idx + 1] == '=') {
- return idx + 2;
- }
- return i;
- }
- int esAritmetico(std::string& s, int i) {
- return i + (s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/' || s[i] == '%');
- }
- int esLogico(std::string& s, int i) {
- if ((s[i] == '&' && s[i + 1] == '&') || (s[i] == '|' && s[i + 1] == '|')) {
- return i + 2;
- }
- return i;
- }
- int esAsignacion(std::string& s, int i) {
- return i + (s[i] == '=');
- }
- int esAgrupacion(std::string& s, int i) {
- std::string temp = "";
- temp += s[i];
- auto it = agrupacion.find(temp);
- if (it != agrupacion.end( )) {
- std::cout << it -> second << " ";
- return i + 1;
- }
- return i;
- }
- int esPuntuacion(std::string& s, int i) {
- return i + (s[i] == ';');
- }
- int main( ) {
- std::ios_base::sync_with_stdio(false);
- std::cin.tie(nullptr);
- std::cout.tie(nullptr);
- //std::freopen("in.txt", "r", stdin);
- std::string line;
- agrupacion["("] = "LPAR";
- agrupacion[")"] = "RPAR";
- agrupacion["["] = "LBRACKET";
- agrupacion["]"] = "RBRACKET";
- agrupacion["{"] = "LCURLY";
- agrupacion["}"] = "RCURLY";
- agrupacion["if"] = "IF";
- agrupacion["else"] = "ELSE";
- agrupacion["do"] = "DO";
- agrupacion["while"] = "WHILE";
- agrupacion["break"] = "BREAK";
- agrupacion["<"] = "RELOP";
- agrupacion[">"] = "RELOP";
- agrupacion["<="] = "RELOP";
- agrupacion[">="] = "RELOP";
- agrupacion["=="] = "RELOP";
- agrupacion["+"] = "ARITHOP";
- agrupacion["-"] = "ARITHOP";
- agrupacion["*"] = "ARITHOP";
- agrupacion["/"] = "ARITHOP";
- agrupacion["%"] = "ARITHOP";
- agrupacion["bool"] = "BOOL";
- agrupacion["true"] = "TRUE";
- agrupacion["false"] = "FALSE";
- agrupacion["num"] = "NUM";
- agrupacion["="] = "ASSIGN";
- int T = 1;
- for (; std::getline(std::cin, line); ++T) {
- std::cout << T << " ";
- std::stringstream ss(line);
- std::string act;
- while (ss >> act) {
- bool antID = false;
- auto it = agrupacion.find(act);
- if (it != agrupacion.end( )) {
- std::cout << it->second << " ";
- } else {
- for (int i = 0; i < act.size( ); ++i) {
- int idx = i;
- idx = esNumero(act, i, antID);
- if (idx != i) {
- std::cout << "LNUM ";
- antID = true;
- i += (idx - i - 1);
- } else {
- idx = esIdentificador(act, i);
- if (idx != i) {
- i += (idx - i - 1);
- antID = true;
- } else {
- idx = esRelacional(act, i);
- if (idx != i) {
- i += (idx - i - 1);
- std::cout << "RELOP ";
- antID = false;
- } else {
- idx = esAritmetico(act, i);
- if (idx != i) {
- i += (idx - i - 1);
- std::cout << "ARITHOP ";
- antID = false;
- } else {
- idx = esLogico(act, i);
- if (idx != i) {
- i += (idx - i - 1);
- std::cout << "LOGOP ";
- antID = false;
- } else {
- idx = esAsignacion(act, i);
- if (idx != i) {
- i += (idx - i - 1);
- std::cout << "ASSIGN ";
- antID = false;
- } else {
- idx = esAgrupacion(act, i);
- if (idx != i) {
- i += (idx - i - 1);
- antID = false;
- } else {
- idx = esPuntuacion(act, i);
- if (idx != i) {
- i += (idx - i - 1);
- std::cout << "SEMICOLON ";
- antID = false;
- } else { // ERROR
- std::cout << "\nERROR LINE " << T << "\n";
- return 0;
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- std::cout << "\n";
- //std::getline(std::cin, line);
- }
- std::cout << T << "\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement