Review Problems for Exam 1            Version 1.05


Problem 1) For the following binary tree, draw a sketch of the tree
and write down its pre-order, in-order, and post-order traversals.
     (a (b c (d e f)) (g h (i j k)))


Problem 2) For the following tree, draw a sketch of the tree
and write down its pre-order and post-order traversals.
     (1 2 3 (4 (5 6)) (7 (8 9 10) 11) 12)


Problem 3) Using the BTree constructor, write a single Java expression that
instantiates the following binary tree. Your Java expression should be written
in a clear way so that it is pretty obvious what the tree's structure is.
      (a (b c (d e f)) (b c ()))


Problem 4) Using the Tree constructor, write a single Java expression that
instantiates the following tree. Your Java expression should be written
in a clear way so that it is pretty obvious what the tree's structure is.
      (1 2 3 (4 (5 6)) (7 8 9) 10)


Problem 5) Describe what each of the following (recursive) functions computes.
Briefly explain why.

 1   public static int h(Tree tree)
 2   {
 3      if ( 1 >= tree.depth() )
 4      {
 5         return tree.degree();
 6      }
 7      else
 8      {
 9         int temp = tree.degree();
10         for (int n, i = 0; i < tree.degree(); i++)
11         {
12            if (temp < (n = h(tree.getSubTree(i))))
13               temp = n;
14         }
15         return temp;
16      }
17   }


 1   public static int h(Tree tree)
 2   {
 3      if ( 1 >= tree.depth() )
 4      {
 5         return 1 + tree.degree();
 6      }
 7      else
 8      {
 9         int temp = 1;
10         for (int i = 0; i < tree.degree(); i++)
11         {
12            temp += h(tree.getSubTree(i));
13         }
14         return temp;
15      }
16   }


 1   public static int h(Tree tree)
 2   {
 3      if ( 0 == tree.depth() )
 4      {
 5         return 1;
 6      }
 7      else
 8      {
 9         int temp = 0;
10         for (int i = 0; i < tree.degree(); i++)
11         {
12            temp += h(tree.getSubTree(i));
13         }
14         return temp;
15      }
16   }



Problem 6) For each of the following expressions, rewrite the expression in both
prefix and postfix notation and draw an abstract syntax tree for the expression.
(a) a * b + c
(b) a * (b + c)
(c) a * b + c * d
(d) a * (b + c) * d


Problem 7) For each of the following expressions, rewrite the expression using
infix notation and draw an abstract syntax tree for the expression.
(Some are in prefix form and some in postfix form.)
(a)  + * * 3 4 5 - 6 7
(b)  2 3 * 4 5 + - 6 +
(c)  4 5 6 7 8 * * * *
(d)  + + + + 4 5 6 7 8
(e)  - 4 + 5 * 6 / 7 8


Problem 8) Find the eighth parameter in the following C function call.
How many parameters are there?

   Can(you, tell, at + a, glance, which * of, these, parameters, is(the, eighth), one ? yeah : sure);

Show that your answer is correct by drawing an AST for this expression. (Use a Language_7 like AST.)

(This problem comes from the paper
  "How Not to Write Fortran in Any Language"
   http://queue.acm.org/detail.cfm?id=1039535
)


Problem 9) Here is a simplified version of the evaluateApply() method from the
interpreter for Language_7. Explain the purpose of each line of code. Be precise.
And do not just "read  aloud" each line of code (i.e., "Line 3 declares a Tree
variable g and then sets it equal to the result from calling evaluateExp with parameter ...").
You should carefully explain how each line of code helps do the job of evaluating
a function call. Your explanation should include a description of the result
returned by most of the method calls in this code, and an explanation of any
non-local variable references.

Also, in what way has this method been simplified (other than it doesn't do any error checking)?

 1   private static Value evaluateSimpleApply(Tree tree)
 2   {
 3      Tree g = evaluateExp( tree.getSubTree(0) ).valueL;
 4      Environment e = new Environment(globalEnv);
 5      e.add(g.getSubTree(0).getElement(), evaluateExp(tree.getSubTree(1)));
 6      Environment o = env;
 7      env = e;
 8      Value result = evaluateExp( g.getSubTree(1) );
 9      env = o;
10      return result;
11   }



Problem 10) Suppose that we write an interpreter for a block-structured language
(with nested scopes) that has a 'set' expression, but no 'var' expression, and
we define evaluateSet() the following way. Describe in detail the semantics of
'set' in this language. In particular, what is it that you can't do in this language?
(Your explanation of the semantics should make use of the terms "declaration",
"assignment", "hiding".)

 1   private static Value evaluateSet(Tree tree) throws EvalException
 2   {
 3      String variable = tree.getSubTree(0).getElement();
 4      if ( env.definedLocal(variable) )
 5      {
 6         throw new EvalException("you can't do that: " + variable);
 7      }
 8      Tree expr = tree.getSubTree(1);
 9      Value result = evaluateExp(expr);
10      env.add(variable, result);
11      return result;
12   }

Suppose that the implementation of evaluateSet() is changed to the following one.
How are the semantics of 'set' changed? (Your explanation of the semantics should
make use of the terms "declaration", "assignment", "hiding".)

 1   private static Value evaluateSet(Tree tree) throws EvalException
 2   {
 3      String variable = tree.getSubTree(0).getElement();
 4      if ( env.defined(variable) )
 5      {
 6         throw new EvalException("you can't do that: " + variable);
 7      }
 8      Tree expr = tree.getSubTree(1);
 9      Value result = evaluateExp(expr);
10      env.add(variable, result);
11      return result;
12   }



Problem 11) The following C function computes the number of decimal digits in a positive integer.
Translate (as best you can) this function into our Language_7.

      int numdigits(int x)
      {  int t = x, n = 1;
         while (t >= 10)
         {  n++;
            t = t/10;
         }
         return n;
      }



