Master the fundamental concepts of intermediate representation through this focused micro-challenge.
You have read the whole brief, and the concepts above stay free on every task. Writing and running the code needs a plan.
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
Every task includes starter code, theory, and hidden tests so you can implement and verify locally in the browser.
How it worksLLVM's -S flag prints human-readable .ll files; GCC dumps GIMPLE with -fdump-tree-ssa. A pretty-printer turns your internal IR into text you can diff, test, and round-trip. Compiler engineers spend hours reading printed IR when optimizations go wrong.
Walk instructions and emit syntax: t1 = add t2, t3. Labels on their own lines. Functions as named regions. A parser that reads the same format validates your printer and enables file-based test cases.
cLoading…
Production compilers embed this step inside a longer pipeline. GCC flows through cpp, cc1, assembly, and ld; Clang uses the driver, Sema, LLVM IR passes, and a target backend. LLVM bitcode, JVM bytecode, and WASM are other familiar IRs at the same layer. The exercise isolates one pass so you can test it alone before chaining it to the next stage.
You will write an IR pretty-printer and optional round-trip parser. This exercise asks you to serialize your instruction list to text and verify that parsed output reconstructs the same structure.
Write a pretty-printer and a parser for three-address code that round-trip: parsing the printer's output and printing it again must give byte-identical text. Hand-written input is messy; the printer's output is canonical.
TAC with any spacing: tokens may be separated by spaces, tabs, or nothing at all (x=a+b). Instructions:
cLoading…
Operands are names ([A-Za-z_][A-Za-z0-9_]*) or integer literals, which may be negative (-3) where an operand is expected. # starts a comment that runs to the end of the line; comments and blank lines are discarded.
goto/if/ifFalse/return).# BK (blocks numbered from 0); put one blank line between blocks.NAME:; every other instruction is indented 4 spaces with single spaces between tokens ( x = a + b).instructions=N blocks=B.round-trip: ok if parsing the canonical text (your own parser must skip the # BK comments) and printing it again reproduces it exactly; otherwise round-trip: FAILED.If any input line cannot be parsed, print only error: line L: cannot parse 'TEXT' (TEXT = the line with leading/trailing whitespace and any comment removed) and stop.
Input:
cLoading…
Output:
cLoading…
Hidden tests cover negative literals, every operator, labels immediately after jumps, comments everywhere, and an unparseable line.