Tutorial 01: A Tutorial on Flex

Flex is the GNU version of lex.

Bison is the GNU version of yacc.

File Name: "my_lex_01.l"

/* File Name: my_lex_01.l
 */
%{
%}

%%

[a-z]+      {
              printf("TOKEN_a_to_z\n");
            }

[A-Z]+      {
              printf("TOKEN_A_to_Z\n");
            }

\n          {
            }

.           {
            }

%%

 

File Name: "input_file_01"

abcd
ABCD
pqrs
PQRS
1234

 

File Name: "input_file_02"

stuv XYZ abcd ABCD pqrs PQRS EFGHIJK

 

Compile and Run:

% flex my_lex_01.l          (The file "lex.yy.c" will be generated.)
% gcc lex.yy.c -ll          (The executable file "a.out" will be generated.)
% ./a.out < input_file_01   (Execute the program)
TOKEN_a_to_z
TOKEN_A_to_Z
TOKEN_a_to_z
TOKEN_A_to_Z
% ./a.out < input_file_02   (Execute the program)
TOKEN_a_to_z
TOKEN_A_to_Z
TOKEN_a_to_z
TOKEN_A_to_Z
TOKEN_a_to_z
TOKEN_A_to_Z
TOKEN_A_to_Z
%

 


last update: January 29, 2010