CS 210 Example Final Exam

I will post answers to questions, commentary, and/or possible solutions to these questions to the extent I am able.
1. (20 points) Explain Flex's regular expression evaluation rules. How does yylex() evaluate its input against the list of regular expressions supplied by the Flex .l specification file? What does it do to decide which regular expression to use?

This question is checking whether you understand Flex's semantics. Logically all regular expressions are evaluated in parallel each time yylex() is called, with the longest successful match selected as the winner. In the event of a tie, it uses whichever tieing regular expression appeared earlier in the .l file.

2. (30 points) Write a recursive Lisp function (norn L) that takes an arbitrary list L and returns a list (call it Lresult) of length two containing two sublists. For each element in L, if it is an atom, it should be placed in the first sublist of Lresult, while if it is not an atom it should be placed in the second sublist of Lresult. You are guaranteed that L will be a list. If L were empty, you would return a list of two empty lists. You may write helper functions. You may not use any global variables.

Example: (norn '(one two (buckle my shoe) three four (shut the door))) would produce ((one two three four) ((buckle my shoe) (shut the door)))

To come up with a solution, derive a basis case and an induction step. The basis case is (norn nil) which returns '(nil nil). The induction step has to combine (norn (cdr L)) with a bit more work, including a test.

3. (20 points) Draw a diagram (using rectangles to depict cons cells, and arrow-lines to depict pointers) of the Lisp list (3 (1 ((c) (2 (1 (c))))))

You had a fair amount of practice with cons cell diagrams. Be able to go this direction, or the other direction from a diagram to a Lisp list.

4. (20 points) Write a regular expression for Java identifiers. A Java identifier is a letter followed by zero or more letters or digits, except that the underscore character and the Unicode escape sequences are considered letters. A Unicode escape is a backslash followed by a u followed by four hexadecimal digits.

The definition of letter here is somewhat cumbersome and it has to be repeated unless you use proper Flex macro syntax.

5. (10 points) Define a context free grammar as mathematically precisely as you can.

The long-hand math definition of a grammar is a tuple consisting of a set of terminal symbols T, a set of non-terminal symbols N, a set of production rules N -> ω where ω are strings of 0 or more terminals and non-terminals, and a designated start symbol selected from N.

6. (30 points) Write a Unicon procedure replace(s1,s2,s3) that generates Unicon-style strings that consist of variants of s3 in which different instances of s1 are replaced by s2. For example,

replace("the", "this", "Watch the quick brown fox jump over the lazy yellow dog")
would generate two results, namely
"Watch this quick brown fox jump over the lazy yellow dog"
and
"Watch the quick brown fox jump over this lazy yellow dog"

Something like
procedure replace(s1, s2, s3)
   every i := find(s2, s1) do
      suspend s1[1:i] || s3 || s1[i+*s2:0]
end

7. (20 points) One of the big ideas in Java was exception handling. If you are writing some Java code that might result in an exception, you have two choices. Describe these two choices, and give a Java code example of each.

You have to put code that might result in an exception inside a try-block, or else declare in your public interface that you might throw an exception.

8. (30 points) Write a Java class named Niklaus (named after famous programming language inventor Niklaus Wirth) that contains a member variable named gifts that is a dynamically sized collection of strings, and a public method named give_present(). Initialize the gifts to contain the strings "tablet PC" and bicycle, and write the body of give_present() to return a random element from gifts. Write a subclass of Niklaus named Santa with fields named naughty and nice. Initialize naughty and nice to an empty collection structure. Give class Santa a public method know(name) that checks whether name is in the naughty or nice collection; if naughty, know() returns "bad"; if nice, know() returns "good". If it is not on the list, insert name into either the naughty (if the length of name is odd) or the nice (if the length of name is even) collection, and then return "bad" or "good" appropriately. Give class Santa a public method give_present(name) that returns the string "lump of coal" if know(name) returns "bad", and returns a random "tablet PC" or "bicycle" if know(name) returns "good".

Have fun.