Crafting A Compiler

Charles N. Fischer / Ron K. Cytron / Richard J. LeBlanc  
Total pages
October 2009
Related Titles


A practical yet thorough treatment of compiler construction.


Crafting a Compiler is an undergraduate-level text that presents a practical approach to compiler construction with thorough coverage of the material and examples that clearly illustrate the concepts in the book. Unlike other texts on the market, Fischer/Cytron/LeBlanc uses object-oriented design patterns and incorporates an algorithmic exposition with modern software practices. The text and its package of accompanying resources allow any instructor to teach a thorough and compelling course in compiler construction in a single semester. Crafting a Compiler  serves as an ideal reference and tutorial.


Use of modern object-oriented patterns (such as Visitor)

Many students are already familiar with object-oriented patterns, and these patterns tend to organize software in such a way that the resulting compliers are more clear and more easily understood, so their use throughout the book allows students to absorb the concepts more readily. For students who have not seen these patterns, their use in a compiler is a compelling example.


Example:  Code generation is cast as a visit of the AST


Use of pseudocode in algorithms

This book offers superior coverage of the algorithms used in the front- and back-end of the compiler.  Unlike other texts that can lead to frustration with a mix of detail and abstraction that can be confusing to students, this book patterns the pseudocode after popular algorithm texts, such as Cormen, Leiserson, Rivest that most students have seen before.


Example:  Many algorithms in a compiler involve sets.  Instead of using low-level data structures to represent sets, they are referenced in pseudocode as sets.  Most languages include implementation of set operations, and those could simply be used for implementing the algorithms.  Students and professors who wish to study the algorithms at  a deeper level can devise more efficient data structures after the basic concepts have been studied.


Exercises as Knowledge-based Inquiry

Most chapters include exercises that are meant to inspire professor- or student-directed exploration. The authors find that such “knowledge-based inquiry” is more fun for students and leads to better retention of concepts. The text forms the foundation for such exploration, but the student and professors are guided into further study by these exercises.


Abstract Syntax Tree (AST) is a pivotal element of the exposition

The book is neatly divided into the phases that construct the AST and those that process the AST.  In contrast with other books, the design and implementation of the AST is forefront in the text, and serves as a central element in type checking and code generation. Interfaces implemented by AST nodes trigger actions by the various compiler phases. As a result, students need not read or write as much code, and new elements are easily incorporated into the AST.


Example:  Type checking can be implemented by visiting the tree and processing interfaces that specify how type rules apply to a given subtree. Semantic changes or additions can be implemented by changing these interfaces and their behavior in the visitors that process the AST.


Supplementary materials

Various components of the compiler can be provided to students so they can focus on the elements of particular interest in a given course offering.


Example:  A front end could be given for parsing Java, and students can then focus on type checking or code generation.  Or, students could develop and incorporate language extensions into the front end, with the back end mostly provided by the instructor.

Table of Contents

Chapter 1 Introduction

1.1 Overview and History of Compilation

1.2 What Compilers Do

    1.2.1 Distinguishing Compilers by the Machine Code Generated

    1.2.2 Target Code Formats

1.3 Interpreters

1.4 Syntax and Semantics of Programming Languages

    1.4.1 Static Semantics

    1.4.2 Run-time Semantics

1.5 Organization of a Compiler

    1.5.1 The Scanner

    1.5.2 The Parser

    1.5.3 The Type Checker (Semantic Analysis)

    1.5.4 The Optimizer

    1.5.5 The Code Generator

    1.5.6 Compiler Writing Tools

1.6 Compiler Design and Programming Language Design

1.7 Architectural Influences of Computer Design

1.8 Compiler Variants

    1.8.1 Debugging (Development) Compilers

    1.8.2 Optimizing Compilers

    1.8.3 Retargetable Compilers

1.9 Program Development Environment


Chapter 2 A Simple Compiler

2.1 An Informal Definition of the ac Language

2.2 Formal Definition of ac

    2.2.1 Syntax Specification

    2.2.2 Token Specification

2.3 Phases of a Simple Compiler

2.4 Scanning

2.5 Parsing

    2.5.1 Predicting a Parsing Procedure

    2.5.2 Implementing the Production

2.6 Abstract Syntax Trees

2.7 Semantic Analysis

    2.7.1 Symbol Tables

    2.7.2 Type Checking

2.8 Code Generation


Chapter 3 Scanning–Theory and Practice</