CS 152: Compiler Design

Spring Quarter 2004: March 29, 2004 - June 4, 2004


Less: Basics Coordinates Books Schedule Downloads Assessment   |   More: Scores Policies

Basics

Catalog Description: Covers the fundamentals of compiler design, including lexical analysis, parsing, semantic analysis, compile-time memory organization, run-time memory organization, code generation, and compiler portability issues. Laboratory work involves exercises covering various aspects of compilers.

Prerequisite(s): CS 061: Machine Organization and Assembly Language Programming, CS 141: Intermediate Data Structures and Algorithms (may be taken concurrently), CS 150: The Theory of Automata and Formal Languages. Let me emphasize that you need good programming skills to pass this course!

Time Requirements: Four units (12-16 hours/week): lecture (3 hours/week), laboratory (3 hours/week), individual study (6-10 hours/week; includes reading, writing, and hacking).


Coordinates

Instructor: Peter H. Fröhlich
Office Hours: Monday, Wednesday, Friday, 11:10 am - noon (email for additional appointments)
Location: Surge, Room 341

Lectures: Monday, Wednesday, Friday, 3:10 pm - 4:00 pm
Location: Bourns Hall, Room A125

Assistant: Lan Gao
Office Hours: Wednesday, 4:00 pm - 6:00 pm (email for additional appointments)
Location: Surge, Room 282

Lab: Tuesday, 2:10 pm - 5:00 pm
Location: Surge, Room 171

Lab: Wednesday 8:10 am - 11:00 am
Location: Surge, Room 170

Mailing List: cs152@lists.cs.ucr.edu (Archive)


Books

Here is something weird: There is no required text for this course. Almost every compiler text out there contains the necessary theoretical background, so feel free to buy any book you like. That said, there are some recommended texts that make a lot of sense for this course. First, obviously, some compiler books; then, since there is a substantial amount of programming to be done, three books to help you organize your project and your code.

Compilers

Andrew Appel, Jens Palsberg: Modern Compiler Implementation in Java. Cambridge University Press, 2nd edition, 2003. The most up-to-date and comprehensive compiler text currently available. Provides much deeper coverage of most topics than the lectures do, and covers many more topics of interest than we have time for. All examples are in Java, but editions using C or ML exist as well. The 1st edition is also good enough for the course, and it's available much cheaper.

Keith Cooper, Linda Torczon: Engineering A Compiler. Morgan Kaufmann, 2004. A worthy contender for Appel's throne, this recent book also covers much more than you would need to know, but in a very different way. Personally I like it better than the Appel book, but that's mainly based on presentation, not necessarily on content. Examples are in all kinds of languages, but mostly pseudo-code.

Niklaus Wirth: Compiler Construction. Addison-Wesley, 1996. The most compact introduction to compiler construction I know, from an acknowledged master of the craft. Covers all the essentials for this course (in some way) and also develops a complete compiler. All examples are in Oberon, which might seem weird at first but turns out to be easy to read after a few hours.

Sadly, this book is currently out of print. However, thanks to Professor Wirth's generosity, I can offer the complete text for download as a PDF file [6.2 MB]. Thanks are also due to Eric Frohnhoefer for scanning the book. Please do not print this at school! Organize yourselves and have someone print it for all interested students, for example at Kinko's.

Programming

Andrew Hunt, David Thomas: The Pragmatic Programmer. Addison-Wesley, 1999. A wealth of practical advice on various topics relevant to software construction, including design, implementation, testing, debugging, etc. Organized in 46 relatively small "lessons," extensively cross-referenced, including 70 "rules" and several check lists on a reference card. You can download additional material for this book here. An errata is available.

Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides: Design Patterns. Addison-Wesley, 1995. The standard reference for object-oriented design patterns: well-documented solutions to recurring design and implementation problems. The case study in chapter 2 is also a great introduction to object-oriented design and programming. We'll use a number of patterns in the course, for example Singleton, Composite, Visitor, and Factory.

Brian Kernighan, Rob Pike: The Practice of Programming. Addison-Wesley, 1999. Succinct discussion of a variety of programming topics. Deals with style, documentation, design, testing, debugging, portability, etc. Uses several programming languages (C, C++, Java, Perl) and discusses the tradeoffs involved. An errata is available.


Schedule (Tentative!)

Note the word tentative above. Things seldom go according to plan, and I expect changes here and there as we go along. Please also read the notes below.

