CSCI 6420
Homework assignment 1

Due: Monday, January 25

Face-to-face students: Submit in class.

Online students: Submit a scanned or typed homework by email, as an attachment.

Please type the answers to these exercises.

  1. Write a Flex regular expression for each of the following sets of strings.

    1. All strings without newlines that contain exactly 3 occurrences of the letter a (a lower case a).
    2. All strings that either start with 0 and have even length or start with 1 and have odd length.
    3. All comments that begin with /* and end with */, and contain any characters inside them except the character sequence */. Watch out for comments such as /***/ and /*/***a/*/.
    4. All comments that begin with // and are followed by any sequence of non-end-of-line characters and end on an end-of-line character.

  2. (Dragon book, Exercise 3.5.2, page 146) Write a Flex program that reads a file, and writes a modified version of the file to the standard output, replacing each nonempty sequence of blanks and tabs by a single blank.

    For this program, your actions will not return results. They will print something, and then continue looking for another regular expression.

    Write a main function in the last part of the Flex program (after the second %%). The name of the file to read is argv[1] on the command line.

  3. (Dragon book, Exercise 3.5.4, page 146) Write a Flex program that converts to Pig-latin. It should read from the standard input and write to the standard output.

    Each sequence of letters that begins with a consonant has the consonant moved to the end of the string and followed by ay.

    Each sequence of letters that begins with a vowel just has ay added to the end.

    Each nonletter is copied to the output unchanged.

    For example, if the input is

    The quick (brown) fox jumped over the lazy dogs.
    
    then the output is
    heTay uickqay (rownbay) oxfay umpedjay overay heay azylay ogsday.