What is the algorithm for parsing expressions in infix notation?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Infix expressions are easy for humans to read but harder for machines to evaluate directly because operator precedence and parentheses must be respected. A standard solution is Dijkstra’s shunting-yard algorithm, which converts infix tokens to postfix order. Postfix form can then be evaluated with a simple stack machine.
Why Infix Parsing Is Nontrivial
Expression 3 + 4 * 2 should evaluate as 3 + (4 * 2), not (3 + 4) * 2. Any parser must handle:
- Operator precedence.
- Operator associativity.
- Parenthesized groups.
Naive left-to-right evaluation fails for many valid expressions.
Shunting-Yard at a Glance
The algorithm maintains two structures:
- Output queue for resulting postfix tokens.
- Operator stack for pending operators and parentheses.
High-level rules:
- Numbers go to output queue.
- Operators pop higher-precedence stack operators to output first.
- Left parenthesis is pushed to stack.
- Right parenthesis pops operators until matching left parenthesis.
- Remaining operators are flushed at end.
This runs in linear time over token count.
Python Conversion Implementation
This version handles precedence, associativity, and parenthesis validation.
Evaluating Postfix Output
Once in postfix, evaluation is straightforward using one value stack.
Separating parse and evaluate stages improves testability and diagnostics.
Handling Unary Operators and Functions
Basic shunting-yard examples often skip unary minus and function calls. Production parsers must extend tokenization and precedence logic for:
- Unary minus and plus.
- Function identifiers such as
sin. - Argument separators like commas.
You can still use shunting-yard, but token grammar needs to be richer.
Alternative Parsing Strategies
Shunting-yard is not the only approach. Other valid designs include:
- Pratt parsers.
- Recursive descent with precedence climbing.
- Parser generators with explicit grammar files.
Shunting-yard stays popular because it is compact and easy to reason about for calculator-like grammars.
Error Handling Recommendations
Good parser UX depends on clear errors. Include:
- Token position in error messages.
- Distinct errors for unknown tokens versus mismatched parentheses.
- Defensive checks for missing operands.
This matters more than micro-optimizing parser loops in most applications.
Common Pitfalls
- Ignoring associativity, especially for exponentiation.
- Mixing tokenization and parsing in one tangled function.
- Failing to detect mismatched parentheses reliably.
- Treating unary minus as binary subtraction in all contexts.
- Returning generic parse failures without useful diagnostics.
Summary
- Shunting-yard is a standard algorithm for parsing infix expressions.
- It converts infix to postfix using an operator stack and output queue.
- Correct precedence and associativity handling is essential.
- Postfix evaluation is then a simple stack process.
- Robust tokenization and clear errors are key for production parsers.
Related reading
- What is the algorithm for query search in the database?
- What is the algorithm for toggling lights up to N?
- What is the algorithm that opencv uses for finding contours?
- What is the benefit for a sort algorithm to be stable?
- What is the best 32bit hash function for short strings tag names?
- What is the best algorithm for arbitrary delimiter/escape character processing?
- What is the best algorithm for finding the closest color in an array to another color?
- What is the best algorithm for overriding GetHashCode?

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 courseTrack 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.