Advertisement
Old_But_Gold

Untitled

Nov 12th, 2024
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.44 KB | None | 0 0
  1. #include <fstream>
  2. #include <vector>
  3. #include <string>
  4. #include <iostream>
  5.  
  6. typedef enum TTypes {
  7. SPACE = 0,
  8. FN,
  9. MAIN,
  10. LEFTFIGURE,
  11. RIGHTFIGURE,
  12. LEFTBRACKET,
  13. RIGHTBRACKET,
  14. PRINTLN,
  15. STRINGCONST,
  16. EOL,
  17. SEMICOLON, //;
  18. INT,
  19. IF,
  20. ELSE,
  21. LOOP,
  22. ERROR
  23. } Types;
  24.  
  25. bool printOperator();
  26. bool operatorIf_1();
  27. bool operatorIf_2();
  28. bool operatorIf_3();
  29. bool operatorIf_4();
  30. bool operatorIf_5();
  31. bool operatorIf_6();
  32. bool operatorIf();
  33. bool operatorLoop();
  34. bool operatorLoop_1();
  35. bool operatorLoop_2();
  36. bool takeOperator();
  37. bool StartLoopForOperator();
  38.  
  39. std::vector<Types> terms;
  40. int next = 0;
  41.  
  42. bool checkTerm(const Types expected) {
  43. return (expected == terms[next++]);
  44. }
  45.  
  46. bool peekTerm(const Types expected) {
  47. return (expected == terms[next]);
  48. }
  49.  
  50. bool checkMainStart() {
  51. bool result = true;
  52. if (!checkTerm(FN)) result = false;
  53. if (!checkTerm(MAIN)) result = false;
  54. if (!checkTerm(LEFTBRACKET)) result = false;
  55. if (!checkTerm(RIGHTBRACKET)) result = false;
  56. if (!checkTerm(LEFTFIGURE)) result = false;
  57. return result;
  58. }
  59.  
  60. bool funcMainBlock() {
  61. if (!checkMainStart()) return false;
  62. bool result = StartLoopForOperator();
  63. if (!checkTerm(RIGHTFIGURE)) result = false;
  64. if (next != terms.size()) result = false;
  65. return result;
  66. }
  67.  
  68. bool printOperator() {
  69. bool result = true;
  70. if (!checkTerm(PRINTLN)) result = false;
  71. if (!checkTerm(LEFTBRACKET)) result = false;
  72. if (!checkTerm(STRINGCONST)) result = false;
  73. if (!checkTerm(RIGHTBRACKET)) result = false;
  74. if (!checkTerm(SEMICOLON)) result = false;
  75. return result;
  76. }
  77.  
  78. bool operatorLoop() {
  79. int save = next;
  80. if (operatorLoop_1()) return true;
  81. next = save;
  82. if (operatorLoop_2()) return true;
  83.  
  84. return false;
  85. }
  86.  
  87. bool operatorLoop_2() {
  88. return checkTerm(LOOP) &&
  89. checkTerm(LEFTFIGURE) &&
  90. StartLoopForOperator() &&
  91. checkTerm(RIGHTFIGURE);
  92. }
  93.  
  94. bool operatorLoop_1() {
  95. return checkTerm(LOOP) &&
  96. takeOperator();
  97. }
  98.  
  99.  
  100.  
  101.  
  102. bool takeOperator() {
  103. int save = next;
  104. if (printOperator()) return true;
  105. next = save;
  106. if (operatorIf()) return true;
  107. next = save;
  108. if (operatorLoop()) return true;
  109. return false;
  110. }
  111.  
  112. bool StartLoopForOperator() {
  113. bool result = true;
  114. while (!peekTerm(RIGHTFIGURE) && result) {
  115. result = takeOperator();
  116. }
  117. return result;
  118. }
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125. bool operatorIf_1() {
  126. return checkTerm(IF) &&
  127. checkTerm(LEFTBRACKET) &&
  128. checkTerm(INT) &&
  129. checkTerm(RIGHTBRACKET) &&
  130. takeOperator() &&
  131. !peekTerm(ELSE);
  132. }
  133.  
  134. bool operatorIf_2() {
  135. return checkTerm(IF) &&
  136. checkTerm(LEFTBRACKET) &&
  137. checkTerm(INT) &&
  138. checkTerm(RIGHTBRACKET) &&
  139. takeOperator() &&
  140. checkTerm(ELSE) &&
  141. takeOperator();
  142. }
  143.  
  144. bool operatorIf_3() {
  145. return checkTerm(IF) &&
  146. checkTerm(LEFTBRACKET) &&
  147. checkTerm(INT) &&
  148. checkTerm(RIGHTBRACKET) &&
  149. checkTerm(LEFTFIGURE) &&
  150. StartLoopForOperator() &&
  151. checkTerm(RIGHTFIGURE) &&
  152. !peekTerm(ELSE);
  153. }
  154.  
  155. bool operatorIf_4() {
  156. return checkTerm(IF) &&
  157. checkTerm(LEFTBRACKET) &&
  158. checkTerm(INT) &&
  159. checkTerm(RIGHTBRACKET) &&
  160. checkTerm(LEFTFIGURE) &&
  161. StartLoopForOperator() &&
  162. checkTerm(RIGHTFIGURE) &&
  163. checkTerm(ELSE) &&
  164. takeOperator();
  165. }
  166.  
  167. bool operatorIf_5() {
  168. return checkTerm(IF) &&
  169. checkTerm(LEFTBRACKET) &&
  170. checkTerm(INT) &&
  171. checkTerm(RIGHTBRACKET) &&
  172. takeOperator() &&
  173. checkTerm(ELSE) &&
  174. checkTerm(LEFTFIGURE) &&
  175. StartLoopForOperator() &&
  176. checkTerm(RIGHTFIGURE);
  177. }
  178.  
  179. bool operatorIf_6() {
  180. return checkTerm(IF) &&
  181. checkTerm(LEFTBRACKET) &&
  182. checkTerm(INT) &&
  183. checkTerm(RIGHTBRACKET) &&
  184. checkTerm(LEFTFIGURE) &&
  185. StartLoopForOperator() &&
  186. checkTerm(RIGHTFIGURE) &&
  187. checkTerm(ELSE) &&
  188. checkTerm(LEFTFIGURE) &&
  189. StartLoopForOperator() &&
  190. checkTerm(RIGHTFIGURE);
  191. }
  192.  
  193. bool operatorIf() {
  194. int save = next;
  195. if (operatorIf_1()) return true;
  196. next = save;
  197. if (operatorIf_2()) return true;
  198. next = save;
  199. if (operatorIf_3()) return true;
  200. next = save;
  201. if (operatorIf_4()) return true;
  202. next = save;
  203. if (operatorIf_5()) return true;
  204. next = save;
  205. if (operatorIf_6()) return true;
  206. return false;
  207. }
  208.  
  209. std::vector<Types> GetTerms(std::ifstream &in) {
  210. std::vector<Types> temp;
  211. std::string line;
  212. getline(in, line);
  213. while (!line.empty())
  214. {
  215. auto type = (Types) stoi(line);
  216. if (type != SPACE && type != EOL)
  217. temp.push_back(type);
  218. getline(in, line);
  219. }
  220. return temp;
  221. }
  222.  
  223. int main() {
  224. std::string path;
  225. std::cout << "Enter your path to the file\n";
  226. getline(std::cin, path);
  227. std::ifstream in(path);
  228.  
  229. if (in.is_open()) {
  230. next = 0;
  231. terms = GetTerms(in);
  232. in.close();
  233.  
  234. printf(funcMainBlock() ? "SUCCESS\n" : "ERROR\n");
  235.  
  236. return 0;
  237. }
  238. }
  239.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement