Implementing a compiler for tiger
as described in the book Modern Compiler Implementation in ML by Andrew Appel. This implementation uses OCaml as opposed to SML as I thought that it'd be easier to be pull this off in OCaml.
- Lexing
- Parsing to AST
- Semantic analysis
- Production of activation records
- Translation to intermediate representation (IR)
- Producing basic blocks and traces from IR
- Instruction selection (Risc-V)
A few steps need to be taken before the code can actually be run (this is only necessary if certain files are edited). All of the below operations are idempotent. A ./run_postprocessing.sh
bash file is included to run all of the below steps.
# Remove extraneous lines (tests dont work otherwise)
ocamllex lib/lexer.mll && sed -i '' "/^#.*$/c\\" lib/lexer.ml
This generates lib/lexer.ml
(this file is typically checked into the repository).
ocamlyacc lib/grammar.mly && sed -i '' "/^#.*$/c\\" lib/grammar.ml
# Additional something that we need to add to the mli file
cat lib/grammar.mli_ >> lib/grammar.mli
This generates lib/grammar.ml
(this file is typically checked into the repository).
To run unit tests
dune runtest
This commands compiles an input tiger
file and produces a Risc-V assembly file.
dune exec bin/main.exe -- input.tig -o output.s