Homework 3

  1. Chapter 4, problem 2
  2. Chapter 4, problem 4
  3. Write regular expressions to match the following.
    1. Write a regular expression that has an a surrounded by any number of bs in front of it and any number of cs after it. (bac bbbac baccc bbbbaccccc but not baab abbb bbba ccab bcabc)
    2. Write a regular expression to represent an arbitrary hexadecimal integer. Hex digits include all the decimal digits plus A, B, C, D, E and F. (1024 2A0FF but not 80G0 X9123 text)

    3. Write a regular expression to represent a literal number in a language that has only one kind of number. (It should match 1.298 1.6 .5 123.4 23. 1e-5 5.67E2 71E-5 5.67e27 .54 23 but not 1.2.3 E5) Try to make your expression as general as possible; it should match any valid floating point literal. A leading minus sign is not part of the number. (You will be writing a function to do this in an upcoming programming assignment.)

    For more information about regular expressions, see the notes from class and the links given in the class web pages as well as the following books which are available at the Albertson's library.

    On onyx, you can test a regular expression that uses perl syntax by copying the file testPattern (a perl script in the ~tcole/teaching/cs354/handouts) into your directory, making sure it is executable and typing the pattern onto the command line when you run the script

        ./testPattern pattern
    

    Note: There is a fair amount of overlap between regular-expression metacharacters and characters that mean something special to the shell. You can use an escape character (\) in front of these or you can put single quotes around the pattern to keep the shell from complaining.

  4. Write a BNF grammar for sentences that have an a preceeded by any number of bs and followed by the same number of cs. (bac bbacc bbbaccc bbbbacccc but not bacc acc bbba ccab bcabc)

    Can you write a regular expression that matches the expressions generated by this grammar?

  5. Try to describe what the following regular expression represents. Try to give a description of what the pattern matches in general terms rather than just parroting the meaning of the pieces of the regular expression. For example, (0|1)*0 is a binary string that ends in 0 or a binary number that is even rather than any number of 0s and 1s in any order followed by a 0. You'll probably want to create some test data to see what matches and what doesn't. List some examples of expressions that match and expressions that don't match the pattern in your answer.
    1. ^[0-9]*(0|5)$
    2. \$[0-9]+(.[0-9][0-9])?
     

This assignment can be turned in

Acceptable formats for electronically submitted assignments are text, OpenOffice or MicrosoftWord. I should be able to open your assignment with OpenOffice. Unless otherwise specified in the assignment, the entire assignment should be in a single document.