the lexical analyzer flex windows10 installation, use, compile and run

"Compile Project"

Today the teacher talked about the first step of the compiler works: lexical analysis.
And describes tools for lexical analysis, wherein the mentioned Flex
Flex Full --fast lexical analyzer generator, i.e. fast lexical analyzer generator

flex either input or standard input file (console, etc.)
and the file input by a regular expression, and the composition of C code
flex input file consists of three segments, two of such symbols will %% file splitting into three parts

Definitions
%%
rules
%%
user code (C code)

Wherein, the definition is divided into two: one is variable declaration, the other is a regular expression declare
variable declaration used to declare variables {%}%, the same as the C language statement format
and the regular expression statement not need to use% {%} this symbol, to directly define statement format: name expression: expression

Rules section: a line rule, each line has two parts:
the regular expression functions {operation}

Code portions: a user-defined process, directly copied to the end lex.yy.c

So get to the point, how to install the windows flex it?
Download URL: http://gnuwin32.sourceforge.net/packages/flex.htm


After entering the Web site, download "Complete package, except sources", click on the "Setup" link.

After the download is complete, you get an exe file, installed directly.
All have to set up some environment variables after the installation Path, that is, you need to add flex installation directory to the Path of the
open right click -> My Computer → Properties → Advanced System Settings -> Environment Variables -> System Variables -> Path-> Edit -> New -> enter flex specific installation directory to the bin directory -> OK
after the exit, open CMD, enter flex - V, v remember here is to be capitalized. If successful installation will display the version number of flex

Ready to flex input file find.l, to the contents of the file as follows:

%%
[+-]?[0-9]+  { printf("%s\n", yytext); }  /* Print integers /
\n     { }  /
newline /
.     { }  /
For others, do nothing */
%%

void main(){
    yylex();
}

int yywrap(){
    return 1;
}

Obviously, this code is used to match numbers, open CMD, switch the working directory to the next file, input current command:

flex find.l

Lex.yy.c will get a file, the file contents lex.yy.c very rich
and want to compile .c file you need to install the MinGW compiler in the windows (of course, compile the .c file is not only an option MinGW just here to use MinGW compiler Bale)
specific to the bin directory) added to the compiler can go directly to the official website to download, the download will need to install the same directory where the compiler (needed in Path

After the addition is complete, open CMD, and change to the directory lex.yy.c file

gcc lex.yy.c

After executing the command, the executable file will be a.exe
a.exe can accept either standard input file, the file can also accept input
accepts standard input:

Accepts file input
File Input

We can see the files written find.l indeed played a role in matching digital

Guess you like

Origin www.cnblogs.com/ASE265/p/12337922.html