Command Palette

Search for a command to run...

Back to skills
skill № 02809

compiler-design

Use when the user asks about writing a compiler or interpreter, lexers, parsers, ASTs, grammars, type checkers, SSA, LLVM IR, MLIR, optimization passes or…

FreeNo executable scripts
j4flmaoby j4flmao★ 47 GitHub stars

Install compiler-design in Claude Code & Claude Desktop

Sign up to install this skill

Create a free account to reveal the install command and save the skill to your library.

  • Reveal the one-line install command
  • Save skills to your synced library
  • Get notified when skills update
Sign up freeI already have an account

About this skill

Compiler Design

Purpose

Build a language implementation that is correct first and fast second. This skill gives decision rules for each phase — which parsing technique, what the AST and IR should look like, which optimisations matter, and when to lean on LLVM/MLIR/Cranelift instead of writing a backend.

Agent Protocol

Trigger

"compiler", "lexer", "parser", "AST", "grammar", "LLVM IR", "SSA", "optimization pass", "codegen", "register allocation", "interpreter", "MLIR", "type checker".

Input Context

  • Source language (existing or new), size of the grammar, target (native, WASM, bytecode VM, JVM)
  • Implementation language and whether LLVM/Cranelift/MLIR may be used
  • Priorities: compile speed, runtime speed, error messages, incremental/IDE support

Output Artifact

Phase design, grammar, data structures, pass code or LLVM IR examples. No file unless requested.

Response Format

Pipeline: {lex -> parse -> resolve/typecheck -> IR -> passes -> codegen}
Choice per phase: {technique + reason}
Code/IR: {snippet}
Tests: {golden files, fuzzing, differential testing}

1. Pipeline

source → lexer (tokens) → parser (CST/AST) → name resolution + type checking (typed AST / HIR)
       → lowering to IR (SSA) → optimisation passes → instruction selection
       → register allocation + scheduling → object code / bytecode

Keep each stage's data structure explicit; most bugs come from phases sharing mutable state.

2. Lexing

  • Tokens = kind + span (byte offsets) + optional interned value. Spans enable good diagnostics.
  • Hand-written lexers (a match on the current char) are simple and fast; generators (re2c, flex, logos in Rust) build DFAs from regular expressions.
  • Handle maximal munch, keywords vs identifiers (look up after scanning an identifier), nested comments (needs a counter, not a regex), and string escapes.

3. Parsing

Technique Strength Use
Recursive descent (LL) readable, great errors, easy recovery most production compilers (GCC, Clang, Go, Rust, TypeScript use hand-written parsers)
Pratt / precedence climbing expressions with many precedence levels embedded inside recursive descent
LR/LALR (yacc/bison), GLR handles left recursion, grammar-checked when a formal grammar is the source of truth
PEG / packrat ordered choice, no ambiguity DSLs; watch memory and error quality
Parser combinators compact in functional languages small/medium languages

Pratt expression parser core:

def parse_expr(self, min_bp=0):
    lhs = self.parse_prefix()                  # literals, identifiers, unary ops, parens
    while True:
        op = self.peek()
        if op.kind not in INFIX: break
        l_bp, r_bp = INFIX[op.kind]            # e.g. '+': (10, 11) left-assoc, '^': (31, 30) right-assoc
        if l_bp < min_bp: break
        self.advance()
        rhs = self.parse_expr(r_bp)
        lhs = Binary(op, lhs, rhs)
    return lhs
  • Error recovery: synchronise on statement boundaries (;, }) and keep parsing to report several errors.
  • For IDEs, a lossless concrete syntax tree (Roslyn, rust-analyzer's rowan, tree-sitter) enables incremental reparsing and formatting.

4. Semantic Analysis

  • Name resolution builds scopes (stack of hash maps) and links uses to declarations.
  • Type checking: simple languages check bottom-up; ML-style languages use Hindley–Milner unification; modern languages often use bidirectional checking (infer + check modes).
  • Desugar after checking so errors refer to user code; keep spans on every node.

5. Intermediate Representation

  • SSA: every value assigned once; φ-nodes merge values at control-flow joins. Simplifies dataflow analyses (constant propagation, GVN, DCE).
  • Construct SSA with the dominance-frontier algorithm (Cytron et al.) or, from an AST directly, Braun et al.'s simple SSA construction. LLVM frontends usually emit allocas and let mem2reg/SROA promote them.

Example LLVM IR for int max(int a, int b) { return a > b ? a : b; }:

define i32 @max(i32 %a, i32 %b) {
entry:
  %cmp = icmp sgt i32 %a, %b
  br i1 %cmp, label %then, label %else
then:
  br label %end
else:
  br label %end
end:
  %r = phi i32 [ %a, %then ], [ %b, %else ]
  ret i32 %r
}

(After simplifycfg this becomes a single select.)

6. Optimisation Passes

  • Analysis passes compute facts (dominator tree, loop info, alias analysis, scalar evolution); transform passes rewrite IR (inlining, SROA, InstCombine, GVN, LICM, loop unrolling/vectorisation, DCE).
  • LLVM's new pass manager (default since LLVM 13; the legacy PM for the middle-end optimisation pipeline was removed in LLVM 17, while codegen still largely uses it) caches analyses and invalidates them based on the PreservedAnalyses each pass returns:
struct CountAdds : llvm::PassInfoMixin<CountAdds> {
  llvm::PreservedAnalyses run(llvm::Function &F, llvm::FunctionAnalysisManager &) {
    unsigne
Full SKILL.md on GitHub

Allowed tools

Tools this skill is permitted to call.

No restriction — this skill can use any tool.

Bundled files

  • references/compiler-llvm.md
  • references/lsp-protocol.md

FAQ

What does the compiler-design skill do?

Use when the user asks about writing a compiler or interpreter, lexers, parsers, ASTs, grammars, type checkers, SSA, LLVM IR, MLIR, optimization passes or register allocation. Produces compiler pipeline design and working front-end and IR code. Not for reading assembly output (use lang-assembly) or CPU microarchitecture (use cpu-architecture).

How do I install the compiler-design skill?

Copy the skill folder into ~/.claude/skills (the Claude Code tab above does this in one command), or install it as a plugin.

Does the compiler-design skill run scripts?

No, this skill is instructions only (SKILL.md) with no executable scripts.

Related skills

Compare compiler-design with