Infix to Postfix Converter (Shunting-Yard)
Convert standard infix arithmetic expressions into postfix (Reverse Polish Notation) with Dijkstra's Shunting-Yard algorithm. Inspect every step in the trace table.
Cómo funciona
Fórmula
Dijkstra's Shunting-Yard Algorithm (Stack-based operator precedence and associativity) Ejemplo práctico
Infix expression 'a + b * c - (d / e ^ f)' converts to Postfix 'a b c * + d e f ^ / -'.
Understanding the Shunting-Yard Algorithm
First invented by computer science pioneer Edsger W. Dijkstra, the Shunting-Yard algorithm takes inspiration from a railroad switching yard: train cars (tokens) either proceed straight down the track (output queue) or are shunted aside onto a siding (operator stack) until tracks are clear.
Operator Precedence & Associativity Table
| Operator | Description | Precedence Level | Associativity |
|---|---|---|---|
| ^ | Exponentiation / Power | 4 (Highest) | Right-to-Left |
| * | Multiplication | 3 | Left-to-Right |
| / | Division | 3 | Left-to-Right |
| + | Addition | 2 | Left-to-Right |
| - | Subtraction | 2 (Lowest) | Left-to-Right |
| (, ) | Grouping Parentheses | Special | Non-associative |
Step-by-Step Worked Example: 3 + 4 * 2 / ( 1 - 5 ) ^ 2
3$\rightarrow$ Operand $\rightarrow$ Output:3+$\rightarrow$ Push to stack:[+]4$\rightarrow$ Operand $\rightarrow$ Output:3 4*$\rightarrow$ Higher precedence than+$\rightarrow$ Push to stack:[+, *]2$\rightarrow$ Operand $\rightarrow$ Output:3 4 2/$\rightarrow$ Same precedence as*, left-associative: Pop*to output, push/: Stack[+, /], Output3 4 2 *($\rightarrow$ Push to stack:[+, /, ( ]1$\rightarrow$ Output:3 4 2 * 1-$\rightarrow$ Push to stack:[+, /, (, -]5$\rightarrow$ Output:3 4 2 * 1 5)$\rightarrow$ Pop until(: Pop-to output, discard(: Stack[+, /], Output3 4 2 * 1 5 -^$\rightarrow$ Higher precedence than/$\rightarrow$ Push: Stack[+, /, ^]2$\rightarrow$ Output:3 4 2 * 1 5 - 2- End of expression $\rightarrow$ Pop remaining operators: Pop
^, pop/, pop+.
Final Postfix Output: 3 4 2 * 1 5 - 2 ^ / +
Preguntas frecuentes
What is the difference between infix, postfix, and prefix notation?
In infix notation, operators sit between operands (e.g. A + B), requiring parentheses to establish precedence. In postfix notation (Reverse Polish Notation), operators follow operands (e.g. A B +), eliminating the need for parentheses. In prefix notation (Polish Notation), operators precede operands (e.g. + A B).
How does Dijkstra's Shunting-Yard algorithm handle operator precedence?
Incoming operators with lower or equal precedence cause operators with higher precedence to be popped from the operator stack onto the output queue. Exponentiation (^) has highest precedence and right-associativity, multiplication/division (*, /) have medium precedence, and addition/subtraction (+, -) have lowest precedence.
Why do compilers and virtual machines convert expressions to postfix?
Postfix expressions can be evaluated linearly in a single pass using an evaluation stack in O(n) time and O(n) space without back-tracking or tree traversal overhead.
What happens when a closing parenthesis ')' is encountered?
The algorithm pops operators from the stack to the output until the matching opening parenthesis '(' is found on the stack. The opening parenthesis is then discarded without being added to the output.
Herramientas relacionadas
← Ver todas las herramientas de otras calculadorasCalculadora Calculator
es-ES: basic + scientific calculator. Safe expression parser (shunting-yard, NO eval), keypad + keyboard input, history, memory keys. MOUNT ON /es/ HOME PAGE (not on /es/otras-calculadoras/calculadora/; 301 that URL to /es/).
Interes Compuesto Calculator
es: compound interest calculator (same engine as en), capital, tasa, periodicidad, anos, aportacion periodica.
Calcular Porcentaje Calculator
es: percentage calculator (X% de Y, aumento/descuento, variacion).
Calculadora De Ritmos Calculator
es-ES pace calculator, native UI/copy (ritmo por km, tiempos de carrera).