Front end, back end and passes
How a compiler is organised into a front end, middle (passes) and back end, what each pass does, and why multiple passes or an IR are used.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Compilers are used by engineers to turn high-level source code into efficient machine code or bytecode. Knowing what the front end, passes, and back end do helps you read compiler output, debug compile-time errors, design language tools (lexers/parsers), and reason about optimisation and portability.
Key ideas
Start from first principles and define terms clearly.
Compiler: a program that translates source code in one language (the source language) into another language (the target language), usually object code or assembly.
Pass: a complete traversal over some representation of the program (for example, source text, parse tree, abstract syntax tree, or intermediate code) that performs analyses or transformations. A pass has a defined input representation and output representation.
Front end: the part of a compiler that checks source-language correctness and produces a language-independent internal representation. The front end includes lexical analysis, syntax analysis, semantic analysis, and error reporting/recovery. The front end is (ideally) target-independent: it does not contain machine-specific code generation.
Back end: the part of a compiler that maps the language-independent representation to target machine code or object code. The back end is target-dependent and includes code generation and machine-specific optimisation.
Intermediate representation (IR): a data structure or code form between front end and back end. Examples: abstract syntax tree (AST), three-address code (TAC), static single assignment (SSA) form, control-flow graph (CFG). IRs are designed to be easy for analysis and transformation.
Lexical analysis (lexer): converts input characters into a stream of tokens. A token is a pair (token-name, attribute) where the attribute typically identifies the lexeme (e.g., a pointer/index into a symbol-table entry for the matched substring) and may include additional info (such as a literal value).
Syntax analysis (parser): reads tokens and produces a parse tree (concrete syntax tree) according to the grammar, or an AST which is a simplified tree capturing structure but omitting unneeded punctuation.
Semantic analysis: checks contextual rules not enforced by grammar (type checking, scope rules, declaration-before-use). It usually builds or consults the symbol table.
Optimisation (middle end): transforms IR to improve performance or reduce size without changing observable behaviour. We separate machine-independent optimisation (performed on IR) from machine-dependent optimisation (performed in back end using target details).
Exact structural statements and assumptions universities expect:
A compiler can be expressed as a sequence of passes: P1; P2; ...; Pn. Each Pi takes representation Ri-1 and produces Ri.
A multipass compiler performs more than one pass over program representations (R0..Rn), possibly producing intermediate forms between passes; passes need not read the entire program or be repeatable/independent. A single‑pass compiler attempts to perform lexical analysis, parsing, semantic checks, and code generation in one left‑to‑right traversal without revisiting earlier input (often interleaving parsing and code generation).
The front end must ensure the source program is well-formed and well-typed before back-end code generation assumes types and scopes are correct. Assumption: semantic checks are decidable for the language subset the compiler supports.
IR design goal: balance between expressiveness and simplicity. A good IR exposes control and data flow explicitly (useful for optimisation) and is less verbose than source-level parse trees.
Diagrams to draw (describe exactly):
Draw a horizontal block diagram: a leftmost box labelled "Source code"; arrow to box labelled Lexical analysis (tokens); arrow to box labelled Parser (parse tree / AST); arrow to box labelled Semantic analysis & Symbol table; arrow to box labelled IR / Middle end (optimiser); arrow to box labelled Back end (code generator & register allocator); arrow to box labelled "Object code / Assembly". Label the middle section as "target-independent" and the rightmost section as "target-dependent". Under the middle block, draw loops for multiple optimisation passes and label them "pass 1, pass 2 ...".
For a pass pipeline, draw vertical boxes for passes P1..Pn with arrows connecting them; annotate inputs/outputs (e.g., tokens -> parse tree, AST -> TAC, TAC -> optimized TAC -> assembly).
Step-by-step derivations commonly asked:
Converting an expression parse tree to TAC: do a post-order traversal; for each operator node generate a new temporary holding the result; emit instructions for child evaluations first, then the operator.
Why multi-pass improves optimization: separating concerns allows repeated global analyses (like liveness, reaching definitions) that require whole-program or whole-function views before safe transformations; a single pass scanning left-to-right cannot in general see all future uses and may miss opportunities.
Formulas
For this programming/topic, the useful syntax forms to remember (IR / TAC patterns):
x = y op z — binary operation, result to x
x = op y — unary operation
x = y — assignment
if x relop y goto L — conditional branch
goto L — unconditional jump
L: — label
param x — push parameter x before call (convention)
call f, n — call function f with n args, result in a temp or return register
return x — return value x
Symbols and their meaning (for TAC syntax above): x,y,z — variables or temporaries (no units); op — arithmetic operator; relop — relational operator; L — label identifier.
Worked examples
Example 1 — Trace of passes for a small C-like statement
Source: x = a + b * 2;
- Lexical analysis: tokens: ID(x) ASSIGN(=) ID(a) PLUS(+) ID(b) MUL(*) NUM(2) SEMI(;). (Write token names with lexemes beside them.)
- Parsing (using grammar for expressions): build parse tree: root
=with left child IDENT(x) and right child+node;+has children IDENT(a) and*node;*node has children IDENT(b) and NUMBER(2). (Draw tree: label each node.) - AST: operator nodes only:
=(x,+(a,*(b, 2))). (Describe as smaller tree.) - TAC generation via post-order traversal:
- t1 = 2
- t2 = b * t1
- t3 = a + t2
- x = t3
- Simple optimisation (constant folding would not apply here because 2 is constant but its multiplication with b is not): final TAC same as above.
- Back end maps TAC to assembly using calling and register conventions (e.g., load b into r1, multiply r1,r2 etc. — exact assembly depends on target). This demonstrates front-end to back-end flow and where target-dependence appears.
Example 2 — Why a single-pass compiler may fail for forward declarations
Source: call to function foo() appears before its definition and uses type information available later.
Step-by-step: single-pass left-to-right parser cannot resolve the call's signature if language requires prototype or overloading checks; multi-pass allows building symbol table in first pass (collect declarations) and resolve calls in a later pass. Model answer: explain use of symbol table built in declaration-collection pass and show two-pass schedule: pass1 collect declarations; pass2 type-check and generate code.
Example 3 — Convert an expression to three-address code (detailed):
Given: z = (a + b) * (c - d)
Post-order:
t1 = a + b
t2 = c - d
t3 = t1 * t2
z = t3
(Show each line as TAC and specify how temporaries are numbered in order of creation.)
Common mistakes
- Mixing up parse tree (concrete) and AST (abstract): AST omits grammar punctuation and extraneous nodes.
- Believing all optimisations are target-independent — some (register allocation, instruction selection) are target-dependent and belong in the back end.
- Forgetting that a pass must have well-defined input/output representations; vague descriptions of passes lose marks.
For your exam
Types of questions and what full-marks answers include:
- Definitions: define front end, back end, pass, IR — give concise definitions plus one sentence of role and an example.
- Short notes: draw the compiler block diagram, explain one pass in detail (lexical analyser or AST→TAC conversion). Include labels and data that flow between boxes.
- Derivations: show step-by-step TAC generation from an expression (use post-order traversal) and explain why the order ensures operands are available.
- Numericals/traces: trace tokens, parse tree, AST, TAC for a given statement — list tokens, draw tree (or describe nodes), give TAC lines numbered.
- Programs: small compiler fragment (e.g., recursive-descent parser function or code to emit TAC from AST) — include input grammar and show how parser calls produce AST nodes.
- Diagrams: properly labelled front/middle/back blocks, with arrows for tokens, AST, IR, object code and loops for optimisation passes.
A full-marks answer always: defines terms, shows the flow of representations between passes, gives an example (tokens/AST/TAC) and explains why front end is target-independent and back end is target-dependent.
Quick check
- What is a pass? 2) Give one example of a target-independent optimisation. 3) Write TAC for
x = (p - q) / r. Answers: 1) A complete traversal over a program representation performing analysis or transformation. 2) Constant folding (or dead-code elimination) performed on IR. 3)t1 = p - qt2 = t1 / rx = t2.
Note on uncertainties: I have aimed to be precise; please verify items listed in the "verify" field below if any institutional conventions differ (for example exact TAC calling convention or the claim that the entire front end is always target-independent in all compilers).
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?