2

I am having trouble understanding what I need to check for in the condition evaluation of the following while loop, which is an excerpt of an algorithm's pseudocode:

 if the token is an operator {
        while(the stack is not empty 
            AND the top of the stack is not a "(" 
            AND the precedence of the token on the top of the stack >= current token) {
                    pop the token on the stack and enqueue it
            } // end while
        push current token onto the stack
        } // end if token is an operator

My approach is this, knowing I am checking for an operator (i.e + - ^ etc..):

while((stack.isEmpty()) == false && (((Comparable<String>)stack.peek()).compareTo( "(") != 0) && ) //Missing a logical && (CHECK ALGORITHM)
    queue.enqueue(stack.pop());
    stack.push(tokenIterator);
}

Some clarifications:

  • I wrote my own Stack and Queue classes and including their functionalities and methods. It was part of the assignment. I am not able to use the default Java Stack and Queue structures (as I said, it was part of the assignment to create our own, and replicate their functionalities). I AM 200% such classes work. I thoroughly tested and debugged them, so don't worry about such thing.

  • Both the Stacks and Queues are of type <String>

  • The stack is an instance of my Stack class, that calls isEmpty() and returns a boolean (true if empty, false otherwise).

  • The stack.peek() call returns an Object, which in this instance is the String at the top of the stack.

  • The application of the entire algorithm is to process the input of a mathematical expression in infix form and convert it into postfix form.

  • Main problem: I am missing that last bit of pseudocode. To be specific, the line: "AND the precedence of the token on the top of the stack >= current token". I don't understand what it is asking me to check for, therefore I haven't coded it (as you can see, my java code above is missing the code after that last && ). My best guess is that it is asking me to check whether the token that is underneath the top of the stack (aka the element before the top) is >= than the current token, which has to be an operator (otherwise, we wouldn't have entered the upper if statement). I don't know how I can compare say an operator against a number and get something reasonable that does not crash.

Example: I wrote a helper function that prints the current queue contents in a nice form. If I enter the expression (6 + 9) - (9 + 6 + 3) + 2, the queue prints:

 ADD->|| 6| 9 | + | 9 | 6 | + | 3 | + | 2 | + | - ||<-RMV

Which of course is the wrong postfix notation of the above infix expression. According to me, it should be: 6 9 + 9 6 + 3 + - 2 +

You don't really need to know much more, since my issue is merely what does "AND the precedence of the token on the top of the stack >= current token" mean. I cannot understand what it is asking me to do. Hopefully solving this third && (and) condition in my while loop will make my algorithm output the correct postfix notation.


EDIT 1:

Now that I know what precedence is, I think that the ( in my queue may play an important role in checking for precedence, therefore, here is the raw output of the queue for the same math expression given as an example above (I deleted the ( from the queue in the other queue print above because I thought they were useless):

#######QUEUE#######
======================================================================
ADD->|| 6| 9 | + | 9 | 6 | + | 3 | + | 2 | + | ( | - | ( ||<-RMV
======================================================================
idelara
  • 1,786
  • 4
  • 24
  • 48
  • I believe it's about operator priority: `*` > `+` etc. If you use parenthesis to prioritize operations, you don't need this condition at all. – Alex Salauyou Apr 15 '15 at 12:28
  • I think I need it. I updated my queue output in my Edit 1 (please refer to it above). Do you think I can come up with a solution using the `( ` in the queue? – idelara Apr 15 '15 at 12:50
  • As @Paul commented, you try to compare operators using *alphabetical* comparison (as `String#compareTo` does). You need to implement comparison *by meaning*. – Alex Salauyou Apr 15 '15 at 13:06

2 Answers2

2

The precedence of an token is its priority when calculating a function. For example: precedence of the most common mathematical operators in descending order: () ^ */% +-.

The condition you mention prevents the algorithm from including any operators that have higher priority than the current operator in the queue.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Is there a built-in Java to check for precedence? If not, I guess I can code a helper function that checks for it. I think I was given the precedence order as follows: ^ * / + - which matches what you posted in your answer. – idelara Apr 15 '15 at 12:42
  • Another question, if I compare say, (Comparable)token).compareTo( "*"), where token = *, will this return 0? – idelara Apr 15 '15 at 12:43
  • Please refer to my edit 1 above. Do you think it will help me out somehow to get to the correct answers now that I've included the outputted `(` in the queue? Thanks in advance – idelara Apr 15 '15 at 12:48
  • 1
    Built in precedence check: shouldn't be too hard to implement that one, just map the operators (as strings) to some constants representing their precendence. Comparison: it will return 0 (spaces might change the result though!!!) - the cast is unneccassary, since `String` implements `Comparable`. `(`: handle these as operator with the highest priority, or the result will be incorrect. –  Apr 15 '15 at 13:01
  • I created a function that maps the given operator to a value ranging from 6 to 0, 6 being "(", 5 being "*" and so on, and the default is 0, that is being a number and it worked! That way I can compare their value returned with the other value (>=) Thanks a lot for your help!!!!! – idelara Apr 15 '15 at 13:29
0

Since the current token has to be an operator I am thinking that what "the precedence" here refers to is operator precedence, so meaning that multiplication and division take precedence over addition and subtraction. Would this make sense or are you just adding and subtracting?

kar288
  • 131
  • 3
  • That makes sense. I was given the precedence that Paul posted in his answer, so it includes ^ * / + -. Please refer to my Edit 1 – idelara Apr 15 '15 at 12:51