After this assignment, you will be able to:
- write regular expressions and DFAs for languages described in English, and combine DFAs with the product construction.
- convert an NFA to an equivalent DFA with the subset construction.
Overview
The goal of the first week is to cover two topics:
A general introduction to compilation. As part of this introduction, you will read about the overall architecture of a compiler, the separate phases of compilation, and how programming language design and computer architecture affects compiler design. In addition, you will read a brief review of key programming language ideas.
Lexical Analysis. The first compilation stage is lexical analysis — the task of breaking source code text into a language’s most basic lexemes. You will read about the basic theory behind lexical analysis (regular expressions, transition diagrams, finite automata) and gain an appreciation for how theory guides the design and implementation of a lexical analyzer.
Readings
- Dragon 1
Fairly light reading — mostly background. - Dragon 3.3, 3.5-3.7
- Regular
Expression Matching Can Be Simple And Fast (but is slow in Java, Perl,
PHP, Python, Ruby, …), Russ Cox, January, 2007.
[For Lab] Don’t worry about the C code implementations — pay more attention to the basic ideas and commentary. - An amusing regular expression
- If you have not used Scala before: the Scala Tutorial and “Scala by Example” on the Resources page.
Exercises
Dragon 1.1.2
Dragon 1.1.4
Dragon 1.6.1
Dragon 3.3.2
Dragon 3.3.9
Write a regular expression for http and ftp URLs. An URL consists of four parts: the protocol (
http://orftp://), 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
//.
Write the DFAs for each of the following:
Binary numbers that contain the substring 011.
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.)
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.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{*/}\)
Draw the DFA that accepts C comments and then use it to write the regular expression that correctly describes C comments.
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.