Wright State University
Department of Computer Science and Engineering
CS 480/680 Comparative Language

Summer  2011                                Final Examination                                     Prasad


1.   Expression Translation (4 + 5 pts)
This problem is based on background material for the Java programming assignment. Consider the following template for a collection of Java programs (called Test), where
<expr> can be replaced with an expression derived from the grammar shown below. Note that <expr>, <term>, and <factor> are nonterminals, while  i, a, +, *, (, and ) are terminals. {} stands for Kleene star. Note also that operator precedence is implicit in the productions, and * has precedence over +.
class Test {
    static double f(
double a, int i){
       return  <expr>;
    }
    public static void main(String[] args){
       System.out.println(f(1,2.0));
    }
}
           <expr>   ->  <term>   { + <term> }
           <term>   -> <factor>  { * <factor> }
           <factor> ->  i | a  |  ( <expr> )

         

The formal argument argument a is located in register 0 and i is located in register 2. The relevant JVM instructions are:  dload_0, iload_2, i2d, dadd, iadd, dmul and imul. (Note that the method returns a double in the end.)
(i) Write the Java bytecode generated for the following arithmetic expression: 
                 i * ( i + i ) + a

(ii) Write the Java bytecode generated for the following arithmetic expression (assuming that + is left associative and * is left associative):      
     
                i * i + i + i * i


2.  Java : Polymorphism and Dynamic Binding (1 + 5 pts)

Does the following program compile without any errors? If not, correct the program "minimally" before proceeding further.

Write the output of executing the (possibly corrected) Java program indicating any run-time exceptions it may generate. (Hint: Focus on the declared type of a variable and the class associated with the object referred to by the variable.)


    class A { 
       String f(A x) { return ("  AA ");}

       String f(B x) { return ("  AB ");}
    }
    class B extends A {
 
       String f(A x) { return ("  BA ");}

       String f(B x) { return ("  BB ");}
    }
 
    class Final { 
        public static void main(String[] args) { 
            A[] arr  = {new A(), new B(), null};

            System.out.println(arr[0].f(arr[0]));
            System.out.println(arr[1].f(arr[0]));                    
            System.out.println(arr[1].f(arr[1]));

            System.out.println(arr[0].f(arr[2]));
            System.out.println(arr[2].f(arr[0]));
     
        }
 
    }


3.  Java Concepts (5*3/3*5 pts) 4.  Language Processing  ((4 +1) + (0 + 2 + 3) pts)
Study the Java program given on the reverse side carefully and answer the following questions.
        x
        x - y
        x - y - y

T. K. Prasad