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
cout << "Value of myinteger " << myinteger << endl;Would you define << as associative, left-associative, or right-associative operator? Justify your answer.
cout << "My string is " << mystring << " plus a null character\n" << flush;
3. Tracing Tail Recursion in Scheme (4
pts)
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?