TheAlgorithms/C-Sharp · error · InvalidOperationException

Invalid postfix expression: Leftover operands.

Error message

Invalid postfix expression: Leftover operands.

What it means

PostfixExpressionEvaluation throws InvalidOperationException when, after consuming all tokens, the stack does not hold exactly one result. This means the string was not a valid postfix expression — too many operands or missing operators.

Solutions

  1. Check the token count: a valid postfix expression of n binary operators needs n+1 operands.
  2. Generate postfix programmatically via InfixToPostfixConversion instead of writing it by hand.
  3. Catch InvalidOperationException and validate the expression shape before retrying.

Example fix

// before
int r = evaluator.PostfixExpressionEvaluation("2 3 4");
// after
int r = evaluator.PostfixExpressionEvaluation("2 3 4 +"); // becomes (3+4) with 2? use "2 3 + 4 -" etc.
Defensive patterns

Strategy: validation

Validate before calling

int operands = postfix.Count(char.IsLetterOrDigit); int operators = postfix.Count(c => "+-*/^".Contains(c)); bool valid = operands == operators + 1;

Try / catch

try { int r = evaluator.PostfixExpressionEvaluation(postfix); } catch (InvalidOperationException) { /* expression is not valid postfix */ }

Prevention

When it happens

Trigger: Evaluating strings like "2 3 4" (operands without enough operators) or "2 3 + 5" where one operand remains unconsumed.

Common situations: Hand-written postfix with a typo; concatenating outputs of multiple conversions; truncating a valid expression.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13). Data as JSON: /api/errors/c04480264ca77a37. Report an issue: GitHub.

Appendix: source

Thrown at Algorithms/Stack/InfixToPostfix.cs:85

                if(char.IsDigit(ch))
                {
                    stack.Push(ch - '0');
                    continue;
                }

                if (IsOperator(ch))
                {
                    EvaluateOperator(stack, ch);
                    continue;
                }

                throw new InvalidOperationException($"Invalid character in expression: {ch}");
            }

            if (stack.Count != 1)
            {
                throw new InvalidOperationException("Invalid postfix expression: Leftover operands.");
            }

            return stack.Pop();
        }

        private static void ProcessInfixCharacter(char c, Stack<char> stack, StringBuilder postfixExpression)
        {
            if (IsOperand(c))
            {
                postfixExpression.Append(c);
                return;
            }

            if (c == '(')
            {
                stack.Push(c);
                return;
            }

View on GitHub (pinned to 96e2905cab)