Compiler Design Laboratory

8. Type Checking

8.1 Introduction

Type checking involves type inference of expressions by analyzing their AST. In principle, the type of a node is inferred based on the types of its descendants. We iterate some of the novelties that show up apart from a procedural language compiler:

It can be noticed that the OOP concepts impact with different intensity the analysis and sinthesis processes.

8.2 The redesign of the language grammar

In order to define better the semantical actions we will proceed to reorganize the grammar proposed in the first laboratory work. Some of the redesign principles are:

Obviously, this redesign represent only some guidelines and is used as a base for the further presentation.

Program ::= MainClass ClassDecl*

MainClass ::= class id { public static void main ( String [] id ) 
              { Statement }}

ClassDecl ::= class id { VarDecl* MethodDecl* }
          ::= class id extends id { VarDecl* MethodDecl* }

VarDecl ::= Type id ;

MethodDecl ::= public Type id ( FormalList ) 
               { VarDecl* Statement* return Exp ;}

FormalList ::= Type id FormalRest*
           ::= FormalRest 
           ::= ,Type id

Type ::= int []
     ::= boolean
     ::= int
     ::= id

Statement ::= { Statement* }						
          ::= if ( Condition ) Statement else Statement
          ::= while ( Condition ) Statement
          ::= System.out.println ( Exp ) ;
          ::= id= Exp ;
          ::= id[ ExpArm ]= ExpArm ;

Exp ::= Condition

Condition ::= ExpLog | ! ExpLog

ExpLog ::= ExpRel ExpLog2 | true ExpLog2 | false ExpLog2

ExpLog2 ::= OpLog ExpRel ExpLog2 | eps

OpLog ::= && | ||

ExpRel ::= (Condition ) | ExpArm OpRel ExpArm | ExpArm

OpRel ::= == | != | < | > | <= | >=

ExpArm ::= ExpTerm ExpArm2

ExpArm2 ::= OpAd ExpTerm ExpArm2 | eps

OpAd ::= + | -

ExpTerm ::= ExpFact ExpTerm2

ExpTerm2 ::= OpMul ExpFact ExpTerm2 | eps

OpMul ::= * | /

ExpFact ::= INTEGER LITERAL | ( ExpArm ) | Access

Access ::= Call [ ExpArm ] | Call

Call ::= CallTarget Call2

Call2 ::= .length | . id ( ExpList ) Call2 | eps

CallTarget ::= new int [ ExpArm ]
           ::= new id ()
           ::= id
           ::= this

ExpList ::= Exp ExpRest*
        ::=

ExpRest ::= ,Exp

8.3 Type Checking Rules

As we said earlier, the general purpose of the laboratory work is to infer type information for the nodes that form the expressions.

8.3.1 Rules for the instructions

if ( Condition ) Statement else Statement
while ( Condition ) Statement
id = Exp ;
id [ ExpArm ]= ExpArm ;

8.3.2 Rules for the logical expressions created with && and || logical operators

Exp ::= Condition
Condition ::= ExpLog | !ExpLog
ExpLog ::= ExpRel ExpLog2 | true ExpLog2 | false ExpLog2
ExpLog2 ::= OpLog ExpRel ExpLog2 | eps

8.3.3 Rules for relational expressions created with ==,!=,<,>,<=,>= relational operators

ExpRel ::= ( Condition ) |  ExpArm OpRel ExpArm | ExpArm

8.3.4 Rules for additive expressions created with +,- additive operators

ExpArm ::= ExpTerm ExpArm2
ExpArm2 ::= OpAd ExpTerm ExpArm2 | eps

8.3.5 Rules for multiplicative expressions created with *,/ multiplicative operators

ExpTerm ::= ExpFact ExpTerm2
ExpTerm2 ::= OpMul ExpFact ExpTerm2 | eps

8.3.6 Rules for factors

ExpFact ::= INTEGER LITERAL | ( ExpArm ) | Access

8.3.7 Rules for acceses

Access ::= Call [ ExpArm ] | Call

8.3.8 Rules for calls

Together with the type attribute we will also infer a message attribute for each node. The message incapsulated the name of the called member (attribute or method) and the orderred list of actual parameter types that makes the call.

Call ::= CallTarget Call2
Call2 ::= . length | . id ( ExpList ) Call2 | eps
CallTarget ::= new int [ ExpArm ] | new id () | id | this

8.3.9 Rules for expression lists

8.4 The checking strategy

For the implementation of a type checker it is recommended the use of the Visitor design pattern that helps type inference. Visiting the nodes imply semantical actions according to the previosly mentioned rules:

The structure to be returned by the verifier methods must model the note type and in the case of call nodes the call's message.

8.5 Requirements