shunting-yard algorithm
unary minus handling
expression parsing
algorithm implementation
computer science

handling unary minus for shunting-yard algorithm

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Handling unary minus in the context of the Shunting-Yard algorithm is a common problem faced when converting infix expressions to postfix notation (or Reverse Polish Notation). This article will delve into the techniques for correctly parsing and processing unary minus to ensure accurate expression evaluation.

Understanding Unary Minus

Unary minus is an operator that indicates negation of a single operand, unlike the binary minus which denotes subtraction between two operands. For example, in the expression `-5`, the minus sign is unary, indicating negation, whereas in `5 - 2`, the minus is binary, indicating subtraction.

Challenges in Handling Unary Minus

There are specific challenges associated with handling unary minus in the Shunting-Yard algorithm which include:

  1. Parsing Confusion: Distinguishing between unary and binary minus during parsing can be confusing since the symbols are identical.
  2. Operator Precedence: Unary minus typically has a higher precedence than binary minus, similar to the precedence of the multiplication operator.
  3. Associativity: Unary operators are right associative, whereas binary operators like subtraction are left associative.

Modifying the Shunting-Yard Algorithm

The Shunting-Yard algorithm needs slight modifications to correctly identify and handle unary minus. Below are the key steps:

Step 1: Reinterpret the Input Tokens

During the initial pass over the infix expression:

  • Identify if a minus sign is unary by checking the context. A minus is unary if:
    • It appears at the beginning of the expression.
    • It follows an opening parenthesis `(`.
    • It follows an operator (e.g., `+`, `*`, etc.) other than closing `)`.

Step 2: Adjust Operator Precedence and Associativity

Assign different precedence or identify unary and binary operations distinctly:

  • Unary minus can be treated with higher precedence than binary operators.
  • Define a separate token or marker internally to represent unary minus to differentiate it during processing.

Step 3: Handle the Unary Minus in the Algorithm

Implement checks within the core algorithm:

  • Adjust the action list in the Shunting-Yard algorithm when scanning the input tokens:
  • Recognize the unary minus as an operation that pops a single operand from the evaluation stack and negates it.
    • Push `3` to output queue.
    • `+` is an operator, push to operator stack.
    • `-` is unary (follows a `+`), treat it as unary and push to stack.
    • Push `5` to output queue.
    • Pop unary `-` and apply to `5` then push to output as it meets higher precedence rules.
    • Push `*` to stack.
    • Add `2` to output queue.
    • Pop operators from stack respecting precedence and add to output.

3 5 unary- 2 * +


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.