Day 04 of 30 · Compiler foundations

Represent syntax with trees

Time
About 150 minutes: 45 reading, 75 lab, 20 testing, 10 notes.
Prerequisites
Day 03: Turn characters into tokens

Toolchain: your host C++ compiler

Today’s lab is the toy expression frontend. Any working C++ compiler builds it; no LLVM libraries are needed yet. Setup guide

Learn

  • Grammar, precedence, associativity and AST ownership.

Read Reading

Reading budgets cover selected sections, not whole chapters. Link to the book; this site does not reproduce it.

Build Build or investigate

  1. Write a small grammar and C++ AST nodes for literals, names and binary expressions.
  2. Use explicit ownership such as unique_ptr.
  3. Hand-draw trees for 2 + 3 * 4 and (2 + 3) * 4, then add an AST printer.

Done when

An AST printer and a written grammar in the learning repository.

Recall question

Why are the trees for those two expressions different?

Optional

  • Add a unary-minus AST node.

Optional work never counts toward completion.

Completion checks

Progress tracking needs JavaScript. The lesson above is complete without it.

Required to complete day 04
Optional (not counted)

Notes

Saved only in this browser. Keep it short enough to finish in ten minutes; none of the prompts are required. All notes, export and import