CS 153: Concepts of Compiler Design

GREEN SHEET

                                                                                      

Fall Semester 2008

Department of Computer Science
San Jose State University

Instructor: Prof. Ron Mak

 

Section 1: Mondays and Wednesdays, 7:00 – 8:15 pm, MacQuarrie Hall, room 225

 

Office hours: Mondays and Wednesdays, 6:00 – 6:45 pm and by arrangement.

Office location: Duncan Hall, room 282

E-mail: mak@cs.sjsu.edu (e-mail preferred over phone)

Phone: (408) 533-2726 (cell)

Course catalog description

“Theoretical aspects of compiler design, including parsing context free languages, lexical analysis, translation specification and machine-independent code generation. Programming projects to demonstrate design topics.” 3 units

 

But we’ll do more:

 

  • Develop working compilers and interpreters for a major subset of the Pascal, a simple but powerful procedure-oriented programming language.
  • Generate executable code for the Java Virtual Machine (JVM).
  • Use the JavaCC compiler-compiler to automatically generate scanners and parsers.
  • Employ good object-oriented design and software engineering practices.

Student learning outcomes

  • Develop a scanner (lexical analyzer) and a parser (syntactic analyzer) for a procedure-oriented programming language such as Pascal.
  • Generate a symbol table and intermediate code (parse trees).
  • Perform semantic analysis such as type checking.
  • Develop an interpreter that creates a suitable runtime environment from the intermediate code and the symbol table and executes the source program.
  • Develop a compiler that generates executable code in the form of assembly language for the Java Virtual Machine (JVM).
  • Use compiler-compilers such as JavaCC or the classic lex and yacc tools.
  • Practice good object-oriented design and software engineering in order to develop complex programs in a small team.

Prerequisites

CS 47

Introduction to Computer Organization

CS 146

Data Structures and Algorithms

CS 154

Formal Languages and Computability

 

A grade C- or better in each, or instructor’s consent.

 

You must be at least a “decent” Java 5.0 programmer. Please review Java array lists, hash maps, enumerated types, sets, and generics. You should know how to use a Java development and debugging environment such as Eclipse.

Required texts

I will provide drafts of chapters from the book I'm currently writing, Writing Compilers and Interpreters: A Software Engineering Approach. Other books you'll need for reference:

Title: 

Programming for the Java Virtual Machine

Author: 

Joshua Engel

Publisher: 

Addison-Wesley Professional

ISBN-10: 

0201309726

ISBN-13: 

978-0201309720

Title: 

Generating Parsers with JavaCC

Author: 

Tom Copeland

Publisher: 

Centennial Books

 

