How can I modify my Shunting-Yard Algorithm so it accepts unary operators?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The standard shunting-yard algorithm works well for binary operators such as +, -, *, and /, but it needs one extra rule to handle unary operators correctly. The key is to classify a token like - based on context, then give the unary form its own operator entry with its own precedence and associativity.
Why Unary Operators Break A Naive Parser
In infix expressions, the same symbol can mean different things. The character - is subtraction in 3 - 2, but negation in -2 or 5 * -x. A naive shunting-yard implementation usually treats every - as binary, which causes incorrect postfix output or stack underflow during evaluation.
The parser therefore needs to answer one question while scanning tokens:
- does this operator expect one operand or two
That decision can be made locally from the previous token type.
Detect Unary Operators From Context
A - should usually be treated as unary if it appears:
- at the beginning of the expression
- after another operator
- after a left parenthesis
It is binary if it appears after a number, identifier, or right parenthesis.
Here is a simple tokenizer and parser-oriented classifier in Python:
Using a separate internal symbol such as NEG is the cleanest approach. It avoids overloading - with two meanings deeper in the algorithm.
Give Unary Operators Their Own Precedence
Once unary minus becomes NEG, define it like any other operator. A common choice is:
- higher precedence than multiplication and division
- right associativity
- arity of one
Right associativity matters for chains such as --x. Without it, the stack pop rules can produce the wrong order.
Adapt The Shunting-Yard Loop
The normal operator-handling logic still applies. The difference is that NEG is now just another operator with its own precedence.
This prints postfix forms where unary minus is explicit, such as ['3', 'NEG', '4', '+'].
Evaluating The Result
Your postfix evaluator must also understand operator arity.
The evaluator logic becomes simpler once unary and binary operators are no longer ambiguous.
Common Pitfalls
The most common mistake is trying to infer unary minus too late, after operators are already on the stack. Classification should happen when the token is first read.
Another mistake is keeping unary minus under the same - symbol internally. That creates confusion in both precedence rules and postfix evaluation.
A third issue is forgetting associativity. Unary operators are usually right-associative, and treating them as left-associative can break repeated unary expressions.
Summary
- Detect unary operators from token context, not from the symbol alone.
- Convert unary minus into a separate internal operator such as
NEG. - Give unary operators their own precedence, associativity, and arity.
- Keep the main shunting-yard loop almost unchanged once the operator table is correct.
- Update the postfix evaluator so unary operators consume one operand instead of two.

