KatInterview

Compile a Mini Language to Three-Address Code

StringsModel answer
MediumModel answerStringsparsingcompilerintermediate-representation

Last updated

Parse a small line-based language of assignments and flat if-else blocks, then emit a deterministic three-address IR with temporaries and jump labels.

Background

Compilers translate source code through several layers before producing machine instructions. One common intermediate step is three-address code (also called operation triads): a flat sequence of simple instructions where each operation has at most two inputs and one output. Temporaries hold intermediate values; labels and jumps encode control flow.

Your task is to build a minimal compiler front end for a toy language and emit this kind of IR.

The Language

The language has two kinds of statement.

Assignments — one per line:

a = 1
x = b + c

If-else blocks — always braced, always paired:

if (condition) {
    x = 10
} else {
    x = 20
}

Expressions are built from integer literals and variable names using +, -, *, / with standard precedence. Parentheses are allowed.

Conditions compare two expressions with a single comparison operator (==, !=, <, >, <=, >=).

Hard constraints:

  • if blocks cannot be nested.
  • Every if must have a matching else.
  • Each brace-delimited block contains exactly one assignment.

Input

A list of strings — one line of source code per string. Brace placement is flexible; } else { may appear on its own line or combined.

[
    "a = 1",
    "if (a + b == 3) {",
    "    x = 10",
    "} else {",
    "    x = 20",
    "}"
]

Output

A flat list of triads. Each triad is a 4-tuple (op, arg1, arg2, result). Use "_" for unused slots.

Introduce temporaries t1, t2, … and labels L1, L2, … in left-to-right, innermost-first emission order. The same input must always produce the same output.

Expected output for the example above:

("=",        "1",  "_",  "a" )
("+",        "a",  "b",  "t1")
("==",       "t1", "3",  "t2")
("if_false", "t2", "L1", "_" )
("=",        "10", "_",  "x" )
("goto",     "L2", "_",  "_" )
("label",    "L1", "_",  "_" )
("=",        "20", "_",  "x" )
("label",    "L2", "_",  "_" )

Requirements

  1. Tokenize the input, handling keywords, identifiers, integers, operators, and punctuation.
  2. Parse using recursive descent; enforce all three hard constraints with descriptive errors.
  3. Emit triads in execution order during parsing (syntax-directed translation).
  4. State clearly how temporaries and labels are numbered.
  5. Handle edge cases: blank lines, parenthesized sub-expressions, == vs =, keywords used as variable names.

Hints

Hint 1

Tokenize the entire program into a flat token list first, rather than processing line by line — it makes brace placement irrelevant and simplifies the parser significantly.

Hint 2

Each expression-parsing function should return the *name* that holds its result: either an atom (like "a" or "3") or a freshly allocated temporary. That way the caller can pass it directly as an argument to the next instruction.

Hint 3

Allocate both labels for an if-else up front, before emitting anything for that block. Emit `if_false … L_else`, then the then-branch, then `goto L_end`, then `label L_else`, then the else-branch, then `label L_end`.