flex
Generate lexical analyzers (scanners)
TLDR
Generate an analyzer from a Lex file, storing it to the file lex.yy.c
Write analyzer to stdout
Specify the output file
Generate a batch scanner instead of an interactive scanner
Compile a C file generated by Lex
SYNOPSIS
flex [options] [input]
PARAMETERS
-b
Generate backing-up information to lex.backup
-B
Use batch (non-interactive) scanner
-C[aefmry]
Control optimizations: a=meta-equivalence, e/ec, f/fm, m/meta, r=CR equiv, y=>256 equiv
-d, --debug
Enable debug mode in generated scanner
-f
Generate faster scanner (less code)
-F
Generate fastest scanner (table-driven)
-h, --help
Display help and exit
-i
Generate case-insensitive scanner
-l
POSIX lex compatibility mode
-L
Suppress '#line' directives
-n
No '#line' for yywrap()
-o, --output-file=FILE
Write output to FILE (default: lex.yy.c)
-p, --perf-report
Performance report on lex.yy.c
-P, --prefix=PREFIX
Use PREFIX instead of 'yy'
-r, --no-reentrancy
Drop reentrant (yyextra, etc.) support
-s, --nodefault
No default rule for unmatched input
-S SKELDIR
Use skeleton files from directory
-t, --stdout
Write scanner to standard output
-T
Treat input as 8-bit, warn on >255
-v, --verbose
Print summary of statistics
-V, --version
Print version information
-w, --nowarn
Suppress warning messages
--help
Display extended help
--version
Display version and exit
DESCRIPTION
Flex is a powerful tool for generating lexical analyzers (scanners) in C or C++ from high-level descriptions. It reads a specification file defining patterns (regular expressions) and actions, producing efficient code that tokenizes input streams. Widely used in compiler frontends, interpreters, and text processors, flex optimizes for speed using finite state automata.
Users define rules in a format like:
pattern action
Flex compiles these into a yywrap()-aware scanner with functions like yylex(). It supports advanced features like start conditions, symbol tables, and reentrancy. Unlike slower alternatives, flex employs techniques like backing-up minimization and equivalence class compression for performance.
Common workflow: Write lexer.l, run flex lexer.l to get lex.yy.c, compile with gcc. Integrates seamlessly with bison or yacc for parsers. Flex ensures portability across Unix-like systems and offers debugging aids.
CAVEATS
Generated code requires yywrap() or -r; large lexers may exceed memory; not thread-safe without reentrancy options; input files use flex-specific syntax differing slightly from lex.
INPUT FORMAT
Flex input consists of declarations (%{ %}), definitions, rules (pattern action;), and user code. Patterns are regex-like, e.g., [a-z]+ { return ID; }.
EXAMPLE USAGE
flex example.l
gcc lex.yy.c -o scanner
Processes example.l into executable scanner.
HISTORY
Flex originated in 1987 by Vern Paxson as a faster, open-source reimplementation of AT&T's proprietary lex. It became a GNU project in the 1990s, with major releases like 2.5.x introducing reentrancy and C++ support. Maintained actively, latest versions (2.6.x) focus on performance, Unicode, and portability.


