Prospectus

nl en

Compiler Construction

Course
2024-2025

Admission requirements

This is a practical-intensive course, which involves lots of low-level systems programming. Mastering all this is also highly rewarding. Students are assumed to have taken courses in (object-oriented) programming, and computer architecture at a BSc level. Knowledge of Java is not required and can be learned during this course.

Description

Computer programs in a higher programming language cannot be executed by a computer until they have been translated into lower-level executable code. In many cases this is done by a compiler.

In this course we learn how compilers convert source code into assembly code in several steps. We study the theory behind compilers with a keen eye on the practical aspects of building a compiler, and apply this in a series of assignments. We construct a compiler for the ChocoPy language, which is a subset of Python. The compiler itself is written in Java and outputs RISC-V assembly code.

Through compiler construction, we learn how programming languages work and how they are implemented. We get insight into the reasons why programming languages are designed the way they are, and what their limitations are. Because of this, and because a lot of programming has to be done in the practical, you will become a better programmer with a much more in-depth understanding of languages and tools.

Topics covered during the lectures relate to the process of constructing a compiler, e.g., parsing, semantic analysis, code generation, and code optimization.

Course Objectives

After this course, students are expected to be able to:

  • Reproduce concepts used in compilers, such as the stages of compilation, syntax-directed translation, type conversions, basic blocks, stack frames, etc.

  • Explain how programming languages work and how they are implemented, including design considerations and limitations.

  • Understand how grammars and intermediate representations are used to implement the syntax and semantics of programming languages.

    • How a grammar can be made unambiguous.
    • How grammars can be used to implement syntax-driven analysis.
    • How to construct and use abstract syntax trees and three-address code.
    • How symbol tables and scoping work.
    • Explain how a type system helps with preventing programming errors and generating more efficient code.
  • Discuss how to generate code, how procedure calls are implemented, how to manage the stack and memory, explain the interplay between generated code and a runtime system.

  • Implement lexical analysis and parsing.

  • Implement semantic analysis and type checking.

  • Implement code generation and optimization.

  • Analyse the quality of the generated code and its performance.

Timetable

You will find the timetables for all courses and degree programmes of Leiden University in the tool MyTimetable (login). Any teaching activities that you have sucessfully registered for in MyStudyMap will automatically be displayed in MyTimeTable. Any timetables that you add manually, will be saved and automatically displayed the next time you sign in.

MyTimetable allows you to integrate your timetable with your calendar apps such as Outlook, Google Calendar, Apple Calendar and other calendar apps on your smartphone. Any timetable changes will be automatically synced with your calendar. If you wish, you can also receive an email notification of the change. You can turn notifications on in ‘Settings’ (after login).

For more information, watch the video or go the the 'help-page' in MyTimetable. Please note: Joint Degree students Leiden/Delft have to merge their two different timetables into one. This video explains how to do this.

Mode of Instruction

The course is composed of two components:
1. Lectures given by the instructor, to setup course format, assignments, and to teach key topics in compiler construction.

  1. Self-study Lab Assignments. Students are expected to work on the lab assignments autonomously, outside of the lectures. We also set up Lab Class, every week, after the lectures. In these labs, students ask questions and get practical help from teaching assistants. Please note that this is a hands-on course, where the lab constitutes a large part of the final grade.

For the practicals of this course, we implement a ChocoPy compiler. ChocoPy is a subset of Python (all ChocoPy programs are valid Python programs). The compiler will be written in Java. The output of the compiler is RISC-V assembly instructions, which can be emulated on any machine. Thanks to the use of Java, the compiler and generated code can run on any platform, so you can do the whole assignment on your own PC or laptop.

A regular compiler has 3 main stages to go from code to machine-understandable instructions: 1. Lexical Analysis and parsing 2. Semantic analysis and type checking 4. machine-code emitting and optimization. There are 3 practical assignments, each one implementing one of the aforementioned stages, designing compiler functionality to treat various language features and compiler components and optimizations. Students can build each next stage on top of their previous stage implementations. However, reference implementations are also provided after each assignment, to make sure you do not get stuck if an earlier assignment goes less well than anticipated.

A base framework will be published to write your implementation in. We also provide tests to verify implementation correctness. The assignments will be implemented in Java.

Assessment method

The final grade of this course is composed of a large practical assignment (75%), an exam (25%), and various optional bonuses which add on top of the regular activities. The bonuses are programming assignments, expanding the compiler designed in the main practical assignment (up to 1p extra). We also organize a competition, where the compiler that generates the fastest code on our benchmarks receives one bonus point. The assignments can be done in pairs.

In this course, the use of generative AI (e.g., ChatGPT, copilot, etc.) is not allowed. We perform a short individual oral examination where you have to explain your solutions and code. Failure to explain your own code leads to disqualification.

To pass the course, students have to get an average grade higher than or equal to 6 for the assignments. All the assignments have to be submitted to get a grade for this course. Deadlines for the assignments are hard deadlines; for late submissions, 1 point per week is deducted, and a submission cannot be more than 3 weeks overdue. The grade for the exam also must be 6 or higher.

Course load

Total hours of study: 168 hrs.
Practical work: 100 hrs
Lectures & Tutoring: 20 hrs
Self-tuition: 48 hrs

Reading list

Alfred V. Aho, Monica S. Lam, Ravi Sethi, Jeffrey D. Ullman, Compilers: Principles, Techniques, and Tools, second edition, Pearson New International Edition, 2013, ISBN 9781292024349.

Registration

From the academic year 2022-2023 on every student has to register for courses with the new enrollment tool MyStudyMap. There are two registration periods per year: registration for the fall semester opens in July and registration for the spring semester opens in December. Please see this page for more information.

Please note that it is compulsory to register for every exam and retake. Not being registered for a course means that you are not allowed to participate in the final exam of the course.

Extensive FAQ on MyStudymap can be found here.

Contact

Teachers of the course: Rob van Nieuwpoort and Miguel Blom for the practical.
Onderwijscoördinator Informatica bachelor: Education coordinator LIACS bachelors.

Remarks

Voor meer informatie over Brightspace kun je op deze link klikken om de handleidingen van de universiteit te bekijken. Bij overige vragen of problemen kan contact opgenomen worden met de helpdesk van de universiteit Leiden.