I'm writing a postfix processor to evaluate sql query. I met a problem that continue statement cannot jump to the next while iteration. Here is the piece of code:
while (index <= end_index && tokens[index] != "ORDER") {
if (isOperator(tokens[index])) {
if (isLeftParenthesis(tokens[index])) {
op_stack.push(tokens[index]);
index++;
continue; // first continue
}
if (isRightParenthesis(tokens[index])) {
while (!op_stack.empty() && op_stack.top() != ")") {
string top = op_stack.top();
postfix_root->addChild(top);
op_stack.pop();
}
op_stack.pop();
index++;
continue; // second continue
}
while (!op_stack.empty() && hasLowerPrecedence(tokens[index], op_stack.top())) {
string top = op_stack.top();
postfix_root->addChild(top);
op_stack.pop();
}
op_stack.push(tokens[index]);
index++;
continue; // third continue
}
postfix_root->addChild(tokens[index]);
index++;
}
My plan is if a token is "(", the "(" will be put into stack(op_stack) and if a token is ")", operator in stack will be popped out until "(". "(" and ")" are not supposed to be left in postfix expression, but my code keeps them. My observation is the first and second continue doesn't work. They are supposed to jump to next while iteration, but my test shows that the two statements before third continue are always executed, which means the first two continue doesn't work. I don't know if I miss any point. Thanks and I appreciate for any advise.
Aucun commentaire:
Enregistrer un commentaire