Compiler Design Laboratory

7. Domain Analysis

In this lab we build symbol table for the analysis and verification of identifiers. Unlike the analysis performed on a procedural language, where the main unit of analysis was the procedure or function and examine global variables, local bodies along with their routines accesses to object-oriented languages we deal with several concepts such as classes the relationship of inheritance, members: attributes and methods, polymorphism, overloading, basic types, user-defined types. It should be noted that in a real object-oriented language we deal with much more concepts such as:

7.1 The Type System

In order to declare variables, method types in a language we need a type system. The set of types is twofold: primitive types (value or expanded) and user-defined types (reference).

7.1.1 Primitive types (value)

Primitive types are: boolean, integer and integer array.

7.1.1.1 Type Boolean

Boolean type is a scalar type is represented by the keyword boolean .

7.1.1.2 Type Integer

Type a whole is a scalar type and is represented by the keyword int.

7.1.1.3 Type Integer Array

Type array of integers is a reference type and is represented by the construction int [] .

7.1.2 User-defined types (reference)

7.1.2.1 Class Type

Object class library class this or if this derby to oricarei MiniJava class. User-Defined Types User-defined types are generated by user-defined classes.

7.2 Semantic Rules

7.2.1 Rules for classes and relations between them

7.2.2 Rules for class members

7.2.3 Rules for method signatures

7.2.4 Rules for the local variables

7.2.5 Rules for types

 Type ::= id

7.2.6 Rules for instructions

Statement ::= id = exp

Statement ::= id [ exp ] = exp

7.2.7 Rules for expressions

Exp ::= new id ()

7.3 The design of the symbol table

7.3.1 The classes table

7.3.2 The types table

7.3.3 The members table

7.3.4 The formal parameters table

7.3.5 The local variables table

7.3.6 Domain analysis strategy

Firstly, we will build the AST with the help of the generated parser.

Then the visitor object will explore the AST collecting data to be added to the symbol table.

The first class to be analyzed will be the one containing the "main" static method.

Then, we will analyze the rest of the classes from the file.

During the class analysis it is possible to refer type which were not analysed yet.

They are inserted into the types table and into the classes table.

For the analysis to be complete is needed that at the end of the class analysis all entries from the class table to be completed.

Otherwise, it means that it was used a type which was not defined.

7.4 Symbol table example

We consider the following classes for an object oriented system:

class Main
{
 public static void main(String args[])
 {
  System.out.println(new TestShape().test());
 }
}

class Shape
{
 int x;
 int y;

 int print()
 {
  return 0;
 }

 int translate (int dx, int dy)
 {
  x = x + dx;
  y = y + dy;
  return 0;
 }
}

class Rectangle extends Shape
{
 int width;
 int height;

 int print()
 {
  System.out.println(x);
  System.out.println(y);
  System.out.println(widht);
  System.out.println(height);
  return 0;
 }
}

class Circle extends Shape
{
 int radius;

 int print()
 {
  System.out.println(x);
  System.out.println(y);
  System.out.println(radius);
  return 0;
 }
}

class TestShape
{
 int test()
 {
  Shape s;
  Rectangle r;
  Circle c;

  r=new Rectangle();
  c=new Circle();

  s=r;
  s=c;

  s.print();
  s.translate(1,1);
  s.print();

  c.print();
  c.translate(2,2);
  c.print();

  return 0;
 }
}

Due to the syntactical restrictions we have only one package in any analysed object-oriented system we build. The class table for the default package is the following:

IndexClass nameParent class indexReference to members table
0Object-1null
1Main0Reference to members table for class Main
2TestShape0Reference to members table for class TestShape
3Shape0Reference to members table for class Shape
4Rectangle3Reference to members table for class Rectangle
5Circle3Reference to members table for class Circle

The types table is unique and global for any analysed object-oriented system. The types table is listed next:

Index Type name Type generating class index
0 void -1
1 boolean -1
2 int -1
3 int[] -1
4 Object 0
5 Main 1
6 TestShape 2
7 Shape 3
8 Rectangle 4
9 Circle 5

At the begining of the table we added the predefined types and afterwards the user defined ones.

The member table for class "Main" is the following:

Index Member name Signature Return type Member kind Reference to formal parameter table Reference to local variables table
0 main {main,String[]} void method Reference to formal parameters tale of method "main" null

In class "Main" there is only one method which is the object-oriented system entry point.

The formal parameter table for method "main" is listed next:

Index Formal parameter name Signature Return type
0 args {String[]} String

The "main" method formal parameter lists consists in one element. This element represents the argument array of the programe.

The member table for class "Shape" is listed next:

Index Member name Signature Return type Member kind Reference to formal parameters table Reference to local variables table
0 x {x} int field null null
1 y {y} int field null null
2 print {print} int method null null
3 translate {translate,int,int} int method Reference to method "translate" table of formal parameters null

For fields the references to formal parameter table and local variable table are both null.

The formal parameters table for the method "translate" is listed next:

Index Formal parameter name Signature Type
0 dx {dx} int
1 dy {dy} int

The member table for class "Rectangle" is listed next:

Index Member name Signature Return type Member kind Reference to formal parameter table Reference to local variable table
0 width {width} int field null null
1 height {height} int field null null
2 print {print} int method null null

The member table for class "Circle" is listed next:

Index Member name Signature Return type Member kind Reference to formal parameter table Reference to local variable table
0 radius {radius} int field null null
1 print {print} int method null null

The member table for class "TestShape" is listed next:

Index Member name Signature Return Member kind Reference to formal parameter table Reference to local variable table
0 test {test} int method null Reference to the local variable table of "test" method

The local variable table for method "test" is listed next:

Index Local variable name Signature Return type
0 s {s} Shape
1 r {s} Rectangle
2 c {s} Circle

7.5 Errorneous MiniJava Code Examples

The following code sequences may be used to check the built analyzer. The examples capture most of the possible error types.

Class

class Main{ public static void main(String args[]){} }

class A extends A {}

class Main{ public static void main(String args[]){} }

class A extends B {}

class B extends A {}

class Main{ public static void main(String args[]){} }

class A extends B {}

class B extends C {}

class C extends A {}

class Main{ public static void main(String args[]){} }

class A {}

class A {}

Members

class Main{ public static void main(String args[]){} }

class A
{
 int x;

 boolean x;
}

class Main{ public static void main(String args[]){} }

class A
{
 int m() {return 0;}

 boolean m() {return false};
}

class Main{ public static void main(String args[]){} }

class A
{
 int m() {return 0;}

 boolean m() {return false};
}

class Main{ public static void main(String args[]){} }

class A
{
 int m(int x, boolean x) {return 0;}
}

Local variable

class Main{ public static void main(String args[]){} }

class A
{
 int m()
 {
  boolean x;

  int x;

  return 0;
 }
}

7.6 Assignments

1. Model a symbol table in order to perform the domain analysis upon the given rules.

Assignments time: 2 weeks