CS 153: Concepts of Compiler Design

(Also SE 153)

 

GREEN SHEET

                                                                                                                        

Spring Semester 2011

Department of Computer Science
San Jose State University

Instructor: Ron Mak

 

Tuesdays and Thursdays, 7:00 – 8:15 pm, MacQuarrie Hall, room 225

 

Office hours: Tuesdays and Thursdays, 6:00 – 6:45 pm and by arrangement.

Office location: Duncan Hall, room 282

E-mail: ron.mak@sjsu.edu

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 much more:

 

  • Develop working compilers and interpreters for a major subset of 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 team-based software engineering practices.

 

This is a major programming class and a Computer Science Department deep course.

Student learning outcomes

  • Develop a scanner and a parser 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.
  • Use the JavaCC compiler-compiler.
  • Develop a compiler that generates object code in the form of assembly language for the Java Virtual Machine (JVM).
  • The source language can be an existing programming language or one that the programming team invents.
  • Assemble the assembly code into executable code that will run on the JVM.

  • Learn critical job skills that employers look for in new college hires!
    • Work together in a small programming team. All students must form teams of around 4 students each. This is a major programming course. There will be too much work for a student to work alone.
    • Understand and modify a Big Hairy Legacy Application (a Pascal interpreter written in Java).
    • Practice good object-oriented design and apply modern software engineering tools and practices to successfully develop a large complex application (your team’s compiler project).

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. The Department of Computer Science strictly enforces prerequisites, especially for a deep course. A student not meeting any prerequisites must fill out an Add Form at the beginning of the semester to explain his or her justifications to take the course, and it will be the instructor’s and the department’s decision whether or not to allow the student to enroll.

 

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

Title: 

Writing Compilers and Interpreters, 3rd edition

Author: 

Ronald Mak

Publisher: 

Wiley Publishers, Inc.

ISBN-13: 

978-0-470-17707-5

Source files: 

http://www.apropos-logic.com/wci/

Title: 

Generating Parsers with JavaCC, 2nd edition

Author: 

Tom Copeland

Publisher: 

Centennial Books

 

(see http://generatingparserswithjavacc.com/)

ISBN-10: 

0-9762214-3-8

 

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

Recommended texts

Title: 

Programming for the Java Virtual Machine

Author: 

Joshua Engel

Publisher: 

Addison-Wesley Professional

ISBN-10: 

0201309726

ISBN-13: 

978-0201309720

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

Procedure

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 be given the source code of a working Pascal interpreter written in Java. After your programming team studies how the interpreter works, your team 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, your team can be creative for your team 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, your team will 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. All students must form teams of 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.

 

This class will move rapidly and cover a lot of complicated material. 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

 

JavaCC

 = Generating Parsers with JavaCC

 

                                                              

Week

Dates

Topics and activities

Readings

1

Jan 27

Overview of the course

What are compilers and interpreters?

Form programming teams

WCI 1

2

Feb 1, 3

A software framework for compilers and interpreters

Scanning (lexical analysis)

Symbol table management

WCI 2, 3, 4

3

Feb 8, 10

Syntax diagrams

Top-down recursive-descent parsing

Parsing assignment statements and expressions

Intermediate code (parse trees)

Interpreting assignment statements and expressions

WCI 5, 6

4

Feb 15, 17

Parsing control statements

Parser error handling

Interpreting control statements

Runtime error handling

WCI 7, 8

5

Feb 22, 24

Parsing declarations

WCI 9


6

Mar 1, 3

Semantic actions and type checking

Scope and the symbol table stack

WCI 10, 11

7

Mar 8, 10

Parsing programs, procedures, and functions

Parsing procedure and function calls

WCI 11

8

Mar 15, 17

Runtime memory management

The runtime stack and activation frames

Passing parameters by value and by reference

Interpreting Pascal programs

WCI 12

9

Mar 22, 24

An interactive source-level debugger

Midcourse review

Midterm exam March 24

WCI 13

10

 

Spring recess

 

11

Apr 5, 7

BNF grammars for programming languages

The JavaCC compiler-compiler

JavaCC 1

12

Apr 12, 14

Generating a scanner with JavaCC

Generating a parser with JavaCC

JavaCC 2, 3

13

Apr 19, 21

JJTrees

JavaCC error handling

The Java Virtual Machine (JVM) architecture

Jasmin assembly language

JavaCC 4, 7

 

WCI 15

14

Apr 26, 28

Code diagrams and code generation

Code for assignment statements and expressions

Code for procedure and function calls

Code for string operations

WCI 16, 17

15

May 3, 5

Code for control statements

Code for arrays and records

Executing compiled Pascal programs

WCI 18

16

May 10, 12

Regular expressions and DFAs

Bottom-up parsing

Lex and Yacc

WCI 19

17

May 17

Code optimization

Heap, stack, and garbage collection

Compiling object-oriented languages

Course review

WCI 19

18

May 19

Final exam

    MacQuarrie Hall, room 225

    7:45-10:00 pm

 

Assignments

Assignments during the first half of the semester will involve modifying, debugging, and adding features to the Java source code of the Pascal interpreter that you’ll be provided. Except for the first one, these assignments will be done by the programming teams. The assignments in the latter half of the semester will help the teams make progress on their compiler projects.

 

Each assignment is worth a maximum of 100 points. Late assignments will lose 20 points and an additional 20 points for each 24 hours after the due date.

Compiler project

During the last half 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 source language. No Lisp or Lisp-like languages.
  • Generating the scanner and the parser for the source language using JavaCC.
  • Developing the code generator that will emit Jasmin code for the JVM.
  • Executing compiled source programs.

                                                          

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 (1 or 2 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, texting, tweeting, or other communication with anyone else during the exams will be strictly forbidden.

Class grade

Your individual class grade will be weighted as follows:

 

30%

Assignments*

35%

Project*

15%

Midterm exam**

20%

Final exam**

 

 

 

 * project team scores
** individual scores

 

Each assignment and exam will be scored (given points) but not assigned a letter grade. The mean score and standard deviation will be announced after each assignment and exam. Final individual class grades will be assigned based on the class curve. Your final class grade can be adjusted up or down depending on your level and quality of participation on your project team as determined by the project tracking tools and your team members’ assessments of your performance.

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

Your own commitment to learning, as evidenced by your enrollment at SJSU, and the University's Academic Integrity Policy requires 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. The policy on academic integrity can be found at http://www.sjsu.edu/studentaffairs/.

Further Information

If you need course adaptations or accommodations because of a disability, or if you need 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 register with the SJSU Disability Resource Center to establish a record of their disability.

 

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 Academic Advising and Retention Services web site. Note that for the Spring 2011 semester, the last day to drop a course without an entry in your permanent record is Monday, February 7, and the last day to add a course and register late is Monday, February 14. 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!