CS 364 - Exam 2 March 31, 2009

  1. Consider the following expression grammar that supports exponentiation ^ and multiplication *.
       Expr  Expr ^ Expr 
       Expr  Expr * Expr 
       Expr  ( Expr ) | Identifier | IntLit 
    The exponentiation operator ^ is left associative and has higher precedence than multiplication. Multiplication is left associative. The expression x^y^z*w is equivalent to (x^(y^z))*w
    1. [5] Show that this grammar is ambiguous.
    2. [15] Rewrite the grammar so that it removes all ambiguity by correctly handling associativity and precedence. Do not use the Extended BNF iteration operator { }.
  2. [5] In the CliteExam2 grammar what is the first set of the non-terminal Statements? Briefly explain (one sentence) why first-sets are useful when parsing.
  3. [20] Develop a Java class hierarchy suitable for representing the abstract syntax of CliteExam2 statements beginning with the non-terminal Statements. You do not have to represent Clite expressions just statements. You may assume that there is an Expression class you can refer to.
  4. [15] Write a function Statements() that parses CliteExam2 statements starting with the non-terminal Statements and constructs an AST defined in the previous question. Don't worry about error checking. You may assume that reasonable lexer methods such as nextToken() and kind() are available to you.
  5. Consider the Java class definition below.
        class Exam2 {
          int a;
          double b;
        
          void f() {
            int c = 7;
            boolean d;
            // point 1
          }
        
          void g() {
             int e;
             double x, y;  
             // point 2
          }
        
          void main() {
             g();
          }
        }
        
    1. [5] Define a suitable Java data structure (a symbol table) that could be used to keep track of symbols while the program is being parsed.
    2. [3] Draw the symbol table at point 1
    3. [3] Draw the symbol table at point 2
  6. [10] Use set notation to describe the set that the following class Line defines.
     
            class Point { int x; int y; }
            class Line { Point end1; Point end2; }
        
  7. [10] Briefly explain the difference between weak typing and strong typing. Address the strengths and weaknesses of each.
  8. [5] Briefly describe two different language features Java has that supports user defined types.
  9. [5] Write a short Java code segment that exhibits a run-time type error.