For this first part of the class project, you will use the flex tool to generate a lexical analyzer for a high-level source code language called "MINI-L". The lexical analyzer should take as input a MINI-L program, parse it, and output the sequence of lexical tokens associated with the program.
[The MINI-L language is described in detail here.]
[The required output format for your lexical analyzer is described here.]
Flex is a tool for generating lexical analyzers. Lexical analyzers scan text
(a sequence of characters) and look for lexical patterns in the text.
Flex requires an input file specifying a description for a lexical analyzer to
generate. From this description, flex will automatically create a C program
for you (called lex.yy.c
) that will perform the lexical analysis.
In our department, flex is installed and can be used on machine "bass".
[A brief introduction to flex can be found here.]
[The detailed manual for flex can be found here.]
mini_l.lex
.
flex mini_l.lex
. This will
create a file called lex.yy.c
in the current directory.
-lfl
flag for gcc.lexer
with the following command: gcc -o lexer lex.yy.c -lfl
. The program
lexer
should now be able to convert an inputted MINI-L program into the
corresponding list of tokens.
Suppose your lexical analyzer is in the executable named lexer
. Then for
the MINI-L program primes.min, your lexical analyzer should be
invoked as follows:
cat primes.min | lexer
The list of tokens outputted by your lexical analyzer should then appear as they do here. The tokens can be printed to the screen (standard out).
Another example: for program mytest.min, the outputted tokens should look like this.
Submit via Moodle. Turn in your source code and the Makefile (or a build script) in a .tar.gz or .zip file. Make sure you can uncompress and compile your submission on bass, otherwise your score for this project will be 0.
If you implement this project in Objective Caml (an ML variant that supports functional, object-oriented, and imperative programming) you will receive 10% extra credit on this project's score.
Functional languages are an excellent choice for writing a compiler: they feature strong static typing, pattern matching, algebraic (variant) data types, parametric polymorphism, type inference, automatic garbage collection. These traits permit rapid program development, and lead to a concise and elegant implementation.
We have installed OCaml on bass. It comes with its own lexer generator, ocamllex (similar to flex), and an impressive array of libraries.