Exemplo n.º 1
0
 //TODO: Be Careful: sin(x) / x => x x / sin. The expected is obtained: (sin x) / x => x sin x /
 std::string InfixToPostfix::transform(const std::string& infix) const {
     std::string p_infix = preprocess(infix);
     std::string postfix;
     std::stack<std::string> stack;
     std::stringstream ss(p_infix);
     std::string token;
     while (ss >> token) {
         if (token == "(") {
             stack.push(token);
         } else if (token == ")") {
             while (!stack.empty()) {
                 if (stack.top() == "(") {
                     stack.pop();
                     break;
                 }
                 postfix += stack.top() + " ";
                 stack.pop();
             }
         } else if (isOperand(token)) {
             postfix += token + " ";
         } else if (isOperator(token)) {
             while (!stack.empty() &&
                     operatorPrecedence(token) <= operatorPrecedence(stack.top())) {
                 postfix += stack.top() + " ";
                 stack.pop();
             }
             stack.push(token);
         }
     }
     while (!stack.empty()) {
         postfix += stack.top() + " ";
         stack.pop();
     }
     return postfix;
 }
Exemplo n.º 2
0
 bool InfixToPostfix::isOperator(const std::string& op) const {
     return operatorPrecedence(op) != -1;
 }
Exemplo n.º 3
0
void Expression::buildPostfix(Tokens *tokens, Parser *parser) {
	Stack<Token*> stack;

	int paranthCnt = 0;
	const Token *next = tokens->checkNext();
	while (tokens->isMore() && next->aType != Token::SEMICOLON && next->aType != Token::COMMA) {

			if (next->aType == Token::VARIABLE_INT || next->aType == Token::VARIABLE_FLOAT) {
				postfix.push_back(new ExpressionTerm(tokens->popNext()));
			} else if (next->aType == Token::VARIABLE_FUNCTION) {
				Token *token = tokens->popNext();

				// Function call
				if (tokens->checkNext()->aType == Token::PARANTH_BEG) {
					FunctionCall *func = new FunctionCall(token);
					func->parseFragment(tokens, parser);
					postfix.push_back(new ExpressionTerm(func));
				} else {
					postfix.push_back(new ExpressionTerm(token));
				}
			} else if (next->aType & Token::OPERATOR) {
				Token *token = tokens->popNext();

				while (stack.Size() && stack.peek()->aType != Token::PARANTH_BEG) {
					if (operatorPrecedence(stack.peek()) > operatorPrecedence(token)) {
						postfix.push_back(new ExpressionTerm(stack.pop()));
					} else {
						break;
					}
				}

				stack.push(token);
			} else if (next->aType == Token::PARANTH_BEG) {
				paranthCnt++;
				stack.push(tokens->popNext());
			} else if (next->aType == Token::PARANTH_END) {
				paranthCnt--;
				if (paranthCnt < 0) break;

				while (stack.Size() && stack.peek()->aType != Token::PARANTH_BEG) {
					postfix.push_back(new ExpressionTerm(stack.pop()));
				}

				// Pop the "("
				stack.pop();

				// Pop the ")"
				tokens->popNext();
			} else {
				throw InvalidTokenException("Unexpected token: " + next->token);
			}

			next = tokens->checkNext();
	}

	while (stack.Size()) {
		postfix.push_back(new ExpressionTerm(stack.pop()));
	}

	if (!isParam) {
		delete tokens->popExpected(Token::SEMICOLON);
	}
}