Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include "PolishNotation.h"
- namespace ExpressionChanging
- {
- int priority(char symbol)
- {
- if (symbol == '+' || symbol == '-')return 2;
- if (symbol == '*' || symbol == '/')return 3;
- return 4;
- }
- void copy_LT(LT::LexTable& lextable, LT::LexTable& lextable2, int i)
- {
- lextable.table[i].lexema = lextable2.table[i].lexema;
- lextable.table[i].sn = lextable2.table[i].sn;
- lextable.table[i].tokenId = lextable2.table[i].tokenId;
- lextable.table[i].idxTI = lextable2.table[i].idxTI;
- lextable.table[i].operation = lextable2.table[i].operation;
- lextable.table[i].value = lextable2.table[i].value;
- }
- void Polish_notation_inverse(int &start_position, LT::LexTable& lextable, IT::IdTable& idtable, LT::LexTable& lextable2)
- {
- std::stack <int> buffer;
- int last_postion;
- for (last_postion = start_position; lextable.table[last_postion].lexema != SEMICOLON; last_postion++);
- vector<int> EXPRESSION;
- for (int i = start_position; i < last_postion; i++)
- {
- if (lextable.table[i].lexema == '(')
- buffer.push(i);
- if (lextable.table[i].lexema == ')')
- {
- while (!buffer.empty() && lextable.table[buffer.top()].lexema != '(')
- {
- EXPRESSION.push_back(buffer.top());
- buffer.pop();
- }
- buffer.pop();
- }
- if (lextable.table[i].lexema == LEX_ID && lextable.table[i + 1].lexema == '(')
- {
- EXPRESSION.push_back(i);
- EXPRESSION.push_back(-1);
- i += 2;
- while (lextable.table[i].lexema != ')' && lextable.table[i - 1].lexema != ')')
- {
- EXPRESSION.push_back(i);
- i += 2;
- }
- EXPRESSION.push_back(-2);
- }
- if (lextable.table[i].lexema == 'v')
- {
- while (!buffer.empty() && priority(lextable.table[i].operation) <= priority(lextable.table[buffer.top()].operation) && lextable.table[buffer.top()].lexema != '(')
- {
- EXPRESSION.push_back(buffer.top());
- buffer.pop();
- }
- buffer.push(i);
- }
- if (lextable.table[i].lexema == LEX_ID || lextable.table[i].lexema == LEX_LITERAL)
- EXPRESSION.push_back(i);
- }
- while (!buffer.empty())
- {
- EXPRESSION.push_back(buffer.top());
- buffer.pop();
- }
- for (int i = 0; i <EXPRESSION.size(); i++)
- {
- if (EXPRESSION[i] == -1)
- {
- lextable2.table[i + start_position].lexema = '(';
- lextable2.table[i + start_position].sn = lextable2.table[i + start_position - 1].sn;
- continue;
- }
- if (EXPRESSION[i] == -2)
- {
- lextable2.table[i + start_position].lexema = ')';
- lextable2.table[i + start_position].sn = lextable2.table[i + start_position - 1].sn;
- continue;
- }
- lextable2.table[i + start_position].lexema = lextable.table[EXPRESSION[i]].lexema;
- lextable2.table[i + start_position].sn = lextable.table[EXPRESSION[i]].sn;
- lextable2.table[i + start_position].idxTI = lextable.table[EXPRESSION[i]].idxTI;
- lextable2.table[i + start_position].tokenId = lextable.table[EXPRESSION[i]].tokenId;
- lextable2.table[i + start_position].operation = lextable.table[EXPRESSION[i]].operation;
- lextable2.table[i + start_position].value = lextable.table[EXPRESSION[i]].value;
- }
- lextable2.table[EXPRESSION.size() + start_position].lexema = SEMICOLON;
- lextable2.table[EXPRESSION.size() + start_position].sn = lextable2.table[EXPRESSION.size() + start_position - 1].sn;
- for (int i = EXPRESSION.size() + start_position + 1; i < last_postion + 1; i++)
- {
- lextable2.table[i].lexema = NULL;
- lextable2.table[i].sn = lextable2.table[i - 1].sn;
- }
- start_position = last_postion;
- }
- void Polish_Notation_main(LT::LexTable& lextable, IT::IdTable& idtable)
- {
- LT::LexTable lextable2 = LT::Create(200);
- IT::IdTable ID_Table2 = IT::Create(200);
- for (int i = 1; i < lextable.size; i++)
- {
- if (lextable.table[i - 2].lexema == LEX_ID && lextable.table[i - 1].lexema == '=')
- {
- int st = i;
- Polish_notation_inverse(i, lextable, idtable, lextable2);
- for (; st < i + 1; st++)
- copy_LT(lextable, lextable2, st);
- }
- }
- for (int i = 1; i < lextable.size; i++)
- {
- if (lextable.table[i].lexema == 'v')
- lextable.table[i].lexema = lextable.table[i].operation;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement