Topic 5 · Weeks 12–14

Topic 5 — Compiling Control Flow — Decisions

if, else, the dangling-else problem, Boolean operators, and switch.

1. Lexicalchars → tokensscanner.l2. Syntaxtokens → ASTparser.y3. SemanticAST → checked ASTsemantic.c4. IRAST → TACtac.c5. OptimizeTAC → better TACtac.c6. CodeGenTAC → MIPScodegen.c

Weeks 12–14  ·  Nov 23 – Dec 13, 2026  ·  if, else, the dangling-else problem, Boolean operators, and switch.

Start here

Lecture notes →

The dangling else and how LR parsers resolve it; short-circuit code and backpatching; three ways to compile a switch; and what retargeting a compiler actually costs.

The full theory for this topic, with diagrams, worked examples and Dragon Book references. This page you are on is only an index.

Your starter code

The compiler you are extending this topic, with every TODO spelled out.
student/topic-5-decisions/

Assignment: Project 5 · Logic and Decisions

Full requirements and submission checklist. Due Dec 13, 2026.

Stuck on the tools rather than the theory? The toolchain tutorials cover make, Git and GitHub, flex and bison.

Class activities

One per class meeting. Ungraded — they exist so that the assignment is not the first time you meet the idea.

Activity 1 — Create the Dangling-Else Conflict, Then Fix It Three Ways

Build the conflict on purpose, read the items in parser.output, fix it with precedence and again with a matched/unmatched grammar, then swap the declarations and watch it break.

Activity 2 — Lower Nested Conditionals

Count the labels, draw the flow graph, execute all four cases, then remove one jump and see which cases still look fine.

Activity 3 — Short-Circuit or Not — Decide It

Both of Dragon's translations, a truth table, the bitwise-and trap, and a measurement.

Activity 4 — switch: Chain, Table, or Binary Search

Lower it by hand with fall-through, then cost all three strategies against four different case distributions.

Activity 5 — Audit Somebody Else's Compiler

Five adversarial programs each, rotate, diagnose by phase, fix one bug before you leave.

Activity 6 — MIPS vs ARM — What Would Actually Change

Hand-translate one function, meet predication and condition flags, then mark every file in your compiler unchanged, edited, or rewritten.