| Lex Programming Tool |
Article Index for Lex |
Website Links For Lex |
Information AboutLex Programming Tool |
| CATEGORIES ABOUT LEX PROGRAMMING TOOL | |
| compiling tools | |
| parser generators | |
| SHOPPER'S DELIGHT | |
|
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". STRUCTURE OF A LEX FILE The structure of a lex file is intentionally similar to that of a yacc file; files are divided up into three sections, separated by lines that contain only two percent signs, as follows: ''Definition section'' %% ''Rules section'' %% ''C code section''
EXAMPLE FLEX FILE The following is an example lex file for the Flex version of lex. It recognizes strings of numbers (integers) in the input, and simply prints them out.
%{
#include %}
%option noyywrap %%
{Link without Title} + {
printf("Saw an integer: %s ", yytext); }
%%
int main(void) {
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:
the program will print: Saw an integer: 123 Saw an integer: 2 Saw an integer: 6 USING LEX WITH YACC Lex and Yacc (a parser generator) are commonly used together. Yacc uses a Formal Grammar to parse an input stream, something which Lex cannot do using simple Regular Expression s (Lex is limited to simple Finite State Automata ). However, Yacc cannot read from a simple input stream - it requires a series of tokens. Lex is often used to provide Yacc with these tokens. SEE ALSO |