Пример #1
0
bool TreeBuilder::isOperand(string s)
{
  if(isBinaryOperator(s) || isUnaryOperator(s))
    return false;
 
  return true;
}
Пример #2
0
 flScalar InfixToPostfix::evaluate(const std::string postfix,
         const std::map<std::string, flScalar>* variables) const {
     std::stack<flScalar> stack;
     std::stringstream ss(postfix);
     std::string token;
     while (ss >> token) {
         if (isOperand(token)) {
             if (isNumeric(token)) {
                 stack.push(atof(token.c_str()));
             } else {
                 if (!variables) {
                     throw FuzzyException(FL_AT, "Impossible to compute operand <" + token + "> because no map was supplied");
                 }
                 std::map<std::string, flScalar>::const_iterator it = variables->find(token);
                 if (it == variables->end()) {
                     throw FuzzyException(FL_AT, "Operand <" + token + "> not found in map");
                 }
                 stack.push(it->second);
             }
         } else if (isOperator(token)) {
             if (isUnaryOperator(token)) {
                 if (stack.empty()) {
                     throw FuzzyException(FL_AT, "Error evaluating postfix expression <" + postfix + ">");
                 }
                 flScalar a = stack.top();
                 stack.pop();
                 stack.push(compute(token, a, 0));
             } else {
                 if (stack.size() < 2) {
                     throw FuzzyException(FL_AT, "Error evaluating postfix expression <" + postfix + ">");
                 }
                 flScalar b = stack.top();
                 stack.pop();
                 flScalar a = stack.top();
                 stack.pop();
                 stack.push(compute(token, a, b));
             }
         } 
     }
     if (stack.size() == 1){
         return stack.top();
     }
     throw FuzzyException(FL_AT, "Error evaluating postfix expression <" + postfix + ">");
 }
