Exam 1 Review Problems for CS 31600, Spring 2023


The first midterm exam will be Thursday, March 23.
The exam will be over the following topics.
   1) Tree data type, tree data structure, tree traversals.
   2) Expressions and expression trees.
   3) Grammars.
   4) Parsers.


Here are some review/practice problems for the exam.


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


Problem 2: Draw the expression tree for this postfix expression.
   "a b c d + + *"
Rewrite the expression in its infix form. Add the minimum number
of parentheses needed for the infix expression to have the same
expression tree as the postfix expression.


Problem 3: Write the infix, prefix, and postfix versions of the
expression defined by the following expression tree. For the infix
expression, add the minimum number of parentheses needed to give it
this expression tree.
                 *
                / \
               /   \
              a     +
                   / \
                  /   \
                 b     +
                      / \
                     /   \
                    +     e
                   / \
                  /   \
                 c     d


Problem 4: Here are six grammars (with six start symbols,
S1, S2, S3, S4, S5, and S6). Describe the list of words in
the language generated by each grammar.

  (a)   S1 ::= 'x' [ 'y' ] 'z'

  (b)   S2 ::= 'x' ( 'y' )* 'z'

  (c)   S3 ::= 'x' ( 'y' )+ 'z'

  (d)   S4 ::= 'x' ( ( 'y' )+ | ( 'w' )+ ) 'z'

  (e)   S5 ::= 'x' [ ( 'y' )+ | ( 'w' )+ ] 'z'

  (f)   S6 ::= 'x' ( 'y' | 'w' )* 'z'


Problem 5: 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 6: 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? For
each word that is in the language, show
a "left-most" derivation of the word.
  (a)  "baab"
  (b)  "bbbab"
  (c)  "bbaaaaa"
  (d)  "bbaab"


Problem 7: The following two grammars, with start
symbols S1 and S2, define similar languages. Describe
how the two languages are different. (Note: Do NOT describe
how the two grammars are different. Describe how their
languages are different.)

    S1 ::= ( '(' | '[' | '{' ) S1 ( ')' | ']' | '}' ) [ S1 ]
         | 'x'

    S2 ::= '(' S2 ')' [ S2 ]
         | '[' S2 ']' [ S2 ]
         | '{' S2 '}' [ S2 ]
         | 'x'


Problem 8: 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? For each word that is
in the language, show a "left-most" derivation of
the word.
  (a)  "abcd"
  (b)  "acccbd"
  (c)  "acccbcc"
  (d)  "acd"
  (e)  "accc"


Problem 9: For each of the following parts, define
a grammar (using either BNF or EBNF) 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 (of any length) that contain the string 0101.
  (d) All words with odd length. Show how to parse
      the word 00100 using your grammar.


Problem 10: Show that the grammar

   expr ::=  '-' expr
          |  expr '-' ( 'x' | 'y' | 'z' )
          |  'x' | 'y' | 'z'

is ambiguous by finding a string that has two different parse trees.



Problem 11: 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'

Part (a) Give 5 examples of strings generated by this grammar.
Your five examples should include an "array declaration", a
"function declaration", and a "pointer declaration".

Part (b) Show that this grammar is ambiguous.



Problem 12: Here is an unambiguous BNF grammar that
gives '*' higher precedence than '+', makes '+' left
associative and makes '*' right associative.

   expr ::=   expr '+' term | term
   term ::= factor '*' term | factor
 factor ::= '(' expr ')'
          | 'a'

Using this grammar, draw both the parse tree and the
expression tree for the following expression.

    "a * ( a + a + a ) * a"



Problem 13: 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 14: 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


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 the previous problem, 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()




Problem 15: Suppose that a grammar for some language has the
following production, P3. Use Java to write the function that
parses this production in a recognizing recursive descent parser.
Assume the usual interface to the Tokenizer class.

   P3  ::= '*'  P4
         | '@'  P4  P5
         | '#'  P5 ( '#' P5 )*
         |  P6