← 返回命令列表

Linux command

flex 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

Generate

flex [analyzer.l]

Specify the output file

flex -o [analyzer.c] [analyzer.l]

Example

flex -t [analyzer.l]

Example

flex -B [analyzer.l]

Example

flex -i [analyzer.l]

Example

flex -d [analyzer.l]

Compile

cc lex.yy.c -o [executable] -lfl

说明

Flex (fast lexical analyzer generator) generates programs that perform pattern-matching on text. Given a specification file describing patterns and actions (typically with .l extension), flex generates C source code implementing a lexical analyzer. The generated scanner reads input, matches it against patterns defined in the specification, and executes associated actions. This is commonly used to build tokenizers for compilers and interpreters, working alongside parser generators like bison or yacc. By default, flex generates output to lex.yy.c. The generated code requires linking with -lfl (flex library) or defining yywrap() to return 1.

参数

-o, --outfile _file_
Write generated scanner to specified file
-t, --stdout
Write scanner to stdout instead of file
-B, --batch
Generate batch scanner (optimized for throughput)
-I, --interactive
Generate interactive scanner (default)
-i, --case-insensitive
Generate case-insensitive scanner
-d, --debug
Enable debug mode in generated scanner
-b, --backup
Generate backup information file (lex.backup)
-p, --perf-report
Generate performance report to stderr
-v, --verbose
Print scanner statistics to stderr
-f, --full
Generate fast, large scanner with full tables (-Cfr).
-F, --fast
Generate fast scanner with alternate table representation (-CFr).
--header-file _file_
Generate C header file
-+, --c++
Generate C++ scanner class
--yylineno
Track line numbers in generated scanner

FAQ

What is the flex command used for?

Flex (fast lexical analyzer generator) generates programs that perform pattern-matching on text. Given a specification file describing patterns and actions (typically with .l extension), flex generates C source code implementing a lexical analyzer. The generated scanner reads input, matches it against patterns defined in the specification, and executes associated actions. This is commonly used to build tokenizers for compilers and interpreters, working alongside parser generators like bison or yacc. By default, flex generates output to lex.yy.c. The generated code requires linking with -lfl (flex library) or defining yywrap() to return 1.

How do I run a basic flex example?

Run `flex [analyzer.l]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -o, --outfile _file_ do in flex?

Write generated scanner to specified file