Flex generates scanners that recognize lexical patterns in text from regular-expression rules. It emits C source defining yylex() for use in compilers, parsers, and text processors.
Overview
Flex is a tool that builds scanners: programs which recognize lexical patterns in text. You describe patterns as regular expressions and attach code to run when each pattern matches; flex reads that specification and writes a C source file that defines a yylex() function. After compiling that output, the scanner runs your attached code whenever matching text is found.
Unmatched text can pass through unchanged when no rule applies, so simple pipelines need few rules. The same pattern-splitting step appears in compilers, interpreters, configuration readers, and log parsers; flex lets you specify patterns and actions instead of hand-writing tokenizer logic.
Scanners produced by flex can pair with parsers built for yacc or bison. Flex reimplements the traditional Unix lex tool and follows the POSIX specification for lex, with documented differences from the original lex named in the manual.
Documentation covers a short tour and full feature reference, with a single-page manual variant available. Source releases are published alongside the maintained codebase.
Key features
- Regular-expression rule input
- C yylex() code generation
- POSIX lex compatibility
- yacc and bison integration
- Per-pattern action hooks
- Pass-through default for unmatched text
- Manual and single-page docs
- Examples for sample scanners
Use cases
- Compiler front-end tokenization
- Configuration file parsing
- Log and data format scanning
- Custom text filters and replacements
- Interpreter lexical analysis