CS321 Prog Lang & Compilers          Assignment # 3
Assigned: Mon. Jan 22, 2007       Due: Wed. Jan 24, 2007

Turn in a listing, and a transcript that shows you have tested your code. A
minimum of 3 tests is necessary. Some functions may require more than 3 tests
to receive full credit.

1) Write the following functions over lists. You must
use pattern matching and recursion.

A. reverse a list so that its elements appear in the oposite order.
reverse [1,2,3,4]  ---->  [4,3,2,1]

B. Count the number of occurrences of an element in a list
count 4 [1,2,3,4,5,4] ---> 2
count 4 [1,2,3,2,1]   ---> 0

C. concatenate together a list of lists
concat [[1,2],[],[5,6]] ----> [1,2,5,6]

2) Using the datatype for Regular Expressions we defined in class

datatype RE
  = Empty
  | Union of RE * RE
  | Concat of RE * RE
  | Star of RE
  | C of char;

Write a function that turns a RE into a string, so that it can be
printed. Minimize the number of parenthesis, but keep the string
unambigouous by using the following rules.
        1) Star has highest precedence so:  ab*  means a(b*)
        2) Concat has the next highest precedence so: a+bc   means   a+(bc)
        3) Union has lowest precedence so: a+bc+c*   means   a+(bc)+(c*)
        4) Use the hash mark (#) as the empty string.
        5) Special characters *+()\ should be escaped by using a
           preceeding backslash.
              So (Concat (C #"+") (C #"a"))  should be   "\+a"

Hints:
1) The string concatenation operator is usefull:
   "abc" ^ "zx" -----> "abczx"
2) Write this is two steps.
  First, fully paranethesize every RE
  Second, Change the function to not add the parenthesis which
  the rules don't require.






Back to CS 321, Languages and Compiler Design, Assignment Page