Review Problems for Exam 1
CS 31600, Spring 2012

NOTE: The midterm exam is on Thursday, March 29.

The exam will cover Chapters 1 - 8 (except for Chapter 4) from the textbook and the following material on expression grammars and recursive descent parsers.

Problem 1:
Do the following Exercises from the textbook.
  • From the end of Chapter 2, do Exercises 1a,c,f,h,i,j,l, 2a,c,f,h,i,j,l (pages 24-25).
  • From the end of Chapter 3, do Exercises 1, 3, 4 (pages 41-42).
  • From the end of Chapter 6, do Exercises 2 and 4 (page 102).
  • From the end of Chapter 8, do Exercises 1 - 5 (pages 130-131).

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

Problem 3:
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.)
         <S> -> <A> <B> <C>
         <A> -> 'a' <A> | 'a'
         <B> -> 'b' <B> | 'b'
         <C> -> 'c' <C> | 'c'

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

Problem 5:
Consider the following grammar.
         <S> -> 'a' <S> 'c' <B> | <A> | 'b'
         <A> -> 'c' <A> | 'c'
         <B> -> <A> | '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 6:
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 7:
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 8:
The following C function computes the number of decimal digits in a positive integer.
      int numdigits(int x)
      {  int t = x, n = 1;
         while (t >= 10)
         {  n++;
            t = t/10;
         }
         return n;
      }
(a) Implement this function in ML.
(b) Modify your function from part (a) so that it works with negative integers.

Problem 9:
Explain in detail exactly what the C, C++, Java expression
i + j / k
means and what its type is when the variables have the following types. Be sure to specify any implicit type conversions.
  • (a) int i, j, k;
  • (b) int i, j; double k;
  • (c) double i; int j, k;

Problem 10:
Part (a) In C (or C++) explain the difference in meaning between the following two lines.
   enum x { a, b };
   enum { c, d } x;
In particular, what does each x represent? (Enumerations are described in our textbook on pages 89-90.)
Part (b) The following code will compile in C but not in C++. Explain why. (Hint: Look up the two kinds of type equivalence. Type equivalence is described in our textbook on pages 100-101.)
   int main()
   {
      enum { a, b } x;
      enum { c, d } y;
      y = c;
      x = y;
      return 0;
   }

Problem 11:
Consider the following grammar for postfix expressions.
     Postfix -> Postfix Postfix BinOp
             |  Postfix UnOp
             |  Number

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

       UnOp  -> 'neg' | 'sqrt'

      Number -> any valid Java double
Part (a) Draw the parse tree (not the expression tree) for the expression
   4 5 + sqrt 3 *
Part (b) Explain why you cannot write a recursive descent parser for this grammar.
Part (c) Draw the abstract syntax tree for the expression
   4 5 + sqrt 3 *

Problem 12:
Consider the following grammar for postfix expressions. (The symbol EMPTY in the grammar means that we produce an empty string. See page 15 of the textbook.)
     Postfix -> Number Postfix2

    Postfix2 -> Postfix BinOp Postfix2
             |  UnOp Postfix2
             |  EMPTY

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

       UnOp  -> 'neg' | 'sqrt'

      Number -> any valid Java double
Part (a) Draw the parse tree (not the expression tree) for the expression
   4 5 + sqrt 3 *
Part (b) Explain why the abstract syntax tree for the expression
   4 5 + sqrt 3 *
is the same as it was for Problem 11, even though the grammar has changed.
Part (c) Explain why you can write a recursive descent parser for this grammar.
Part (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()


Return to the CS 31600 lecture page.
Return to the CS 31600 home page.


compliments and criticisms