Advertisement
aLT22

Untitled

Dec 25th, 2014
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. struct node // Везде, где лист, переделать на node
  8. {
  9. string s;
  10. string str; // переделать s1 на str
  11. int nomer; // переделать number na nomer
  12. node *prev;
  13. node *next;
  14. };
  15.  
  16. void add(node**pend, string stroka); // переделать str na stroka
  17.  
  18. const int OBR = 1, CBR = 2, ILI = 3, TILDA = 4, RAVNO = 5, AMP = 6, LETTER = 7, DIGIT = 8, DC = 9;
  19.  
  20. int getLC();
  21. string fromlc(int lc);
  22. void nextStep(int lc);
  23. void ERROR (char ch, int lc); // изменил (переделать) error na ERROR
  24. void S();
  25. void E();
  26. void T(int sign);
  27. void M();
  28. void I();
  29. void C();
  30. bool A();
  31. bool D();
  32. char lctochar(int lc);
  33. void Sort();
  34. void deletenode(node *pend); // изменил (переделать) dellist na deletenode and list na node
  35. node *find(node *pbeg, int key);
  36. string inttostr(int n);
  37.  
  38. ifstream fin("program.txt");
  39. ofstream fout("result.txt");
  40. ofstream fouts ("output.txt"); // изменил (переделать) f1out na fouts
  41.  
  42. int lexical_class, num;
  43. char symbol; // изменил (переделать) c na symbol
  44. node *pend;
  45. node *pbeg;
  46.  
  47. int main ()
  48. {
  49. fout << "Begin parsing!" << endl; // изменил (переделать) "Razbor nachalsya" na "Begin parsing!" and '\n' na endl
  50. bool flag = 1; // изменил (переделать) true na 1
  51. int i = 0;
  52. num = 0;
  53. while (!fin.eof() && flag)
  54. {
  55. i++;
  56. pbeg = new node;
  57. pbeg -> next = 0;
  58. pbeg -> prev = 0;
  59. pbeg -> nomer = 0;
  60. pend = pbeg;
  61. fouts << "Operator " << i << endl; // изменил (переделать) "Operator prisvaivaniya nomer " na "Operator " and '\n' na endl
  62. try
  63. {
  64. lexical_class = getLC();
  65. S();
  66. node *pv;
  67. pv = pbeg;
  68. Sort();
  69. pv = pbeg -> next;
  70. int k = 1;
  71. fout << "Stroka: " << i << '\n'; // изменил (переделать) "Stroka nomer " na "Stroka: "
  72. while (pv != 0)
  73. {
  74.  
  75. fouts << pv -> nomer << ": "<< pv -> s << " "<< pv -> str <<'\n'; // изменил (переделать) melkie ispravleniya v strokah (" \\") and '\n'
  76. if (pv -> s[0] != 'С' && pv -> s[0] != 'V')
  77. {
  78. fout << "Operator " << k << ": " << pv -> str <<'\n';
  79. k++;
  80. }
  81. pv = pv -> next;
  82. }
  83. fouts << endl; // изменил (переделать) '\n' na endl
  84. deletenode (pbeg);
  85. }
  86.  
  87. catch(string s)
  88. {
  89. flag = false;
  90. cout << "ERROR" << endl; // изменил (переделать) "error" na "ERROR" and '\n' na endl
  91. fout << "Stroka: " << i << endl;
  92. node *pv = pbeg -> next;
  93. int k = 1;
  94. while (pv != 0)
  95. {
  96. int a = pv -> s.length();
  97. if (pv -> s != "" && pv -> s[0] != 'С' && pv -> s[0] != 'V' && pv -> s[a-1] == ')')
  98. {
  99. fout << "Operator " << k << ": " << pv -> str << endl; // изменил (переделать) endl
  100. k++;
  101. }
  102. pv = pv -> next;
  103. }
  104. fout << s;
  105. fout << endl <<"End of syntax analysing"; // изменил (переделать) endl
  106. fouts.close();
  107. ofstream foutt ("output.txt"); // изменил (переделать) f2out na foutt
  108. foutt << "GRAMMAR ERROR" << endl; // изменил (переделать) dobavil posle GRAMMAR ERROR endl
  109. foutt.close();
  110. }
  111. catch(...)
  112. {
  113. cout<<"ERROR FOUND";
  114. }
  115. }
  116. if (flag)
  117. {
  118. fout << "Syntax analysing was successful"; // изменил (переделать) rus on eng phrase in "" and add bracets {}
  119. }
  120. fin.close();
  121. fout.close();
  122. fouts.close();
  123. system ("pause");
  124. return 0;
  125. }
  126.  
  127. void add(node **pend, string stroka)
  128. {
  129. node *pv = new node;
  130. pv -> s = stroka;
  131. pv -> next = 0;
  132. pv -> prev = *pend;
  133. (*pend) -> next = pv;
  134. *pend = pv ;
  135. }
  136.  
  137. void Sort() //параметр - указатель на начало списка.
  138. {
  139. node *p_node; // указатель для внешнего цикла for
  140. node *pp_node; // для внутреннего
  141. for (p_node = pbeg -> next; p_node != NULL; p_node = p_node -> next) //пробегаемся по всех узлах списка
  142. for (pp_node = pbeg -> next; pp_node -> next != NULL; pp_node = pp_node -> next) //а здесь только до узла, next которого указывает на NULL
  143. //обычный обмен значениями
  144. if (pp_node -> nomer > pp_node -> next -> nomer)
  145. {
  146. int temp; // изменил (переделать) tmp na temp
  147. string temp1,temp2; // изменил (переделать) tmp1 and tmp2 na temp1 and temp2
  148. temp = pp_node -> nomer;
  149. pp_node -> nomer = pp_node -> next -> nomer;
  150. pp_node -> next -> nomer = temp;
  151. temp1 = pp_node -> s;
  152. pp_node -> s = pp_node -> next -> s;
  153. pp_node -> next -> s = temp1;
  154. temp2 = pp_node -> str;
  155. pp_node -> str = pp_node -> next -> str;
  156. pp_node -> next -> str = temp2;
  157. }
  158. }
  159.  
  160. node *find(node *pbeg, int key) //поиск элемента по ключу
  161. {
  162. node *pv = pbeg;
  163. while (pv)
  164. {
  165. if(pv -> nomer == key)
  166. {
  167. break;
  168. }
  169. pv = pv -> next;
  170. }
  171. return pv;
  172. }
  173.  
  174. void deletenode(node *pbeg)
  175. {
  176. if (pbeg != NULL)
  177. {
  178. deletenode(pbeg -> next);
  179. pbeg -> next = 0;
  180. pbeg -> prev = 0;
  181. delete pbeg;
  182. }
  183. }
  184.  
  185. void S() // Vrode izmenena
  186. {
  187. add(&pend, "");
  188. node *pv = pend;
  189. I();
  190. pv -> s = ":(^" + inttostr(pend -> nomer) + ',';
  191. pv -> str = pend -> str;
  192. nextStep (RAVNO);
  193. E();
  194. // nextStep (CBR);
  195. pv -> nomer = num++;
  196. node *pt = find(pbeg, pv -> nomer - 1);
  197. pv -> str = pv -> str + '(' + pt -> str + ')';
  198. string stroka = inttostr(pt -> nomer);
  199. pv -> s = pv -> s + '^' + stroka + ')';
  200. }
  201.  
  202. void E()
  203. {
  204. T();
  205. lexical_class = getLC();
  206. if (lexical_class == ILI)
  207. {
  208. add(&pend, "");
  209. node *pv = pend;
  210. lexical_class = getLC();
  211. E();
  212. pv -> nomer = num++;
  213. node *pt = find(pbeg, pv -> nomer - 1);
  214. pv -> str = '|' + pt -> str;
  215. string stroka = inttostr(pt -> nomer);
  216. pv -> s = "|(^" + stroka +",<>)";
  217. }
  218. switch (lexical_class)
  219. {
  220. /*case MIN:
  221. {
  222. add(&pend, "");
  223. node *pv = pend;
  224. lexical_class = getLC();
  225. E();
  226. pv -> nomer = num++;
  227. node *pt = find(pbeg, pv -> nomer - 1);
  228. pv -> str = '-' + pt -> str;
  229. string stroka = inttostr(pt -> nomer);
  230. pv -> s = "-(^" + stroka +",<>)";
  231. break;
  232. }*/
  233. //case KAV: nextStep (KAV); T(); break;
  234. /*case LETTER:
  235. {
  236. I();
  237. if (lexical_class != CBR)
  238. {
  239. if (lexical_class == OBR)
  240. {
  241. add(&pend, "");
  242. node *pv = pend;
  243. pv -> str = pv -> prev -> str;
  244. pv -> s = "=(^" + inttostr(pv -> prev -> nomer) + ",^";
  245. nextStep(OBR);
  246. E();
  247. nextStep (CBR);
  248. pv -> nomer = num++;
  249. node *pt = find(pbeg, pv -> nomer - 1);
  250. pv -> s = pv -> s + inttostr(pt -> nomer) + ')';
  251. pv -> str = pv -> str + '(' + pt -> str + ')';
  252. }
  253. }
  254. break;
  255. }*/
  256. default: ERROR('E',0);
  257. }
  258. }
  259.  
  260. void T()
  261. {
  262. add(&pend, "");
  263. node *pv = pend;
  264. M();
  265.  
  266. if (lexical_class==AMP) // tyt nado peredelivat' pod svoi variant
  267. {
  268. lexical_class = getLC();
  269.  
  270. }
  271. else
  272. {
  273. node *pt = pv;
  274. (pv -> prev) -> next = pv -> next;
  275. pv -> next -> prev = pv -> prev;
  276. delete pt;
  277. return;
  278. }
  279. pv -> nomer = num++;
  280. node *pt = find(pbeg, pv -> nomer - 1);
  281. pv -> str = pv -> str + '&' + pt -> str + ')';
  282. string stroka = inttostr(pt -> nomer);
  283. pv -> s = pv -> s + '^' + stroka + ')';
  284. }
  285.  
  286.  
  287.  
  288. void M()
  289. {
  290. switch (lexical_class) // nado peredelivat' pod svoi variant (est' ideya vzyat' ochen' pohozhii s ih variantom, chtobi menshe peredelivat')
  291. {
  292. case TILDA:
  293. nextStep(TILDA);
  294. M();
  295. case OBR:
  296. nextStep(OBR);
  297. E();
  298. nextStep(CBR);
  299. case LETTER:
  300. I();
  301. case DIGIT:
  302. C();
  303. }
  304.  
  305.  
  306. bool D()
  307. {
  308. return(lexical_class=='0' || lexical_class=='1');
  309. }
  310.  
  311. bool A()
  312. {
  313. return(lexical_class>='a' && lexical_class<='z' || lexical_class=='_');
  314. }
  315.  
  316. void C() // chyt' peredelat'
  317. {
  318. add(&pend, "");
  319. pend -> s += '#'; // nado peredelivat' pod svou variant
  320. if (lexical_class != DIGIT)
  321. {
  322. ERROR('C',0);
  323. }
  324. while (lexical_class != CBR && lexical_class != COMMA) // nado peredelivat'
  325. {
  326. if (lexical_class != DIGIT)
  327. {
  328. ERROR('C',0);
  329. break;
  330. }
  331. pend -> s += symbol;
  332. lexical_class = getLC();
  333. }
  334. pend -> str = pend -> s;
  335. pend -> s= "С(" + pend -> s + ",<>)";
  336. pend -> nomer = num++;
  337. }
  338.  
  339. void I()
  340. {
  341. add(&pend, "");
  342. pend -> s += symbol;
  343. if (lexical_class == LETTER)
  344. lexical_class = getLC();
  345. else throw string("WANTED IDENTIFICATOR");
  346. while (lexical_class != OBR && lexical_class != CBR && lexical_class != COMMA)
  347. {
  348. if (lexical_class != DIGIT)
  349. {
  350. ERROR('I',0);
  351. break;
  352. }
  353. pend -> s += symbol;
  354. lexical_class = getLC();
  355. }
  356. pend -> str = pend -> s;
  357. pend -> s = "V(" + pend -> s + ",<>)";
  358. pend -> nomer = num++;
  359. }
  360.  
  361. int getLC()
  362. {
  363. fin.get(symbol);
  364. if (!fin.eof() && symbol != '\n')
  365. {
  366. if (symbol >= 'a' && symbol <= 'z' || symbol == '_') return 7;
  367. else if (symbol == '0' || symbol == '1') return 8;
  368. switch (symbol)
  369. {
  370. case ('('):
  371. return 1;
  372. case (')'):
  373. return 2;
  374. case ('|'):
  375. return 3;
  376. case ('~'):
  377. return 4;
  378. case ('='):
  379. return 5;
  380. case ('&'):
  381. return 6;
  382. case (';'):
  383. return 9;
  384. default: ERROR('G', 0);
  385. }
  386. }
  387. return 0;
  388. }
  389.  
  390. void nextStep(int lc)
  391. {
  392. if (lc == lexical_class) lexical_class = getLC();
  393. else ERROR('N', lc);
  394. }
  395.  
  396. void ERROR (char ch, int lc)
  397. {
  398. string s = "";
  399. switch (ch)
  400. {
  401. case ('E'):
  402. s = "Waiting special terminal \n Error in rule E"; throw string(s); break;
  403. case ('C'):
  404. s = "Waiting DIGIT "+pend->s+"\n Error in rule E"; throw string(s); break;
  405. case ('I'):
  406. s = "Unreal identificator: Waiting digit after " + pend -> s + "\n" + "Error in rule I" ; throw string(s); break;
  407. case ('N'):
  408. s = "Error: instead symbol \'"; s += symbol; s += fromlc(lc); throw string(s); break;
  409. case ('G'):
  410. s = "Symbol "; s += symbol; s += " in \""; s += pend -> s; s += symbol; s += "\" not satisfy Grammar";
  411. throw string(s);
  412. break;
  413.  
  414. }
  415. }
  416.  
  417. string inttostr(int n)
  418. {
  419. int i;
  420. string stroka = "";
  421. while (n != 0)
  422. {
  423. i = n % 10;
  424. char r = i + '0';
  425. stroka = r + stroka;
  426. n = n / 10;
  427. }
  428. return stroka;
  429. }
  430.  
  431. string fromlc(int lc)
  432. {
  433. switch(lc)
  434. {
  435. case 1: return "\' ОЖИДАЛСЯ СИМВОЛ \'(\'";
  436. case 2: return "\' ОЖИДАЛСЯ СИМВОЛ \')\'";
  437. case 3: return "\' ОЖИДАЛСЯ СИМВОЛ \'-\'";
  438. case 4: return "\' ОЖИДАЛСЯ СИМВОЛ \'+\'";
  439. case 5: return "\' ОЖИДАЛСЯ СИМВОЛ \',\'";
  440. case 6: return "\' ОЖИДАЛСЯ СИМВОЛ \'#\'";
  441. case 7: return "\' ОЖИДАЛСЯ СИМВОЛ \'*\'";
  442. case 8: return "\' ОЖИДАЛСЯ БУКВА";
  443. case 9: return "\' ОЖИДАЛСЯ ЦИФРА";
  444. default: return "";
  445. }
  446. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement