Reading 2 + 3 * 4 strictly from left to right gives 20. Under the arithmetic rules we usually use, the answer is 14. A parser needs those rules to be stated explicitly.
Structure, not just order
Although the expression looks like a list, a tree can show its meaning:
+
/ \
2 *
/ \
3 4
Addition is the root. Its left child is 2; its right child is a multiplication expression. We must evaluate the right child before finishing the addition: 3 * 4 = 12, then 2 + 12 = 14.
An AST, or abstract syntax tree, represents a program’s meaningful structure. Not every source character needs its own node. Parentheses may influence the structure without remaining as separate nodes.
Expressing precedence
We can give addition and multiplication different grammar layers:
expression → product (("+" | "-") product)*
product → unary (("*" | "/") unary)*
unary → ("-" | "+") unary | primary
primary → NUMBER | IDENT | "(" expression ")"
The final * in these rules means “zero or more repetitions.” The quoted "*" is the actual multiplication operator.
The function reading addition calls the multiplication layer to read each operand. A complete multiplication expression can therefore become one operand of addition.
Precedence and associativity differ
In 10 - 3 - 2, both operators have the same precedence. We also need an associativity rule:
(10 - 3) - 2 = 5
10 - (3 - 2) = 9
Subtraction in this example is left-associative. We can implement that by reading the repeated operators in a loop, using the previous result as the left child of the next node.
Precedence groups operators at different levels. Associativity groups operators at the same level.
Try it
Sketch and compare the trees for print 2 + 3 * 4; and print (2 + 3) * 4; on paper. Then do the same for print 10 - 3 - 2;. Think about the structure before calculating the result.
Vocabulary
Precedence: which operators bind more tightly. Associativity: how operators at the same precedence level group.
Multiplication has higher precedence than addition. Subtraction is left-associative in this small language.
Further reading: Crafting Interpreters — Parsing Expressions.