Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- using namespace std;
- struct node // Везде, где лист, переделать на node
- {
- string s;
- string str; // переделать s1 на str
- int nomer; // переделать number na nomer
- node *prev;
- node *next;
- };
- void add(node**pend, string stroka); // переделать str na stroka
- const int OBR = 1, CBR = 2, ILI = 3, TILDA = 4, RAVNO = 5, AMP = 6, LETTER = 7, DIGIT = 8, DC = 9;
- int getLC();
- string fromlc(int lc);
- void nextStep(int lc);
- void ERROR (char ch, int lc); // изменил (переделать) error na ERROR
- void S();
- void E();
- void T(int sign);
- void M();
- void I();
- void C();
- bool A();
- bool D();
- char lctochar(int lc);
- void Sort();
- void deletenode(node *pend); // изменил (переделать) dellist na deletenode and list na node
- node *find(node *pbeg, int key);
- string inttostr(int n);
- ifstream fin("program.txt");
- ofstream fout("result.txt");
- ofstream fouts ("output.txt"); // изменил (переделать) f1out na fouts
- int lexical_class, num;
- char symbol; // изменил (переделать) c na symbol
- node *pend;
- node *pbeg;
- int main ()
- {
- fout << "Begin parsing!" << endl; // изменил (переделать) "Razbor nachalsya" na "Begin parsing!" and '\n' na endl
- bool flag = 1; // изменил (переделать) true na 1
- int i = 0;
- num = 0;
- while (!fin.eof() && flag)
- {
- i++;
- pbeg = new node;
- pbeg -> next = 0;
- pbeg -> prev = 0;
- pbeg -> nomer = 0;
- pend = pbeg;
- fouts << "Operator " << i << endl; // изменил (переделать) "Operator prisvaivaniya nomer " na "Operator " and '\n' na endl
- try
- {
- lexical_class = getLC();
- S();
- node *pv;
- pv = pbeg;
- Sort();
- pv = pbeg -> next;
- int k = 1;
- fout << "Stroka: " << i << '\n'; // изменил (переделать) "Stroka nomer " na "Stroka: "
- while (pv != 0)
- {
- fouts << pv -> nomer << ": "<< pv -> s << " "<< pv -> str <<'\n'; // изменил (переделать) melkie ispravleniya v strokah (" \\") and '\n'
- if (pv -> s[0] != 'С' && pv -> s[0] != 'V')
- {
- fout << "Operator " << k << ": " << pv -> str <<'\n';
- k++;
- }
- pv = pv -> next;
- }
- fouts << endl; // изменил (переделать) '\n' na endl
- deletenode (pbeg);
- }
- catch(string s)
- {
- flag = false;
- cout << "ERROR" << endl; // изменил (переделать) "error" na "ERROR" and '\n' na endl
- fout << "Stroka: " << i << endl;
- node *pv = pbeg -> next;
- int k = 1;
- while (pv != 0)
- {
- int a = pv -> s.length();
- if (pv -> s != "" && pv -> s[0] != 'С' && pv -> s[0] != 'V' && pv -> s[a-1] == ')')
- {
- fout << "Operator " << k << ": " << pv -> str << endl; // изменил (переделать) endl
- k++;
- }
- pv = pv -> next;
- }
- fout << s;
- fout << endl <<"End of syntax analysing"; // изменил (переделать) endl
- fouts.close();
- ofstream foutt ("output.txt"); // изменил (переделать) f2out na foutt
- foutt << "GRAMMAR ERROR" << endl; // изменил (переделать) dobavil posle GRAMMAR ERROR endl
- foutt.close();
- }
- catch(...)
- {
- cout<<"ERROR FOUND";
- }
- }
- if (flag)
- {
- fout << "Syntax analysing was successful"; // изменил (переделать) rus on eng phrase in "" and add bracets {}
- }
- fin.close();
- fout.close();
- fouts.close();
- system ("pause");
- return 0;
- }
- void add(node **pend, string stroka)
- {
- node *pv = new node;
- pv -> s = stroka;
- pv -> next = 0;
- pv -> prev = *pend;
- (*pend) -> next = pv;
- *pend = pv ;
- }
- void Sort() //параметр - указатель на начало списка.
- {
- node *p_node; // указатель для внешнего цикла for
- node *pp_node; // для внутреннего
- for (p_node = pbeg -> next; p_node != NULL; p_node = p_node -> next) //пробегаемся по всех узлах списка
- for (pp_node = pbeg -> next; pp_node -> next != NULL; pp_node = pp_node -> next) //а здесь только до узла, next которого указывает на NULL
- //обычный обмен значениями
- if (pp_node -> nomer > pp_node -> next -> nomer)
- {
- int temp; // изменил (переделать) tmp na temp
- string temp1,temp2; // изменил (переделать) tmp1 and tmp2 na temp1 and temp2
- temp = pp_node -> nomer;
- pp_node -> nomer = pp_node -> next -> nomer;
- pp_node -> next -> nomer = temp;
- temp1 = pp_node -> s;
- pp_node -> s = pp_node -> next -> s;
- pp_node -> next -> s = temp1;
- temp2 = pp_node -> str;
- pp_node -> str = pp_node -> next -> str;
- pp_node -> next -> str = temp2;
- }
- }
- node *find(node *pbeg, int key) //поиск элемента по ключу
- {
- node *pv = pbeg;
- while (pv)
- {
- if(pv -> nomer == key)
- {
- break;
- }
- pv = pv -> next;
- }
- return pv;
- }
- void deletenode(node *pbeg)
- {
- if (pbeg != NULL)
- {
- deletenode(pbeg -> next);
- pbeg -> next = 0;
- pbeg -> prev = 0;
- delete pbeg;
- }
- }
- void S() // Vrode izmenena
- {
- add(&pend, "");
- node *pv = pend;
- I();
- pv -> s = ":(^" + inttostr(pend -> nomer) + ',';
- pv -> str = pend -> str;
- nextStep (RAVNO);
- E();
- // nextStep (CBR);
- pv -> nomer = num++;
- node *pt = find(pbeg, pv -> nomer - 1);
- pv -> str = pv -> str + '(' + pt -> str + ')';
- string stroka = inttostr(pt -> nomer);
- pv -> s = pv -> s + '^' + stroka + ')';
- }
- void E()
- {
- T();
- lexical_class = getLC();
- if (lexical_class == ILI)
- {
- add(&pend, "");
- node *pv = pend;
- lexical_class = getLC();
- E();
- pv -> nomer = num++;
- node *pt = find(pbeg, pv -> nomer - 1);
- pv -> str = '|' + pt -> str;
- string stroka = inttostr(pt -> nomer);
- pv -> s = "|(^" + stroka +",<>)";
- }
- switch (lexical_class)
- {
- /*case MIN:
- {
- add(&pend, "");
- node *pv = pend;
- lexical_class = getLC();
- E();
- pv -> nomer = num++;
- node *pt = find(pbeg, pv -> nomer - 1);
- pv -> str = '-' + pt -> str;
- string stroka = inttostr(pt -> nomer);
- pv -> s = "-(^" + stroka +",<>)";
- break;
- }*/
- //case KAV: nextStep (KAV); T(); break;
- /*case LETTER:
- {
- I();
- if (lexical_class != CBR)
- {
- if (lexical_class == OBR)
- {
- add(&pend, "");
- node *pv = pend;
- pv -> str = pv -> prev -> str;
- pv -> s = "=(^" + inttostr(pv -> prev -> nomer) + ",^";
- nextStep(OBR);
- E();
- nextStep (CBR);
- pv -> nomer = num++;
- node *pt = find(pbeg, pv -> nomer - 1);
- pv -> s = pv -> s + inttostr(pt -> nomer) + ')';
- pv -> str = pv -> str + '(' + pt -> str + ')';
- }
- }
- break;
- }*/
- default: ERROR('E',0);
- }
- }
- void T()
- {
- add(&pend, "");
- node *pv = pend;
- M();
- if (lexical_class==AMP) // tyt nado peredelivat' pod svoi variant
- {
- lexical_class = getLC();
- }
- else
- {
- node *pt = pv;
- (pv -> prev) -> next = pv -> next;
- pv -> next -> prev = pv -> prev;
- delete pt;
- return;
- }
- pv -> nomer = num++;
- node *pt = find(pbeg, pv -> nomer - 1);
- pv -> str = pv -> str + '&' + pt -> str + ')';
- string stroka = inttostr(pt -> nomer);
- pv -> s = pv -> s + '^' + stroka + ')';
- }
- void M()
- {
- switch (lexical_class) // nado peredelivat' pod svoi variant (est' ideya vzyat' ochen' pohozhii s ih variantom, chtobi menshe peredelivat')
- {
- case TILDA:
- nextStep(TILDA);
- M();
- case OBR:
- nextStep(OBR);
- E();
- nextStep(CBR);
- case LETTER:
- I();
- case DIGIT:
- C();
- }
- bool D()
- {
- return(lexical_class=='0' || lexical_class=='1');
- }
- bool A()
- {
- return(lexical_class>='a' && lexical_class<='z' || lexical_class=='_');
- }
- void C() // chyt' peredelat'
- {
- add(&pend, "");
- pend -> s += '#'; // nado peredelivat' pod svou variant
- if (lexical_class != DIGIT)
- {
- ERROR('C',0);
- }
- while (lexical_class != CBR && lexical_class != COMMA) // nado peredelivat'
- {
- if (lexical_class != DIGIT)
- {
- ERROR('C',0);
- break;
- }
- pend -> s += symbol;
- lexical_class = getLC();
- }
- pend -> str = pend -> s;
- pend -> s= "С(" + pend -> s + ",<>)";
- pend -> nomer = num++;
- }
- void I()
- {
- add(&pend, "");
- pend -> s += symbol;
- if (lexical_class == LETTER)
- lexical_class = getLC();
- else throw string("WANTED IDENTIFICATOR");
- while (lexical_class != OBR && lexical_class != CBR && lexical_class != COMMA)
- {
- if (lexical_class != DIGIT)
- {
- ERROR('I',0);
- break;
- }
- pend -> s += symbol;
- lexical_class = getLC();
- }
- pend -> str = pend -> s;
- pend -> s = "V(" + pend -> s + ",<>)";
- pend -> nomer = num++;
- }
- int getLC()
- {
- fin.get(symbol);
- if (!fin.eof() && symbol != '\n')
- {
- if (symbol >= 'a' && symbol <= 'z' || symbol == '_') return 7;
- else if (symbol == '0' || symbol == '1') return 8;
- switch (symbol)
- {
- case ('('):
- return 1;
- case (')'):
- return 2;
- case ('|'):
- return 3;
- case ('~'):
- return 4;
- case ('='):
- return 5;
- case ('&'):
- return 6;
- case (';'):
- return 9;
- default: ERROR('G', 0);
- }
- }
- return 0;
- }
- void nextStep(int lc)
- {
- if (lc == lexical_class) lexical_class = getLC();
- else ERROR('N', lc);
- }
- void ERROR (char ch, int lc)
- {
- string s = "";
- switch (ch)
- {
- case ('E'):
- s = "Waiting special terminal \n Error in rule E"; throw string(s); break;
- case ('C'):
- s = "Waiting DIGIT "+pend->s+"\n Error in rule E"; throw string(s); break;
- case ('I'):
- s = "Unreal identificator: Waiting digit after " + pend -> s + "\n" + "Error in rule I" ; throw string(s); break;
- case ('N'):
- s = "Error: instead symbol \'"; s += symbol; s += fromlc(lc); throw string(s); break;
- case ('G'):
- s = "Symbol "; s += symbol; s += " in \""; s += pend -> s; s += symbol; s += "\" not satisfy Grammar";
- throw string(s);
- break;
- }
- }
- string inttostr(int n)
- {
- int i;
- string stroka = "";
- while (n != 0)
- {
- i = n % 10;
- char r = i + '0';
- stroka = r + stroka;
- n = n / 10;
- }
- return stroka;
- }
- string fromlc(int lc)
- {
- switch(lc)
- {
- case 1: return "\' ОЖИДАЛСЯ СИМВОЛ \'(\'";
- case 2: return "\' ОЖИДАЛСЯ СИМВОЛ \')\'";
- case 3: return "\' ОЖИДАЛСЯ СИМВОЛ \'-\'";
- case 4: return "\' ОЖИДАЛСЯ СИМВОЛ \'+\'";
- case 5: return "\' ОЖИДАЛСЯ СИМВОЛ \',\'";
- case 6: return "\' ОЖИДАЛСЯ СИМВОЛ \'#\'";
- case 7: return "\' ОЖИДАЛСЯ СИМВОЛ \'*\'";
- case 8: return "\' ОЖИДАЛСЯ БУКВА";
- case 9: return "\' ОЖИДАЛСЯ ЦИФРА";
- default: return "";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement