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;
      }
    }

     

  2. (10) For the MiniJava abstract syntax, what methods need to be implemented for building the symbol table?
  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;
       
    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*;