Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <string>
- #include <cstring>
- #include <sstream>
- #include <vector>
- #include <cassert>
- #include <map>
- #include <set>
- #include <math.h>
- #include <algorithm>
- #include <functional>
- #include <cctype>
- #include <locale>
- namespace fz1 {
- using namespace System;
- using namespace System::ComponentModel;
- using namespace System::Collections;
- using namespace System::Windows::Forms;
- using namespace System::Data;
- using namespace System::Drawing;
- using namespace std;
- stringstream *input;
- set<string> non_terminals;
- set<string> terminals;
- map< string, vector< vector<string> > > rules;
- string startSymbol;
- string curSymbol;
- /// <summary>
- /// Сводка для Form1
- ///
- /// Внимание! При изменении имени этого класса необходимо также изменить
- /// свойство имени файла ресурсов ("Resource File Name") для средства компиляции управляемого ресурса,
- /// связанного со всеми файлами с расширением .resx, от которых зависит данный класс. В противном случае,
- /// конструкторы не смогут правильно работать с локализованными
- /// ресурсами, сопоставленными данной форме.
- /// </summary>
- public ref class Form1 : public System::Windows::Forms::Form
- {
- public:
- Form1(void)
- {
- InitializeComponent();
- //
- //TODO: добавьте код конструктора
- //
- }
- protected:
- /// <summary>
- /// Освободить все используемые ресурсы.
- /// </summary>
- ~Form1()
- {
- if (components)
- {
- delete components;
- }
- }
- private: System::Windows::Forms::TextBox^ non_input;
- private: System::Windows::Forms::TextBox^ non_output;
- protected:
- protected:
- private: System::Windows::Forms::Button^ button1;
- private: System::Windows::Forms::Button^ button2;
- private: System::Windows::Forms::Button^ button3;
- private: System::Windows::Forms::Button^ button4;
- private: System::Windows::Forms::OpenFileDialog^ openFileDialog1;
- private: System::Windows::Forms::SaveFileDialog^ saveFileDialog1;
- private: System::Windows::Forms::TextBox^ ter_input;
- private: System::Windows::Forms::TextBox^ rules_input;
- private: System::Windows::Forms::TextBox^ start_input;
- private: System::Windows::Forms::TextBox^ ter_output;
- private: System::Windows::Forms::TextBox^ rules_output;
- private: System::Windows::Forms::TextBox^ start_output;
- private: System::Windows::Forms::Label^ label1;
- private: System::Windows::Forms::Label^ label2;
- private: System::Windows::Forms::Label^ label3;
- private: System::Windows::Forms::Label^ label4;
- private: System::Windows::Forms::Label^ label5;
- private: System::Windows::Forms::Label^ label6;
- private:
- /// <summary>
- /// Требуется переменная конструктора.
- /// </summary>
- System::ComponentModel::Container ^components;
- #pragma region Windows Form Designer generated code
- /// <summary>
- /// Обязательный метод для поддержки конструктора - не изменяйте
- /// содержимое данного метода при помощи редактора кода.
- /// </summary>
- void InitializeComponent(void)
- {
- this->non_input = (gcnew System::Windows::Forms::TextBox());
- this->non_output = (gcnew System::Windows::Forms::TextBox());
- this->button1 = (gcnew System::Windows::Forms::Button());
- this->button2 = (gcnew System::Windows::Forms::Button());
- this->button3 = (gcnew System::Windows::Forms::Button());
- this->button4 = (gcnew System::Windows::Forms::Button());
- this->openFileDialog1 = (gcnew System::Windows::Forms::OpenFileDialog());
- this->saveFileDialog1 = (gcnew System::Windows::Forms::SaveFileDialog());
- this->ter_input = (gcnew System::Windows::Forms::TextBox());
- this->rules_input = (gcnew System::Windows::Forms::TextBox());
- this->start_input = (gcnew System::Windows::Forms::TextBox());
- this->ter_output = (gcnew System::Windows::Forms::TextBox());
- this->rules_output = (gcnew System::Windows::Forms::TextBox());
- this->start_output = (gcnew System::Windows::Forms::TextBox());
- this->label1 = (gcnew System::Windows::Forms::Label());
- this->label2 = (gcnew System::Windows::Forms::Label());
- this->label3 = (gcnew System::Windows::Forms::Label());
- this->label4 = (gcnew System::Windows::Forms::Label());
- this->label5 = (gcnew System::Windows::Forms::Label());
- this->label6 = (gcnew System::Windows::Forms::Label());
- this->SuspendLayout();
- //
- // non_input
- //
- this->non_input->Location = System::Drawing::Point(176, 77);
- this->non_input->Multiline = true;
- this->non_input->Name = L"non_input";
- this->non_input->ScrollBars = System::Windows::Forms::ScrollBars::Vertical;
- this->non_input->Size = System::Drawing::Size(304, 107);
- this->non_input->TabIndex = 0;
- //
- // non_output
- //
- this->non_output->Location = System::Drawing::Point(504, 77);
- this->non_output->Multiline = true;
- this->non_output->Name = L"non_output";
- this->non_output->ScrollBars = System::Windows::Forms::ScrollBars::Vertical;
- this->non_output->Size = System::Drawing::Size(304, 107);
- this->non_output->TabIndex = 1;
- //
- // button1
- //
- this->button1->Location = System::Drawing::Point(179, 12);
- this->button1->Name = L"button1";
- this->button1->Size = System::Drawing::Size(81, 26);
- this->button1->TabIndex = 2;
- this->button1->Text = L"Открыть";
- this->button1->UseVisualStyleBackColor = true;
- this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
- //
- // button2
- //
- this->button2->Location = System::Drawing::Point(266, 12);
- this->button2->Name = L"button2";
- this->button2->Size = System::Drawing::Size(81, 26);
- this->button2->TabIndex = 3;
- this->button2->Text = L"Сохранить";
- this->button2->UseVisualStyleBackColor = true;
- this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
- //
- // button3
- //
- this->button3->Location = System::Drawing::Point(353, 12);
- this->button3->Name = L"button3";
- this->button3->Size = System::Drawing::Size(81, 26);
- this->button3->TabIndex = 4;
- this->button3->Text = L"Запуск";
- this->button3->UseVisualStyleBackColor = true;
- this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
- //
- // button4
- //
- this->button4->Location = System::Drawing::Point(440, 12);
- this->button4->Name = L"button4";
- this->button4->Size = System::Drawing::Size(81, 26);
- this->button4->TabIndex = 5;
- this->button4->Text = L"Выход";
- this->button4->UseVisualStyleBackColor = true;
- this->button4->Click += gcnew System::EventHandler(this, &Form1::button4_Click);
- //
- // openFileDialog1
- //
- this->openFileDialog1->FileName = L"openFileDialog1";
- //
- // ter_input
- //
- this->ter_input->Location = System::Drawing::Point(176, 190);
- this->ter_input->Multiline = true;
- this->ter_input->Name = L"ter_input";
- this->ter_input->ScrollBars = System::Windows::Forms::ScrollBars::Vertical;
- this->ter_input->Size = System::Drawing::Size(304, 107);
- this->ter_input->TabIndex = 6;
- //
- // rules_input
- //
- this->rules_input->Location = System::Drawing::Point(176, 303);
- this->rules_input->Multiline = true;
- this->rules_input->Name = L"rules_input";
- this->rules_input->ScrollBars = System::Windows::Forms::ScrollBars::Vertical;
- this->rules_input->Size = System::Drawing::Size(304, 107);
- this->rules_input->TabIndex = 7;
- //
- // start_input
- //
- this->start_input->Location = System::Drawing::Point(176, 416);
- this->start_input->Name = L"start_input";
- this->start_input->Size = System::Drawing::Size(304, 20);
- this->start_input->TabIndex = 8;
- //
- // ter_output
- //
- this->ter_output->Location = System::Drawing::Point(504, 190);
- this->ter_output->Multiline = true;
- this->ter_output->Name = L"ter_output";
- this->ter_output->ScrollBars = System::Windows::Forms::ScrollBars::Vertical;
- this->ter_output->Size = System::Drawing::Size(304, 107);
- this->ter_output->TabIndex = 9;
- //
- // rules_output
- //
- this->rules_output->Location = System::Drawing::Point(504, 303);
- this->rules_output->Multiline = true;
- this->rules_output->Name = L"rules_output";
- this->rules_output->ScrollBars = System::Windows::Forms::ScrollBars::Vertical;
- this->rules_output->Size = System::Drawing::Size(304, 107);
- this->rules_output->TabIndex = 10;
- //
- // start_output
- //
- this->start_output->Location = System::Drawing::Point(504, 416);
- this->start_output->Name = L"start_output";
- this->start_output->Size = System::Drawing::Size(304, 20);
- this->start_output->TabIndex = 11;
- //
- // label1
- //
- this->label1->AutoSize = true;
- this->label1->Location = System::Drawing::Point(25, 80);
- this->label1->Name = L"label1";
- this->label1->Size = System::Drawing::Size(145, 13);
- this->label1->TabIndex = 12;
- this->label1->Text = L"Нетерминальные символы";
- //
- // label2
- //
- this->label2->AutoSize = true;
- this->label2->Location = System::Drawing::Point(25, 193);
- this->label2->Name = L"label2";
- this->label2->Size = System::Drawing::Size(133, 13);
- this->label2->TabIndex = 13;
- this->label2->Text = L"Терминальные символы";
- //
- // label3
- //
- this->label3->AutoSize = true;
- this->label3->Location = System::Drawing::Point(25, 306);
- this->label3->Name = L"label3";
- this->label3->Size = System::Drawing::Size(62, 13);
- this->label3->TabIndex = 14;
- this->label3->Text = L"Продукции";
- //
- // label4
- //
- this->label4->AutoSize = true;
- this->label4->Location = System::Drawing::Point(25, 416);
- this->label4->Name = L"label4";
- this->label4->Size = System::Drawing::Size(103, 13);
- this->label4->TabIndex = 15;
- this->label4->Text = L"Стартовый символ";
- //
- // label5
- //
- this->label5->AutoSize = true;
- this->label5->Location = System::Drawing::Point(176, 61);
- this->label5->Name = L"label5";
- this->label5->Size = System::Drawing::Size(95, 13);
- this->label5->TabIndex = 16;
- this->label5->Text = L"Входные данные:";
- //
- // label6
- //
- this->label6->AutoSize = true;
- this->label6->Location = System::Drawing::Point(501, 61);
- this->label6->Name = L"label6";
- this->label6->Size = System::Drawing::Size(62, 13);
- this->label6->TabIndex = 17;
- this->label6->Text = L"Результат:";
- //
- // Form1
- //
- this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
- this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
- this->ClientSize = System::Drawing::Size(853, 499);
- this->Controls->Add(this->label6);
- this->Controls->Add(this->label5);
- this->Controls->Add(this->label4);
- this->Controls->Add(this->label3);
- this->Controls->Add(this->label2);
- this->Controls->Add(this->label1);
- this->Controls->Add(this->start_output);
- this->Controls->Add(this->rules_output);
- this->Controls->Add(this->ter_output);
- this->Controls->Add(this->start_input);
- this->Controls->Add(this->rules_input);
- this->Controls->Add(this->ter_input);
- this->Controls->Add(this->button4);
- this->Controls->Add(this->button3);
- this->Controls->Add(this->button2);
- this->Controls->Add(this->button1);
- this->Controls->Add(this->non_output);
- this->Controls->Add(this->non_input);
- this->Name = L"Form1";
- this->Text = L"FZ1";
- this->ResumeLayout(false);
- this->PerformLayout();
- }
- #pragma endregion
- #define ft first
- #define sc second
- #define END "'\"'\""
- private: bool isSpace(char c) {
- return c == ' ' || c == '\t' || c == 13 || c == 10;
- }
- private: bool isQuota(char c) {
- return c == '\'' || c == '"';
- }
- private: string removeSpaces(const string& s) {
- string res = "";
- for(int i = 0; i < int(s.size()); i++) {
- if (!isSpace(s[i]))
- res += s[i];
- }
- return res;
- }
- private: string removeTrailingSpaces(const string& s) {
- int l = 0;
- int r = s.size() - 1;
- for(; l < int(s.size()) && isSpace(s[l]); l++);
- for(; r >= 0 && isSpace(s[r]); r--);
- string res = "";
- for(int i = l; i <= r; i++)
- res += s[i];
- return res;
- }
- private: string removeQuotas(string s) {
- s = removeTrailingSpaces(s);
- if (int(s.size()) > 0 && isQuota(s[0]))
- s = s.substr(1, int(s.size()) - 2);
- return s;
- }
- private: int parseToInt(string s) {
- s = removeSpaces(s);
- int res = 0;
- for(int i = 0; i < int(s.size()); i++) {
- res = res * 10 + s[i] - '0';
- }
- return res;
- }
- private: string toString(int n) {
- string res = "";
- while(n) {
- res += char(n % 10 + '0');
- n /= 10;
- }
- if (int(res.size()) == 0)
- res = "0";
- reverse(res.begin(), res.end());
- return res;
- }
- private: string toString(const vector<string>& v) {
- string res = "";
- for(int i = 0; i < int(v.size()); i++) {
- if (int(v[i].size()) > 1)
- res += "'" + v[i] + "'";
- else
- res += v[i];
- }
- if (res.size()==0){
- return "''";
- }
- return res;
- }
- private: string norm(string s) {
- if (int(s.size()) != 1)
- s = "'" + s + "'";
- return s;
- }
- private: string readToken() {
- string line;
- if (!getline(*input, line))
- return END;
- return removeTrailingSpaces(line);
- }
- private: set<string> readSet() {
- set<string> res;
- string line;
- while ((line = readToken()) != END) {
- line = removeQuotas(line);
- res.insert(line);
- }
- return res;
- }
- private: vector<string> parseSeq(string s) {
- s = removeTrailingSpaces(s);
- char separator = 0;
- vector<string> res;
- string cur = "";
- for(int i = 0; i < int(s.size()); i++) {
- if (isQuota(s[i])) {
- if (separator == 0) {
- separator = s[i];
- } else {
- assert(separator == s[i]);
- separator = 0;
- if (cur.size()) {
- res.push_back(cur);
- }
- cur = "";
- }
- } else {
- if (separator)
- cur += s[i];
- else
- res.push_back(string(1, s[i]));
- }
- }
- if (cur.size() != 0)
- res.push_back(cur);
- return res;
- }
- private: map< string, vector< vector<string> > > readRules() {
- map< string, vector< vector<string> > > res;
- string line;
- while ((line = readToken()) != END) {
- assert(line.find(' ') != string::npos);
- int spacePos = int(line.find(' '));
- if (isQuota(line[0])) {
- spacePos = line.find(line[0], 1) + 1;
- }
- string left = line.substr(0, spacePos);
- string right = line.substr(spacePos + 1, line.size());
- left = removeQuotas(left);
- res[left].push_back(parseSeq(right));
- }
- return res;
- }
- private: bool isTerminal(const string& s) {
- return bool(terminals.count(s));
- }
- private: void removeEpsRules() {
- bool need = false;
- for(map< string, vector< vector< string> > >::iterator it = rules.begin(); it != rules.end(); it++) {
- vector< vector<string> >& seqs = it->sc;
- for(int i = 0; i < int(seqs.size()); i++) {
- for(int y = 0; y < int(seqs[i].size()); y++) {
- if(seqs[i][y] == startSymbol)
- need = true;
- }
- }
- }
- if(need){
- string oldStartSymbol = startSymbol;
- string newStartSymbol = nextSymbol();
- for(int i = 0; i < int(rules[oldStartSymbol].size()); i++){
- rules[newStartSymbol].push_back(rules[oldStartSymbol][i]);
- }
- startSybmol = newStartSymbol;
- non_terminals.insert(newStartSymbol);
- }
- for(set<string>::iterator it = non_terminals.begin(); it !=non_terminals.end(); it++){
- string sym =*it;
- bool need = false;
- for(int i = 0; i<int(rules[sym].size()); i++){
- if(rules[sym][i].size == 0)
- need = true;
- }
- if(!need)
- continue;
- bool was = false;
- for(map<string, vector<vector<srting>>>:: iterator it1 = rules.begin(); it1 = rules.end(); it1++){
- vector<vector<string>> & seqs = it1->sc;
- vector<vector<string>> add;
- for(int i = 0; i<int(seqs.size()); i++){
- need = false;
- for(int y = 0; y<int(seqs[i].size()); y++){
- if(seqs[i][y] == sym)
- need = true;
- }
- if(!need)
- continue;
- vector<string> cur;
- for(int y = 0; y < int(seqs[i].size()); y++)
- if(seqs[i][y] != sym)
- cur.push_back(seqs[i][y]);
- add.push_back(cur);
- }
- if(add.size())
- was = true;
- for(int i = 0; i < int(add.size()); i++){
- seqs.push_back(add[i]);
- }
- }
- if(!was)
- continue;
- for(;;){
- vector< vector< string > >::iterator pos = rules[sym].find(vector<string>());
- if (pos ==rules[sym].end())
- break;
- rules[sym].erase(pos);
- }
- }
- }
- private: void removeSeqRules() {
- for(set<string>::iterator it1 = non_terminals.begin(); it1 != non_terminals.end(); it1++) {
- string from = *it1;
- string to = "";
- for(map< string, vector< vector< string> > >::iterator it2 = rules.begin(); it2 != rules.end(); it2++) {
- vector< vector<string> >& seqs = it2->sc;
- for(int i = 0; i < int(seqs.size()); i++) {
- vector<string>& seq = seqs[i];
- if (int(seq.size()) != 1)
- continue;
- if (seq[0] == from && seq[0] != it2->ft) {
- to = it2->ft;
- }
- }
- }
- if (int(to.size()) == 0)
- continue;
- if (startSymbol == from)
- startSymbol = to;
- for(map< string, vector< vector< string> > >::iterator it2 = rules.begin(); it2 != rules.end(); it2++) {
- vector< vector<string> >& seqs = it2->sc;
- for(int i = 0; i < int(seqs.size()); i++) {
- vector<string>& seq = seqs[i];
- for(int j = 0; j < int(seq.size()); j++) {
- if (seq[j] == from)
- seq[j] = to;
- }
- }
- }
- vector< vector<string> >& seqs = rules[from];
- for(int i = 0; i < int(seqs.size()); i++) {
- rules[to].push_back(seqs[i]);
- }
- rules.erase(from);
- }
- set<string> new_nt;
- for(set<string>::iterator it = non_terminals.begin(); it != non_terminals.end(); it++) {
- if (rules.count(*it))
- new_nt.insert(*it);
- }
- non_terminals = new_nt;
- for(map< string, vector< vector< string> > >::iterator it = rules.begin(); it != rules.end(); it++) {
- vector< vector<string> >& seqs = it->sc;
- vector< vector<string> > newseqs;
- sort(seqs.begin(), seqs.end());
- seqs.erase(unique(seqs.begin(), seqs.end()), seqs.end());
- for(int i = 0; i < int(seqs.size()); i++) {
- if (int(seqs[i].size()) == 1 && seqs[i][0] == it->ft)
- continue;
- if (int(seqs[i].size()) == 1)
- assert(isTerminal(seqs[i][0]));
- newseqs.push_back(seqs[i]);
- }
- rules[it->ft] = newseqs;
- }
- }
- private: string nextSymbol() {
- do {
- int idx = int(curSymbol.size()) - 1;
- for(; idx >= 0 && curSymbol[idx] == 'Z'; idx--)
- curSymbol[idx] = 'A';
- if (idx == -1) {
- int sz = int(curSymbol.size());
- curSymbol = string(sz + 1, 'A');
- } else {
- curSymbol[idx]++;
- }
- } while(non_terminals.count(curSymbol) || terminals.count(curSymbol));
- return curSymbol;
- }
- private: bool isNormalSeq(const vector<string>& seq) {
- if (int(seq.size()) == 0)
- return true;
- if (int(seq.size()) == 1)
- return isTerminal(seq[0]);
- if (int(seq.size()) == 2 && isTerminal(seq[0]) && !isTerminal(seq[1]))
- return true;
- return false;
- }
- private: void replaceLongRules() {
- map<string, vector< vector<string> > > newRules;
- for(map<string, vector< vector<string> > >::iterator it = rules.begin(); it != rules.end(); it++) {
- vector< vector<string> >& seqs = it->sc;
- string mem = curSymbol;
- for(int i = 0; i < int(seqs.size()); i++) {
- vector<string> seq = seqs[i];
- if (isNormalSeq(seq)) {
- newRules[it->ft].push_back(seq);
- continue;
- }
- string cur = it->ft;
- string next = nextSymbol();
- for(int j = 0; j < int(seq.size()) - 1; j++) {
- vector<string> nseq;
- nseq.push_back(seq[0]);
- nseq.push_back(next);
- non_terminals.insert(next);
- newRules[cur].push_back(nseq);
- seq.erase(seq.begin(), seq.begin() + 1);
- cur = next;
- next = nextSymbol();
- if (isNormalSeq(seq)) {
- newRules[cur].push_back(seq);
- break;
- }
- }
- }
- curSymbol = mem;
- }
- rules = newRules;
- }
- private: void check() {
- for(map<string, vector< vector<string> > >::iterator it = rules.begin(); it != rules.end(); it++) {
- vector< vector<string> >& seqs = it->sc;
- assert(!isTerminal(it->ft));
- assert(non_terminals.count(it->ft));
- for(int i = 0; i < int(seqs.size()); i++) {
- assert(isNormalSeq(seqs[i]));
- }
- }
- for(set<string>::iterator it = non_terminals.begin(); it != non_terminals.end(); it++) {
- assert(rules.count(*it));
- }
- }
- private: void printNonTerminals() {
- for(set<string>::iterator it = non_terminals.begin(); it != non_terminals.end(); it++) {
- this->non_output->AppendText(convertToString(norm(*it)));
- }
- }
- private: void printTerminals() {
- for(set<string>::iterator it = terminals.begin(); it != terminals.end(); it++) {
- if (*it == "")
- continue;
- this->ter_output->AppendText(convertToString(norm(*it)));
- }
- }
- private: void printRules() {
- for(map<string, vector< vector<string> > >::iterator it = rules.begin(); it != rules.end(); it++) {
- vector< vector<string> >& seqs = it->sc;
- for(int i = 0; i < int(seqs.size()); i++) {
- this->rules_output->AppendText(convertToString(norm(it->ft) + " " + toString(seqs[i])));
- }
- }
- }
- private: void printGramm() {
- printNonTerminals();
- printTerminals();
- printRules();
- this->start_output->AppendText(convertToString(startSymbol));
- }
- private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {
- this->non_output->Clear();
- this->ter_output->Clear();
- this->rules_output->Clear();
- this->start_output->Clear();
- input = convertToStringStream(this->non_input->Text);
- non_terminals = readSet();
- input = convertToStringStream(this->ter_input->Text);
- terminals = readSet();
- //terminals.insert("");
- input = convertToStringStream(this->rules_input->Text);
- rules = readRules();
- input = convertToStringStream(this->start_input->Text);
- startSymbol = readToken();
- removeSeqRules();
- curSymbol = string(1, 'A' - 1);
- removeEpsRules();
- removeSeqRules();
- // curSymbol = string(1, 'A' - 1);
- replaceLongRules();
- check();
- printGramm();
- }
- private: stringstream* convertToStringStream(String^ s) {
- stringstream *input = new stringstream(ios::in | ios::out);
- int n = s->Length;
- array<Char>^ chars = s->ToCharArray();
- string str = "";
- for (int i = 0; i < n; i++) {
- str.push_back(chars[i]);
- }
- *input << str;
- return input;
- }
- private: String^ convertToString(string s) {
- s += char(13);
- s += char(10);
- return gcnew String(s.c_str());
- }
- private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e) {
- Close();
- }
- private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
- openFileDialog1->InitialDirectory = "d:\\";
- openFileDialog1->Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
- openFileDialog1->FilterIndex = 2;
- openFileDialog1->RestoreDirectory = true;
- if (openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK) {
- IO::Stream^ stream;
- if ((stream = openFileDialog1->OpenFile()) != nullptr) {
- readStream(stream);
- stream->Close();
- }
- }
- }
- private: void readStream(IO::Stream^ stream) {
- stringstream text;
- int c;
- while ((c = stream->ReadByte()) != -1) {
- text << char(c);
- }
- this->non_input->Clear();
- this->ter_input->Clear();
- this->rules_input->Clear();
- this->start_input->Clear();
- string line;
- assert(getline(text, line));
- int n = parseToInt(line);
- for(int i = 0; i < n; i++) {
- assert(getline(text, line));
- this->non_input->AppendText(convertToString(line));
- }
- assert(getline(text, line));
- n = parseToInt(line);
- for(int i = 0; i < n; i++) {
- assert(getline(text, line));
- this->ter_input->AppendText(convertToString(line));
- }
- assert(getline(text, line));
- n = parseToInt(line);
- for(int i = 0; i < n; i++) {
- assert(getline(text, line));
- this->rules_input->AppendText(convertToString(line));
- }
- assert(getline(text, line));
- this->start_input->AppendText(convertToString(line));
- }
- private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
- SaveFileDialog^ saveFileDialog1 = gcnew SaveFileDialog;
- saveFileDialog1->Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
- saveFileDialog1->FilterIndex = 2;
- saveFileDialog1->RestoreDirectory = true;
- if ( saveFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK ) {
- IO::Stream^ stream;
- if ( (stream = saveFileDialog1->OpenFile()) != nullptr ) {
- String^ s;
- input = convertToStringStream(this->non_output->Text);
- non_terminals = readSet();
- int n = int(non_terminals.size());
- s += convertToString(toString(n));
- for(set<string>::iterator it = non_terminals.begin(); it != non_terminals.end(); it++) {
- s += convertToString(norm(*it));
- }
- input = convertToStringStream(this->ter_output->Text);
- terminals = readSet();
- n = int(terminals.size());
- s += convertToString(toString(n));
- for(set<string>::iterator it = terminals.begin(); it != terminals.end(); it++) {
- s += convertToString(norm(*it));
- }
- input = convertToStringStream(this->rules_output->Text);
- rules = readRules();
- n = 0;
- for(map<string, vector< vector<string> > >::iterator it = rules.begin(); it != rules.end(); it++) {
- n += int(it->sc.size());
- }
- s += convertToString(toString(n));
- for(map<string, vector< vector<string> > >::iterator it = rules.begin(); it != rules.end(); it++) {
- vector< vector<string> >& seqs = it->sc;
- for(int i = 0; i < int(seqs.size()); i++) {
- s += convertToString(norm(it->ft) + " " + toString(seqs[i]));
- }
- }
- input = convertToStringStream(this->start_output->Text);
- startSymbol = readToken();
- s += convertToString(startSymbol);
- array<Char>^ a = s->ToCharArray();
- for(int i = 0; i < s->Length; i++) {
- stream->WriteByte(a[i]);
- }
- stream->Close();
- }
- }
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement