Advertisement
Mike_be

hui

May 17th, 2018
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.47 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <fstream>
  3. #include <iostream>
  4. #include <string>
  5. #include <vector>
  6. #include <streambuf>
  7. #include <malloc.h>
  8. #include <algorithm>
  9.  
  10. template <typename T>
  11. typename T::size_type levenshtein_distance(const T & src, const T & dst)
  12. {
  13.     const typename T::size_type m = src.size();
  14.     const typename T::size_type n = dst.size();
  15.     if (m == 0)
  16.     {
  17.         return n;
  18.     }
  19.     if (n == 0)
  20.     {
  21.         return m;
  22.     }
  23.  
  24.     std::vector<std::vector<typename T::size_type>> matrix(m + 1);
  25.  
  26.     for (typename T::size_type i = 0; i <= m; ++i)
  27.     {
  28.         matrix[i].resize(n + 1);
  29.         matrix[i][0] = i;
  30.     }
  31.     for (typename T::size_type i = 0; i <= n; ++i)
  32.     {
  33.         matrix[0][i] = i;
  34.     }
  35.  
  36.     typename T::size_type above_cell, left_cell, diagonal_cell, cost;
  37.  
  38.     for (typename T::size_type i = 1; i <= m; ++i)
  39.     {
  40.         for (typename T::size_type j = 1; j <= n; ++j)
  41.         {
  42.             cost = src[i - 1] == dst[j - 1] ? 0 : 1;
  43.             above_cell = matrix[i - 1][j];
  44.             left_cell = matrix[i][j - 1];
  45.             diagonal_cell = matrix[i - 1][j - 1];
  46.             matrix[i][j] = std::min(std::min(above_cell + 1, left_cell + 1), diagonal_cell + cost);
  47.         }
  48.     }
  49.  
  50.     return matrix[m][n];
  51. }
  52.  
  53. #include <Windows.h>
  54.  
  55.  
  56. /*
  57. Printing color messages in console
  58. Usage: print_str(sring_you_want_to_print, color_of_text, time_between_letters);
  59. Colors: https://i.stack.imgur.com/ZG625.png
  60. */
  61. inline bool print_str(char* str, int k, int n)
  62. {
  63.     HANDLE hConsole = GetStdHandle(((DWORD)-11)); //Console address to change text color
  64.     SetConsoleTextAttribute(hConsole, k);
  65.     for (int l = 0; l < int(strlen(str)); l++)
  66.     {
  67.         std::cout << str[l];
  68.         Sleep(n);
  69.     }
  70.     SetConsoleTextAttribute(hConsole, 14);
  71.     return true;
  72. }
  73.  
  74. enum States { Start, Identifier, Num, Negative_num, Operations, Less, More, Assign, Equal, Fail,
  75.             Ident_out, Num_out, Operat_out, Comparison_out, Assign_out,  Fail_out, Ident_end, Num_end};
  76.  
  77. const int matrix[10][10] =
  78. {
  79.     Identifier,    Identifier, Fail,    Fail,       Fail,       Fail,           Fail,           Fail,       Fail,           Fail,
  80.     Num,           Identifier, Num,     Num,        Fail,       Fail,           Fail,           Fail,       Fail,           Fail,
  81.     Negative_num,  Fail,       Fail,    Fail,       Fail,       Fail,           Fail,           Fail,       Fail,           Fail,
  82.     Operations,    Fail,       Fail,    Fail,       Fail,       Fail,           Fail,           Fail,       Fail,           Fail,
  83.     Less,          Fail,       Fail,    Fail,       More,       Fail,           Fail,           Fail,       Fail,           Fail,
  84.     More,          Fail,       Fail,    Fail,       More,       More,           Fail,           Fail,       Fail,           Fail,
  85.     Assign,        Fail,       Fail,    Fail,       Fail,       Fail,           Fail,           Equal,      Fail,           Fail,
  86.     Start,         Ident_out,  Num_out, Operat_out, Operat_out, Comparison_out, Comparison_out, Assign_out, Comparison_out, Fail_out,
  87.     Fail,          Ident_end,  Num_end, Fail,       Fail,       Fail,           Fail,           Fail,       Fail,           Fail,
  88.     Fail,          Fail,       Fail,    Fail,       Fail,       Fail,           Fail,           Fail,       Fail,           Fail,
  89. };
  90.  
  91. /*
  92. * Most important function in automaton. Determines how automaton will work. Add states or new conditions in it for new functionality
  93. */
  94. bool keyword(char* word)
  95. {
  96.     bool is_keyword = false;
  97.     std::vector<std::string> keywords = { "if", "then", "elseif", "end", "or", "not", "and" };
  98.     for (int i = 0; i < 7; i++)
  99.         if (word == keywords[i])
  100.             is_keyword = true;
  101.     return  is_keyword;
  102. }
  103.  
  104. int row(char& a)
  105. {
  106.     if ((a >= 'a' && a <= 'z') || (a >= 'A' && a <= 'Z'))
  107.         return 0;
  108.     if (a >= '0' && a <= '9')
  109.         return 1;
  110.     if (a == '-')
  111.         return 2;
  112.     if (a == '+' || a == '*' || a == '/')
  113.         return 3;
  114.     if (a == '<')
  115.         return 4;
  116.     if (a == '>')
  117.         return 5;
  118.     if (a == '=')
  119.         return 6;
  120.     if (a == ' ' || a == '\t' || a == '\n' || a == '\r' || a == '\0')
  121.         return 7;
  122.     if (a == ';')
  123.         return 8;
  124.     return 9;
  125. }
  126.  
  127. /*
  128. Automaton
  129. Usage: automaton(string_you_want_to_check, results_1, ... , results_n);
  130. */
  131. void auto_rifle(char* str, std::vector<char*>& vec_1, std::vector<int>& vec_2)
  132. {
  133.     int size = 16;
  134.     char* word = static_cast<char*>(malloc(size * sizeof(char)));
  135.     int state = 0, i = 0;
  136.     for (unsigned long long k = 0; k < strlen(str); k++)
  137.     {
  138.         if (state == 0)
  139.         {
  140.             i = 0;
  141.             word = static_cast<char*>(malloc(size * sizeof(char)));
  142.         }
  143.         state = matrix[(row(str[k]))][state];
  144.         word[i] = str[k];
  145.         i++;
  146.         if (i == size)
  147.         {
  148.             size *= 2;
  149.             word = static_cast<char*>(realloc(word, size * sizeof(char)));
  150.         }
  151.         if (state == Ident_out || state == Ident_end)
  152.         {
  153.             if (state == Ident_end)
  154.                 word[i - 1] = 0;
  155.             word[i - 1] = 0;
  156.             if (keyword(word))
  157.             {
  158.                 if(word == "if")
  159.                     vec_2.push_back(1);
  160.                 if (word == "then")
  161.                     vec_2.push_back(2);
  162.                 if (word == "elseif")
  163.                     vec_2.push_back(3);
  164.                 if (word == "or")
  165.                     vec_2.push_back(4);
  166.                 if (word == "not"|| word == "and")
  167.                     vec_2.push_back(5);
  168.                 if (word == "end")
  169.                     vec_2.push_back(6);
  170.                 print_str("\" ", 10, 10); std::cout << word; print_str(" \"  ", 10, 10); print_str("Keyword\n", 11, 10);
  171.             }
  172.             else
  173.             {
  174.                 vec_2.push_back(7);
  175.                 print_str("\" ", 10, 10); std::cout << word; print_str(" \"  ", 10, 10); print_str("Variable / Identifier\n", 10, 10);
  176.             }
  177.         }
  178.         if (state == Num_out || state == Num_end)
  179.         {
  180.             if (state == Num_end)
  181.                 word[i - 1] = 0;
  182.             vec_2.push_back(8);
  183.             word[i - 1] = 0;
  184.             print_str("\" ", 10, 10); std::cout << word; print_str(" \"  ", 10, 10); print_str("Constant numbers\n", 9, 10);
  185.         }if (state == Operat_out)
  186.         {
  187.             vec_2.push_back(9);
  188.             word[i - 1] = 0;
  189.             print_str("\" ", 10, 10); std::cout << word; print_str(" \"  ", 10, 10); print_str("Arithmetic operations\n", 15, 10);
  190.         }if (state == Comparison_out)
  191.         {
  192.             vec_2.push_back(10);
  193.             word[i - 1] = 0;
  194.             print_str("\" ", 10, 10); std::cout << word; print_str(" \"  ", 10, 10); print_str("Comparison operations\n", 13, 10);
  195.         }
  196.         if (state == Assign_out)
  197.         {
  198.             vec_2.push_back(11);
  199.             word[i - 1] = 0;
  200.             print_str("\" ", 10, 10); std::cout << word; print_str(" \"  ", 10, 10); print_str("Assignment operation\n", 5, 10);
  201.         }
  202.         if (state == Fail_out)
  203.         {
  204.             vec_2.push_back(13);
  205.             word[i - 1] = 0;
  206.             print_str("\" ", 10, 10); std::cout << word; print_str(" \"  ", 10, 10); print_str("Can't analyze\n", 12, 10);
  207.         }
  208.         if (state >= Ident_out && state <= Num_end)
  209.         {
  210.             vec_1.push_back(word);
  211.  
  212.             if (state == Ident_end || state == Num_end)
  213.             {
  214.                 vec_1.push_back(";");
  215.                 vec_2.push_back(12);
  216.                 print_str("\" ", 10, 10); std::cout << ";"; print_str(" \"  ", 10, 10); print_str("End or operator\n", 3, 10);
  217.             }
  218.             size = 16;
  219.             state = 0;
  220.             i = 0;
  221.             word = 0;
  222.         }
  223.     }
  224.     return;
  225. }
  226.  
  227. #undef max
  228.  
  229. const int Lex_matrix[][] =
  230. {
  231.  
  232. }
  233.  
  234. /*
  235. * Read function, reads from given path/.../file
  236. * Usage: read(vec_you_want_to_fill)
  237. * Now automatically reads everything and fills linked string to further work
  238. */
  239. void read(char* &file)
  240. {
  241.     int size = 256, i = -1, k = 0;
  242.     std::ifstream in;
  243.     char ch;
  244.     print_str("Write file name or path to it: ", 10, 10);
  245.     do {
  246.         char* file_path = static_cast<char*>(malloc(256 * sizeof(char)));
  247.         file_path[255] = '\n';
  248.         std::cin.getline(file_path, 256);
  249.         if (file_path[255] != '\n')
  250.         {
  251.             print_str("You wrote too long file path and i ", 4, 10);
  252.             std::cin.clear();
  253.             std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  254.         }
  255.         in.open(file_path);
  256.         free(file_path);
  257.     } while (!in.is_open() && print_str("Couldn't read the file, type again: ", 4, 10));
  258.     do
  259.     {
  260.         if (i == size)
  261.         {
  262.             size *= 2;
  263.             file = static_cast<char*>(realloc(file, size * sizeof(char)));
  264.         }
  265.         file[++i] = in.get();
  266.     } while (!in.eof());
  267.     if (i == size)
  268.     {
  269.         size *= 2;
  270.         file = static_cast<char*>(realloc(file, size * sizeof(char)));
  271.     }
  272.     file[i++] = '\n';
  273.     file[i] = 0;
  274.     in.close();
  275. }
  276.  
  277. int main()
  278. {
  279.     setlocale(LC_ALL, "Russian");
  280.     std::vector<char*> variables;
  281.     std::vector<int> type;
  282.     char* file = static_cast<char*>(malloc(256 * sizeof(char)));
  283.     read(file);
  284.     std::cout << file;
  285.     auto_rifle(file, variables, type);
  286.     free(file);
  287.     print_str("Variable / Identifier:\n", 10, 10);
  288.     for (unsigned int i = 0; i < type.size(); i++)
  289.     {
  290.         if (type[i] == 7)
  291.         {
  292.             print_str("\" ", 10, 10);
  293.             std::cout << variables[i];
  294.             print_str(" \"  ", 10, 10);
  295.         }
  296.     }
  297.     print_str("\nConstant numbers:\n", 9, 10);
  298.     for (unsigned int i = 0; i < type.size(); i++)
  299.     {
  300.         if (type[i] == 8)
  301.         {
  302.             print_str("\" ", 10, 10);
  303.             std::cout << variables[i];
  304.             print_str(" \"  ", 10, 10);
  305.         }
  306.     }
  307.     print_str("\n\nPress enter to continue. . .", 7, 5);
  308.     getchar();
  309.     return 0;
  310. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement