CSCI 435: Compiler Construction
Assignment 4

Overview

Write a scanner for the C- programming language defined in Appendix A of the textbook. To this end, code a flex specification file ("Lexer.l" (extension of dot ell)) and a driver file ("CMinus.cc") that repeatedly invokes the generated scanner. In the DRIVER, print the token, lexeme, and lexeme's value (for ID and NUM tokens) in a NEAT tabular format. Do NOT PRINT in the scanner or you will receive a ZERO! Code in C++ and use ".cc" extensions. Report any invalid input, along with the line and column numbers. It is okay if the column numbers are SLIGHTLY off.

Avoid unsafe or deprecated functions like "atoi" and "atof" — use modern, safe alternatives.

Use a Makefile to build your project. Eventually it will build your full compiler. See the Code directory for a sample Makefile that you can modify. Be sure to indicate dependencies correctly, including HEADER files!

For token definitions use TokenType.h. Do NOT modify the enum type! Note that LBRACK represents "[", RBRACK "]", LBRACE "{", and RBRACE "}".

Follow the formatting requirements for C++ sources files. Also ensure your Flex file is neatly formatted: "cat" it before submitting to view alignment issues. If you copy my Emacs config file, which I strongly recommend, you shouldn't have issues with spaces versus tabs.

Input Specification

Take the name of a C- program as a command-line argument. If no argument is specified read from standard input. Use ".cm" as the extension for C- source files.

Output Specification

Output tokens, lexemes, and values as follows. You may use different column widths, but ensure alignment. NO PRINTING should be done in the scanner. DO NOT use a GIANT "switch" or "if" statement to output each token — use an array or std::map. Continue scanning after encountering an ERROR token.

Sample Session

> ./CMinus Test.cm  
TOKEN           LEXEME           VALUE
=====           ======           =====
VOID            "void"
ID              "main"           "main"
LPAREN          "("
VOID            "void"
RPAREN          ")"
...
ERROR           "&"              Line: 18; Column: 3
...
NUM             "32"             32
...

Submission

Submit your driver file ("CMinus.cc"), header file(s), Flex specification ("Lexer.l"), and Makefile. ENSURE your Makefile is named "Makefile" and that it properly builds your program, and that "make clean" removes the executable and all generated files.

Hints

Be sure to handle C-style comments correctly. Familiarize yourself with the Flex manual. Review the examples in the code directory, particularly the Pascal lexer (note how I handle the IF token).

If in your driver you want to access a global variable defined in your scanner, declare it as "extern" in your driver.


Gary M. Zoppetti, Ph.D.