/Grace-Compiler

A full compiler for the Grace programming language

Primary LanguageJavaMIT LicenseMIT

Grace-Compiler

Project for MSc Compilers 2017 course.

A full compiler for the Grace programming language, consisting of lexical, syntax, semantic analysis, intermediate code and final code generation. Grace is a procedural programming language with Pascal-like symbol scoping, whose specification can be found in grace.pdf. The final code is based on x86 32-bit instruction set architecture. A naive approach is used for register allocation.

Instructions

  • If you are using a 64-bit Linux distribution, install the gcc-multilib package on Ubuntu (or the equivalent package for your distribution), which installs the 32-bit x86 C standard library files.
  • Compile the compiler with make
  • Compile a Grace program with ./gracec.sh [optional_arguments] <input_file>
  • Compile the assembly output file with gcc -m32 src/main/standard-library/sl.s <output_file>
  • Clean with make clean

Optional arguments

  • -ir: print an Intermediate Language Representation of the program. The method names are in the format _<name>_<scope>, with the standard library functions belonging at scope -1.
  • -O: print an Intermediate Language Representation of the program grouped by basic blocks. Specifying this argument ignores -ir.
  • -ast: print an Abstract Syntax Tree representation of the program.

Notes

  • The output file name is identical to the input file name, but with the original extension replaced by .s . If there was no extension, .s in appended at the end. Subsequently, the output file is generated in the same directory as the input file.
  • The standard library is written in C. The assembly file is generated by Makefile at the initial compiler build and it should always be linked with other object files.

Optimization

While no actual optimization is implemented at this point, the basic blocks of the program can be displayed with the -O optional argument.