(see http://generatingparserswithjavacc.com/)

ISBN-10: 

0-9762214-3-8

Title: 

The Essentials of Pascal I

Author: 

Gary W. Wester

Publisher: 

Research and Education Association

ISBN-10: 

0-87891-694-6

ISBN-13: 

978-0878916948

Title: 

The Essentials of Pascal II

Author: 

Gary W. Wester

Publisher: 

Research and Education Association

ISBN-10: 

0-87891-718-7

ISBN-13: 

978-0878917181

 

There may be additional reading assignments and use of tools from the Internet. URLs will be provided.

Recommended texts

There is no shortage of compiler books on the market! Prof. Louden's book is at about the same level as mine but more rigorous. Cooper and Torczon is a more advanced text. Lex and yacc are classic compiler-compiler tools that nearly every compiler writer will encounter.

 

Title: 

Compiler Construction: Principles and Practice

Author: 

Kenneth C. Louden

Publisher: 

PWS Publishing Company

ISBN-10: 

0534939724

ISBN-13: 

978-0534939724

Title: 

Engineering a Compiler

Author: 

Keith D. Cooper and Linda Torczon

Publisher: 

Morgan Kaufman

ISBN-10: 

155860698X

ISBN-13: 

978-1558606982

Title: 

lex & yacc

Author: 

Doug Brown

Publisher: 

O'Reilly

ISBN-10: 

1565920007

ISBN-13: 

978-1565920002

Procedure

This will be primarily a programming class. Compilers and interpreters are complex programs that will challenge your software development skills. You will learn both by example and by doing. During the first part of the course, you’ll have access to the source code of a working Pascal compiler and interpreter written in Java. After you study how it works, your assignments will be to modify, debug, and add features to it. This practice will give you a good understanding of the parts of a compiler or interpreter, how they work, and how they operate together.

 

During the last part of the course, you can be creative for your compiler project. You’ll write a “grammar” for a programming language and use the JavaCC compiler-compiler to automatically generate a scanner and a parser for that language. Using the code and what you learned from the first part of the course, you’ll develop a new compiler for a language that you chose – or invent your own language! I’ll also introduce you to some of the theory and advanced algorithms of compiler writing, especially in the areas of parsing and code generation.

                                                                                   

Compiler writing is best tackled by programming teams. Therefore, you will form small teams of 2 or 3 students each to work on the assignments and on the compiler project. Choose your team members carefully! Project tracking and source control tools will be available. Bring your laptops to class – we’ll try to set aside some “lab” time for all the teams to get together and share tips and accomplishments.

 

Class attendance and participation are important. At the end of the semester, each team will turn in its project deliverables, and each student will individually turn in a short postmortem report. Details are below.

Schedule

Subject to change with fair notice.

 

Readings:     WCI = Writing Compilers and Interpreters (chapter drafts)
JVM = Programming for the Java Virtual Machine
JavaCC = Generating Parsers with JavaCC
Pascal = Essentials of Pascal I and II

 

Week

Dates

Topics and activities (activities in italics)

Readings

1

Aug 25, 27

Overview of the course

What are compilers and interpreters?

A software framework for compilers and interpreters

Form project teams

WCI 1, 2

Pascal

2

Sep 3

Scanning (lexical analysis)

WCI 3

3

Sep 8, 10

Symbol table management

Syntax diagrams

Top-down recursive-descent parsing

Parsing assignment statements and expressions

WCI 4, 5

4

Sep 15, 17

Intermediate code (parse trees)

Interpreting assignment statements and expressions

Parser error handling

Parsing control statements

WCI 6, 7

5

Sep 22, 24

Runtime error handling

Interpreting control statements

Parsing declarations

WCI 8, 9


6

Sep 29
Oct 1

Parsing declarations (cont’d)

Semantic actions and type checking

WCI 9, 10

7

Oct 6, 8

Scope and the symbol table stack

Parsing programs, procedures, and functions

Parsing procedure and function calls

WCI 11

8

Oct 13, 15

Runtime memory management

The runtime stack and activation frames

Passing parameters by value and by reference

Interpreting Pascal programs

An interactive symbolic debugger

WCI 12

9

Oct 20, 22

Midcourse review

Midterm exam (Oct 22)

 

10

Oct 27, 29

BNF grammars for programming languages

Compiler development tools

The JavaCC compiler-compiler

JavaCC 1

11

Nov 3, 5

Generating a scanner with JavaCC

Generating a parser with JavaCC

JJTrees

JavaCC error handling

JavaCC 2, 3, 4, 7

12

Nov 10, 12

The Java Virtual Machine (JVM) architecture

The Jasmin assembly language

Code diagrams and code generation

Code for assignment statements and expressions

JVM 1-5

WCI 16

13

Nov 17, 19

Code for control statements

Code for procedure and function calls

Compiling and executing Pascal programs

WCI 17

14

Nov 24, 26

Register allocation

Regular expressions and DFAs

Bottom-up parsing

Handouts

15

Dec 1, 3

Lex and Yacc

Code optimization

Handouts

16

Dec 8, 10

Compiling object-oriented languages

Course review

Compiler projects and postmortem reports due

Handouts

17

Dec 15

Final exam

    MacQuarrie Hall, room 225

    7:45-10:00 pm

 

Assignments

Most of the assignments will involve modifying, debugging, and adding features to the Java source code of the Pascal compiler that you’ll be provided. These assignments will be done by the programming teams, although a few of them may be assigned individually. Each assignment is worth a maximum of 100 points. Late assignments lose 25 points for each class meeting after the due date. Extra-credit options will be discussed in class.

Compiler project

During the final weeks of the semester, each programming team will develop a compiler using the JavaCC compiler-compiler. This project will involve:

 

  • Creating the grammar for the source language. This can be Pascal or any other procedure-oriented language (or subset thereof), or you can invent your own language.
  • Generating the scanner and the parser for the language using JavaCC.
  • Developing the compiler that will generate code for the JVM.

 

Deliverables (what each team turns in) for the project include:

 

  • The Java source files of a working compiler.
  • A written report (5-10 pp.) that includes:
    • A high-level description of the design of the compiler. UML diagrams of the major classes are acceptable.
    • The grammar for your source language, either in BNF or as syntax diagrams.
    • Code diagrams that show the Jasmin code your compiler generates for some key constructs of the source language.
  • Instructions on how to build your compiler. Unix shell, DOS .bat, or Ant scripts are acceptable.
  • Instructions on how to run your compiler. Once again, shell, .bat, or Ant scripts are acceptable.
  • Sample source programs to compile and execute on the JVM.

 

As part of the deliverables, each student must also turn in a short (2-3 pp.) individual postmortem report that includes:

 

  • A brief description of what you learned in the course.
  • An assessment of your accomplishments for your project team on the assignments and the compiler project.
  • An assessment of each of your other project team members.

 

You should start thinking about and planning for your project early in the semester.

Exams

The midterm and final examinations will be open book, notes, and laptops. Instant messaging, e-mails, or other communications with anyone else during the exams will be strictly forbidden.

Class grade

30%

Assignments

30%

Compiler project

15%

Midterm exam

25%

Final exam

A-

90-92

A

93-100

 

 

B-

80-82

B

83-86

B+

87-89

C-

70-72

C

73-76

C+

77-79

D-

60-62

D

63-66

D+

67-69

 

 

F

< 60

 

 

Classroom protocol

As mentioned above, it is very important for each student to attend classes and to participate. Cell phones in silent mode, please.

Academic integrity

Students are expected to be familiar with the University’s Academic Integrity Policy. Please review this at http://sa.sjsu.edu/judicial_affairs/. “Your own commitment to learning, as evidenced by your enrollment at San Jose State University and the University’s integrity policy, require you to be honest in all your academic course work. Faculty members are required to report all infractions to the office of Student Conduct and Ethical development.”

 

“Instances of academic dishonesty will not be tolerated. Cheating on exams or plagiarism (presenting the work of another as your own, or the use of another person’s ideas without giving proper credit) will result in a failing grade and sanctions by the University. For this class, all assignments are to be completed by the individual student unless otherwise specified. If you would like to include in your assignment any material you have submitted, or plan to submit for another class, please note that SJSU’s Academic Policy F06-1 requires approval of instructors.”

Campus Policy in Compliance with the American Disabilities Act

“If you need course adaptations or accommodations because of a disability, or if you need to make special arrangements in case the building must be evacuated, please make an appointment with me as soon as possible, or see me during office hours. Presidential Directive 97-03 requires that students with disabilities requesting accommodations must register with the DRC (Disability Resource Center) to establish a record of their disability.”

 

Special accommodations for exams require ample notice to the testing office and must be submitted to the instructor well in advance of the exam date.

University Policies and Procedures

Please familiarize yourself with SJSU policies and procedures:

 

 

particularly the add/drop policy. It is your responsibility to know and observe these policies. However, if there is something about a policy that you don't understand, please feel free to ask! You can also find answers to many questions at the Student Advising Center. Note that for the Fall 2008 semester, the last day to drop without consequence is Friday, September 5, and the last day to add is Friday, September 12. After these dates it becomes very difficult to drop or add a class, so be sure you are in good shape before these dates arrive!