CSCI 4627
Homework exercise 1

Due: Jan 27, at the end of the day.

Read the flex lexer handed out in class. You will also want to get tokens.h.

Draw a finite state machine for the same set of tokens. Implement a lexer directly from the finite state machine, without using a tool such as flex to build the lexer. Write it in C or C++. Test the lexer.

Include a tester with your lexer that has exactly the same effect as the flex lexer. Create a test file, and test your lexer. You can run the flex lexer and see whether your program produces the exact same results. To run the flex lexer, get example1.lex and run the following commands on one of the Unix machines in the lab.

  flex example1.lex
  gcc lex.yy.c
  a.out
I will run your lexer and expect it to produce the same results on my test file as the flex lexer does. If it produces different results, you will lose points.

What to hand in

  1. Hand in your lexer using the handin command on the lab machines. If your program is called lexer.cpp, use the following command to hand it in.

       /export/stu/classes/csci4627/bin/handin csci4627 1 lexer.cpp
    
    You should get a response indicating that it was handed in successfully. If you do not get such a response, it was not successful.

  2. Hand in your finite state machine diagram as well. You can either hand it in as a gif file using the handin command, or on paper. In either case, make it clear and readable. The finite state machine diagram is required. NO CREDIT WILL BE GIVEN FOR A PROGRAM SUBMISSION THAT IS NOT ACCOMPANIED BY A FINITE STATE MACHINE DIAGRAM.

Notes

  1. For this assignment you must use the finite-state machine approach. Your program must follow your finite-state machine diagram. An ad-hoc lexer is not acceptable.

  2. Your lexer must refer to the tokens by their names, not by their numbers, with the exception of tokens '+' and '-', which should be returned as character codes. (The '+' token is returned as '+', the code for the + character.) For example, to return INTEGER_CONSTANT, your lexer should return INTEGER_CONSTANT, not 257.