Премиум
Overview
Compilers is often considered a capstone course for computer science majors. There is a good reason for that--compilers are tools that programmers use in their day-to-day work regardless of the application domain. Moreover, implementing a compiler touches nearly every topic of computer science ranging from the theoretical to the practical. Thus, learning more about how a compiler works makes you a more informed programmer.That said, it might not seem like writing a compiler would be directly related to the typical kinds of programming you'd do at work. This is probably true. However, writing a compiler is really an exercise in managing software complexity. Compilers have a lot of moving parts, are difficult to test, and challenging to debug. Thus, compilers a great playground for studying problems that confront all sorts of real-world software projects.
A true story: I once had a former student stop by my office to tell me how he helped save a major coding disaster involving health insurance. I asked "how'd you do that?" He said, "I used something I learned while writing a compiler."
Target Audience
This class is for more experienced programmers who have an interest in software design, data handling, type systems, and related topics. Not many programmers get the opportunity to write a compiler unless they happen to take such a course as a CS undergraduate or they enroll in graduate school. Thus, this course could be a good way to fill in gaps. If you've already taken compilers, this course may put a more practical spin on what you already learned.Syllabus
The course is structured around the goal of creating a small programming language called
Для просмотра ссылок пройдите регистрацию
. Wabbit is a small, statically typed, imperative language. You'll write a compiler that can take Wabbit code and compile it to a native executable program via LLVM.The project involves the following core problems:
- Data model. Computer programs need to be represented as a proper data structure, not as raw text. This is typically called an Abstract Syntax Tree (AST).
- Parsing. You need to parse programs by converting them from text to the data model. This involves tokenizing text and understanding grammars. You'll write a recursive descent parser from scratch.
- Program Transformation. Compilers often perform steps that transform a program into an equivalent program. For example, many advanced programming language features can be implemented using simpler features that already exist. Similarly, many compiler optimization techniques are based on program transforms.
- Type checking. You'll write a simple static program analyzer that checks the source code for type errors and other semantic problems. Time permitting, we may also discuss a few advanced topics such as Algebraic Type Systems.
- Code generation. You'll have your compiler generate code for LLVM and/or WebAssembly. Once you have this, you'll have programs that execute at native speed comparable to C programs. Time permitting, we'll discuss other possible code generation targets such as byte-code interpreters and virtual machines.
Practical Takeaways
Although you are unlikely to write a compiler in your day-to-day work, this course touches on a wide variety of practical topics that are applicable elsewhere. These include:- Text manipulation and parsing skills. These can be applied to a variety of problems such as data munging, scraping, protocol implementation and more.
- Complex data structure manipulation. Writing a compiler involves a vast array of data structures beyond your basic list or map. You'll build trees and directed graphs. You'll need to write code that navigates through those data structures--often using algorithms involving recursion. Managing the complexity is a challenge and you'll likely get ideas for how you might do it in other contexts (e.g., machine learning, networking, etc.).
- Testing. A compiler is one of the most complicated programs you will ever write, consisting of many interlocking components that interact in non-trivial ways. Figuring out how to test it is a challenge. As such, you'll be exploring different facets of unit testing, integration testing, testing oracles, assertions, contracts and more.
- Object Oriented Programming. One approach for managing the complexity in a compiler project is to utilize different object oriented programming techniques and design patterns. Although OO is not the main focus, there are many facets of the project where OO thinking might help. So, you can view it as an opportunity to practice.
- Functional Programming. Another approach for managing complexity is to adopt ideas from functional programming. Compilers are a good match for recursive algorithms, pattern matching, combinators, and other techniques.
- Programming Language Semantics. How does your favorite programming language actually work? No, I mean do you REALLY understand how it works? In order to write a compiler, you have to wrap your brain around a lot of important, often esoteric, usually subtle under-the-hood details. For example, the underlying type system, memory management, evaluation rules, call stacks, and more. After you're done writing a compiler, you'll never look at a programming language in the same way--you'll have a much greater awareness of everything.
- Deeper Computing Knowledge. The next time you're at a party and two people are about to get into a heated fisticuffs battle over tail-call optimization, you'll be able to step in and break up an ugly scene. Why? Because you took compilers, that's why. You don't need to be a computer science major to take compilers, but when you're done, you might be mistaken for one.