Week Lecture Lab Assignment Exam Reading
1 Introduction and Overview [Lecture 1], Compiler Architecture [Lecture 2], Formal Languages [Lecture 3] LaTeX [The Not So Short Introduction to LaTeX], Review: Input and Output, Exceptions, and Templates in C++ [Assignment 1: Calculator] [LaTeX Template] - A 1, 2; W 1, 2
2 Lexical Analysis [Lecture 4] [Lecture 5], Syntactic Analysis [Lecture 6] Subversion, Review: Standard Template Library (STL) [Assignment 2: Scanner] - A 2, 3; W 3, 4
3 Syntactic Analysis [Lecture 7] [Lecture 8] Converting NDFAs to DFAs, Flex [Assignment 3: Parser] [Quiz 1]
[Solution]
A 3; W 4, 7
4 Symbol Tables [Lecture 9] [Lecture 10] [Lecture 11] Bottom-Up Parsing, Bison [Assignment 4: Symbol Table] - A 4, 5; W 5, 8
5 Guest Lecture [Lecture 12], Midterm Review Open Lab: Midterm Review - [Midterm]
[Solution]
-
6 Abstract Syntax Trees [Lecture 13] [Lecture 14] [Lecture 15] TBD [Assignment 5: Abstract Syntax Tree] - A 4, 5; W 16
7 Interpreters [Lecture 16] [Lecture 17] [Lecture 18] TBD [Assignment 6: Interpreter] - A 4, 5
8 Code Generation [Lecture 19] [Lecture 20] [Lecture 21] TBD [Assignment 7: Code Generation] [Mips Code Patterns] [Quiz 2]
[Solution]
A 6, 7, 8, 9; W 10, 11, 13
9 Code Generation [Lecture 22] [Lecture 23] [Lecture 24] TBD [Assignment 8: Free For All] [Reflections on Trusting Trust] - A 6, 7, 8, 9; W 10, 11, 13
10 Summary and Outlook [Lecture 25], Comprehensive Review Open Lab: Comprehensive Review - [Comprehensive]
[Solution]
-

Notes

Open Lab means you can attend any lab section offered that week: You don't have to attend the one you're enrolled in, and you can even attend all of them if you want to.

The term "A c" refers to the text by Appel. The term "C c" refers to the text by Cooper. The term "W c" refers to the text by Wirth. For example, "A 3" refers to chapter 3 in Appel, while "W 3.1" refers to section 1 in chapter 3 in Wirth.

Some topics we go over in lecture or lab are not covered in the text; I will try to provide additional references for those. Note that the readings don't always "match up" with the lectures very well, so the chapters given are approximations. Use your own judgement regarding what is important!

You are expected to do the assigned reading before a topic is covered in lecture or lab. Reading assigned in the week of an exam is part of the exam (except for the entrance exam of course).

The department's programming guidelines make for quite interesting reading, often highly relevant to this course.


Downloads

Here are a bunch of more or less useful resources for the course. Note that in the case of source code, we do not give any guarantee for its correctness. If you reuse our code for your assignment, you have the responsibility for testing it. If you find a bug, please notify us at once!

Feel free to email me any comments you might have. Thanks!

Winter 2003 Slides [286 KB]

The slides I used for the Winter 2003 lecture, edited ever so lightly. Feel free to use these as a supplement, but there is no guarantee that we will follow them this quarter.

Problem Set 1

Just for exercise, not for points. Enjoy!

Problem Set 2

Just for exercise, not for points. Enjoy!

C++ Code

Calculator (Assignment 1)

Lan's C++ implementation of the simple calculator from Assignment 1.

Scanner (Assignment 2) Updated 2004/04/22!

Lan's C++ implementation of the scanner for Simple from Assignment 2 (not updated for new comment style).

Parser (Assignment 3)

Lan's C++ implementation of the parser for Simple from Assignment 3.

Symbol Table (Assignment 4)

Lan's C++ implementation of the symbol table for Simple from Assignment 4.

Abstract Syntax Tree (Assignment 5)

Lan's C++ implementation of the abstract syntax tree for Simple from Assignment 5.

Python Code

Select Compiler

An example compiler (and interpreter) for a language even simpler than Simple. Feel free to study this as we go on with the course, looking at a "complete" example might be helpful now and then. Note that this version still generates VMICS code, not MIPS code; feel free to try upgrading it, and let me know if you succeed.

Calculator (Assignment 1)

Peter's Python implementation of the simple calculator from Assignment 1.

Scanner (Assignment 2)

Peter's Python implementation of the scanner for Simple from Assignment 2 (not updated for new comment style).


Assessment

Assignments (8): 60%, Quizzes (2): 10%, Midterm (1): 15%, Comprehensive (1): 15%. See my policies for more information.


Copyright © 2001-2004 Peter H. Fröhlich. All rights reserved.
$Id: index.html,v 1.16 2004/05/26 15:47:17 phf Exp $
Valid XHTML 1.1!