A small compiler-analysis project written in Python that parses a subset of C, builds a Control-Flow Graph (CFG), and solves classic data-flow problems using a reusable worklist algorithm.
- Parses C code with
pycparser - Builds CFGs for declarations, assignments,
if/else,while,for,return,break, andcontinue - Implements Reaching Definitions as a forward may-analysis
- Implements Live Variables as a backward may-analysis
- Uses a generic fixed-point worklist solver
- Exports CFGs as PNG and Graphviz DOT files
- Includes sample C programs and automated regression tests
Compiler-Data-Flow-Analysis/
├── src/
│ ├── main.py
│ ├── cfg.py
│ ├── analyses.py
│ └── utils.py
├── examples/
│ ├── if_else.c
│ ├── while_if.c
│ └── for_loop.c
├── tests/
│ └── test_engine.py
├── docs/
│ ├── report.md
│ └── assets/
├── requirements.txt
├── .gitignore
├── .gitattributes
└── README.md
For each basic block B:
IN[B] = union(OUT[P]) for P in predecessors(B)
OUT[B] = GEN[B] union (IN[B] - KILL[B])
For each basic block B:
OUT[B] = union(IN[S]) for S in successors(B)
IN[B] = USE[B] union (OUT[B] - DEF[B])
python -m venv .venvActivate the environment, then install the dependencies:
pip install -r requirements.txtRun all built-in examples:
python src/main.pyAnalyze a supplied C file:
python src/main.py --file examples/if_else.c --outdir outThe command prints the CFG blocks, reaching-definition sets, live-variable sets, and worklist updates. It also writes PNG and DOT CFG files to the selected output directory.
Run the regression tests with:
python -m unittest discover -s tests -vThe tests cover important correctness cases including for initializers, repeated definitions, returning branches, compound assignments, and loop break handling.
This is an educational analysis engine, not a full C compiler. It intentionally supports a practical subset of C and uses simplified alias/memory modeling. Pointer aliasing, full preprocessing, interprocedural analysis, and all C control-flow constructs are outside the current scope.
See docs/report.md for the theory, equations, implementation outline, and experiment discussion.
- Python
- pycparser
- NetworkX
- Matplotlib
- Compiler data-flow analysis