Пример #3
0
void ExpressionParser::tokenize(const String & expression)
{
    auto stateMemory = 0;
    size_t len = expression.length();
    for(size_t i = 0; i < len; i++)
    {
        char ch = expression[i];
        switch(ch)
        {
        case '[':
        {
            stateMemory++;
            _curToken += ch;
        }
        break;

        case ']':
        {
            if(stateMemory)
                stateMemory--;
            _curToken += ch;
        }
        break;

        default:
        {
            if(stateMemory)
                _curToken += ch;
            else
            {
                switch(ch)
                {
                case '(':
                    addOperatorToken(ch, Token::Type::OpenBracket);
                    break;
                case ')':
                    addOperatorToken(ch, Token::Type::CloseBracket);
                    break;
                case '~':
                    addOperatorToken(ch, Token::Type::OperatorNot);
                    break;
                case '*':
                    addOperatorToken(ch, Token::Type::OperatorMul);
                    break;
                case '`':
                    addOperatorToken(ch, Token::Type::OperatorHiMul);
                    break;
                case '/':
                    addOperatorToken(ch, Token::Type::OperatorDiv);
                    break;
                case '%':
                    addOperatorToken(ch, Token::Type::OperatorMod);
                    break;
                case '+':
                    if(!isUnaryOperator())    //skip all unary add operators
                        addOperatorToken(ch, Token::Type::OperatorAdd);
                    break;
                case '-':
                    if(isUnaryOperator())
                        addOperatorToken(ch, Token::Type::OperatorUnarySub);
                    else
                        addOperatorToken(ch, Token::Type::OperatorSub);
                    break;
                case '<':
                    if(i + 1 < len && expression[i + 1] == ch)
                    {
                        addOperatorToken(ch, Token::Type::OperatorShl);
                        i++;
                    }
                    else
                        addOperatorToken(ch, Token::Type::Error);
                    break;
                case '>':
                    if(i + 1 < len && expression[i + 1] == ch)
                    {
                        addOperatorToken(ch, Token::Type::OperatorShr);
                        i++;
                    }
                    else
                        addOperatorToken(ch, Token::Type::Error);
                    break;
                case '&':
                    addOperatorToken(ch, Token::Type::OperatorAnd);
                    break;
                case '^':
                    addOperatorToken(ch, Token::Type::OperatorXor);
                    break;
                case '|':
                    addOperatorToken(ch, Token::Type::OperatorOr);
                    break;
                case ' ': //ignore spaces
                    break;
                default:
                    _curToken += ch;
                    break;
                }
            }
        }
        break;
        }
    }
    if(_curToken.length() != 0)  //make sure the last token is added
        _tokens.push_back(Token(_curToken, Token::Type::Data));
}
Пример #4
0
bool Token::isOperator() const{

    return isBinaryOperator() || isUnaryOperator();

}
Пример #5
0
void TreeBuilder::build()
{
  tokenizer = new ExpressionTokenizer(expression);

  tokens = tokenizer->tokenize(); 

  cout  <<"\nExpression In Tree Builder SIZE::: " << expression.size() << endl;
  cout  <<"\nTokenizing ended SIZE::: " << tokens.size() << endl;

  postfixer.makePostfix(tokens); // create the postfix vec
  vector<string> postfix = postfixer.getPostfix(); // holder of the
						   // post fix array

  stack<CompoundNode*> nodeStack; //stack of pointers to hold nodes
				  //temp

  CompoundNode *root; //root holder

  //make tree
  for(int i = 0; i < postfix.size(); i++)
    {
      if(isBinaryOperator(postfix[i]))
	{
	  if(i == postfix.size() - 1) //if root
	    {
	      CompoundNode *node = new CompoundNode(postfix[i], true);

	      nodeStack.top()->setParent(node);
	      node->setRight(nodeStack.top());
	      nodeStack.pop();//pop from the stack

	      nodeStack.top()->setParent(node);
	      node->setLeft(nodeStack.top());
	      nodeStack.pop();//pop from the stack

	      nodeStack.push(node);
	      root = node;
	      cout<< "Root set: " <<node->getType() << endl;
	      cout<< "Root Right: " <<node->getRight()->getType() << endl;
	      cout<< "Root Left: " <<node->getLeft()->getType() << endl;
	    }
	  else
	    {
	      CompoundNode *node = new CompoundNode(postfix[i]);
	   
	      nodeStack.top()->setParent(node);//set parent
	      node->setRight(nodeStack.top());//setRight
	      nodeStack.pop();//pop from the stack

	      nodeStack.top()->setParent(node);//set parent
	      node->setLeft(nodeStack.top());//set left
	      nodeStack.pop();//pop from the stack

	      nodeStack.push(node);
	      cout<< "Binary set: " <<node->getType() << endl;
	      cout<< "Binary Right: " <<node->getRight()->getType() << endl;
	      cout<< "Binary Left: " <<node->getLeft()->getType() << endl;

	      nodes.push_back(node); // save node in the vec of pointers

	    }
	}
      else if(isUnaryOperator(postfix[i]))
	{
	  if(i == postfix.size() - 1) //if root
	    {
	      CompoundNode *node = new CompoundNode(postfix[i], true);

	      nodeStack.top()->setParent(node);
	      node->setRight(nodeStack.top());
	      nodeStack.pop();//pop from the stack
	      
	      node->setIsNotNode(true);
	      nodeStack.push(node);
	      
	      root = node; // make it the root

	      cout<< "Unary set: " <<node->getType() << endl;
	      cout<< "Unary Right: " <<node->getRight()->getType() << endl;
	      cout<< "Unary Left: " <<node->getLeft()->getType() << endl;

	    }
	  else
	    {
	      CompoundNode *node = new CompoundNode(postfix[i]);

	      nodeStack.top()->setParent(node);
	      node->setRight(nodeStack.top());
	      nodeStack.pop();//pop from the stack
	      node->setIsNotNode(true); // set its a not node
	      nodeStack.push(node);
	      
	      cout<< "Unaryy set: " <<node->getType() << endl;
	      cout<< "Unaryy Right: " <<node->getRight()->getType() << endl;
	      cout<< "Unary Left: " <<node->getLeft()->getType() << endl;

	      nodes.push_back(node); // save node in the vec of pointers
	    }
	}
      else if(isOperand(postfix[i]))
	{
	  DiagramLoader loader(postfix[i]);
	  loader.load();
	  Diagram dNode = loader.getDiagram();
	  
	  CompoundNode *node = new CompoundNode(dNode, "LEAF");
	  node->setIsLeaf(true);

	  nodeStack.push(node);
	  cout<< "LEAF: " <<node->getType() << endl;

	  nodes.push_back(node); // save node in the vec of pointers
	}
	
    }//end of for

  nodes.push_back(root); // save root at the end of vec of pointers
  tree.setRoot(root); // set tree's root
  tree.setNodes(nodes);
}
void ExpressionParser::tokenize(const std::string & expression)
{
    size_t len = expression.length();
    for (size_t i = 0; i < len; i++)
    {
        char ch = expression[i];
        switch (ch)
        {
        case '(':
            addOperatorToken(ch, Token::Type::OpenBracket);
            break;
        case ')':
            addOperatorToken(ch, Token::Type::CloseBracket);
            break;
        case '~':
            addOperatorToken(ch, Token::Type::OperatorNot);
            break;
        case '*':
            addOperatorToken(ch, Token::Type::OperatorMul);
            break;
        case '`':
            addOperatorToken(ch, Token::Type::OperatorHiMul);
            break;
        case '/':
            addOperatorToken(ch, Token::Type::OperatorDiv);
            break;
        case '%':
            addOperatorToken(ch, Token::Type::OperatorMod);
            break;
        case '+':
            if (!isUnaryOperator()) //skip all unary plus operators
                addOperatorToken(ch, Token::Type::OperatorAdd);
            break;
        case '-':
            if (isUnaryOperator())
                addOperatorToken(ch, Token::Type::OperatorUnarySub);
            else
                addOperatorToken(ch, Token::Type::OperatorSub);
            break;
        case '<':
            addOperatorToken(ch, Token::Type::OperatorShl);
            break;
        case '>':
            addOperatorToken(ch, Token::Type::OperatorShr);
            break;
        case '&':
            addOperatorToken(ch, Token::Type::OperatorAnd);
            break;
        case '^':
            addOperatorToken(ch, Token::Type::OperatorXor);
            break;
        case '|':
            addOperatorToken(ch, Token::Type::OperatorOr);
            break;
        case ' ': //ignore spaces
            break;
        default:
            _curToken += ch;
            break;
        }
    }
    if (_curToken.length() != 0) //make sure the last token is added
        _tokens.push_back(Token(_curToken, Token::Type::Data));
}