Lesson 2 of 9 · 26m
Parsing Expressions, Precedence, and Errors People Can Use
Use precedence-aware parsing to build correct trees, preserve operator meaning, and report syntax errors that point at the next repair.
Most language implementations do not die because parsing theory is hard. They die because somebody wrote parse_expression() as a recursive puzzle, bolted on precedence later, and ended up accepting -x ** 2 with the opposite meaning of every language their users already know. Parsing is a user interface. Incorrect trees are security and correctness bugs; bad errors are operational costs.
Recursive descent is enough for a first serious language
You do not need a parser generator to learn parsing. Recursive descent gives you control, simple debugging, and excellent diagnostics:
def parse_expression(parser):
return parse_binary(parser, min_precedence=0)
def parse_binary(parser, min_precedence):
left = parse_unary(parser)
while precedence(parser.peek()) >= min_precedence:
op = parser.advance()
right = parse_binary(parser, precedence(op) + 1)
left = Binary(left, op, right)
return leftThis is precedence climbing. Operators with higher precedence bind tighter. Left-associative operators recurse with precedence + 1; right-associative operators recurse with the same precedence. Write those rules as tests before implementing assignment, exponentiation, comparison, and boolean logic.
Precedence is semantics, not syntax trivia
1 + 2 * 3 -> 1 + (2 * 3)
-a ** 2 often -(a ** 2), not (-a) ** 2
a = b = c usually a = (b = c)
not a == b language-specific; choose deliberatelyIf your language copies another language, copy its precedence exactly or document every deviation. Half-remembered precedence is worse than unfamiliar precedence because users write correct-looking code with the wrong meaning.
Build nodes with spans
The parser should construct an AST that keeps enough information for errors and tooling:
@dataclass
class Binary:
left: object
operator: Token
right: object
span: SpanDo not eagerly fold constants while parsing unless the language specifies it. Constant folding is an optimization pass. The parser's job is faithful structure. If 1 / 0 is invalid, later semantic analysis can decide; the parser should still represent the expression.
Synchronization produces useful recovery
When a parse fails, skip to a predictable boundary such as a semicolon or the next declaration rather than cascading fifty meaningless errors.
Report one primary error with expected tokens, then resume. Users fix one error at a time. Compilers that print every downstream consequence train users to ignore all diagnostics.
Field notes
- Left recursion in naive recursive descent loops forever; rewrite it using loops or precedence climbing.
- Never use exceptions for normal token lookahead; reserve exceptions for unrecoverable internal invariants.
- Treat
elsebinding explicitly. The dangling-else problem is small, famous, and still rediscovered by hobby languages. - Parse a corpus of invalid programs, not only valid ones. Error behavior is part of language quality.
Was this lesson helpful?