Creating a lexical analyzer
CSCI 4627
Spring 2002

Due: Monday, Feb 4.

The language

Appendix A of the text describes a language called "C-".

The assignment

Write a lexical analyzer using flex for the C- language. File lexer.h provides definitions for tokens and attributes. (See lexerdos.h for the same file in DOS file system format.) An identifier has a string as its attribute. Treat relop, addop and mulop as tokens, not as nonterminals. The attributes of TOK_RELOP, TOK_ADDOP and TOK_MULOP have attributes of types RelopAttr, AddopAttr and MulopAttr, respectively. A number has an attribute of type int.

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 are small enough to fit into one word.

I will test your program on the assumption that it uses the definitions in lexer.h as I have given them to you. Do not change these definitions, or your lexer will not pass the tests.

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 to test it. Put the main program in a separate file. 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 handin4627 '/export/stu/classes/csci4627/bin/handin csci4627'
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.
     handin4627 1 lexer.lex
You should receive a notification that the handin was successful. If you do not, then it was not successful.