Advertisement
Mike_be

Lab 3

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