background gridbackground grid
Back

While language

December 2022

Documentation here

🧱 While Compiler

The goal of this project is to create a new programming language and a compiler for it. The language is called While and is a simple imperative language. The compiler is written in Java and compile to Python.

This project has been realised by a group of 4 students of ESIR. The goal of this project is to learn how works and how to write a compiler.

⭐ Features

🧪 Example

This is an example of a While program that compute the the double of a number.

function double :
read X
%
  for X do
    X := (cons nil X)
  od
%
write X

function main:
read UserInput
%
  Output := (cons int (double UserInput))
%
write Output

Note : Standard input and output are available through the main function. Thus the main function is mandatory.

🔨 Build the compiler jar

Requirements

Build the jar

mvn clean
mvn package

The jar is generated in the target folder as whilec.jar.

📟 Run the compiler

The goal of the whilec.jar is to compile a While program into a Python program.

java -jar whilec.jar [filepath] [...arguments?] [...options?]

Example

java -jar target/whilec.jar sample/good/double.while 5 -x -v -o

Output

###########################################
### STARTING VALIDATION OF double.while ###
###########################################

###########################
###  VALIDATION REPORT  ###
###  0 ERROR(S) FOUND   ###
###########################

File output.py generated at /Project_TLC/output.py
Output value 0 :

  _CONS_____
 /          \
int      _CONS_____
        /          \
       NIL      _CONS_____
               /          \
              NIL      _CONS_____
                      /          \
                     NIL      _CONS_____
                             /          \
                            NIL      _CONS_____
                                    /          \
                                   NIL      _CONS_
                                           /      \
                                          NIL    NIL

    as Integer:  7
    as Boolean:  1
     as String:  "int"

  Pretty print:  6

📂 Project Structure

📦while-lang
 ┣ 📂doc                  // Documentation
 ┣ 📂grammar              // ANTLR grammar
 ┃ ┗ 📜AST.g
 ┣ 📂resources            // Runtime target libraries
 ┃ ┣ 📜base.py            // Default python runtime
 ┃ ┗ 📜base_verbose.py    // Verbose python runtime
 ┣ 📂src/main/java
 ┃ ┣ 📂runner             // Main program
 ┃ ┣ 📂AST                // Generated AST
 ┃ ┣ 📂C3A                // Generated 3AC
 ┃ ┣ 📂translate          // Generated target from 3AC
 ┃ ┗ 📂Validation         // Validation system
 ┃ ┗ 📂Optimization       // 3AC code optimization system
 ┣ 📂sample               // Sample while files
 ┃ ┣ 📂bad                // Should raise compiler error
 ┃ ┗ 📂good               // Should compile
 ┣ 📜.gitignore
 ┣ 📜ASTPrinted.puml      // AST diagram PlantUML for debugging
 ┣ 📜README.md
 ┣ 📜generate_grammar.sh  // Generate ANTLR grammar
 ┗ 📜pom.xml              // Maven project configuration