Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.77 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string>
  4. #include <iostream>
  5. #include <list>
  6. #include <vector>
  7. #include <map>
  8. #include <regex>
  9. #include <assert.h>
  10. #include <fstream>
  11. #include <set>
  12.  
  13. #include <readline/readline.h>
  14. #include <readline/history.h>
  15. #include <boost/regex.hpp>
  16. using namespace std;
  17.  
  18. class Tokenizer {
  19. public:
  20.    
  21.     Tokenizer() {}
  22.  
  23.  
  24. struct TokenExpr {
  25.     boost::regex TOKEN_STRING {"\".*?\""};
  26.     boost::regex TOKEN_PARAM {"\\(.*?\\)"};
  27.     boost::regex TOKEN_BRACKETS {"\\{.*?\\}"};
  28.     boost::regex TOKEN_FILE {"\\w*\\.\\w*"};
  29.     boost::regex TOKEN_WORD {"[^\\W\\s]\\w+"};
  30.     boost::regex TOKEN_INT {"\\b\\d*?\\.*?\\d?\\b"};
  31.     boost::regex TOKEN_DASH {"-\\b\\w+\\s"};
  32.     boost::regex TOKEN_EQUAL {"="};
  33.     boost::regex TOKEN_QUOTE {"\""};
  34.     boost::regex TOKEN_COMMA {","};
  35.     boost::regex TOKEN_OP {"(\\+|-|\\*|\\/|%)"};
  36.     boost::regex TOKEN_PIPE {"\\|"};
  37.     boost::regex TOKEN_AMPERSAND {"\\&"};
  38.     boost::regex TOKEN_PAREN_LEFT {"\\("};
  39.     boost::regex TOKEN_PAREN_RIGHT {"\\)"};
  40.     } te;
  41.  
  42.     struct TokenType {
  43.         string TOKEN;
  44.     } tt;
  45.  
  46.     TokenType TOKEN_STRING = {"STRING"},
  47.             TOKEN_PARAM = {"PARAM"},
  48.             TOKEN_BRACKETS = {"BRACKETS"},
  49.             TOKEN_FILE = {"FILE"},
  50.             TOKEN_WORD = {"WORD"},
  51.             TOKEN_INT = {"INT"},
  52.             TOKEN_DASH = {"DASH"},
  53.             TOKEN_EQUAL = {"EQUAL"},
  54.             TOKEN_QUOTE = {"QUOTE"},
  55.             TOKEN_COMMA = {"COMMA"},
  56.             TOKEN_OP = {"OP"},
  57.             TOKEN_PIPE = {"PIPE"},
  58.             TOKEN_AMPERSAND = {"AMPERSAND"},
  59.             TOKEN_PAREN_LEFT = {"PAREN_LEFT"},
  60.             TOKEN_PAREN_RIGHT = {"PAREN_RIGHT"};
  61.    
  62.  
  63.     struct TokenMap {
  64.         string str;
  65.         int begin_pos;
  66.         int end_pos;
  67.         int index;
  68.         TokenType tt;
  69.     };
  70.  
  71.  
  72.     TokenMap tmap;
  73.  
  74.  
  75.     TokenType get_token_type(string token) {
  76.         string::const_iterator start, end;
  77.         start = token.begin();
  78.         end = token.end();
  79.         boost::match_results<string::const_iterator> what;
  80.         while (regex_search(start, end, what, te.TOKEN_STRING)) {
  81.             cout << "TOKEN_STRING" << endl;
  82.             start = what[0].second;
  83.             return TOKEN_STRING;
  84.         }
  85.         while (regex_search(start, end, what, te.TOKEN_PARAM)) {
  86.             cout << "TOKEN_PARAM" << endl;
  87.             start = what[0].second;
  88.             return TOKEN_PARAM;
  89.         }
  90.         while (regex_search(start, end, what, te.TOKEN_BRACKETS)) {
  91.             cout << "TOKEN_BRACKETS" << endl;
  92.             start = what[0].second;
  93.             return TOKEN_BRACKETS;
  94.         }
  95.         while (regex_search(start, end, what, te.TOKEN_FILE)) {
  96.             cout << "TOKEN_FILE" << endl;
  97.             start = what[0].second;
  98.             return TOKEN_FILE;
  99.         }
  100.         while (regex_search(start, end, what, te.TOKEN_WORD)) {
  101.             cout << "TOKEN_WORD" << endl;
  102.             start = what[0].second;
  103.             return TOKEN_WORD;
  104.         }
  105.         while (regex_search(start, end, what, te.TOKEN_INT)) {
  106.             cout << "TOKEN_INT" << endl;
  107.             start = what[0].second;
  108.             return TOKEN_INT;
  109.         }
  110.         while (regex_search(start, end, what, te.TOKEN_DASH)) {
  111.             cout << "TOKEN_DASH" << endl;
  112.             start = what[0].second;
  113.             return TOKEN_DASH;
  114.         }
  115.         while (regex_search(start, end, what, te.TOKEN_EQUAL)) {
  116.             cout << "TOKEN_EQUAL" << endl;
  117.             start = what[0].second;
  118.             return TOKEN_EQUAL;
  119.         }
  120.         while (regex_search(start, end, what, te.TOKEN_QUOTE)) {
  121.             cout << "TOKEN_QUOTE" << endl;
  122.             start = what[0].second;
  123.             return TOKEN_QUOTE;
  124.         }
  125.         while (regex_search(start, end, what, te.TOKEN_COMMA)) {
  126.             cout << "TOKEN_COMMA" << endl;
  127.             start = what[0].second;
  128.             return TOKEN_COMMA;
  129.         }
  130.         while (regex_search(start, end, what, te.TOKEN_OP)) {
  131.             cout << "TOKEN_OP" << endl;
  132.             start = what[0].second;
  133.             return TOKEN_OP;
  134.         }
  135.         while (regex_search(start, end, what, te.TOKEN_PIPE)) {
  136.             cout << "TOKEN_PIPE" << endl;
  137.             start = what[0].second;
  138.             return TOKEN_PIPE;
  139.         }
  140.         while (regex_search(start, end, what, te.TOKEN_AMPERSAND)) {
  141.             cout << "TOKEN_AMPERSAND" << endl;
  142.             start = what[0].second;
  143.             return TOKEN_AMPERSAND;
  144.         }
  145.         while (regex_search(start, end, what, te.TOKEN_PAREN_LEFT)) {
  146.             cout << "TOKEN_PAREN_LEFT" << endl;
  147.             start = what[0].second;
  148.             return TOKEN_PAREN_LEFT;
  149.         }
  150.         while (regex_search(start, end, what, te.TOKEN_PAREN_RIGHT)) {
  151.             cout << "TOKEN_PAREN_RIGHT" << endl;
  152.             start = what[0].second;
  153.             return TOKEN_PAREN_RIGHT;
  154.         }
  155.     }
  156.  
  157.     void tokenize(char* s) {
  158.  
  159.         str_ = string(s);
  160.         vector<string> vec;
  161.         boost::regex re {("\\w+|\\W")};
  162.         boost::sregex_token_iterator i(str_.begin(), str_.end(), re);
  163.         boost::sregex_token_iterator j;
  164.         string t;
  165.         string str = "";
  166.         TokenType last_type;
  167.  
  168.         bool QUOTED = false;
  169.         bool PARENS = false;
  170.         bool BRACKETS = false;
  171.         unsigned count = 0;
  172.         int first_pos = 0;
  173.         while (i != j) {
  174.             if (*i != " " && *i != "\"" && *i != "(" && *i != ")" && *i != "{"
  175.                     && *i != "}" && QUOTED == false && PARENS == false && BRACKETS == false) {
  176.                 vec.push_back(*i);
  177.                 t = *i;
  178.                 tmap.str = *i;
  179.                 tmap.begin_pos = first_pos;
  180.                 tmap.end_pos = first_pos + t.size();
  181.                 tmap.index++;
  182.                 tmap.tt = get_token_type(t);
  183.                 last_type = tmap.tt;
  184.                 i++;
  185.                 count++;
  186.             } else if ((*i != "\"" && QUOTED == true)
  187.                         || *i != ")" && PARENS == true
  188.                         || *i != "}" && BRACKETS == true) {
  189.                 str += *i;
  190.                 i++;
  191.             } else if ((*i == "\"" && QUOTED == true)
  192.                         || *i == ")" && PARENS == true
  193.                         || *i == "}" && BRACKETS == true) {
  194.                 str += *i;
  195.                 vec.push_back(str);
  196.                 tmap.str = str;
  197.                 tmap.begin_pos = first_pos;
  198.                 tmap.end_pos = first_pos + str.size();
  199.                 tmap.index++;
  200.                 tmap.tt = get_token_type(str);
  201.                 last_type = tmap.tt;
  202.                 i++;
  203.                 count++;
  204.                 QUOTED = false;
  205.                 PARENS = false;
  206.                 BRACKETS = false;
  207.                 str = "";
  208.             } else if (*i == "\"" && QUOTED == false) {
  209.                 str += *i;
  210.                 QUOTED = true;
  211.                 i++;
  212.             } else if (*i == "(" && PARENS == false) {
  213.                 str += *i;
  214.                 PARENS = true;
  215.                 i++;
  216.             } else if (*i == "{" && BRACKETS == false) {
  217.                 str += *i;
  218.                 BRACKETS = true;
  219.                 i++;
  220.             } else {
  221.                 i++;
  222.             }
  223.         }
  224.  
  225.         for (int n=0;n<vec.size();n++) {
  226.             cout << vec[n] << endl;
  227.         }
  228.         cout << "There were " << count << " tokens found." << endl;
  229.         prompt_token_list = vec;
  230.     }
  231.    
  232.  
  233. private:
  234.     string str_;
  235.     unsigned count;
  236.     vector<string> prompt_token_list;
  237. };
  238.  
  239.  
  240.  
  241. int main()
  242. {
  243.     char* buf;
  244.     Tokenizer tk;
  245.     while ((buf = readline("Shell>> ")) != nullptr) {
  246.         if (strlen(buf) > 0) {
  247.             add_history(buf);
  248.             tk.tokenize(buf);
  249.         }
  250.     }
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement