Published June 10, 2026 | Version v1
Book Open

Building a C-Subset Compiler for the FRISC Architecture: From Formal Languages to Executable Code

Authors/Creators

Description

Building a C-Subset Compiler for the FRISC Architecture: From Formal Languages to Executable Code is a narrative monograph that follows a complete, working compiler from a deterministic subset of C all the way down to code that runs on the FRISC (FER Instruction Set Computer) educational architecture.

Rather than presenting compilation as a catalogue of disconnected algorithms, the book builds a single real compiler end to end and explains every phase as it is implemented: lexical analysis (NFA construction, subset construction to a DFA, and maximal-munch tokenization), LR(1) parsing with an automatically constructed parse table, semantic analysis and type checking, lowering to a typed three-address intermediate representation, sixteen semantics-preserving optimization passes, and final code generation for the FRISC instruction set, followed by simulation.

Part IV then revisits the same intermediate representation through two additional back ends — a tree-walking IR interpreter and a stack-based bytecode virtual machine — and uses them, together with the native FRISC back end, to study program behaviour and optimization impact through reproducible measurements on real example programs.

The text is written in a deliberately accessible, first-person narrative voice, while preserving formal rigour: every optimization pass is accompanied by a semantic-preservation proof, and the formal language theory underlying each phase is presented precisely but without ceremony. Worked examples are drawn from a real compiler implementation; no traces or measurements are fabricated.

The book is suitable for advanced undergraduate and graduate students, self-taught engineers, and anyone who wants to understand not just how a compiler is described in theory, but how one is actually built, optimized, and executed.

A companion repository accompanies the book with a chapter-by-chapter "build-along" implementation of tinyC, an integer-only C subset, including starter code, reference solutions, and tests.

Notes (English)

Topics covered:
  - Formal-language foundations: regular languages, finite automata, context-free grammars, LR(1) parsing
  - Front end: tokenizer (NFA→DFA, maximal munch), LR(1) parser, semantic analysis and type checking
  - Typed three-address intermediate representation and its design rationale
  - Sixteen optimization passes (constant folding, copy propagation, dead-code elimination, loop-invariant code motion, common-subexpression elimination, induction-variable strength reduction, and more), each with a correctness proof
  - Code generation for the FRISC ISA, calling convention, runtime helpers, and stack-frame layout
  - Two additional execution models for the same IR: a tree-walking interpreter and a bytecode virtual machine
  - Reproducible performance study comparing native code, interpretation, and bytecode execution

Companion code: A separate "build-along" repository implements the tinyC subset, chapter by chapter (starter / solution / tests).

The FRISC architecture is an educational instruction-set architecture used in computer-science teaching at the University of Zagreb, Faculty of Electrical Engineering and Computing (FER).

This work is independent and carries no institutional affiliation.

Files

Building a C-Subset Compiler for the FRISC Architecture_KarloKnezevic.pdf

Additional details

Identifiers

ISBN
978-953-471-980-0
DOI
10.5281/zenodo.20511074

Dates

Updated
2026-06-10
Published

Software

Repository URL
https://github.com/KarloKnezevic/ccompiler
Programming language
Java , C
Development Status
Active