Programming assignment 1
Creating a lexical analyzer
CSCI 5220
Spring 2002

Due: Monday, Feb. 4.

The language

The appendix in the text describes a small programming language. The tokens are in bold, and are described in more detail in A.4.

The assignment

Write a lexical analyzer using flex for the language described in the appendix. Assign numbers to the tokens. Do not hard-code the numbers into the lexer. Give names to the tokens, and provide definitions that associate those names with numbers.

Tokens that can stand for different lexemes (such as addop and id) should have attributes associated with them. Use name yylval for the variable that holds the attribute and YYSTYPE for the type of yylval. Choose reasonable attributes, so that you can get desired information later. You will find it convenient to use an integer attribute for addop, relop, etc. You must use names for the integers. Do not hard-code particular integers into your lexer. For example, the attribute for addop might be PLUS. You can define PLUS to stand for an integer.

You can limit identifiers to 64 letters, but make sure that this limit is very easy to change. (It should be possible to change the limit by changing one line of the program.) Assume that integers and real numbers are small enough to fit into one long integer or real number (type double), respectively.

Testing

You will need an account on the Unix computers in the lab. If you already have an account, it will not be modified. If you need an account, please let me know.

I will assume that you are writing your lexer in C. If it is in a file called lexer.lex, you can compile it as follows.

    flex lexer.lex
    gcc -o lexer lex.yy.c
This creates file lexer, containing the executable code of the lexer.

Create a file that contains at least one of each kind of token. (Try different kinds of operators too.) Test the lexer. You will need to write a main program in the lexer to test it. See the example lexer. Run the lexer on file test.txt as follows.

    lexer <test.txt

Turning in your assignment

Turn in your assignment using the handin utility. You can do that as follows. Add the line

     alias handin5220 '/export/stu/classes/csci5220/bin/handin csci5220'
to your .cshrc file (assuming you are using csh). Then turn in the lexer (which I am assuming is called lexer.lex) using the following command.
     handin5220 1 lexer.lex
You should receive a notification that the handin was successful. If not, then it was not successful.