Skip to main content

Week 2 Studio: Arithmetic expressions

Extending week 1's lexer to arithmetic, and parsing it with a small Pratt parser

1 March 2027

Before the session

Bring week 1’s integer-literal lexer. You’ll be extending it, not starting over.

In the session

Sample transcript

Genuine captured output from this course’s own week-2 reference parser and evaluator, run against four small arithmetic expressions:

$ dune exec bin/main.exe -- examples/precedence.sno
14
20
5
2

The third and fourth lines are the ones worth tracing by hand: 10 - 2 - 3 evaluates to 5, not 11, and 20 / 4 / 2 evaluates to 2, not 10 — both are left-associativity checks, since a right-associative reading of either expression would produce the other answer.

This week’s real starter — the Pratt parser skeleton with precedence climbing left as the gap to fill in — is downloadable at week-02-starter.zip.

Afterwards

This week’s parser is the one you’ll extend for variables (week 3) and strings (week 4) rather than replace — by the end of week 4 it should be handling all of Movement I’s expression forms in one place.

The spec

What you bring to the session. Written so a reader can tell whether it has been met without asking you.

Teaching team