Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.78 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <string>
  4. #include <cstring>
  5. #include <sstream>
  6. #include <vector>
  7. #include <cassert>
  8. #include <map>
  9. #include <set>
  10. #include <math.h>
  11. #include <algorithm>
  12. #include <functional>
  13. #include <cctype>
  14. #include <locale>
  15.  
  16.  
  17. namespace fz1 {
  18.  
  19. using namespace System;
  20. using namespace System::ComponentModel;
  21. using namespace System::Collections;
  22. using namespace System::Windows::Forms;
  23. using namespace System::Data;
  24. using namespace System::Drawing;
  25. using namespace std;
  26.  
  27.  
  28. stringstream *input;
  29. set<string> non_terminals;
  30. set<string> terminals;
  31. map< string, vector< vector<string> > > rules;
  32. string startSymbol;
  33. string curSymbol;
  34.  
  35.  
  36. /// <summary>
  37. /// Сводка для Form1
  38. ///
  39. /// Внимание! При изменении имени этого класса необходимо также изменить
  40. /// свойство имени файла ресурсов ("Resource File Name") для средства компиляции управляемого ресурса,
  41. /// связанного со всеми файлами с расширением .resx, от которых зависит данный класс. В противном случае,
  42. /// конструкторы не смогут правильно работать с локализованными
  43. /// ресурсами, сопоставленными данной форме.
  44. /// </summary>
  45. public ref class Form1 : public System::Windows::Forms::Form
  46. {
  47. public:
  48. Form1(void)
  49. {
  50. InitializeComponent();
  51. //
  52. //TODO: добавьте код конструктора
  53. //
  54. }
  55.  
  56. protected:
  57. /// <summary>
  58. /// Освободить все используемые ресурсы.
  59. /// </summary>
  60. ~Form1()
  61. {
  62. if (components)
  63. {
  64. delete components;
  65. }
  66. }
  67. private: System::Windows::Forms::TextBox^ non_input;
  68. private: System::Windows::Forms::TextBox^ non_output;
  69. protected:
  70.  
  71. protected:
  72.  
  73. private: System::Windows::Forms::Button^ button1;
  74. private: System::Windows::Forms::Button^ button2;
  75. private: System::Windows::Forms::Button^ button3;
  76. private: System::Windows::Forms::Button^ button4;
  77. private: System::Windows::Forms::OpenFileDialog^ openFileDialog1;
  78. private: System::Windows::Forms::SaveFileDialog^ saveFileDialog1;
  79. private: System::Windows::Forms::TextBox^ ter_input;
  80. private: System::Windows::Forms::TextBox^ rules_input;
  81. private: System::Windows::Forms::TextBox^ start_input;
  82.  
  83.  
  84.  
  85. private: System::Windows::Forms::TextBox^ ter_output;
  86. private: System::Windows::Forms::TextBox^ rules_output;
  87. private: System::Windows::Forms::TextBox^ start_output;
  88.  
  89.  
  90.  
  91. private: System::Windows::Forms::Label^ label1;
  92. private: System::Windows::Forms::Label^ label2;
  93. private: System::Windows::Forms::Label^ label3;
  94. private: System::Windows::Forms::Label^ label4;
  95. private: System::Windows::Forms::Label^ label5;
  96. private: System::Windows::Forms::Label^ label6;
  97.  
  98. private:
  99. /// <summary>
  100. /// Требуется переменная конструктора.
  101. /// </summary>
  102. System::ComponentModel::Container ^components;
  103.  
  104. #pragma region Windows Form Designer generated code
  105. /// <summary>
  106. /// Обязательный метод для поддержки конструктора - не изменяйте
  107. /// содержимое данного метода при помощи редактора кода.
  108. /// </summary>
  109. void InitializeComponent(void)
  110. {
  111. this->non_input = (gcnew System::Windows::Forms::TextBox());
  112. this->non_output = (gcnew System::Windows::Forms::TextBox());
  113. this->button1 = (gcnew System::Windows::Forms::Button());
  114. this->button2 = (gcnew System::Windows::Forms::Button());
  115. this->button3 = (gcnew System::Windows::Forms::Button());
  116. this->button4 = (gcnew System::Windows::Forms::Button());
  117. this->openFileDialog1 = (gcnew System::Windows::Forms::OpenFileDialog());
  118. this->saveFileDialog1 = (gcnew System::Windows::Forms::SaveFileDialog());
  119. this->ter_input = (gcnew System::Windows::Forms::TextBox());
  120. this->rules_input = (gcnew System::Windows::Forms::TextBox());
  121. this->start_input = (gcnew System::Windows::Forms::TextBox());
  122. this->ter_output = (gcnew System::Windows::Forms::TextBox());
  123. this->rules_output = (gcnew System::Windows::Forms::TextBox());
  124. this->start_output = (gcnew System::Windows::Forms::TextBox());
  125. this->label1 = (gcnew System::Windows::Forms::Label());
  126. this->label2 = (gcnew System::Windows::Forms::Label());
  127. this->label3 = (gcnew System::Windows::Forms::Label());
  128. this->label4 = (gcnew System::Windows::Forms::Label());
  129. this->label5 = (gcnew System::Windows::Forms::Label());
  130. this->label6 = (gcnew System::Windows::Forms::Label());
  131. this->SuspendLayout();
  132. //
  133. // non_input
  134. //
  135. this->non_input->Location = System::Drawing::Point(176, 77);
  136. this->non_input->Multiline = true;
  137. this->non_input->Name = L"non_input";
  138. this->non_input->ScrollBars = System::Windows::Forms::ScrollBars::Vertical;
  139. this->non_input->Size = System::Drawing::Size(304, 107);
  140. this->non_input->TabIndex = 0;
  141. //
  142. // non_output
  143. //
  144. this->non_output->Location = System::Drawing::Point(504, 77);
  145. this->non_output->Multiline = true;
  146. this->non_output->Name = L"non_output";
  147. this->non_output->ScrollBars = System::Windows::Forms::ScrollBars::Vertical;
  148. this->non_output->Size = System::Drawing::Size(304, 107);
  149. this->non_output->TabIndex = 1;
  150. //
  151. // button1
  152. //
  153. this->button1->Location = System::Drawing::Point(179, 12);
  154. this->button1->Name = L"button1";
  155. this->button1->Size = System::Drawing::Size(81, 26);
  156. this->button1->TabIndex = 2;
  157. this->button1->Text = L"Открыть";
  158. this->button1->UseVisualStyleBackColor = true;
  159. this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
  160. //
  161. // button2
  162. //
  163. this->button2->Location = System::Drawing::Point(266, 12);
  164. this->button2->Name = L"button2";
  165. this->button2->Size = System::Drawing::Size(81, 26);
  166. this->button2->TabIndex = 3;
  167. this->button2->Text = L"Сохранить";
  168. this->button2->UseVisualStyleBackColor = true;
  169. this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
  170. //
  171. // button3
  172. //
  173. this->button3->Location = System::Drawing::Point(353, 12);
  174. this->button3->Name = L"button3";
  175. this->button3->Size = System::Drawing::Size(81, 26);
  176. this->button3->TabIndex = 4;
  177. this->button3->Text = L"Запуск";
  178. this->button3->UseVisualStyleBackColor = true;
  179. this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
  180. //
  181. // button4
  182. //
  183. this->button4->Location = System::Drawing::Point(440, 12);
  184. this->button4->Name = L"button4";
  185. this->button4->Size = System::Drawing::Size(81, 26);
  186. this->button4->TabIndex = 5;
  187. this->button4->Text = L"Выход";
  188. this->button4->UseVisualStyleBackColor = true;
  189. this->button4->Click += gcnew System::EventHandler(this, &Form1::button4_Click);
  190. //
  191. // openFileDialog1
  192. //
  193. this->openFileDialog1->FileName = L"openFileDialog1";
  194. //
  195. // ter_input
  196. //
  197. this->ter_input->Location = System::Drawing::Point(176, 190);
  198. this->ter_input->Multiline = true;
  199. this->ter_input->Name = L"ter_input";
  200. this->ter_input->ScrollBars = System::Windows::Forms::ScrollBars::Vertical;
  201. this->ter_input->Size = System::Drawing::Size(304, 107);
  202. this->ter_input->TabIndex = 6;
  203. //
  204. // rules_input
  205. //
  206. this->rules_input->Location = System::Drawing::Point(176, 303);
  207. this->rules_input->Multiline = true;
  208. this->rules_input->Name = L"rules_input";
  209. this->rules_input->ScrollBars = System::Windows::Forms::ScrollBars::Vertical;
  210. this->rules_input->Size = System::Drawing::Size(304, 107);
  211. this->rules_input->TabIndex = 7;
  212. //
  213. // start_input
  214. //
  215. this->start_input->Location = System::Drawing::Point(176, 416);
  216. this->start_input->Name = L"start_input";
  217. this->start_input->Size = System::Drawing::Size(304, 20);
  218. this->start_input->TabIndex = 8;
  219. //
  220. // ter_output
  221. //
  222. this->ter_output->Location = System::Drawing::Point(504, 190);
  223. this->ter_output->Multiline = true;
  224. this->ter_output->Name = L"ter_output";
  225. this->ter_output->ScrollBars = System::Windows::Forms::ScrollBars::Vertical;
  226. this->ter_output->Size = System::Drawing::Size(304, 107);
  227. this->ter_output->TabIndex = 9;
  228. //
  229. // rules_output
  230. //
  231. this->rules_output->Location = System::Drawing::Point(504, 303);
  232. this->rules_output->Multiline = true;
  233. this->rules_output->Name = L"rules_output";
  234. this->rules_output->ScrollBars = System::Windows::Forms::ScrollBars::Vertical;
  235. this->rules_output->Size = System::Drawing::Size(304, 107);
  236. this->rules_output->TabIndex = 10;
  237. //
  238. // start_output
  239. //
  240. this->start_output->Location = System::Drawing::Point(504, 416);
  241. this->start_output->Name = L"start_output";
  242. this->start_output->Size = System::Drawing::Size(304, 20);
  243. this->start_output->TabIndex = 11;
  244. //
  245. // label1
  246. //
  247. this->label1->AutoSize = true;
  248. this->label1->Location = System::Drawing::Point(25, 80);
  249. this->label1->Name = L"label1";
  250. this->label1->Size = System::Drawing::Size(145, 13);
  251. this->label1->TabIndex = 12;
  252. this->label1->Text = L"Нетерминальные символы";
  253. //
  254. // label2
  255. //
  256. this->label2->AutoSize = true;
  257. this->label2->Location = System::Drawing::Point(25, 193);
  258. this->label2->Name = L"label2";
  259. this->label2->Size = System::Drawing::Size(133, 13);
  260. this->label2->TabIndex = 13;
  261. this->label2->Text = L"Терминальные символы";
  262. //
  263. // label3
  264. //
  265. this->label3->AutoSize = true;
  266. this->label3->Location = System::Drawing::Point(25, 306);
  267. this->label3->Name = L"label3";
  268. this->label3->Size = System::Drawing::Size(62, 13);
  269. this->label3->TabIndex = 14;
  270. this->label3->Text = L"Продукции";
  271. //
  272. // label4
  273. //
  274. this->label4->AutoSize = true;
  275. this->label4->Location = System::Drawing::Point(25, 416);
  276. this->label4->Name = L"label4";
  277. this->label4->Size = System::Drawing::Size(103, 13);
  278. this->label4->TabIndex = 15;
  279. this->label4->Text = L"Стартовый символ";
  280. //
  281. // label5
  282. //
  283. this->label5->AutoSize = true;
  284. this->label5->Location = System::Drawing::Point(176, 61);
  285. this->label5->Name = L"label5";
  286. this->label5->Size = System::Drawing::Size(95, 13);
  287. this->label5->TabIndex = 16;
  288. this->label5->Text = L"Входные данные:";
  289. //
  290. // label6
  291. //
  292. this->label6->AutoSize = true;
  293. this->label6->Location = System::Drawing::Point(501, 61);
  294. this->label6->Name = L"label6";
  295. this->label6->Size = System::Drawing::Size(62, 13);
  296. this->label6->TabIndex = 17;
  297. this->label6->Text = L"Результат:";
  298. //
  299. // Form1
  300. //
  301. this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
  302. this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
  303. this->ClientSize = System::Drawing::Size(853, 499);
  304. this->Controls->Add(this->label6);
  305. this->Controls->Add(this->label5);
  306. this->Controls->Add(this->label4);
  307. this->Controls->Add(this->label3);
  308. this->Controls->Add(this->label2);
  309. this->Controls->Add(this->label1);
  310. this->Controls->Add(this->start_output);
  311. this->Controls->Add(this->rules_output);
  312. this->Controls->Add(this->ter_output);
  313. this->Controls->Add(this->start_input);
  314. this->Controls->Add(this->rules_input);
  315. this->Controls->Add(this->ter_input);
  316. this->Controls->Add(this->button4);
  317. this->Controls->Add(this->button3);
  318. this->Controls->Add(this->button2);
  319. this->Controls->Add(this->button1);
  320. this->Controls->Add(this->non_output);
  321. this->Controls->Add(this->non_input);
  322. this->Name = L"Form1";
  323. this->Text = L"FZ1";
  324. this->ResumeLayout(false);
  325. this->PerformLayout();
  326.  
  327. }
  328. #pragma endregion
  329.  
  330. #define ft first
  331. #define sc second
  332. #define END "'\"'\""
  333.  
  334. private: bool isSpace(char c) {
  335. return c == ' ' || c == '\t' || c == 13 || c == 10;
  336. }
  337.  
  338. private: bool isQuota(char c) {
  339. return c == '\'' || c == '"';
  340. }
  341.  
  342. private: string removeSpaces(const string& s) {
  343. string res = "";
  344.  
  345. for(int i = 0; i < int(s.size()); i++) {
  346. if (!isSpace(s[i]))
  347. res += s[i];
  348. }
  349.  
  350. return res;
  351. }
  352.  
  353. private: string removeTrailingSpaces(const string& s) {
  354. int l = 0;
  355. int r = s.size() - 1;
  356.  
  357. for(; l < int(s.size()) && isSpace(s[l]); l++);
  358. for(; r >= 0 && isSpace(s[r]); r--);
  359.  
  360. string res = "";
  361.  
  362. for(int i = l; i <= r; i++)
  363. res += s[i];
  364.  
  365. return res;
  366. }
  367.  
  368. private: string removeQuotas(string s) {
  369. s = removeTrailingSpaces(s);
  370.  
  371. if (int(s.size()) > 0 && isQuota(s[0]))
  372. s = s.substr(1, int(s.size()) - 2);
  373.  
  374. return s;
  375. }
  376.  
  377. private: int parseToInt(string s) {
  378. s = removeSpaces(s);
  379. int res = 0;
  380.  
  381. for(int i = 0; i < int(s.size()); i++) {
  382. res = res * 10 + s[i] - '0';
  383. }
  384.  
  385. return res;
  386. }
  387.  
  388. private: string toString(int n) {
  389. string res = "";
  390.  
  391. while(n) {
  392. res += char(n % 10 + '0');
  393. n /= 10;
  394. }
  395. if (int(res.size()) == 0)
  396. res = "0";
  397. reverse(res.begin(), res.end());
  398.  
  399. return res;
  400. }
  401.  
  402. private: string toString(const vector<string>& v) {
  403. string res = "";
  404.  
  405. for(int i = 0; i < int(v.size()); i++) {
  406. if (int(v[i].size()) > 1)
  407. res += "'" + v[i] + "'";
  408. else
  409. res += v[i];
  410. }
  411. if (res.size()==0){
  412. return "''";
  413. }
  414. return res;
  415. }
  416.  
  417. private: string norm(string s) {
  418. if (int(s.size()) != 1)
  419. s = "'" + s + "'";
  420. return s;
  421. }
  422.  
  423. private: string readToken() {
  424. string line;
  425. if (!getline(*input, line))
  426. return END;
  427. return removeTrailingSpaces(line);
  428. }
  429.  
  430. private: set<string> readSet() {
  431. set<string> res;
  432. string line;
  433. while ((line = readToken()) != END) {
  434. line = removeQuotas(line);
  435. res.insert(line);
  436. }
  437. return res;
  438. }
  439.  
  440. private: vector<string> parseSeq(string s) {
  441. s = removeTrailingSpaces(s);
  442. char separator = 0;
  443. vector<string> res;
  444. string cur = "";
  445.  
  446. for(int i = 0; i < int(s.size()); i++) {
  447. if (isQuota(s[i])) {
  448. if (separator == 0) {
  449. separator = s[i];
  450. } else {
  451. assert(separator == s[i]);
  452. separator = 0;
  453. if (cur.size()) {
  454. res.push_back(cur);
  455. }
  456. cur = "";
  457. }
  458. } else {
  459. if (separator)
  460. cur += s[i];
  461. else
  462. res.push_back(string(1, s[i]));
  463. }
  464. }
  465.  
  466. if (cur.size() != 0)
  467. res.push_back(cur);
  468.  
  469. return res;
  470. }
  471.  
  472. private: map< string, vector< vector<string> > > readRules() {
  473. map< string, vector< vector<string> > > res;
  474. string line;
  475. while ((line = readToken()) != END) {
  476. assert(line.find(' ') != string::npos);
  477. int spacePos = int(line.find(' '));
  478.  
  479. if (isQuota(line[0])) {
  480. spacePos = line.find(line[0], 1) + 1;
  481. }
  482.  
  483. string left = line.substr(0, spacePos);
  484. string right = line.substr(spacePos + 1, line.size());
  485.  
  486. left = removeQuotas(left);
  487. res[left].push_back(parseSeq(right));
  488. }
  489.  
  490. return res;
  491. }
  492.  
  493. private: bool isTerminal(const string& s) {
  494. return bool(terminals.count(s));
  495. }
  496.  
  497. private: void removeEpsRules() {
  498. bool need = false;
  499. for(map< string, vector< vector< string> > >::iterator it = rules.begin(); it != rules.end(); it++) {
  500. vector< vector<string> >& seqs = it->sc;
  501. for(int i = 0; i < int(seqs.size()); i++) {
  502. for(int y = 0; y < int(seqs[i].size()); y++) {
  503. if(seqs[i][y] == startSymbol)
  504. need = true;
  505. }
  506. }
  507. }
  508. if(need){
  509. string oldStartSymbol = startSymbol;
  510. string newStartSymbol = nextSymbol();
  511. for(int i = 0; i < int(rules[oldStartSymbol].size()); i++){
  512. rules[newStartSymbol].push_back(rules[oldStartSymbol][i]);
  513. }
  514. startSybmol = newStartSymbol;
  515. non_terminals.insert(newStartSymbol);
  516. }
  517. for(set<string>::iterator it = non_terminals.begin(); it !=non_terminals.end(); it++){
  518. string sym =*it;
  519. bool need = false;
  520. for(int i = 0; i<int(rules[sym].size()); i++){
  521. if(rules[sym][i].size == 0)
  522. need = true;
  523. }
  524. if(!need)
  525. continue;
  526. bool was = false;
  527. for(map<string, vector<vector<srting>>>:: iterator it1 = rules.begin(); it1 = rules.end(); it1++){
  528. vector<vector<string>> & seqs = it1->sc;
  529. vector<vector<string>> add;
  530. for(int i = 0; i<int(seqs.size()); i++){
  531. need = false;
  532. for(int y = 0; y<int(seqs[i].size()); y++){
  533. if(seqs[i][y] == sym)
  534. need = true;
  535. }
  536. if(!need)
  537. continue;
  538. vector<string> cur;
  539. for(int y = 0; y < int(seqs[i].size()); y++)
  540. if(seqs[i][y] != sym)
  541. cur.push_back(seqs[i][y]);
  542. add.push_back(cur);
  543. }
  544. if(add.size())
  545. was = true;
  546. for(int i = 0; i < int(add.size()); i++){
  547. seqs.push_back(add[i]);
  548. }
  549. }
  550. if(!was)
  551. continue;
  552. for(;;){
  553. vector< vector< string > >::iterator pos = rules[sym].find(vector<string>());
  554. if (pos ==rules[sym].end())
  555. break;
  556. rules[sym].erase(pos);
  557. }
  558. }
  559. }
  560.  
  561.  
  562.  
  563.  
  564. private: void removeSeqRules() {
  565. for(set<string>::iterator it1 = non_terminals.begin(); it1 != non_terminals.end(); it1++) {
  566. string from = *it1;
  567. string to = "";
  568. for(map< string, vector< vector< string> > >::iterator it2 = rules.begin(); it2 != rules.end(); it2++) {
  569. vector< vector<string> >& seqs = it2->sc;
  570. for(int i = 0; i < int(seqs.size()); i++) {
  571. vector<string>& seq = seqs[i];
  572. if (int(seq.size()) != 1)
  573. continue;
  574. if (seq[0] == from && seq[0] != it2->ft) {
  575. to = it2->ft;
  576. }
  577. }
  578. }
  579. if (int(to.size()) == 0)
  580. continue;
  581.  
  582. if (startSymbol == from)
  583. startSymbol = to;
  584.  
  585. for(map< string, vector< vector< string> > >::iterator it2 = rules.begin(); it2 != rules.end(); it2++) {
  586. vector< vector<string> >& seqs = it2->sc;
  587. for(int i = 0; i < int(seqs.size()); i++) {
  588. vector<string>& seq = seqs[i];
  589. for(int j = 0; j < int(seq.size()); j++) {
  590. if (seq[j] == from)
  591. seq[j] = to;
  592. }
  593. }
  594. }
  595. vector< vector<string> >& seqs = rules[from];
  596. for(int i = 0; i < int(seqs.size()); i++) {
  597. rules[to].push_back(seqs[i]);
  598. }
  599. rules.erase(from);
  600. }
  601. set<string> new_nt;
  602. for(set<string>::iterator it = non_terminals.begin(); it != non_terminals.end(); it++) {
  603. if (rules.count(*it))
  604. new_nt.insert(*it);
  605. }
  606. non_terminals = new_nt;
  607. for(map< string, vector< vector< string> > >::iterator it = rules.begin(); it != rules.end(); it++) {
  608. vector< vector<string> >& seqs = it->sc;
  609. vector< vector<string> > newseqs;
  610.  
  611. sort(seqs.begin(), seqs.end());
  612. seqs.erase(unique(seqs.begin(), seqs.end()), seqs.end());
  613.  
  614. for(int i = 0; i < int(seqs.size()); i++) {
  615. if (int(seqs[i].size()) == 1 && seqs[i][0] == it->ft)
  616. continue;
  617. if (int(seqs[i].size()) == 1)
  618. assert(isTerminal(seqs[i][0]));
  619. newseqs.push_back(seqs[i]);
  620. }
  621. rules[it->ft] = newseqs;
  622. }
  623. }
  624.  
  625. private: string nextSymbol() {
  626. do {
  627. int idx = int(curSymbol.size()) - 1;
  628. for(; idx >= 0 && curSymbol[idx] == 'Z'; idx--)
  629. curSymbol[idx] = 'A';
  630. if (idx == -1) {
  631. int sz = int(curSymbol.size());
  632. curSymbol = string(sz + 1, 'A');
  633. } else {
  634. curSymbol[idx]++;
  635. }
  636. } while(non_terminals.count(curSymbol) || terminals.count(curSymbol));
  637. return curSymbol;
  638. }
  639.  
  640. private: bool isNormalSeq(const vector<string>& seq) {
  641. if (int(seq.size()) == 0)
  642. return true;
  643. if (int(seq.size()) == 1)
  644. return isTerminal(seq[0]);
  645. if (int(seq.size()) == 2 && isTerminal(seq[0]) && !isTerminal(seq[1]))
  646. return true;
  647. return false;
  648. }
  649.  
  650. private: void replaceLongRules() {
  651. map<string, vector< vector<string> > > newRules;
  652. for(map<string, vector< vector<string> > >::iterator it = rules.begin(); it != rules.end(); it++) {
  653. vector< vector<string> >& seqs = it->sc;
  654. string mem = curSymbol;
  655. for(int i = 0; i < int(seqs.size()); i++) {
  656. vector<string> seq = seqs[i];
  657. if (isNormalSeq(seq)) {
  658. newRules[it->ft].push_back(seq);
  659. continue;
  660. }
  661. string cur = it->ft;
  662. string next = nextSymbol();
  663. for(int j = 0; j < int(seq.size()) - 1; j++) {
  664. vector<string> nseq;
  665. nseq.push_back(seq[0]);
  666. nseq.push_back(next);
  667. non_terminals.insert(next);
  668.  
  669. newRules[cur].push_back(nseq);
  670. seq.erase(seq.begin(), seq.begin() + 1);
  671.  
  672. cur = next;
  673. next = nextSymbol();
  674.  
  675. if (isNormalSeq(seq)) {
  676. newRules[cur].push_back(seq);
  677. break;
  678. }
  679. }
  680. }
  681. curSymbol = mem;
  682. }
  683. rules = newRules;
  684. }
  685.  
  686. private: void check() {
  687. for(map<string, vector< vector<string> > >::iterator it = rules.begin(); it != rules.end(); it++) {
  688. vector< vector<string> >& seqs = it->sc;
  689. assert(!isTerminal(it->ft));
  690. assert(non_terminals.count(it->ft));
  691. for(int i = 0; i < int(seqs.size()); i++) {
  692. assert(isNormalSeq(seqs[i]));
  693. }
  694. }
  695. for(set<string>::iterator it = non_terminals.begin(); it != non_terminals.end(); it++) {
  696. assert(rules.count(*it));
  697. }
  698. }
  699.  
  700. private: void printNonTerminals() {
  701. for(set<string>::iterator it = non_terminals.begin(); it != non_terminals.end(); it++) {
  702. this->non_output->AppendText(convertToString(norm(*it)));
  703. }
  704. }
  705.  
  706. private: void printTerminals() {
  707.  
  708. for(set<string>::iterator it = terminals.begin(); it != terminals.end(); it++) {
  709. if (*it == "")
  710. continue;
  711. this->ter_output->AppendText(convertToString(norm(*it)));
  712. }
  713. }
  714.  
  715. private: void printRules() {
  716. for(map<string, vector< vector<string> > >::iterator it = rules.begin(); it != rules.end(); it++) {
  717. vector< vector<string> >& seqs = it->sc;
  718. for(int i = 0; i < int(seqs.size()); i++) {
  719. this->rules_output->AppendText(convertToString(norm(it->ft) + " " + toString(seqs[i])));
  720. }
  721. }
  722. }
  723.  
  724. private: void printGramm() {
  725. printNonTerminals();
  726. printTerminals();
  727. printRules();
  728. this->start_output->AppendText(convertToString(startSymbol));
  729. }
  730.  
  731.  
  732. private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {
  733. this->non_output->Clear();
  734. this->ter_output->Clear();
  735. this->rules_output->Clear();
  736. this->start_output->Clear();
  737.  
  738.  
  739. input = convertToStringStream(this->non_input->Text);
  740. non_terminals = readSet();
  741.  
  742. input = convertToStringStream(this->ter_input->Text);
  743. terminals = readSet();
  744. //terminals.insert("");
  745.  
  746. input = convertToStringStream(this->rules_input->Text);
  747. rules = readRules();
  748.  
  749. input = convertToStringStream(this->start_input->Text);
  750. startSymbol = readToken();
  751.  
  752. removeSeqRules();
  753. curSymbol = string(1, 'A' - 1);
  754.  
  755. removeEpsRules();
  756.  
  757. removeSeqRules();
  758.  
  759. // curSymbol = string(1, 'A' - 1);
  760. replaceLongRules();
  761. check();
  762. printGramm();
  763. }
  764.  
  765. private: stringstream* convertToStringStream(String^ s) {
  766. stringstream *input = new stringstream(ios::in | ios::out);
  767.  
  768. int n = s->Length;
  769. array<Char>^ chars = s->ToCharArray();
  770. string str = "";
  771.  
  772. for (int i = 0; i < n; i++) {
  773. str.push_back(chars[i]);
  774. }
  775. *input << str;
  776. return input;
  777. }
  778. private: String^ convertToString(string s) {
  779. s += char(13);
  780. s += char(10);
  781. return gcnew String(s.c_str());
  782. }
  783.  
  784. private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e) {
  785. Close();
  786. }
  787. private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
  788. openFileDialog1->InitialDirectory = "d:\\";
  789. openFileDialog1->Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
  790. openFileDialog1->FilterIndex = 2;
  791. openFileDialog1->RestoreDirectory = true;
  792.  
  793. if (openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK) {
  794. IO::Stream^ stream;
  795. if ((stream = openFileDialog1->OpenFile()) != nullptr) {
  796. readStream(stream);
  797. stream->Close();
  798. }
  799. }
  800.  
  801. }
  802. private: void readStream(IO::Stream^ stream) {
  803. stringstream text;
  804. int c;
  805. while ((c = stream->ReadByte()) != -1) {
  806. text << char(c);
  807. }
  808.  
  809. this->non_input->Clear();
  810. this->ter_input->Clear();
  811. this->rules_input->Clear();
  812. this->start_input->Clear();
  813.  
  814. string line;
  815. assert(getline(text, line));
  816. int n = parseToInt(line);
  817.  
  818. for(int i = 0; i < n; i++) {
  819. assert(getline(text, line));
  820. this->non_input->AppendText(convertToString(line));
  821. }
  822.  
  823. assert(getline(text, line));
  824. n = parseToInt(line);
  825.  
  826. for(int i = 0; i < n; i++) {
  827. assert(getline(text, line));
  828. this->ter_input->AppendText(convertToString(line));
  829. }
  830.  
  831. assert(getline(text, line));
  832. n = parseToInt(line);
  833.  
  834. for(int i = 0; i < n; i++) {
  835. assert(getline(text, line));
  836. this->rules_input->AppendText(convertToString(line));
  837. }
  838.  
  839. assert(getline(text, line));
  840. this->start_input->AppendText(convertToString(line));
  841. }
  842.  
  843. private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
  844. SaveFileDialog^ saveFileDialog1 = gcnew SaveFileDialog;
  845. saveFileDialog1->Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
  846. saveFileDialog1->FilterIndex = 2;
  847. saveFileDialog1->RestoreDirectory = true;
  848. if ( saveFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK ) {
  849. IO::Stream^ stream;
  850. if ( (stream = saveFileDialog1->OpenFile()) != nullptr ) {
  851. String^ s;
  852.  
  853. input = convertToStringStream(this->non_output->Text);
  854. non_terminals = readSet();
  855.  
  856. int n = int(non_terminals.size());
  857.  
  858. s += convertToString(toString(n));
  859.  
  860. for(set<string>::iterator it = non_terminals.begin(); it != non_terminals.end(); it++) {
  861. s += convertToString(norm(*it));
  862. }
  863.  
  864. input = convertToStringStream(this->ter_output->Text);
  865. terminals = readSet();
  866.  
  867. n = int(terminals.size());
  868.  
  869. s += convertToString(toString(n));
  870.  
  871. for(set<string>::iterator it = terminals.begin(); it != terminals.end(); it++) {
  872. s += convertToString(norm(*it));
  873. }
  874.  
  875.  
  876. input = convertToStringStream(this->rules_output->Text);
  877. rules = readRules();
  878.  
  879. n = 0;
  880. for(map<string, vector< vector<string> > >::iterator it = rules.begin(); it != rules.end(); it++) {
  881. n += int(it->sc.size());
  882. }
  883.  
  884. s += convertToString(toString(n));
  885.  
  886. for(map<string, vector< vector<string> > >::iterator it = rules.begin(); it != rules.end(); it++) {
  887. vector< vector<string> >& seqs = it->sc;
  888. for(int i = 0; i < int(seqs.size()); i++) {
  889. s += convertToString(norm(it->ft) + " " + toString(seqs[i]));
  890. }
  891. }
  892.  
  893. input = convertToStringStream(this->start_output->Text);
  894. startSymbol = readToken();
  895. s += convertToString(startSymbol);
  896.  
  897. array<Char>^ a = s->ToCharArray();
  898.  
  899. for(int i = 0; i < s->Length; i++) {
  900. stream->WriteByte(a[i]);
  901. }
  902. stream->Close();
  903. }
  904. }
  905. }
  906. };
  907. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement