CS 651 Compilers

Sample Midterm Examination

Bill Campbell

 

This is a closed book examination.  Write your solutions in your blue examination booklets.

 

  1. Consider the regular expression  (a* | b*)*
    1. Use Thompson's algorithm to produce a non-deterministic finite state automaton to recognize the language described by this expression. [10]
    2. Use subset construction to produce a deterministic FSA recognizing the same language.  [15]

 

  1. Show that the following grammar is ambiguous. [10]

 

E ->  E + E | E * E | ( E ) | i

 

  1. Explain why, or why not, Java is a context free language.  An example might help.  [10]

 

  1. Consider the following grammar.

 

S -> A a

S -> a

A -> c

A-> b A

 

    1. Compute first and follow for S and A. [10]
    2. Construct an LL(1) parsing table for this grammar. [10]
    3. Is follow necessary for constructing this table?  Why or why not? [5]
    4. Show the stack and parsing actions for parsing the input string:  bbca [5]
    5. Write a recursive descent parser for this grammar, using the tools you were given for parsing Java by recursive descent. [10]

 

  1. Say you wanted to add an until statement to Java.  For example, one might write

 

until ( i >= j )

     System.out.print( i++ );

 

    1. What changes would one have to make to the Lexer in our compiler? [5]          
    2. Write a method for Parser that would parse the until statement by recursive descent, making use of any methods available in Parser. Produce an ast node for it using the constructor with header

UntilStatement(int line, Expression e, Statement s)  [10]