I'm currently building a compiler for COOL in Stanford CS143. The project is an end-to-end systems build that moves from parsing and semantic analysis toward backend and code generation work.
Right Now: Lexical Analyzers
As the first component, I'm building a lexical analyzer in C++ with Flex. The objective is to parse Cool files and output an appropriate set of tokens which are used to generate code. Flex allows us to implement a lexical analyzer by defining rules that match user-defined regexes and perform specific actions for each matched pattern. It compiles the rule file into C source code implementing a finite automaton recognizing the defined regexes.
Lexer Trace
Watching a tiny COOL snippet become tokens
This is a small representative scan of a COOL program fragment. The highlight shows the lexeme currently being matched, while the token stream on the right fills in as the lexer emits tokens.
Source
class Main inherits IO {
main(): Object {
let x: Int <- 42 in
out_string("Hello, COOL!\n");
};
};
Tokens