next up previous
Next: Writing a MOOL compiler Up: The Assignment Previous: The Objective

The MOOL language

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. 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 $ \longmapsto$ ClassDeclarationSequence MainBody
ClassDeclarationSequence $ \longmapsto$ ClassDeclaration OtherClasses
OtherClasses $ \longmapsto$ ClassDeclarationSequence | $ \varepsilon$
ClassDeclaration $ \longmapsto$ class ClassName {
    AttributeDeclarationSequence
    MethodDeclarationSequence
    }
AttributeDeclarationSequence $ \longmapsto$ AttributeDeclaration OtherAttributes
OtherAttributes $ \longmapsto$ AttributeDeclarationSequence | $ \varepsilon$
AttributeDeclaration $ \longmapsto$ Type AttributeName ;
Type $ \longmapsto$ ClassName
Type $ \longmapsto$ int
ClassName $ \longmapsto$ Id
AttributeName $ \longmapsto$ id
MethodDeclarationSequence $ \longmapsto$ NewDeclaration OtherMethods
NewDeclaration $ \longmapsto$ new ( FormalParamterList ) {
    MethodBody
    }
OtherMethods $ \longmapsto$ MethodDeclaration OtherMethods | $ \varepsilon$
MethodDeclaration $ \longmapsto$ MethodName ( FormalParamterList ) {
    MethodBody
    }
MethodName $ \longmapsto$ id
FormalParameterList $ \longmapsto$ FormalParameter , FormalParamterList
FormalParamterList $ \longmapsto$ FormalParameter | $ \varepsilon$
FormalParameter $ \longmapsto$ Type ParameterName
ParamterName $ \longmapsto$ id
MethodBody $ \longmapsto$ Body return ArithmeticExpression
MainBody $ \longmapsto$ main {
    Body
    }
We make the following observations. 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 $ \longmapsto$ E
E $ \longmapsto$ E + T
E $ \longmapsto$ E - T
E $ \longmapsto$ T mod T
E $ \longmapsto$ T
T $ \longmapsto$ T * F
T $ \longmapsto$ F
F $ \longmapsto$ ( E )
F $ \longmapsto$ num
F $ \longmapsto$ id
BooleanExpression $ \longmapsto$ B
B $ \longmapsto$ E = E
B $ \longmapsto$ E $ \bf <>$ E
B $ \longmapsto$ E $ \bf <$ E
B $ \longmapsto$ E $ \bf <=$ E
B $ \longmapsto$ B and B
B $ \longmapsto$ B or B
B $ \longmapsto$ ( B )
B $ \longmapsto$ not B
The meaning of operators +, -, mod, *, =, $ \bf <$, $ \bf <=$, and, or and not are as usual. The operator $ \bf <>$ 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 $ \longmapsto$ StatementSequence
StatementSequence $ \longmapsto$ Statement ; StatementSequence | $ \varepsilon$
Statement $ \longmapsto$ Assignment
Statement $ \longmapsto$ Alternative
Statement $ \longmapsto$ WhileLoop
Statement $ \longmapsto$ MethodCall
Statement $ \longmapsto$ writeln ( ArithmeticExpression )
Assignment $ \longmapsto$ LeftValue := RightValue
LeftValue $ \longmapsto$ id
LeftValue $ \longmapsto$ AttributeCall
RightValue $ \longmapsto$ LeftValue
RightValue $ \longmapsto$ ObjectCreation
RightValue $ \longmapsto$ MethodCall
RightValue $ \longmapsto$ ArithmeticExpression
AttributeCall $ \longmapsto$ CalledObject $ \bf\cdot$ AttributeName
CalledObject $ \longmapsto$ this | id
ObjectCreation $ \longmapsto$ ClassName ( CallingParameterList )
MethodCall $ \longmapsto$ CalledObject $ \bf\cdot$ MethodName ( CallingParameterList )
CallingParameterList $ \longmapsto$ CallingParameter , CallingParameterList
CallingParameterList $ \longmapsto$ CallingParameter | $ \varepsilon$
CallingParameter $ \longmapsto$ LeftValue
CallingParameter $ \longmapsto$ ArithmeticExpression
CallingParameter $ \longmapsto$ this
Alternative $ \longmapsto$ if Test then IfTrue else IfFalse
Alternative $ \longmapsto$ if Test then IfTrue
Test $ \longmapsto$ ( BooleanExpression )
IfTrue $ \longmapsto$ { StatementSequence }
IfFalse $ \longmapsto$ { StatementSequence }
WhileLoop $ \longmapsto$ while Test repeat IfTrue
We make the following observations. Some comments about the terminal symbols of the grammar.


next up previous
Next: Writing a MOOL compiler Up: The Assignment Previous: The Objective
Marc Moreno Maza
2004-12-01