flex

The fast lexical analyzer generator. Current version: 2.6.5-rc1.

What flex is

flex is a tool for generating scanners. A scanner is a program that recognizes lexical patterns in text. You describe the patterns as regular expressions, and attach a piece of code to each one. flex reads that description and writes a source file, normally in C, that defines a function called yylex(). Compile that file and you have a scanner. Whenever it finds text matching one of your patterns, it runs the code you attached to that pattern.

A small example

This flex input copies its input to its output, replacing each occurrence of the word username with the name of the person running the program:

%%
username    printf( "%s", getlogin() );

Any text that no rule matches is copied through unchanged, so one rule is all it takes.

Why you might want it

Splitting text into meaningful pieces is the first step in many programs. Compilers and interpreters do it to turn source code into words, numbers and symbols before they work out what the program means. The same job comes up in programs that read configuration files, log files and data formats. Writing that code by hand is slow and easy to get wrong. With flex you write only the patterns and what to do when each one matches.

flex scanners can be used with a parser written for yacc or bison, the usual way to build a compiler front end. flex is a rewrite of the traditional Unix lex tool and is compliant with the POSIX specification for it. The manual lists the places where flex and the original lex differ.

Where to go next