Lex/Flex generates lexical analyzers, also called scanners, which read input text and split it into meaningful pieces called tokens. This is the first step in compiling source code.
Lex and Flex generate C code for lexical analyzers, so the actions in the specification are written in C or C++.
Lex/Flex uses the longest match rule: it selects the pattern that matches the longest portion of the input text. If there is a tie, it chooses the rule that appears first.
Flex is an open-source tool that is generally faster and more flexible than the original proprietary Lex tool.
%{
#include <stdio.h>
%}
%%
[a-z]+ { printf("Word\n"); }
[0-9]+ { printf("Number\n"); }
. { /* ignore other characters */ }
%%
int main() {
yylex();
return 0;
}The input "abc123" is scanned left to right. The pattern [a-z]+ matches "abc" first, printing "Word". Then [0-9]+ matches "123", printing "Number".
