Hello all,
I've been working on an algebraic symbol manipulator that handles numerics and binary operators like +-*/ and ^. It uses the the well-worn algorithm:
while more infix terms do
begin
infix = next term
if infix is operand then
send infix to output
else begin // infix is operator
while operator stack not empty do
begin
top = top operator on operator stack
if top.precedence > infix.precedence then
pop top to output
else if (top.precedence = infix.precedence) and top.associative then
pop top to output
else
break while loop
end
push operator to stack
end
end
while operator stack is not empty do
pop operator to output
I hope I've sufficiently described that algorithm. It seems to work well enough, but I cannot find any direct discussion of how prefixing unary operators should be handled. Is this algorithm sufficient? I'm unsure.
The most obvious unary prefix operator would be arithmetic negation, 2 * -3, for instance. In my case, that has been permuted to binary multiplication with -1 as the other operand. That solves it, I guess, so as another example, the logical not operator, seen in object pascal. It has a very high operator precedence, and there's no binary ^ operator, so I cannot see how they interact to test the algorithm.
This is getting long-winded now, so I'll close with the request for any adjacent commentary on this as I've overworked it in my mind and need a clean take.