This project is a Grace programming language Compiler, developed at Spring of 2017 by Chrysinas Ioannis and Kamaras Georgios as a semester-long group-project for Compilers course. The project's development is going to progress in three stages, during the course of the semester:
- The first stage (Parts I and II) involves the creation of the compiler's Lexical and Syntactical Analyser.
- The second stage (Parts III and IV) involves creating the compiler's Semantical Analyser and generating Intermediate Code.
- The third, and final, stage (Parts V and VI -- current stage) is about Optimization and Program Execution.
Stable Version for second stage (Parts III and IV) of the project
Our compiler is currently in the beginning of the third stage of it's development. For now, it only has complete Lexing, Parsing, Semantic Analysing and Intermediate Code production capabilities. It processes a program, given to it's script (grace_compiler.sh) as a command line argument and produces an accurate Low Level Intermediate Representation which in a large part can easily be transformed to assembly x8086 commands. However, the produced assembly code most of the time is not accurate or complete. (But, works for hello.grace)
Important Note Due to time constraints regarding other courses' assignments the produced assembly code most of the time is not accurate or complete. (But, works for hello.grace)
Important Files
- at
src/main/java/compiler/Main.java
we have our Main class from which we read the Grace code from a text file (.grace, .txt, etc) passed as an argument in the execution command or from the stdin, if no argument was given, we process it to spot Lexer, Parser or IO Exceptions that it may create and we load it's Syntactical components in a Parsing Tree, which we print in the end. - at
src/main/java/compiler/PTPrintingVisitor.java
we have our PTPrintingVisitor class, in which we use the Visitor Pattern to traverse and print our Parsing Tree (PT) in an efficient and easily maintainable way. - at
src/main/sablecc/parser.grammar
we have our Grammar's file, created using the SableCC Framework. - at
src/main/java/compiler/ASTPrintingVisitor.java
we have our ASTPrintingVisitor class, in which we use the Visitor Pattern to traverse and print our Abstract Syntax Tree (AST) in an efficient and easily maintainable way. - at
src/main/java/compiler/SymbolTable.java
we have our SymbolTable class, which is the core of our Symbol Tables implementation. To improve our project structure we have included many utility functions and structures, which can be found atsrc/main/java/compiler/STRecord.java
andsrc/main/java/compiler/NSRecord.java
. - at
src/main/java/compiler/IntermediateCode.java
we have our IntermediateCode class, which is the core of our LLIR code production infrastructure implementation. - at
src/main/java/compiler/MachineCode.java
we have our MachineCode class, which is the core of our Assembly x8086 code production infrastructure implementation. - at
src/main/java/compiler/ActivationRecord.java
we have our ActivationRecord class, which can be used to simulate the Activation Records exchanges that take place in the background while running our program. It has not been used in our implementation, but it is a nice utility, so we included it. - with
grace_compiler.sh <input> [flags]
our Grace compiler can be used in a variety of ways illustrated at To use the Grace Compiler section of this document. Careful, a chmod command likechmod 777
may need to be applied on the script to make it runnable to your computer.
Grace is a simple imperative programming language. Her main attributes, in short, are the following:
- Simple structure and syntax of commands and expressions, which really resembles C.
- Basic data types for characters, integers and arrays.
- Simple functions, pass by value and by reference.
- Variables scopes like Pascal.
- Library of functions.
- Linux Ubuntu 16.04 (and Linux Mint)
- Apache Maven 3.3.9
- Java version: 1.8.0_121, vendor: Oracle Corporation
- Default locale: en_US, platform encoding: UTF-8
- OS name: "linux", version: "4.4.0-72-generic", arch: "amd64", family: "unix"
Having maven
and java
installed.
Build compiler and execute up-to Assembly x8086 production
./grace_compiler.sh <name of .grace file>
Build compiler and execute up-to Low Level Intermediate Representation production
./grace_compiler.sh <name of .grace file> --upto-ir
Just execute up-to Assembly x8086 production (no rebuild)
./grace_compiler.sh <name of .grace file> --no-rebuild
RECOMMENDED BASED ON OUR LEVEL OF PROGRESS
Just execute up-to Low Level Intermediate Representation production (no rebuild)
./grace_compiler.sh <name of .grace file> --no-rebuild --upto-ir
Build compiler and execute up-to Assembly x8086 production
./grace_compiler.sh examples/general/hello.grace
Build compiler and execute up-to Low Level Intermediate Representation production
./grace_compiler.sh examples/general/hello.grace --upto-ir
Just execute up-to Assembly x8086 production (no rebuild)
./grace_compiler.sh examples/general/hello.grace --no-rebuild
RECOMMENDED BASED ON OUR LEVEL OF PROGRESS
Just execute up-to Low Level Intermediate Representation production (no rebuild)
./grace_compiler.sh examples/general/hello.grace --no-rebuild --upto-ir
Important Notes
The name (or the path, if not in the same directory) of the file of grace code that we want to compile, must be included in the execution's command, like we do in other compilers, e.g. the gcc. In our example, the grace-code file can be found in the path
examples/general/hello.grace
.
Intermediate Representation:
Low Level Intermediate Representation:
1: unit, hello, -, -
2: par, "Hello world!\n", R, -
3: call, -, -, puts
4: endu, hello, -, -
Hello world!
Please see the AUTHORS
file for Contact & Feedback details.