L-Compiler Project
Building a compiler from scratch, as final project for Compilation course.
- The compiler compile and run the made up language L (which is somwhere between C and Java).
- The implementation was done mainly on Java, and the generated code is for MIPS architecture.
How does the compiler work?
- Our compiler performs:
- Lexical Analysis: Using
jlex
(Added in ex1).
- Syntax Analysis: Using
cup
(added in ex2).
- Semantic Analysis (added in ex3).
- MIPS-code generation (added in ex4).
- Execution: The generated MIPS code is then run using
SPIM
(MIPS simulator).
Environment Setup
- Requirements: Making the compiler should be done in Linux and requires the following dependencies:
- Ubuntu
- Java 8
- Install
JFlex
- Install
Cup
- Install
SPIM
- $ sudo apt-get install spim xspim
.
Usage - Compile & Run
In the following steps we'll demonstrate compiling and running the L source file code.l
and write its output to code_output.txt
.
- Compile the compiler: Make the compiler (from
ex4
dir):
$ make compile
- The compiler is now saved as binary named
COMPILER
.
- Once the compiler is created it can be reused, and this step may not be repeated.
- Compile with the compiler: We may use
COMPILER
to compile L code files.
$ java -jar COMPILER code.l compiler_run.log
- This run creates two artifaces -
compiler_run.log
which logs the latest compilation, and MIPS.txt
which contains the generated code.
- Run the L program: Now we use MIPS to run the generated MIPS code.
$ mips -f MIPS.txt #> code_output.txt
- Enjoy your code output in
code_output.txt
. You've just brought L to life!
Files
- The directories
ex{1,2,3}
are intermediate versions of ex4
, which contains the final project.