How to run lex to create C code from lex specifications, then how to compile and run the lexical analyzer program.
lex lex.l
lex produces a C file called lex.yy.c.
There are several options available with the lex command. If you use one or more of them, place them between the command name lex and the filename argument.
The -t option sends lex's output to the standard output rather than to the file lex.yy.c.
The -v option prints out a small set of statistics describing the so-called finite automata that lex produces with the C program lex.yy.c. (For a detailed account of finite automata and their importance to lex, see the Aho, Sethi, and Ullman book, Compilers: Principles, Techniques, and Tools, Addison-Wesley, 1986.)
lex uses a table (a two-dimensional array in C) to represent its finite automaton. The maximum number of states that the finite automaton requires is set by default to 4000. If your lex source has a large number of rules or the rules are very complex, this default value may be too small. You can enlarge the value by placing the following entry in the definitions section of your lex source:
Running lex
If lex.l is the file containing the lex specification, the C source for the lexical analyzer is produced by running lex with the following command:lex lex.l
lex produces a C file called lex.yy.c.
Options
There are several options available with the lex command. If you use one or more of them, place them between the command name lex and the filename argument.
The -t option sends lex's output to the standard output rather than to the file lex.yy.c.
The -v option prints out a small set of statistics describing the so-called finite automata that lex produces with the C program lex.yy.c. (For a detailed account of finite automata and their importance to lex, see the Aho, Sethi, and Ullman book, Compilers: Principles, Techniques, and Tools, Addison-Wesley, 1986.)
lex uses a table (a two-dimensional array in C) to represent its finite automaton. The maximum number of states that the finite automaton requires is set by default to 4000. If your lex source has a large number of rules or the rules are very complex, this default value may be too small. You can enlarge the value by placing the following entry in the definitions section of your lex source:
%n 6000This entry tells lex to make the table large enough to handle as many as 6000 states. (The -v option indicates how large a number you should choose.) If you have need to increase the maximum number of state transitions beyond 16000, the designated parameter is a, for example:
%a 20000
Compiling a lexical analyzer
The file lex.yy.c may be compiled and linked in the same way as any C program. The -ll option is used to link the object file created from this C source with lex library: cc lex.yy.c -llThe lex library provides a default main() program that calls the lexical analyzer under the name yylex(), so you do not have to supply your own main().If you have the lex specification spread across several files, you can run lex on each of them individually, but be sure to rename or move each lex.yy.c file before you run lex on the next one. Otherwise, each file overwrites the previous one. Once you have generated all the C files, you can compile all of them in one command line.To compile and link the output files produced by lex and yacc, run: cc lex.yy.c y.tab.c -ly -llNote that the yacc library is linked (with the -ly option) before the lex library (with the -ll option) to ensure that the main() program supplied will call the yacc parser.Running the lexical analyzer
By default, the lexical analyzer takes input from the standard input. To have it take input from a file, use redirection; for example: a.out < text.ina.out is the executable lexical analyzer.
Output is sent to the standard output. You can redirect this as well: a.out < text.in > text.out
No comments:
Post a Comment