A compilation project for Thrid year students of IMT Atlantique (the former Telecom Bretagne)
The specification of the project can be found here (Authorization Required)
Using git clone
git clone https://redmine-df.telecom-bretagne.eu/git/f2b304_compiler_cn
then input your Username and Password in the command prompt
The project is divided by 2 parts
- phase1: the first delivery which contains the lexical and syntax analyzer for the language minijava
- phase2: the second delivery which contains the semantic analyzer for the language minijava based on the lexical and syntax analyzer that offered by professors
to build or execute the compiler, you should entre one of these folder, cd ./phase1
or cd ./phase2
Using the shell script build
./build
or using ocamlbuild
to build the compiler
ocamlbuild Main.byte
Notes
The main file is
Main/Main.ml
, it should not be modified. It opens the given file,creates a lexing buffer, initializes the location and call the compile function of the moduleMain/compile.ml
. It is this function that you should modify to call your parser.
Using the shell script minijavac
./minijavac <filename>
or using the following command to build and then execute the compiler on the given file named <filename>
ocamlbuild Main.byte -- <filename>
By default, the program searches for file with the extension
.java
and append it to the given filename if it does not end with it.
Using the shell script test
./test
it will execute Main.byte
on all files in the directory Evaluator
If you are a team member of the project, please follow the Working Standard to make appropriate contributions
Deadline 15/01/2018
- Line Terminators
- Input Elements and Tokens
- White Space
- Comments
- Identifiers
- Keywords
- for
- while
- else
- if
- Literals
- Int
- String
- Separators
- brace
- parenthese
- dot
- comma
- semicolon
- Operators
-
=
Simple Assignment Operator -
+ - * / %
Arithmetic Operators -
+ - ++ -- !
Unary Operators -
== != > < <= >=
Equality and Relational Operators -
&& ||
Conditional Operators
-
- Keywords
- class
- static
- extends
- return
- new
- Classes
- Class Declaration
- simple class declaration
- simple class declaration with extends
- Field Declarations
- static Fields
- non-static Fields
- Method Declarations
- static Methods
- non-static Methods
- Class Declaration
Deadline 25/02/2018
- The construction of the class definition environment. This environment contains the type of methods for each class. This phase ignores the attributes (which are not visible outside the class) and the method bodies.
- create a class definition environment type called
class_env
, it contains 4 fields as follows- methods: a
Hashtbl
that maps from methode name to methode return type and argument type - constructors: a
Hashtbl
that maps from constructor name to class reference type and argument type - attributes: a
Hashtbl
that maps from attribute name to attribute type (declared type) - parent: a class reference type that refers to its class
- methods: a
- create a
Hashtbl
that maps from class
- create a class definition environment type called
- [] The second phase is concerned with verifying that the inside of classes is correct (mainly the body of methods). She will also make sure of the correction of the higher level expression.
- create 3 verification methode that verifies the following aspects of the program
-
verify_methods
that checks the type of methods- create a local definition environment type called
current_env
it contains 3 fields as follows- returntype: the declared return type of the methode
- variables: a
Hashtbl
that maps from local variable name to local variable declared type - this_class: the id of the class
- env_type: a string that identifies the type of the local definition environment, it could be
constructor
,methode
orattribute
, in this case, theenv_type
ismethode
- write a verification methode (
verify_declared_args
) that checks the declared type of variables in the methode arguments- check if there exists Duplicate Local Variable
- [] write a verification methode (
verify_statement
) that checks the body of the methode- check declared variables
- check block of statement
- check expression
- check return statement when it's none, ex:
return;
- [] check return statement when it's not none, ex:
return x;
- [] check throw statement
- [] check while statement
- [] check if statement when it doesn't have
else
- [] check if statement when it has
else
- [] check for statement
- [] check try statement
- create a local definition environment type called
-
verify_constructors
that checks the type of constructors -
verify_attributes
that checks the type of attributes
-
- create 3 verification methode that verifies the following aspects of the program
- ArgumentAlreadyExists
- AttributeAlreadyExists
- ClassAlreadyExists
- ConstructorAlreadyExists
- DuplicateLocalVariable
- IncompatibleTypes
- when constructor try to return a variable -> IncompatibleTypes("unexpected return value")
- when methode return does not contain variable -> IncompatibleTypes("missing return value")
- when methode return type does not corresponds with the declared one -> IncompatibleTypes("missing return value")
- MethodAlreadyExists
- UnknowVariable
- WrongTypesAssignOperation
- WrongTypesOperation
- errors related to overloading
- errors related to overriding
Evaluation and execute by certain means
Construction of descriptors and tables
Construction of descriptors
- [] object descriptor ? object descriptor space
- [] class descriptor ? class descriptor space
- [] methode descriptor ? methode descriptor space
Allocations
working from specail cases:
-
object -> null, true, false
-
class -> Object, Int, Boolean, String
-
[] allocation of special classes
-
[] allocation of special objects
-
[] allocation of class if it is not allocated