Lex (software) - Example of A Lex File

Example of A Lex 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.

/*** 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); } .|\n { /* 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.!&*2gj6

the program will print:

Saw an integer: 123 Saw an integer: 2 Saw an integer: 6

Read more about this topic:  Lex (software)

Famous quotes containing the word file:

    A common and natural result of an undue respect for law is, that you may see a file of soldiers, colonel, captain, corporal, privates, powder-monkeys, and all, marching in admirable order over hill and dale to the wars, against their wills, ay, against their common sense and consciences, which makes it very steep marching indeed, and produces a palpitation of the heart.
    Henry David Thoreau (1817–1862)