Temporary chapter name for all chapters
precedence-bug

Time to face the next problem. 2+3*4 should be 14 in math. Our parser gives 20. Write a failing test:

    testCase("2+3*4", 14);

Run it. Watch FAIL print. Don't fix anything yet -- just sit with it. Why does it go wrong?

Trace parseExpression on 2+3*4 in your head. It reads 2 as a numberOrParens. It sees +. It reads the next numberOrParens. What does numberOrParens see, and what does it return?

FAIL: got 20, want 14.

Trace:

parseExpression reads 2 as numberOrParens. Sees +. Reads next numberOrParens: just 3.
Adds: 2+3 = 5. Sees *. Reads next numberOrParens: 4.
Multiplies: 5*4 = 20.

The bug: after the +, the right operand was a single numberOrParens (just 3). But the * was waiting to grab that 3 first. We need the + to wait until the * has done its work on the right side.