Topic: A Lexical Scanner.
This page last updated: Sunday September 27, 1998 01:07This assignment implements and tests the foundation of your lexical analyser: The Scanner. The input to the lexical analyser is a stream of source text. The output of each call to the Scanner is a single Token, to be read, in later assignments, by The Parser. This assignment only counts and prints the tokens; it doesn't try to parse them or build a grammar.
Use the main() program given in the source directory to test your Scanner. The main() function of this assignment is a modification of that in the previous assignment. Instead of looping reading and printing lines using my_prompt(), we loop reading and printing lexemes using the new scanner() function. The Scanner will return a TT_EOF token when end-of-file is reached.
The Scanner uses a table-driven deterministic finite automaton (DFA) to recognize the lexemes. To change the lexemes recognized; simply change the tables. We will be changing the tables in subsequent assignments.
You are given most of the code for the Scanner.c file and all of the code for the Scanner.h file (and all the other header files) in the source directory. Your task is to finish the coding of Scanner.c so that your Scanner recognizes and returns Identifier (TokenType TT_ID) tokens and the end-of-file (TokenType TT_EOF) token. (The TokenType enum is defined in Scanner.h.) Identifiers in this assignment follow the rules for C language identifiers. The TT_EOF token is returned at end-of-file when the scanner is called and no other tokens are available.
Your Scanner may also, at your discretion, recognize and return other TokenTypes, though this is not required (yet). The given main() driver program will print any TokenType and lexeme returned by your Scanner. If you don't handle them another way, have your Scanner silently skip over characters that are not part of an identifier.
This function was omitted by mistake from the earlier assignment. It is the companion to my_open() and it has the following prototype and specifications:
void my_close( FILE *fd, /* IN: stream descriptor to close */ char *fbuf /* IN: file name associated with fd */ );
Copy these source files and modify them according to the assignment instructions. The header files are examples only; you are not obligated to use them; however, you must divide up your program source into more than one module.