Lex is a program that generates lexical analyzers ("scanners" or "lexers"). Lex is commonly used with the yacc parser generator. Lex, originally written by Eric Schmidt and Mike Lesk, is the standard lexical analyzer generator on Unix systems, and is included in the POSIX standard. Lex reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in the C programming language.
Though traditionally proprietary software, versions of Lex based on the original AT&T code are available as open source, as part of systems such as OpenSolaris and Plan 9 from Bell Labs. Another popular open source version of Lex is Flex, the "fast lexical analyzer".
/*** Definition section ***/%{ /* C code to be copied verbatim */
%}
- include
/* This tells flex to read only one input file */ %option noyywrap
%% /*** Rules section ***/
/* *+ matches a string of one or more digits */ *+ { /* yytext is a string containing the matched text. */ printf("Saw an integer: %s\n", yytext); }
. { /* Ignore all other characters. */ }
%% /*** C Code section ***/
int main(void) { /* Call the lexer, then quit. */ yylex(); return 0; }
If this input is given to flex, it will be converted into a C file, lex.yy.c. This can be compiled into an executable which matches and outputs strings of integers. For example, given the input: abc123z.!&*2ghj6 the program will print: Saw an integer: 123 Saw an integer: 2 Saw an integer: 6
Lex (Informatik) | Herramienta de programación Lex | Lex | Lex (informatyka)
This article is licensed under the GNU Free Documentation License.
It uses material from the
"Lex programming tool".
Home Page • arts • business • computers • games • health • hospitals • home • kids & teens • news • physicians • recreation• reference • regional • science • shopping • society • sports • world