Exercise 5        Name __________________        Score __/30

  1. (10) Examine the SymbolTable package containing SymbolTable, Class, Method and Variable classes. Diagram the organization of the class, fields, methods, parameters, and local variables of the following program.

     
    class ex5 {
       public static void main(String [] args) {
         System.out.println((new B()).bm(10, 20, 30));
       }
    }

    class A {
      int aglobal1;
      int aglobal2;
      public int am1(int am1parm1, int am1parm2, int am1parm3) {
       int am1local1;
       int am1local2;
       return aglobal1;
      }
      public int am2(int am2parm1, int am2parm2, int am2parm3) {
        int am2local1;
        int am2local2;
        return aglobal1;
      }
    }

    class B extends A {
      int bglobal;
      public int bm(int bparm1, int bparm2, int bparm3) {
        int bmlocal1;
        int bmlocal2;
        bmlocal2 = 40;
        return bmlocal2;
      }
    }
    Classes

    B

                Fields

                            bglobal            class minijava.node.AIntType

                Methods

                            bm       class minijava.node.AIntType

                                        Parameters

                                                    bparm1            class minijava.node.AIntType

                                                    bparm2            class minijava.node.AIntType

                                                    bparm3            class minijava.node.AIntType

                                        Variables

                                        bmlocal1         class minijava.node.AIntType
                                        bmlocal2         class minijava.node.AIntType

    ex5

                Fields

                Methods

    A

                Fields

                            aglobal1          class minijava.node.AIntType

                            aglobal2          class minijava.node.AIntType

                Methods

                            am2     class minijava.node.AIntType

                                        Parameters
                                                    am2parm1       class minijava.node.AIntType

                                                    am2parm2       class minijava.node.AIntType

                                                    am2parm3       class minijava.node.AIntType
                                        Variables

                                        am2local2       class minijava.node.AIntType

                                        am2local1       class minijava.node.AIntType

                            am1     class minijava.node.AIntType

                                        Parameters
                                                    am1parm1       class minijava.node.AIntType

                                                    am1parm2       class minijava.node.AIntType

                                                    am1parm3       class minijava.node.AIntType

                                        Variables

                                        am1local2       class minijava.node.AIntType

                                        am1local1       class minijava.node.AIntType

     

  2. (10) For the MiniJava abstract syntax, what methods need to be implemented for building the symbol table?

    Basically any declaration must be placed in the symbol table along with its declared type:
    Abstract Syntax Tree
     
    program = main_class class_decl*;
    main_class = [classname]:id [arg]:id statement;
    class_decl =
         id var_decl* method_decl* |
                 {extends} [classname]:id
                                 [extend]:id var_decl* method_decl*;
    var_decl = type id;
    method_decl = type id formal_list var_decl* statement* exp;
    formal_list = type id formal_rest* | {empty};
    formal_rest = type id;
     
    type=   {int_array} |
                {boolean}   |
                {int}        |
                {id} id;
    public void caseAProgram(AProgram node)
    public void caseAMainClass(AMainClass node)
    public void caseAClassDecl(AClassDecl node)
    public void caseAExtendsClassDecl(AExtendsClassDecl node)
    public void caseAVarDecl(AVarDecl node)
    public void caseAMethodDecl(AMethodDecl node)
    public void caseAFormalList(AFormalList node)

    public void caseAFormalRest(AFormalRest node)

    public void caseAIntType(AIntType node)
    public void caseABooleanType(ABooleanType node)
    public void caseAIntArrayType(AIntArrayType node)
    public void caseAIdType(AIdType node)
  3. (10) For the MiniJava abstract syntax, what methods need to be implemented for type checking the following?
    1. 5+4
    2. M(3,2);
    3. true && false
    4. if true then x=5 else x=3;

    Any use of a symbol (all must be declared) or literal in an expression or statement, the declarations must also be implemented in order to apply type checking to method declarations which include statements and expressions. Every rule in the grammar must be implemented in type checking.
     

    Abstract Syntax Tree
     
    program = main_class class_decl*;
    main_class = [classname]:id [arg]:id statement;
    class_decl =
         id var_decl* method_decl* |
                 {extends} [classname]:id
                                 [extend]:id var_decl* method_decl*;
    var_decl = type id;
    method_decl = type id formal_list var_decl* statement* exp;
    statement =
                 {statementlist} statement* |
                 {if} exp [true]:statement [false]:statement |
                 {while} exp statement |
                 {println} exp |
                 {assign} id exp |
                 {array_assign} id [index]:exp [r]:exp;
    formal_list = type id formal_rest* | {empty};
    formal_rest = type id;
     
    type=   {int_array} |
                {boolean}   |
                {int}        |
                {id} id;
    exp =    {and}   [l]:exp [r]:exp |
                 {lt}    [l]:exp [r]:exp |
                 {plus}  [l]:exp [r]:exp |
                 {minus} [l]:exp [r]:exp |
                 {times} [l]:exp [r]:exp |
                 {length} exp                   |
                 {arrayindex} [array]:exp [index]:exp |
                 {methodcall} exp id exp_list |
                 {integerliteral} integer_literal |
                 {trueliteral} true |
                 {falseliteral} false |
                 {id} id |
                 {this} this |
                 {new} id |
                 {newarray} exp |
                 {not}     exp ;
    exp_list = exp*;