Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <vector>
- #include <string>
- #include <iostream>
- typedef enum TTypes {
- SPACE = 0,
- FN,
- MAIN,
- LEFTFIGURE,
- RIGHTFIGURE,
- LEFTBRACKET,
- RIGHTBRACKET,
- PRINTLN,
- STRINGCONST,
- EOL,
- SEMICOLON, //;
- INT,
- IF,
- ELSE,
- LOOP,
- ERROR
- } Types;
- bool printOperator();
- bool operatorIf_1();
- bool operatorIf_2();
- bool operatorIf_3();
- bool operatorIf_4();
- bool operatorIf_5();
- bool operatorIf_6();
- bool operatorIf();
- bool operatorLoop();
- bool operatorLoop_1();
- bool operatorLoop_2();
- bool takeOperator();
- bool StartLoopForOperator();
- std::vector<Types> terms;
- int next = 0;
- bool checkTerm(const Types expected) {
- return (expected == terms[next++]);
- }
- bool peekTerm(const Types expected) {
- return (expected == terms[next]);
- }
- bool checkMainStart() {
- bool result = true;
- if (!checkTerm(FN)) result = false;
- if (!checkTerm(MAIN)) result = false;
- if (!checkTerm(LEFTBRACKET)) result = false;
- if (!checkTerm(RIGHTBRACKET)) result = false;
- if (!checkTerm(LEFTFIGURE)) result = false;
- return result;
- }
- bool funcMainBlock() {
- if (!checkMainStart()) return false;
- bool result = StartLoopForOperator();
- if (!checkTerm(RIGHTFIGURE)) result = false;
- if (next != terms.size()) result = false;
- return result;
- }
- bool printOperator() {
- bool result = true;
- if (!checkTerm(PRINTLN)) result = false;
- if (!checkTerm(LEFTBRACKET)) result = false;
- if (!checkTerm(STRINGCONST)) result = false;
- if (!checkTerm(RIGHTBRACKET)) result = false;
- if (!checkTerm(SEMICOLON)) result = false;
- return result;
- }
- bool operatorLoop() {
- int save = next;
- if (operatorLoop_1()) return true;
- next = save;
- if (operatorLoop_2()) return true;
- return false;
- }
- bool operatorLoop_2() {
- return checkTerm(LOOP) &&
- checkTerm(LEFTFIGURE) &&
- StartLoopForOperator() &&
- checkTerm(RIGHTFIGURE);
- }
- bool operatorLoop_1() {
- return checkTerm(LOOP) &&
- takeOperator();
- }
- bool takeOperator() {
- int save = next;
- if (printOperator()) return true;
- next = save;
- if (operatorIf()) return true;
- next = save;
- if (operatorLoop()) return true;
- return false;
- }
- bool StartLoopForOperator() {
- bool result = true;
- while (!peekTerm(RIGHTFIGURE) && result) {
- result = takeOperator();
- }
- return result;
- }
- bool operatorIf_1() {
- return checkTerm(IF) &&
- checkTerm(LEFTBRACKET) &&
- checkTerm(INT) &&
- checkTerm(RIGHTBRACKET) &&
- takeOperator() &&
- !peekTerm(ELSE);
- }
- bool operatorIf_2() {
- return checkTerm(IF) &&
- checkTerm(LEFTBRACKET) &&
- checkTerm(INT) &&
- checkTerm(RIGHTBRACKET) &&
- takeOperator() &&
- checkTerm(ELSE) &&
- takeOperator();
- }
- bool operatorIf_3() {
- return checkTerm(IF) &&
- checkTerm(LEFTBRACKET) &&
- checkTerm(INT) &&
- checkTerm(RIGHTBRACKET) &&
- checkTerm(LEFTFIGURE) &&
- StartLoopForOperator() &&
- checkTerm(RIGHTFIGURE) &&
- !peekTerm(ELSE);
- }
- bool operatorIf_4() {
- return checkTerm(IF) &&
- checkTerm(LEFTBRACKET) &&
- checkTerm(INT) &&
- checkTerm(RIGHTBRACKET) &&
- checkTerm(LEFTFIGURE) &&
- StartLoopForOperator() &&
- checkTerm(RIGHTFIGURE) &&
- checkTerm(ELSE) &&
- takeOperator();
- }
- bool operatorIf_5() {
- return checkTerm(IF) &&
- checkTerm(LEFTBRACKET) &&
- checkTerm(INT) &&
- checkTerm(RIGHTBRACKET) &&
- takeOperator() &&
- checkTerm(ELSE) &&
- checkTerm(LEFTFIGURE) &&
- StartLoopForOperator() &&
- checkTerm(RIGHTFIGURE);
- }
- bool operatorIf_6() {
- return checkTerm(IF) &&
- checkTerm(LEFTBRACKET) &&
- checkTerm(INT) &&
- checkTerm(RIGHTBRACKET) &&
- checkTerm(LEFTFIGURE) &&
- StartLoopForOperator() &&
- checkTerm(RIGHTFIGURE) &&
- checkTerm(ELSE) &&
- checkTerm(LEFTFIGURE) &&
- StartLoopForOperator() &&
- checkTerm(RIGHTFIGURE);
- }
- bool operatorIf() {
- int save = next;
- if (operatorIf_1()) return true;
- next = save;
- if (operatorIf_2()) return true;
- next = save;
- if (operatorIf_3()) return true;
- next = save;
- if (operatorIf_4()) return true;
- next = save;
- if (operatorIf_5()) return true;
- next = save;
- if (operatorIf_6()) return true;
- return false;
- }
- std::vector<Types> GetTerms(std::ifstream &in) {
- std::vector<Types> temp;
- std::string line;
- getline(in, line);
- while (!line.empty())
- {
- auto type = (Types) stoi(line);
- if (type != SPACE && type != EOL)
- temp.push_back(type);
- getline(in, line);
- }
- return temp;
- }
- int main() {
- std::string path;
- std::cout << "Enter your path to the file\n";
- getline(std::cin, path);
- std::ifstream in(path);
- if (in.is_open()) {
- next = 0;
- terms = GetTerms(in);
- in.close();
- printf(funcMainBlock() ? "SUCCESS\n" : "ERROR\n");
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement