Next: Writing a MOOL compiler
Up: The Assignment
Previous: The Objective
A MOOL program Program consists of
a sequence of class definitions followed by a body of instructions.
An example of such program is
class Pair {
int left;
int right;
new (int l, int r) {
this.left := l;
this.right := r;
return 0;
}
writeln() {
writeln(this.left);
writeln(this.right);
return 0;
}
}
main {
p := Pair(1, 2);
p.writeln();
q := Pair(2, 1);
q.writeln();
}
Then a possible intermediate representation could be:
struct Pair
{
int left;
int right;
};
struct Pair *new_Pair(int l, int r)
{
struct Pair *tmp_Pair = (struct Pair *) malloc(sizeof(struct Pair));
(tmp_Pair) -> left = l;
(tmp_Pair) -> right = r;
return (tmp_Pair);
};
int writeln_Pair(struct Pair *tmp_Pair)
{
int tmp_int_0 = 0;
printf("%d \n", (tmp_Pair) -> left );
printf("%d \n", (tmp_Pair) -> right);
return (tmp_int_0);
};
int main()
{
int tmp_int_1 = 1;
int tmp_int_2 = 2;
int tmp_int_3 = 2;
int tmp_int_4 = 1;
struct Pair *p = new_Pair(tmp_int_1, tmp_int_2);
struct Pair *q = new_Pair(tmp_int_3, tmp_int_4);
writeln_Pair(p);
writeln_Pair(q);
return 0;
};
Let us comment the above MOOL program.
- The declaration of a MOOL class is similar to that of a JAVA class.
The members of a MOOL class are either attributes or methods.
- However, on the contrary of JAVA
- all attributes of a MOOL class are private and none can be static,
- all methods are public and none can be static.
- There is no inheritance in the MOOL language.
- A class must be completely defined before being used in another class.
- A method must be completely defined before being called by another method.
- As in JAVA a new method create a new object.
This is done by assigning each of its attributes.
If an object is not properly defined (for instance
if one of its attribute is not initialized)
then an error should be generated at compile time.
Observe that the creation of an object has the form
ClassName (
CallingParameterList )
rather than new ClassName (
CallingParameterList )
as in JAVA.
- Every method of a MOOL class returns an integer value,
except the new methods which return a (newly created) object.
This integer value can be seen as an error code (like the integer
value returned by the main function of a C program).
Of course it can be used for getting data from an object.
- The printing of objects is performed by a method whose name
must be writeln and which has no arguments.
The primitive type int supports a writeln procedure
which takes the integer as parameter.
Now let us describe the syntax of the MOOL language in more detail.
We start with the top level, that is from Program to function or bloc Body.
Program |
|
ClassDeclarationSequence MainBody |
ClassDeclarationSequence |
|
ClassDeclaration
OtherClasses |
OtherClasses |
|
ClassDeclarationSequence |
|
ClassDeclaration |
|
class ClassName { |
|
|
AttributeDeclarationSequence |
|
|
MethodDeclarationSequence |
|
|
} |
AttributeDeclarationSequence |
|
AttributeDeclaration
OtherAttributes |
OtherAttributes |
|
AttributeDeclarationSequence |
|
AttributeDeclaration |
|
Type
AttributeName ; |
Type |
|
ClassName |
Type |
|
int |
ClassName |
|
Id |
AttributeName |
|
id |
MethodDeclarationSequence |
|
NewDeclaration
OtherMethods |
NewDeclaration |
|
new (
FormalParamterList ) { |
|
|
MethodBody |
|
|
} |
OtherMethods |
|
MethodDeclaration
OtherMethods |
|
MethodDeclaration |
|
MethodName (
FormalParamterList ) { |
|
|
MethodBody |
|
|
} |
MethodName |
|
id |
FormalParameterList |
|
FormalParameter ,
FormalParamterList |
FormalParamterList |
|
FormalParameter |
|
FormalParameter |
|
Type
ParameterName |
ParamterName |
|
id |
MethodBody |
|
Body return
ArithmeticExpression |
MainBody |
|
main { |
|
|
Body |
|
|
} |
We make the following observations.
- A MOOL program contains at least one class declaration.
- A MOOL class possesses at least one attribute
and at least one method called new.
In a MOOL class, all the attributes must be declared before the methods.
- The MOOL language possesses a unique basic (= primitive) type which is int
standing for the C type with the same name.
Therefore all the attributes of the first class defined in a MOOL program
are of type integers and MOOL classes are essentially records!
We continue the description of the syntax of the MOOL language
by giving its low level, that is the grammar of arithmetic expressions
and boolean expressions.
ArithmeticExpression |
|
E |
E |
|
E + T |
E |
|
E - T |
E |
|
T mod T |
E |
|
T |
T |
|
T * F |
T |
|
F |
F |
|
( E ) |
F |
|
num |
F |
|
id |
BooleanExpression |
|
B |
B |
|
E = E |
B |
|
E E |
B |
|
E E |
B |
|
E E |
B |
|
B and B |
B |
|
B or B |
B |
|
( B ) |
B |
|
not B |
The meaning of operators +, -, mod, *, =, ,
, and, or and not are as usual.
The operator stands for the different predicate.
The terminal num stands for integer values.
We conclude the description of the syntax of the MOOL language.
by giving its middle level, that is the grammar of bodies
(method bodies and main body) in terms of
ArithmeticExpression and
BooleanExpression.
Body |
|
StatementSequence |
StatementSequence |
|
Statement ;
StatementSequence |
|
Statement |
|
Assignment |
Statement |
|
Alternative |
Statement |
|
WhileLoop |
Statement |
|
MethodCall |
Statement |
|
writeln (
ArithmeticExpression ) |
Assignment |
|
LeftValue :=
RightValue |
LeftValue |
|
id |
LeftValue |
|
AttributeCall |
RightValue |
|
LeftValue |
RightValue |
|
ObjectCreation |
RightValue |
|
MethodCall |
RightValue |
|
ArithmeticExpression |
AttributeCall |
|
CalledObject
AttributeName |
CalledObject |
|
this | id |
ObjectCreation |
|
ClassName (
CallingParameterList ) |
MethodCall |
|
CalledObject
MethodName (
CallingParameterList ) |
CallingParameterList |
|
CallingParameter ,
CallingParameterList |
CallingParameterList |
|
CallingParameter |
|
CallingParameter |
|
LeftValue |
CallingParameter |
|
ArithmeticExpression |
CallingParameter |
|
this |
Alternative |
|
if Test then IfTrue else IfFalse |
Alternative |
|
if Test then IfTrue |
Test |
|
(
BooleanExpression ) |
IfTrue |
|
{
StatementSequence } |
IfFalse |
|
{
StatementSequence } |
WhileLoop |
|
while Test repeat IfTrue |
We make the following observations.
- Each MOOL statement is terminated by a ;.
- An assignment can be of one the following two forms
- id :=
RightValue which means that a variable is assigned
to a integer value or refers to an object.
-
AttributeCall :=
RightValue which means that an attribute of an object
is assigned to an integer value or refers to an object.
where
RightValue can be id,
AttributeCall,
ObjectCreation,
MethodCall.
As in JAVA the name this
- can only appear in an
MethodBody
- and refers to this object to which the method is applied.
On the contrary of JAVA the expression
this
AttributeName
cannot be simplified into
AttributeName.
- Since all attributes are private, an expression of the form
CalledObject
AttributeName
can only appear
in the definition of the class to which
CalledObject belongs.
- Let us assume that a MOOL program defines
a class A and then a class B.
Assume that in class B an object a of class A
is defined.
Then, the only way to use a is by
- applying a method of class A to a or by
- calling a method of class B which takes an object of type A
among its parameters.
- MOOL variables are declared when they are initialized.
There is no preliminary declaration at the beginning of a scope.
Moreover a variable is visible
- only after its initialization
- in the body where it is initialized
- and in the sub-bodies (appearing after the initialization) of this body.
Some comments about the terminal symbols of the grammar.
- Id is the token for class identifiers. These can be any alphanumeric strings
starting with a capital letter.
- id is the token for identifiers denotaing attributes, methods and variables.
These can be any alphanumeric strings
starting with a lower case letter.
- new, class, int, main, return, mod
and, or, not, this, if, then, else,
while, repeat are reserved keywords. They cannot be
used as an identifier.
- num is the token for integers. You can restrict yourselves to unsigned integers.
Next: Writing a MOOL compiler
Up: The Assignment
Previous: The Objective
Marc Moreno Maza
2004-12-01