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 "bolt".
[A brief introduction to flex can be found here.]
[For detailed information on flex here.]
For the complete flex documentation, run the command info flex
from bolt.
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 fibonacci.min, your lexical analyzer should be
invoked as follows:
cat fibonacci.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.
For program primes.min, the outputted tokens
should look like this.