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] [inputfile]
PARAMETERS
-b
Generate a backup report to flex.backup, listing scanner states which require backing up and the characters on which they do so.
-c
Is a do-nothing option, a deprecated option for POSIX compliance.
-d
Enable debug mode. The scanner will print debugging information to stderr.
-f
Specifies fast scanner. The table compression is not used, resulting in bigger tables but a faster scanner.
-i
Generate a case-insensitive scanner. Letters in the input will be matched regardless of case.
-l
Maximum compatibility with the original AT&T Lex implementation.
-n
Is a do-nothing option. It's included for POSIX compliance, but has no effect.
-p
Generate performance reports to stderr.
-s
Causes the default rule (that unmatched scanner input is echoed to stdout) to be suppressed. If the scanner encounters input that does not match any of its rules, it aborts with an error.
-t
Write the generated C code to standard output instead of creating a file.
-v or -V
Write the version number to standard error and exit.
-w
Suppress warning messages.
-8
Enable 8-bit scanner.
[inputfile]
The input file containing the flex specification. If not provided, flex reads from standard input.
DESCRIPTION
Flex is a tool for generating scanners: programs which recognize lexical patterns in text. It reads a description of the patterns to be recognized, in the form of regular expressions and associated C code, and generates a C source file containing a routine `yylex()`. When compiled and linked, this program can then scan input, matching the patterns and executing the corresponding C code. Flex is often used in conjunction with a parser generator like Bison, where Flex provides the lexical analysis and Bison handles the grammar. It's a powerful tool for building compilers, interpreters, and other text-processing applications.
Compared to the older Lex, Flex offers improvements in speed and flexibility, hence the name. It provides more control over the scanning process and handles more complex regular expressions and input handling requirements. Because of its performance and extended functionalities, Flex is very common compared to its predecessor Lex. It is generally backward compatible with Lex, but with a lot of enhancements. Flex can be also used for many text processing programs that look for patterns on an input text.
CAVEATS
Generated code heavily relies on global variables and can be difficult to integrate with other C code without careful consideration of naming conflicts.
INPUT FILE FORMAT
The input file to Flex typically consists of three sections, separated by lines containing only '%%': definitions, rules, and user code. The definitions section allows you to define named regular expressions and start states. The rules section contains regular expressions and corresponding C code to execute when a match is found. The user code section allows you to add additional C code to the generated scanner, such as helper functions or the `main()` function.
REGULAR EXPRESSIONS
Flex supports a wide range of regular expression operators, including character classes, repetition operators, alternation, and anchoring. These allow you to define complex patterns to match in the input text. Consult the Flex documentation for a complete list of supported operators.
HISTORY
Flex was created by Vern Paxson in 1987 as a faster and more flexible alternative to the original Lex program developed at Bell Labs. Its development was driven by the need for better performance and extended functionality in lexical analysis tools. Over the years, Flex has become a standard tool in compiler construction and other text processing applications, widely used and actively maintained.