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
- The manual starts with a short tour and covers every feature. If you prefer to search a whole document at once, use the single-page version.
- Source releases are on the GitHub releases page. The code is kept in Git on GitHub.
- Report bugs and send patches through GitHub's issues and pull requests.
- There are three mailing lists, hosted at SourceForge: flex-announce for release announcements, flex-help for questions about using flex, and flex-devel for discussion of flex itself. Posting is allowed only from subscribed addresses. See the list archive and subscription page.