Problem 12) Each of the following expressions tries to apply a lambda expression
to some parameters. For each one, either find the result of the application, or
explain what is wrong with the expression.

(a)  (apply (lambda x y (+ x y)) 6 7)

(b)  (apply (lambda x y (+ 4 5)) 6 7)

(c)  (apply (lambda 2 3 (+ 4 5)) 6 7)

(d)  (apply (lambda x y (+ 4 5)) u v)

(e)  (apply (lambda x y (+ x y z) 6 7)



Problem 13) Describe in English the language defined by the following grammar.
(Note: Do not use English to describe the grammar, describe the language defined by the grammar.)
          ->   
          -> 'a'  | 'a'
          -> 'b'  | 'b'
          -> 'c'  | 'c'


Problem 14) Consider the following grammar.
          ->  'a'  'b'
          ->  'b' | 'b'
          -> 'a'  | 'a'
Which of the following words are in the language generated by this grammar?
(a) baab
(b) bbbab
(c) bbaaaaa
(d) bbaab


Problem 15) Consider the following grammar.
          -> 'a'  'c'  |  | 'b'
          -> 'c'  | 'c'
          ->  | 'd'
Which of the following words are in the language generated by this grammar?
(a) abcd
(b) acccbd
(c) acccbcc
(d) acd
(e) accc


Problem 16) For each of the following parts, define a grammar for the words,
over the alphabet containing just 0 and 1, that are described.
(a) All words ending with a 1.
(b) All words of length exactly 8.
(c) All words that contain the string 0101.
(d) All words with odd length. Show how to parse the word 00100 using your grammar.


Problem 17) The following grammar is motivated by declarations in C.
Declaration ::= Type Declarator ';'
Type ::= 'int' | 'char'
Declarator ::= '*' Declarator
            |  Declarator '[' Digit ']'
            |  Declarator '(' Type ')'
            |  '(' Declarator ')'
            |  Letter
Letter ::= 'a' | 'b' | ... | 'y' | 'z'
Digit  ::= '1' | '2' | ... | '8' | '9'
(a) Give 5 examples of strings generated by this grammar.
(b) Show that this grammar is ambiguous.



Problem 18) Consider the following grammar for postfix expressions.

     Postfix -> Postfix Postfix BinOp
             |  Postfix UnOp
             |  Number

       BinOp -> '+' | '-' | '*' | '/'

       UnOp  -> 'neg' | 'sqrt'

      Number -> any valid Java double

(a) Draw the parse tree (not the AST tree) for the expression
   4 5 + 3 sqrt *

(b) Explain why you cannot write a recursive descent parser for this grammar.

(c) Draw the abstract syntax tree for the expression
   4 5 + 3 sqrt *



Problem 19) Consider the following grammar for postfix expressions.
(The symbol EMPTY in the grammar means that we produce an empty string.)

     Postfix -> Number Postfix2

    Postfix2 -> Postfix BinOp Postfix2
             |  UnOp Postfix2
             |  EMPTY

       BinOp -> '+' | '-' | '*' | '/'

       UnOp  -> 'neg' | 'sqrt'

      Number -> any valid Java double

(a) Draw the parse tree (not the AST tree) for the expression
   4 5 + 3 sqrt *

(b) Explain why the abstract syntax tree for the expression
   4 5 + 3 sqrt *
is the same as it was for previous problem, even though the grammar has changed.

(c) Explain why you can write a recursive descent parser for this grammar.

(d) Write the Java code for the getPostfix() and getPostfix2() methods that would
implement a recognizing parser (assume the usual interface to the Tokenizer).

   private static void getPostfix(Tokenizer tokens)
   {




   }//getPostfix()


   private static void getPostfix2(Tokenizer tokens)
   {




   }//getPostfix2()


(e) Write the Java code for the getPostfix2() method that would implement a
AST building parser (assume the usual interface to Tokenizer and Tree).

   private static Tree getPostfix2(Tokenizer tokens)
   {




   }//getPostfix2()