Laboratory 4, February 5


Objectives

During one of our recent lectures, we discussed how the generation of the finite state automata for each regular expression (for each token) is so repetitive, mechanical, and, well, boring.  The theory of finite state automata and regular expressions tells us that there must be a way to design an automatic scanner generator, because we saw the following theorems while studying the theory  

In addition, we have seen as we noted above that the process of turning a finite state automaton into a procedure that does the work of that finite state automaton is mechanical, so we could give our own new theorem:

What does this all mean?  It means that  it should be possible to write a program that takes a set of regular expressions as input and produces a program as output that will scan an input stream for the tokens represented by the regular expression:

      A list of           Scanner      Scanner 
----------------------->  Generator ------------>
  regular expressions     Program      Program

 

One widely used scanner generator is the program lex (short for lexical analyzer generator, which is another term for scanner generator).  It comes with most Unix distributions.  We also have the gnu equivalent available, called flex.  Flex accepts a list of regular expressions which it processes, producing a scanner in C.  In order to use the scanner generated by flex, it, of course, must itself be compiled by a C compiler.

The objective today is to learn how to use a scanner generator.

Turning Your Project In

Turn your project in according to the TA's instructions.

Preparing for Lab Exercises

Follow the TA's directions for preparing your workstation for running flex.

To Do

Do a "man" on flex to learn about flex.  As seniors you should now be able to read, understand, and apply information that you read about tools such as flex. Remember that this is a working laboratory. You should be actively collaborating with other team members.  You will need to follow your TA's instructions about how to prepare a file for input to flex.  Do this in increments.  First get flex to generate a scanner for something simple, such as a file of identifiers.  Try it out on some short files of identifiers.  Then, as you get more confidence, add more tokens.

Work with flex

To Turn In

With 10 or 15 minutes to go in lab, build a small text file that has representatives of all the tokens you are now able to scan with your flex generated scanner.  Turn a listing of this text file along with the corresponding list output from a run of your flex generated scanner. Turn these two lists in. As usual, include the following information on the standard cover page (see the previous lab for a reminder).
  1. Your group number
  2. Lab 4
  3. Today's date
  4. Your team names and esus e-mail addresses