FLEX

  • FLEX (fast lexical analyzer generator) is a tool/computer program for generating lexical analyzers (scanners or lexers) written by Vern Paxson in C around 1987.
  • It is used together with Berkeley Yacc parser generator or GNU Bison parser generator. Flex and Bison both are more flexible than Lex and Yacc and produces faster code.
  • Bison produces parser from the input file provided by the user.
  • The function yylex() is automatically generated by the flex when it is provided with a .l file and this yylex() function is expected by parser to call to retrieve tokens from current/this token stream.
  • Note: The function yylex() is the main flex function which runs the Rule Section and extension (.l) is the extension used to save the programs.

Installing Flex on Ubuntu:

sudo apt-get update
sudo apt-get install flex
  • Note: If Update command is not run on the machine fom a while, it’s better to run it first so that a newer version is installed as an older version might not work with the other packages installed or may not be present now.
  • Given image describes how the Flex is used:

  1. Step 1: An input file describes the lexical analyzer to be generated named lex.l is written in lex language. The lex compiler transforms lex.l to C program, in a file that is always named lex.yy.c.
  2. Step 2: The C complier compile lex.yy.c file into an executable file called a.out.
  3. Step 3: The output file a.out take a stream of input characters and produce a stream of tokens.
  • Program Structure:

In the input file, there are 3 sections:

  • 1. Definition Section: The definition section contains the declaration of variables, regular definitions, manifest constants. In the definition section, text is enclosed in “%{ %}” brackets. Anything written in this brackets is copied directly to the file lex.yy.c

Syntax:

%{
   // Definitions
%}
  • 2. Rules Section: The rules section contains a series of rules in the form: pattern action and pattern must be unintended and action begin on the same line in {} brackets. The rule section is enclosed in “%% %%”.

Syntax:

%%
pattern  action
%%
Posted on by