Flex is a high-speed parser generator tool that is used to create lexical analyzers.
To get a sense of how Flex works, here are some simple examples. The first example specifies a scanner that replaces the string "username" with the user's login name:
```
%%
username printf("%s", getlogin());
```
This scanner has only one rule: "username" is the pattern and "printf" is the corresponding action. The default behavior of Flex is to copy any text not matched by a scanner to the output. Thus, in this example, the input file is copied to the output with each occurrence of "username" expanded.
The second example counts the number of characters and lines in its input:
```
int num_lines = 0, num_chars = 0;
%%
n ++num_lines; ++num_chars;
. ++num_chars;
%%
main() {
yylex();
printf("# of lines = %d, # of chars = %dn", num_lines, num_chars);
}
```
This scanner has two rules: one that matches a newline and increments both the line count and the character count, and another that matches any character other than a newline and increments only the character count. The scanner produces no output other than the final report on the counts.
A more complex example is a scanner for a toy Pascal-like language:
```
%{
#include
%}
DIGIT [0-9]
ID [a-z][a-z0-9]*
%%
{DIGIT}+ printf("An integer: %s (%d)n", yytext, atoi(yytext));
{DIGIT}+"."{DIGIT}* printf("A float: %s (%g)n", yytext, atof(yytext));
if|then|begin|end|procedure|function printf("A keyword: %sn", yytext);
{ID} printf("An identifier: %sn", yytext);
"+"|"-"|"*"|"/" printf("An operator: %sn", yytext);
"{"[^}n]*"}" /* eat up one-line comments */
[ tn]+ /* eat up whitespace */
. printf("Unrecognized character: %sn", yytext);
%%
main(argc, argv)
int argc;
char **argv;
{
++argv, --argc; /* skip over program name */
if (argc > 0) yyin = fopen(argv[0], "r");
else yyin = stdin;
yylex();
}
```
This scanner recognizes different types of tokens and reports on what it has seen. The scanner first includes a math library needed for the "atof()" function. Next, the regular expressions and corresponding C code for each type
Version 2.5.35: N/A