English | 中文
This project implements a MiniLang compiler as a staged course project (M0~M9). Each milestone builds a piece of the toolchain, from lexical analysis through LALR(1) parsing, IR generation with backpatching, CFG construction, local optimizations, and stack-machine codegen. All stages can be run individually or via a single --stage all pipeline that writes every artifact into out/<input_name>/.
- Lexer →
tokens.csv - Lexer also emits symbol table →
symtab.txt(identifiers, first occurrence, count) - LALR(1) table generation →
action_goto.csv - Shift/reduce parser →
parse_trace.txtwith English syntax errors (Expected tokens) - IR (quads) with backpatch →
ir.quad - Basic blocks + CFG →
cfg.txt - Block-local optimizations →
ir_opt.quad,opt_report.txt(English; passes/changes/stats) - Stack VM codegen →
target.asm - One-command pipeline:
--stage allgenerates everything above
- Python 3.10+
- No mandatory third-party dependencies
python -m src.main --mode cli --input examples/demo.min --stage allAll stages share the same entrypoint:
python -m src.main --mode cli --input <file> --stage <stage>Available stages: lexer, table, parse, ir, cfg, opt, codegen, all.
- Outputs are written to
out/<input_basename>/. - Running
--stage allproduces at least:
out/<name>/
├─ tokens.csv
├─ action_goto.csv
├─ parse_trace.txt
├─ ir.quad
├─ cfg.txt
├─ ir_opt.quad
├─ opt_report.txt
└─ target.asm
examples/demo.min: canonical end-to-end sample.examples/expr.min: arithmetic expressions.examples/control.min: control flow with if/else/while.examples/bad.min: triggers parse error with English “Expected ...” message (line:col).examples/opt_showcase.min: highlights visible optimization effects.
bad.minfails at parse with:Error line:col: Expected ..., but got ...and stops the pipeline.- Codegen will fail if a jump target label is missing:
Error: undefined label Lk.