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

Fall 2011                               Midterm                                     Prasad


1.   Ambiguity in Grammars (3 + 3 pts) 
Is it true that a context-free grammar is ambiguous when the same string has multiple derivations? Justify your answer.
Show that the following grammar is ambiguous. (Note that <EXPR> is a  nonterminal, while 
if, then, else, and b are terminals.)
     
   <EXPR> ::=    if <EXPR> then <EXPR>  if <EXPR> then <EXPR> else <EXPR> | b

2.   Expression Translation (4 + 4 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 operator precedence is implicit and + has precedence over *.

class Test {
    static double f(int i, double a){

       return  <expr>;
    }
    public static void main(String[] args){

       System.out.println(f(33,2.2));
    }
}

       <expr>   ->  <term>   { * <term> }
       <term>   -> <factor>  { + <factor> }

       <factor> ->  <var>  |  ( <expr> )

        <var>   ->  i | a  

The formal argument i is located in register 0 and argument a is located in register 1. The relevant MSIL instructions are: ldarg.0, ldarg.1, conv.r8, add, and mul.
(i) Write the MSIL code generated for the following arithmetic expression:
 
                 a + (i + i) * a

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

3.  OOPL Concepts (3 + 3 pts)
What are the benefits of (i) inheritance and (ii) data abstraction? 

4.  Recursive Definition in Scheme (4 pts)
Write a function init, which takes a list of symbols L, and produces a list of all (top-level) symbols of L except the last one. The input must be a non-empty list (precondition). E.g.,
(init  '(a b c d))
> (a b c)

5.  Recursive Definition in Scheme (5 + 1 pts)
Write a function duplicate, which takes a nested list of symbols L, and produces a nested list of symbols by "immediately duplicating" each symbol at all levels. E.g.,
(duplicate '(a b c))
> (a a b b c c)
(duplicate '( (a) b ((c)) )
>( (a a) b b ((c c)) )
Is the length of the list (duplicate L) always greater than the length of the list L? Justify your answer.