Homework 1



  1. Draw a deterministic finite state machine that accepts the language of file paths.

    A file path consists of optional directory names and one file name, separated by "/".

    1. The path must start with "/"

    2. A directory name can include letters and digits

    3. A file name can include letters, digits and dots

    4. A file name cannot have two dots in a row

    5. A file name cannot end with a dot

    6. CLARIFICATION: Both file name and directory name should contain at least one character

    7. Label the edges of your FSM with words slash, dot, letter and digit.

    Valid examples:
    /P1.java
    /a/very/long/directory/.file
    /foo/I.have.many.many.dots
    /bar/NoDot

    Invalid examples:
    P1.java
    /bad/file..name
    /another/bad/filename.


  2. Write a regular expression for a UsernameTag. A UsernameTag is of the following format : UserName#Number

    1. UserName and Number are separated by "#"

    2. UserName can include letters, digits and "-"

    3. UserName must start with a letter, and cannot end with "-"

    4. UserName cannot have two "-" in a row

    5. Number can only include digits, and should contain at least one digit

    Valid examples:
    hello-world#45
    life4compiler#0102

    Invalid examples:
    not-a-user-tag#
    also-not-a-user-tag#number123
    invalid--usertag#319
    32invalid-#987
  3. You may use hyphen to represent "-", digit to represent any digit, and letter to represent any letter.



  4. Java (as well as C and C++) allows comments delimited by "/*" and "*/". This kind of comment can be defined in English as follows:

    A comment consists of three parts:
    1. a slash followed by a star, followed by

    2. the body of the comment, followed by

    3. a star followed by a slash.

    The body of the comment can be empty, or can contain any characters except the two-character sequence "*/".

    Note that the body of a comment can include stars and slashes, just not "*/".

    Assume that the following JLex macros have been defined:

    SLASH     = [/]
    STAR = [*]

    Label each of the following JLex patterns as being correct/incorrect for the type of the comment described above.

              
    1. {SLASH}{STAR}[^(*/)]*{STAR}{SLASH}
    2. {SLASH}{STAR}(.)*{STAR}{SLASH}
    3. {SLASH}{STAR}([^*]*{STAR}+[^*/])*{STAR}+{SLASH}
    4. {SLASH}{STAR}([^*]|[^/])+{STAR}{SLASH}
    5. {SLASH}{STAR}[^*]*{STAR}+{SLASH}+
    6. {SLASH}{STAR}([^*]|({STAR}+[^*/]))*{STAR}+{SLASH}
    For each incorrect pattern, explain what is wrong. For example, you might say The pattern does not allow the comment body to include stars, or The pattern does allow the comment body to include */.

    If the pattern both disallows some "good" comments and allows some "bad" comments, give two explanations, one for each problem with the pattern.

    Then, for each problem with the pattern, give a string that illustrates the problem; i.e., give a string that is not matched by the pattern but is a "good" comment and/or give a string that is matched by the pattern but is a "bad" comment.

    CLARIFICATION: You will need to list down ALL of the problems for each incorrect pattern. An incorrect pattern might have one or multiple problems.

    Be sure that it is clear whether your example strings are intended to be "good" or "bad" comments.


  5. Give a correct JLex pattern for single-line comments in Java (and also C and C++). Note that single-line comments begin with // and end with a newline character.

    For the purposes of this homework you may assume that \n is the newline character.

    You may define and use additional macros. Make your pattern as readable as possible.