Skip to content

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.

Use + - * / ^ and parentheses. Examples: 3+4*2, (A+B)*C, 2^3+1

Postfix (Reverse Polish Notation)
Prefix (Polish Notation)
Evaluated Result
Original Infix

Como funciona

Fórmula

Dijkstra's Shunting-Yard Algorithm (Stack-based operator precedence and associativity)

Exemplo prático

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

OperatorDescriptionPrecedence LevelAssociativity
^Exponentiation / Power4 (Highest)Right-to-Left
*Multiplication3Left-to-Right
/Division3Left-to-Right
+Addition2Left-to-Right
-Subtraction2 (Lowest)Left-to-Right
(, )Grouping ParenthesesSpecialNon-associative

Step-by-Step Worked Example: 3 + 4 * 2 / ( 1 - 5 ) ^ 2

  1. 3 $\rightarrow$ Operand $\rightarrow$ Output: 3
  2. + $\rightarrow$ Push to stack: [+]
  3. 4 $\rightarrow$ Operand $\rightarrow$ Output: 3 4
  4. * $\rightarrow$ Higher precedence than + $\rightarrow$ Push to stack: [+, *]
  5. 2 $\rightarrow$ Operand $\rightarrow$ Output: 3 4 2
  6. / $\rightarrow$ Same precedence as *, left-associative: Pop * to output, push /: Stack [+, /], Output 3 4 2 *
  7. ( $\rightarrow$ Push to stack: [+, /, ( ]
  8. 1 $\rightarrow$ Output: 3 4 2 * 1
  9. - $\rightarrow$ Push to stack: [+, /, (, -]
  10. 5 $\rightarrow$ Output: 3 4 2 * 1 5
  11. ) $\rightarrow$ Pop until (: Pop - to output, discard (: Stack [+, /], Output 3 4 2 * 1 5 -
  12. ^ $\rightarrow$ Higher precedence than / $\rightarrow$ Push: Stack [+, /, ^]
  13. 2 $\rightarrow$ Output: 3 4 2 * 1 5 - 2
  14. End of expression $\rightarrow$ Pop remaining operators: Pop ^, pop /, pop +.

Final Postfix Output: 3 4 2 * 1 5 - 2 ^ / +

Perguntas frequentes

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.

Última atualização: 19 de mar. de 2026
datas e tempo

Contador De Dias Calculator

pt-BR: dias entre duas datas (incluir ultimo dia), dias ate uma data futura (contagem regressiva), somar/subtrair dias.

datas e tempo

Calculadora De Horas Trabalhadas Calculator

pt-BR: rows of entrada, saida, intervalo -> horas do dia; week/month total; hora extra above jornada (default 8h/44h); output hh:mm and decimal.

datas e tempo

Calculadora Entre Datas Calculator

pt-BR: diferenca entre duas datas in anos/meses/dias, total dias/semanas/meses; adicionar/subtrair periodo.

financas

Calculadora De Investimentos Calculator

pt-BR: aporte inicial, aporte mensal, taxa (a.a. or a.m. toggle), prazo -> montante = FV with contributions; optional IR regressivo (22.5% up to 180d, 20% 181-360d, 17.5% 361-720d, 15% >720d); CDI/Selic/poupanca reference rates come from data/rates.json and are editable, never hard-coded.

saude

Calculadora De Pace Calculator

Same logic as pace calculator, native pt-BR UI/copy: ritmo por km/milha, provas (5K,10K,21K,42K).