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>
- #include <stdio.h>
- #include <string.h>
- using namespace std;
- enum States {
- State_Start,
- State_Variable,
- State_Assignment,
- State_Complete,
- State_Skip
- };
- void getStrFromFile (char* str, char* way) {
- ifstream fin(way);
- if (!fin.is_open()) {
- cout << "Файл не найден!\n";
- exit(-1);
- }
- string line;
- char line_char[300] = "";
- int i = 0;
- while (getline(fin, line)) {
- while (line[i] != '\0') {
- line_char[i] = line[i];
- i++;
- }
- strcat(str, " ");
- strcat(str, line_char);
- i = 0;
- while (line_char[i] != '\0') {
- line_char[i] = '\0';
- i++;
- }
- i = 0;
- }
- fin.close();
- }
- bool checkWord(char* var, char* key_words) {
- bool correct = 1;
- return correct;
- }
- bool verificationKeyword(char* operation) {
- bool correct = 1;
- char var1[250] = "";
- char var2[250] = "";
- char key_words[500] = "";
- int i = 0;
- bool found_equal = 0;
- getStrFromFile(key_words, "/Users/dmitrijrabovskij/Desktop/C++/labs/5/5/1keywords.txt"); // 1
- //cout << 1 << endl << key_words << endl;
- while (operation[i] != ';') {
- if (operation[i] == '=') {
- found_equal = 1;
- } else {
- char symb[2];
- symb[0] = operation[i];
- if (operation[i] != ' ') {
- if (!found_equal) {
- strcat(var1, symb);
- } else {
- strcat(var2, symb);
- }
- }
- }
- i++;
- }
- //cout << var1 << ' ' << var2 << endl;
- correct = (checkWord(var1, key_words) && checkWord(var2, key_words));
- return correct;
- }
- void assignOperatorSearch(string text) { //char* text) {
- States state = State_Start;
- char operation[100] = "";
- bool count = 0;
- int current = int(text[0]);
- char symb[2];
- for (int i = 0; current != '\0'; i++) {
- current = int(text[i]);
- symb[0] = text[i];
- strcat(operation, symb);
- switch (state) {
- case State_Start:
- if (current < 57 && current > 48) {
- state = State_Skip;
- count = 0;
- } else if ((current <= 122 && current >= 97) || (current <= 90 && current >= 65)
- || (current == 95)) {
- state = State_Variable;
- } else {
- if (current != 32) {
- count = 0;
- state = State_Skip;
- }
- }
- break;
- case State_Variable:
- if ((current == 32) || (cout && (current == 59)) || (!count && current == 61) ) {
- if (count) {
- //cout << "\n +++ " << text[i] << " " << i<< "\n";
- verificationKeyword(operation);
- cout << operation << endl;
- count = 0;
- state = State_Start;
- } else {
- if (current == 61) {
- state = State_Start;
- count = 1;
- } else {
- state = State_Assignment;
- }
- }
- } else if ((current > 122 || current < 97) && (current > 90 || current < 65)
- && (current > 57 || current < 48) && (current != 95)) { // a-z 97-122 A-Z 65-90 0-9 48-57 _ 95
- state = State_Skip;
- }
- break;
- case State_Assignment:
- if (current == 32) {
- } else if (current == 61) { // =
- state = State_Start;
- count = 1;
- } else {
- i = i - 2;
- state = State_Skip;
- }
- break;
- case State_Complete:
- if (current == 32) {
- } else if (current == 59) { // ;
- //cout << "\n +++ \n";
- verificationKeyword(operation);
- cout << operation << endl;
- } else {
- state = State_Skip;
- }
- break;
- case State_Skip:
- operation[0] = '\0'; // !!! !!! !!! !!! !!! !!!
- if (current == 32 || current == 59) {
- state = State_Start;
- }
- break;
- }
- }
- }
- int main() {
- char str_cin[9999] = "";
- getStrFromFile(str_cin, "/Users/dmitrijrabovskij/Desktop/C++/labs/5/5/cin.txt");
- //cout << str_cin;
- assignOperatorSearch(str_cin);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement