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

Summer 2011                                Midterm (30 pts)                                    Prasad



1.   Ambiguity in Grammars (3 + 3 pts)
When is a context-free grammar considered ambiguous? Show that the following grammar is ambiguous.  (Note that <E> is a  nonterminal, while  ?, :, and x are terminals.)
     
   <E> ::=    <E>  ?  <E> | <E> ? <E> : <E> |  x

2.  OOPL Concepts (3 + 4 + 3 pts)

  1. Explain clearly the benefits of inheritance from software engineering perspective.
  2. What is binding of methods? Explain clearly the difference between static binding and dynamic binding?
  3. C++ provides output redirection operator << which can be used to print values of a sequence of expressions as illustrated below.
	cout << "Value of myinteger " << myinteger << endl;
cout << "My string is " << mystring << " plus a null character\n" << flush;

            Would you define << as associative, left-associative, or right-associative operator? Justify your answer.

3.  Tracing Tail Recursion in Scheme (4 pts)

    (define (fast-fibonacci n)
        (fast-fibonacci-helper n 0 1))

    (define (fast-fibonacci-helper n base-0 base-1)

        (cond ((zero? n) base-0)
               ((zero? (- n 1)) base-1)
               (else (fast-fibonacci-helper (- n 1) base-1 (+ base-0 base-1)))))

   
Determine the value of (fast-fibonacci 4) after showing all the intermediate function calls with actual argument values and returned results.

4.  Linear Recursion in Scheme (4 pts)
Write a function exch, which takes a list of symbols sLis, and two symbols say p and q, and produces another list containing all top level occurrences of p replaced by q and
all top level occurrences q replaced by p  in sLis. E.g.,
    (exch '(ca b cc d ca ra) 'ca 'ra)
    > (ra b cc d ra ca)
    (exch '(b b a d) 'a 'b)
    > (a a b d)



5.  Double Recursion in Scheme (5 + 1 pts)
 Write a function exch-all which takes a nested list of symbols LLis, and
two symbols say p and q, and produces another nested list containing all occurrences of p replaced by q and all occurrences q replaced by p in LLis.
E.g.,
    (exch-all '(a b c) 'a 'c)
    > (c b a)
    (exch-all '(a ((((bat))))  (a1 b2 (ee) cat) dog) 'bat 'cat)
    >
(a ((((cat))))  (a1 b2 (ee) bat) dog)
 

Is it possible that the number of occurrences of a symbol in a nested list be greater than its length? Why or why not?