HW 1: Regular Expressions and Automata

Learning Objectives

After this assignment, you will be able to:

Overview

The goal of the first week is to cover two topics:

Readings

Exercises

  1. Dragon 1.1.2

  2. Dragon 1.1.4

  3. Dragon 1.6.1

  4. Dragon 3.3.2

  5. Dragon 3.3.9

  6. Write a regular expression for http and ftp URLs. An URL consists of four parts: the protocol (http:// or ftp://), the DNS name or the IP address of a host, an optional port number, and an optional pathname for a file. For simplicity, we assume that:

    • A DNS name is a list of non-empty alphabetical strings separated by periods.

    • An IP address consists of four non-negative integers of at most three digits each, separated by periods.

    • A port number is a positive integer following a colon. (e.g. :8080)

    • The pathname part is a unix-style absolute pathname. The allowed symbols are letters, digits, period and slash. A sequence of two consecutive slashes // is forbidden, i.e. no empty directory name.

    • A URL may end with a slash as long as it does not create the sequence //.

  7. Write the DFAs for each of the following:

    1. Binary numbers that contain the substring 011.

    2. Binary numbers that are multiples of 3 and have no consecutive 1’s. Your solution can accept or reject the empty string – either is fine. (You may find it easiest to create DFAs for each of the two requirements and then think about how to combine them.)

  8. A comment in the C language begins with the two-character sequence /*, followed by the body of the comment, and then the sequence */. The body of the comment may not contain the sequence */, although it may contain the sequence /*, or the characters * and /. In the expressions below, literal characters are set in \(\t{typewriter}\) on the baseline, and we write \((E)^*\) for the Kleene closure of \(E\) — so every raised star is the Kleene operator, and every \(\t{*}\) on the baseline is the character itself.

    1. Show that the following regular expression does not correctly describe C comments:

      \(\t{/*}\; (\t{/})^{*}\; \big(\; \nc{*/} \mid \nc{*}\t{/} \mid \t{*}\nc{/} \;\big)^{*}\; (\t{*})^{*}\; \t{*/}\)

    2. Draw the DFA that accepts C comments and then use it to write the regular expression that correctly describes C comments.

  9. Convert the following NFA to a DFA. For each DFA state, indicate the set of NFA states to which it corresponds. Make sure you show the initial state and the final states in the constructed DFA.