daily pastebin goal
30%
SHARE
TWEET

OTYAP 2.5

Mike_be Jun 14th, 2018 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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" || word == "and")
  165.                     vec_2.push_back(4);
  166.                 if (word == "not")
  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[13][11] =
  230. {
  231.     1,  10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  232.     10, 10, 5,  10, 10, 10, 10, 10, 10, 10, 10,
  233.     10, 10, 1,  10, 10, 10, 10, 10, 10, 1,  10,
  234.     10, 3,  10, 10, 3,  10, 10, 10, 10, 10, 10,
  235.     10, 10, 1,  10, 10, 10, 10, 10, 10, 10, 10,
  236.     10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 10,
  237.     10, 2,  10, 2,  2,  6,  10, 8,  10, 6,  10,
  238.     10, 2,  10, 10, 2,  10, 10, 8,  10, 10, 10,
  239.     10, 10, 10, 10, 10, 10, 10, 10, 7,  10, 10,
  240.     10, 10, 4,  10, 10, 10, 10, 10, 10, 10, 10,
  241.     10, 10, 10, 10, 10, 10, 7,  10, 10, 10, 10,
  242.     10, 10, 10, 10, 10, 10, 10, 10, 9,  10, 10,
  243.     10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10
  244. };
  245.  
  246. int text_row(int num)
  247. {
  248.     if (num == 1)
  249.         return 0;
  250.     if (num == 2)
  251.         return 1;
  252.     if (num == 3)
  253.         return 2;
  254.     if (num == 4)
  255.         return 3;
  256.     if (num == 5)
  257.         return 4;
  258.     if (num == 6)
  259.         return 5;
  260.     if (num == 7)
  261.         return 6;
  262.     if (num == 8)
  263.         return 7;
  264.     if (num == 9)
  265.         return 8;
  266.     if (num == 10)
  267.         return 9;
  268.     if (num == 11)
  269.         return 10;
  270.     if (num == 12)
  271.         return 11;
  272.     return 12;
  273. }
  274.  
  275. void text_analyze(std::vector<int>& vec)
  276. {
  277.     int state = 0;
  278.     for (signed int i = 0; i < vec.size(); i++)
  279.     {
  280.         state = Lex_matrix[(text_row(vec[i]))][state];
  281.         if (state == 11)
  282.             std::cout << "happy end";
  283.     }
  284. }
  285.  
  286. /*
  287. * Read function, reads from given path/.../file
  288. * Usage: read(vec_you_want_to_fill)
  289. * Now automatically reads everything and fills linked string to further work
  290. */
  291. void read(char* &file)
  292. {
  293.     int size = 256, i = -1, k = 0;
  294.     std::ifstream in;
  295.     char ch;
  296.     print_str("Write file name or path to it: ", 10, 10);
  297.     do {
  298.         char* file_path = static_cast<char*>(malloc(256 * sizeof(char)));
  299.         file_path[255] = '\n';
  300.         std::cin.getline(file_path, 256);
  301.         if (file_path[255] != '\n')
  302.         {
  303.             print_str("You wrote too long file path and i ", 4, 10);
  304.             std::cin.clear();
  305.             std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  306.         }
  307.         in.open(file_path);
  308.         free(file_path);
  309.     } while (!in.is_open() && print_str("Couldn't read the file, type again: ", 4, 10));
  310.     do
  311.     {
  312.         if (i == size)
  313.         {
  314.             size *= 2;
  315.             file = static_cast<char*>(realloc(file, size * sizeof(char)));
  316.         }
  317.         file[++i] = in.get();
  318.     } while (!in.eof());
  319.     if (i == size)
  320.     {
  321.         size *= 2;
  322.         file = static_cast<char*>(realloc(file, size * sizeof(char)));
  323.     }
  324.     file[i++] = '\n';
  325.     file[i] = 0;
  326.     in.close();
  327. }
  328.  
  329. int main()
  330. {
  331.     setlocale(LC_ALL, "Russian");
  332.     std::vector<char*> variables;
  333.     std::vector<int> type;
  334.     char* file = static_cast<char*>(malloc(256 * sizeof(char)));
  335.     read(file);
  336.     std::cout << file;
  337.     auto_rifle(file, variables, type);
  338.     free(file);
  339.     text_analyze(type);
  340.     print_str("\n\nPress enter to continue. . .", 7, 5);
  341.     getchar();
  342.     return 0;
  343. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top