void ExprParser::close() { while (getOperator()!='(') pop(); popOperator(); }
ParseStatus Parser::popDelimiter(TokenValue t) { // Apply all pending operators until we hit the matching paren. while (op_stack.back() != t) { popOperator(); } // Remove the matching paren from the operator stack. op_stack.pop(); return PARSE_OK; }
void ExprParser::pop() { char op = getOperator(); switch (op) { case 'm': Generator::negate (mCode, getOperandType()); popOperator(); break; case '+': Generator::add (mCode, getOperandType (1), getOperandType()); popOperator(); replaceBinaryOperands(); break; case '-': Generator::sub (mCode, getOperandType (1), getOperandType()); popOperator(); replaceBinaryOperands(); break; case '*': Generator::mul (mCode, getOperandType (1), getOperandType()); popOperator(); replaceBinaryOperands(); break; case '/': Generator::div (mCode, getOperandType (1), getOperandType()); popOperator(); replaceBinaryOperands(); break; case 'e': case 'n': case 'l': case 'L': case 'g': case 'G': Generator::compare (mCode, op, getOperandType (1), getOperandType()); popOperator(); popOperand(); popOperand(); mOperands.push_back ('l'); break; default: throw std::logic_error ("unknown operator"); } }
ParseStatus Parser::pushOperator(Token& t) { assert(t.isOperator()); // check priority while(!op_stack.empty() && (getPriority(op_stack.back()) >= getPriority(t))) { popOperator(); } op_stack.push(t); return PARSE_OK; }
void ExprEvaluator::E() { P(); tokenizer.nextToken(); while(tokenizer.isBinary()) { pushOperator(binary(tokenizer.nextToken())); tokenizer.consumeToken(); P(); tokenizer.nextToken(); } while(operators.back() != oper::SANTINEL) popOperator(); }
void convertToPostfix(char *str){ /* o1: Holds the precedence of the new operator * o2: Holds the precendence of the topmost operator on the stack * ptoken: The pointer to the start of each string delimited by commas * stringToAdd: Houses a single character so strcat() can add singular characters * aCopy: A copy of the given string used to read from as we are modifying the original string */ int o1, o2; char *ptoken, stringToAdd[] = "0", aCopy[100]; /* Copy the given string to a second string to be read from */ strcpy(aCopy, str); /* Set the first element of the given string to NULL, effectively erasing the string from the point of view of strcat() */ *str = '\0'; /* Cycle through each group of characters delimited by a whitespace */ for(ptoken = strtok(aCopy," "); ptoken != NULL; ptoken = strtok(NULL, " ")){ /* If the character is a bracket or operator, then run this code */ if(isOperator(*ptoken) && strlen(ptoken) == 1){ /* Code to figure out precedences of operators */ if(*ptoken == '*' || *ptoken == '/') o1 = 3; else if(*ptoken == '^') o1 = 4; else if(*ptoken == '+' || *ptoken == '-' ) o1 = 2; if(seeOperator() == '*' || seeOperator() == '/') o2 = 3; else if(seeOperator() == '^') o2 = 4; else if(seeOperator() == '+' || seeOperator() == '-' ) o2 = 2; else o2 = 0; /* If the operator stack isn't empty, and the precedence of the new operator is less than or equal to the top operator * in the stack, then pop the top operator in to the post-fix string, and push the new operator in to the stack. */ if(emptyOperator() != 1 && o1 <= o2){ *stringToAdd = popOperator(); strcat(str," "); strcat(str,stringToAdd); pushOperator(*ptoken); } /* Otherwise, if it's empty or the new operator has greater precedence, push to the operator stack */ else{ pushOperator(*ptoken); } } /* If the character is a left bracket, push it into the operator stack */ else if(*ptoken == '('){ pushOperator(*ptoken); } /* If the character is a right bracket, pop operators off until you reach a left bracket, then pop off the left bracket without * adding it to the string */ else if(*ptoken == ')'){ while(seeOperator() != '('){ *stringToAdd = popOperator(); strcat(str," "); strcat(str, stringToAdd); } popOperator(); } /* Otherwise(i.e. the string is a operand), just catenate the operand to the end of the string */ else{ strcat(str, " "); strcat(str, ptoken); } } /* Check if the operator stack is empty, if it isn't, add the top most operator to the end of the string, repeat until * stack is empty. * If the character is a bracket, then just pop it and don't add to the string. */ while(emptyOperator() == FALSE){ if(seeOperator() == '(' || seeOperator() == ')') popOperator(); else{ *stringToAdd = popOperator(); strcat(str," "); strcat(str, stringToAdd); } } }
void ExprEvaluator::pushOperator(oper o) { while(isHigherPriority(operators.back(), o)) popOperator(); operators.push_back(o); }
int buildTree(command_t commList, int i, int rootListIndex, int* rootList) { int maxOperandSize = 100; int maxOperatorSize = 100; int operandIndex = 0; // first empty index in operand stack int operatorIndex = 0; // first empty index in operator stack //printf("rootListIndex: %d\n", rootListIndex); int* operatorList = malloc(sizeof(int)*maxOperatorSize); int* operandList = malloc(sizeof(int)*maxOperandSize); int returnIndex = -1; //int i; //printf("# of commands: %d\n", g_commandListLength); for(; i < g_commandListLength; i++) { // printf("ii: %d\n", i); //printf("p..operatorIndex: %d\n", operatorIndex); //printf("p..operandIndex: %d\n", operandIndex); if(operandIndex == maxOperandSize) { maxOperandSize *= 2; operandList = realloc(operandList, sizeof(int)*maxOperandSize); } else if(operatorIndex == maxOperatorSize) { maxOperatorSize *= 2; operatorList = realloc(operatorList, sizeof(int)*maxOperatorSize); } else if(commList[i].type == SIMPLE_COMMAND) { //printf("simple command\n"); //printf("operandIndex: %d\n", operandIndex); operandList[operandIndex] = i; operandIndex++; // printStack(operatorIndex-1, operandIndex-1, operatorList, operandList); continue; } else if(commList[i].type == OPEN_PAREN) { // push onto operator stack operatorList[operatorIndex] = i; operatorIndex++; continue; } else if(commList[i].type == CLOSED_PAREN) { // pop off until reach open paren int topIndex = operatorList[operatorIndex-1]; enum command_type top = commList[topIndex].type-1; while(top != OPEN_PAREN) { //printf("pop til open paren \n"); popOperator(operatorIndex-1, operandIndex-1, operatorList, operandList, commList); operandIndex--; operatorIndex--; //printStack(operatorIndex-1, operandIndex-1, operatorList, operandList); // print stack for debugging //printf("topIndex: %d\n", topIndex); topIndex = operatorList[operatorIndex-1]; top = commList[topIndex].type; } //end loop: top = OPEN_PAREN //printf("end paren popping\n"); int subShellIndex = operatorList[operatorIndex-1]; setSubShell(subShellIndex, operandIndex-1, operandList, commList); operatorIndex--; //printCommandList(commList, g_commandListLength); //printStack(operatorIndex-1, operandIndex-1, operatorList, operandList); continue; } else if(commList[i].type == NEW_LINE) { returnIndex = i; if(i+1 == g_commandListLength) { // printf("return\n"); returnIndex = -1; } break; } else if(commList[i].type == IGNORE_COMMAND) { continue; } /*else if(commList[i].type == SEQUENCE_COMMAND) { //printf("i: %d\n", i); returnIndex = i; if(i == g_commandListLength-2 && commList[i+1].type == NEW_LINE) { returnIndex = -1; } break; }*/ else { enum command_type input = commList[i].type; if(operatorIndex == 0) { operatorList[0] = i; operatorIndex++; //printStack(operatorIndex-1, operandIndex-1, operatorList, operandList); continue; } int topIndex = operatorIndex-1; enum command_type top = commList[operatorList[topIndex]].type; /* printf("topIndex: %d\n", topIndex); printf("top: "); printCommandType(top); printf("input: "); printCommandType(input);*/ while(checkPrecedence(input, top) && operatorIndex > 0) { // printf("PRECEDENCE\n"); // printf("stack1:\n"); //printStack(operatorIndex-1, operandIndex-1, operatorList, operandList); popOperator(operatorIndex-1, operandIndex-1, operatorList, operandList, commList); operandIndex--; operatorIndex--; // printf("stack2:\n"); //printStack(operatorIndex-1, operandIndex-1, operatorList, operandList); //printf("op index: %d\n", operatorIndex); // print stack for debugging topIndex = operatorList[operatorIndex-1]; top = commList[topIndex].type; /* printf("in loop topIndex: %d\n", topIndex); printf("in loop top: "); printCommandType(top); printf("in loop input: "); printCommandType(input);*/ } operatorList[operatorIndex] = i; operatorIndex++; } } while(operatorIndex > 0) { popOperator(operatorIndex-1, operandIndex-1, operatorList, operandList, commList); operandIndex--; operatorIndex--; // printf("stack3:\n"); // printStack(operatorIndex-1, operandIndex-1, operatorList, operandList); } free(operatorList); free(operandList); rootList[rootListIndex] = operandList[0]; return returnIndex; }
ParseStatus Parser::emitEval() { while(op_stack.size()) { popOperator(); } return PARSE_OK; }