Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <fstream>
- #include <iostream>
- #include <string>
- #include <vector>
- #include <streambuf>
- #include <malloc.h>
- #include <algorithm>
- #include <Windows.h>
- /*
- Printing color messages in console
- Usage: print_str(sring_you_want_to_print, color_of_text, time_between_letters);
- Colors: https://i.stack.imgur.com/ZG625.png
- */
- inline bool print_str(char* str, int k, int n)
- {
- HANDLE hConsole = GetStdHandle(((DWORD)-11)); //Console address to change text color
- SetConsoleTextAttribute(hConsole, k);
- for (int l = 0; l < int(strlen(str)); l++)
- {
- std::cout << str[l];
- if (str[l] == ' ' || str[l] == '\n')
- Sleep(n);
- }
- SetConsoleTextAttribute(hConsole, 14);
- return true;
- }
- enum States { Start, Identifier, Num, Negative_num, Operations, Less, More, Assign, Equal, Fail,
- Ident_out, Num_out, Operat_out, Comparison_out, Assign_out, Fail_out, Ident_end, Num_end};
- const int matrix[10][10] =
- {
- Identifier, Identifier, Fail, Fail, Fail, Fail, Fail, Fail, Fail, Fail,
- Num, Identifier, Num, Num, Fail, Fail, Fail, Fail, Fail, Fail,
- Negative_num, Fail, Fail, Fail, Fail, Fail, Fail, Fail, Fail, Fail,
- Operations, Fail, Fail, Fail, Fail, Fail, Fail, Fail, Fail, Fail,
- Less, Fail, Fail, Fail, More, Fail, Fail, Fail, Fail, Fail,
- More, Fail, Fail, Fail, More, More, Fail, Fail, Fail, Fail,
- Assign, Fail, Fail, Fail, Fail, Fail, Fail, Equal, Fail, Fail,
- Start, Ident_out, Num_out, Operat_out, Operat_out, Comparison_out, Comparison_out, Assign_out, Comparison_out, Fail_out,
- Fail, Ident_end, Num_end, Fail, Fail, Fail, Fail, Fail, Fail, Fail,
- Fail, Fail, Fail, Fail, Fail, Fail, Fail, Fail, Fail, Fail,
- };
- /*
- * Most important function in automaton. Determines how automaton will work. Add states or new conditions in it for new functionality
- */
- bool keyword(char* word)
- {
- bool is_keyword = false;
- std::vector<std::string> keywords = { "if", "then", "elseif", "end", "or", "not", "and" };
- for (int i = 0; i < 7; i++)
- if (word == keywords[i])
- is_keyword = true;
- return is_keyword;
- }
- int row(char& a)
- {
- if ((a >= 'a' && a <= 'z') || (a >= 'A' && a <= 'Z'))
- return 0;
- if (a >= '0' && a <= '9')
- return 1;
- if (a == '-')
- return 2;
- if (a == '+' || a == '*' || a == '/')
- return 3;
- if (a == '<')
- return 4;
- if (a == '>')
- return 5;
- if (a == '=')
- return 6;
- if (a == ' ' || a == '\t' || a == '\n' || a == '\r' || a == '\0')
- return 7;
- if (a == ';')
- return 8;
- return 9;
- }
- /*
- Automaton
- Usage: automaton(string_you_want_to_check, results_1, ... , results_n);
- */
- void auto_rifle(char* str, std::vector<char*>& vec_1, std::vector<int>& vec_2)
- {
- int size = 16;
- char* word = static_cast<char*>(malloc(size * sizeof(char)));
- int state = 0, i = 0;
- for (unsigned long long k = 0; k < strlen(str); k++)
- {
- if (state == 0)
- {
- i = 0;
- word = static_cast<char*>(malloc(size * sizeof(char)));
- }
- state = matrix[(row(str[k]))][state];
- word[i] = str[k];
- i++;
- if (i == size)
- {
- size *= 2;
- word = static_cast<char*>(realloc(word, size * sizeof(char)));
- }
- if (state == Ident_out || state == Ident_end)
- {
- if (state == Ident_end)
- word[i - 1] = 0;
- word[i - 1] = 0;
- if (keyword(word))
- {
- if (!strcmp(word, "if"))
- vec_2.push_back(1);
- if (!strcmp(word, "then"))
- vec_2.push_back(2);
- if (!strcmp(word, "elseif"))
- vec_2.push_back(3);
- if (!strcmp(word, "and") || !strcmp(word, "or"))
- vec_2.push_back(4);
- if (!strcmp(word, "not"))
- vec_2.push_back(5);
- if (!strcmp(word, "end"))
- vec_2.push_back(6);
- print_str("\" ", 10, 5); std::cout << word; print_str(" \" ", 10, 5); print_str("Keyword\n", 11, 5);
- }
- else
- {
- vec_2.push_back(7);
- print_str("\" ", 10, 5); std::cout << word; print_str(" \" ", 10, 5); print_str("Variable / Identifier\n", 10, 5);
- }
- }
- if (state == Num_out || state == Num_end)
- {
- if (state == Num_end)
- word[i - 1] = 0;
- vec_2.push_back(8);
- word[i - 1] = 0;
- print_str("\" ", 10, 5); std::cout << word; print_str(" \" ", 10, 5); print_str("Constant numbers\n", 9, 5);
- }if (state == Operat_out)
- {
- vec_2.push_back(9);
- word[i - 1] = 0;
- print_str("\" ", 10, 5); std::cout << word; print_str(" \" ", 10, 5); print_str("Arithmetic operations\n", 15, 5);
- }if (state == Comparison_out)
- {
- vec_2.push_back(10);
- word[i - 1] = 0;
- print_str("\" ", 10, 5); std::cout << word; print_str(" \" ", 10, 5); print_str("Comparison operations\n", 13, 5);
- }
- if (state == Assign_out)
- {
- vec_2.push_back(11);
- word[i - 1] = 0;
- print_str("\" ", 10, 5); std::cout << word; print_str(" \" ", 10, 5); print_str("Assignment operation\n", 5, 5);
- }
- if (state == Fail_out)
- {
- vec_2.push_back(13);
- word[i - 1] = 0;
- print_str("\" ", 10, 5); std::cout << word; print_str(" \" ", 10, 5); print_str("Can't analyze\n", 12, 5);
- }
- if (state >= Ident_out && state <= Num_end)
- {
- vec_1.push_back(word);
- if (state == Ident_end || state == Num_end)
- {
- vec_1.push_back(";");
- vec_2.push_back(12);
- print_str("\" ", 10, 5); std::cout << ";"; print_str(" \" ", 10, 5); print_str("End of operator\n", 3, 5);
- }
- size = 16;
- state = 0;
- i = 0;
- word = 0;
- }
- }
- return;
- }
- #undef max
- bool logic_comp(std::vector<int> &v, int &p)
- {
- if ((v[p] == 7 || v[p] == 8) && v[p + 1] == 10 && (v[p + 2] == 7 || v[p + 2] == 8))
- {
- p += 3;
- return true;
- }
- else if ((v[p] == 7 || v[p] == 8) && (v[p+1] == 2 || v[p+1] == 4))
- {
- p += 1;
- return true;
- }
- else
- {
- print_str("Not correct statement at ", 12, 5); std::cout << p; print_str(" position\n", 12, 5);
- p++;
- return false;
- }
- }
- void lex_analyze(std::vector<int> &v)
- {
- enum lol { If = 1, then, elseif, or , and = 4, not, end, var, num, ArOper, CoOper, AsOper, EndOper };
- int pos = 0;
- bool correctness = true;
- if (v.size() == 0)
- {
- print_str("File is empty\n", 12, 5);
- return;
- }
- while (v[pos++] != If)
- {
- if (pos >= v.size() - 1)
- {
- print_str("There is no beginning of construction\n", 12, 5);
- return;
- }
- }
- while (v[pos] != end)
- {
- while (v[pos] != then)
- {
- if (v[pos] == elseif)
- pos++;
- if (v[pos] == var || v[pos] == num)
- {
- logic_comp(v, pos);
- }
- if (v[pos] == not)
- {
- pos++;
- logic_comp(v, pos);
- }
- if (v[pos] == or || v[pos] == and)
- pos++;
- if (v[pos] != or && v[pos] != var && v[pos] != num && v[pos] != not && v[pos] != then)
- {
- print_str("Something is wrong at ", 12, 5); std::cout << pos; print_str(" position, skipping. . .\n", 12, 5);
- pos++;
- }
- }
- pos++;
- while (v[pos] != end && v[pos] != elseif)
- {
- int wait = 0;
- if (v[pos] == var && v[pos + 1] == AsOper)
- pos += 1;
- else
- {
- print_str("No assignment operation at ", 12, 5); std::cout << pos; print_str(" position, skipping...\n", 12, 5);
- }
- while (v[pos++] != EndOper && v[pos] != elseif)
- {
- if (v[pos] == var && v[pos + 1] == AsOper && (v[pos - 1] != EndOper && v[pos - 1] != then))
- {
- 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);
- pos += 2;
- }
- if ((v[pos] == var || v[pos] == num) && wait == 0)
- wait = 1;
- else if (v[pos] == ArOper && wait == 1)
- wait = 0;
- else if (v[pos] == ArOper && wait == 0)
- {
- print_str("Expected variable at ", 12, 5); std::cout << pos; print_str(" position.\n", 12, 5);
- }
- else if ((v[pos] == var || v[pos] == num) && wait == 1)
- {
- print_str("Expected arithmetic operation at ", 12, 5); std::cout << pos; print_str(" position.\n", 12, 5);
- }
- }
- }
- }
- }
- /*
- * Read function, reads from given path/.../file
- * Usage: read(vec_you_want_to_fill)
- * Now automatically reads everything and fills linked string to further work
- */
- void read(char* &file)
- {
- int size = 256, i = -1, k = 0;
- std::ifstream in;
- char ch;
- print_str("Write file name or path to it: ", 10, 10);
- do {
- char* file_path = static_cast<char*>(malloc(256 * sizeof(char)));
- file_path[255] = '\n';
- std::cin.getline(file_path, 256);
- if (file_path[255] != '\n')
- {
- print_str("You wrote too long file path and i ", 4, 10);
- std::cin.clear();
- std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
- }
- in.open(file_path);
- free(file_path);
- } while (!in.is_open() && print_str("Couldn't read the file, type again: ", 4, 10));
- do
- {
- if (i == size)
- {
- size *= 2;
- file = static_cast<char*>(realloc(file, size * sizeof(char)));
- }
- file[++i] = in.get();
- } while (!in.eof());
- if (i == size)
- {
- size *= 2;
- file = static_cast<char*>(realloc(file, size * sizeof(char)));
- }
- file[i++] = '\n';
- file[i] = 0;
- in.close();
- }
- int main()
- {
- setlocale(LC_ALL, "Russian");
- std::vector<char*> variables;
- std::vector<int> type;
- char* file = static_cast<char*>(malloc(256 * sizeof(char)));
- read(file);
- std::cout << file;
- auto_rifle(file, variables, type);
- free(file);
- lex_analyze(type);
- print_str("\n\nPress enter to continue. . .", 7, 5);
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement