Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Пусть в файле храняться строки, представляющие собой
- // исходный код программы на языке с++. Вывести синтаксически
- // правильные операторы присваивания одной переменной значение
- // другой. Список зарезервированных слов задать в отдельном файле.
- //
- // Created by Дмитрий Рябовский on 14/03/2019.
- // Copyright © 2019 Дмитрий Рябовский. All rights reserved.
- //
- #include <iostream>
- #include <fstream>
- using namespace std;
- enum States {
- State_Start,
- State_Variable,
- State_Assignment,
- State_Complete,
- State_Skip
- };
- void getWay(char* way_cin_file) {
- cout << "\nВведите путь к файлу: (../filename.txt)" << endl; // /Users/dmitrijrabovskij/Desktop/C++/labs/4/4/cin.txt
- gets(way_cin_file);
- cout << endl;
- }
- void getWordsFromOperation (string operation, string& word1, string& word2) {
- int i = 0;
- int k = 0;
- bool check = 0;
- while (operation[i] != '\0') {
- if (operation[i] == '=') {
- check = 1;
- k = 0;
- i++;
- }
- if (operation[i] != ' ' && operation[i] != ';' && int(operation[i]) != 10) {
- if (check) {
- word2.insert(k, 1, operation[i]);
- k++;
- } else {
- word1.insert(k, 1, operation[i]);
- k++;
- }
- }
- i++;
- }
- }
- bool wordsMatchCheck (string word1, string word2) {
- bool difference = true;
- int i = 0;
- bool check1 = 0;
- while (word1[i] != '\0' && !check1) {
- if (word1[i] != word2[i]) check1 = 1;
- i++;
- }
- difference = check1;
- return difference;
- }
- bool checkWordsForKeyword (string word1, string word2) {
- bool correct = true;
- string way_keywords_file = "/Users/dmitrijrabovskij/Desktop/C++/labs/4/4/keywords.txt";
- ifstream keywords_file(way_keywords_file);
- if (!keywords_file.is_open()) {
- cout << "Файл не найден! invalid way_keywords_file!!! " << way_keywords_file << "\n";
- exit(-1);
- }
- int i = 0;
- bool check1 = false;
- bool check2 = false;
- while (!keywords_file.eof()) {
- string buff;
- keywords_file >> buff;
- while (buff[i] != '\0') {
- if (word1[i] != buff[i]) {
- check1 = 1;
- }
- if (word2[i] != buff[i]) {
- check2 = 1;
- }
- i++;
- }
- if (check1 == 0 || check2 == 0) {
- correct = 0;
- }
- check1 = 0;
- check2 = 0;
- i = 0;
- }
- keywords_file.close();
- return correct;
- }
- bool checkOperationForKeyword (string operation) {
- bool correct = true;
- string word1 = "";
- string word2 = "";
- getWordsFromOperation(operation, word1, word2);
- correct = wordsMatchCheck(word1, word2);
- if (!correct) {
- return correct;
- }
- correct = checkWordsForKeyword(word1, word2);
- return correct;
- }
- void assignOperatorSearch_found(bool& check, int& i, States& state, string& operation) {
- if (checkOperationForKeyword(operation)) {
- cout << operation << endl << endl;
- }
- check = 0;
- state = State_Start;
- operation = "";
- i = 0;
- }
- void assignOperatorSearch() {
- char way_cin_file[256] = "";
- getWay(way_cin_file);
- ifstream cin_file(way_cin_file);
- if (!cin_file.is_open()) {
- cout << "Файл не найден!\n";
- assignOperatorSearch();
- } else {
- int symb;
- bool check = 0;
- string operation = "";
- int i = 0;
- States state = State_Start;
- while (!cin_file.eof())
- {
- symb = cin_file.get();
- operation.insert(i, 1, char(symb));
- i++;
- switch (state) {
- case State_Start:
- if ((symb >= 65 && symb <= 90) || (symb >= 97 && symb <= 122) || symb == 95) {
- state = State_Variable;
- } else if (symb == 32 || symb == 10 || (symb == 59 && !check)) {
- state = State_Start;
- } else {
- state = State_Skip;
- }
- break;
- case State_Variable:
- if ((symb >= 65 && symb <= 90) || (symb >= 97 && symb <= 122) || symb == 95 || (symb >= 48 && symb <= 57)) {
- state = State_Variable;
- } else if (symb == 32 || symb == 10) {
- if (!check) {
- state = State_Assignment;
- check = 1;
- } else {
- state = State_Complete;
- }
- } else {
- if (check) {
- if (symb == 59) {
- assignOperatorSearch_found(check, i, state, operation); //end
- } else {
- state = State_Skip;
- }
- } else {
- if (symb == 61) {
- check = 1; //State_Assignment
- state = State_Start;
- } else {
- state = State_Skip;
- }
- }
- }
- break;
- case State_Assignment:
- if (symb == 61) {
- state = State_Start;
- } else if (symb == 32 || symb == 10) {
- state = State_Assignment;
- } else {
- if ((symb >= 65 && symb <= 90) || (symb >= 97 && symb <= 122) || symb == 95) {
- check = 0; //state_start
- state = State_Variable;
- operation = "";
- operation.insert(0, 1, char(symb));
- i = 1;
- } else {
- state = State_Skip;
- }
- }
- break;
- case State_Complete:
- if (symb == 59) {
- assignOperatorSearch_found(check, i, state, operation); //end
- i = 0;
- } else if (symb == 32 || symb == 10) {
- state = State_Complete;
- } else {
- if ((symb >= 65 && symb <= 90) || (symb >= 97 && symb <= 122) || symb == 95) {
- check = 0; //state_start
- state = State_Variable;
- operation = "";
- operation.insert(0, 1, char(symb));
- i = 1;
- } else {
- state = State_Skip;
- }
- }
- break;
- case State_Skip:
- check = 0;
- if (symb == 32 || symb == 10 || symb == 59) {
- state = State_Start; //skip
- operation = "";
- i = 0;
- }
- break;
- }
- }
- cin_file.close();
- }
- }
- int main() {
- cout << "Пусть в файле храняться строки, представляющие собой\nисходный код программы на языке с++. Вывести синтаксически\nправильные операторы присваивания одной переменной значение\nдругой. Список зарезервированных слов задать в отдельном файле.\n" << endl;
- assignOperatorSearch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement