LEX

Published by

on

What is LEX ?

Lex is a tool which generates LEXERS which are also known as Lexical Analyzers.

Lexical Analyzer converts source program to tokens.

LEX is a character input/output stream lexical processing programme generator. From a simple text search programme that looks for patterns in its input-output file to a C compiler that converts a programme into optimised code.

LEX file is saved with .l extenstion

Structure of a LEX File

Structure of LEX is divided into 3 types

1. Specification/ Declarations

2. Recognition and Actions

3. Miscallenous Function

1. Specifications/ Declartions:

This Section Defines macro instructions and import header files which are written in C. It is also possible to write any C code here, which will be copied same into the generated source file.

2. Recognition and Actions:

This section basically associates regular expression patterns with C statements.

This is in the form of

P1{action 1}

P2{action 2}

P3{action 3}

——Pn{action n}

{action i} defines the action to be performed when Pi pattern is recognized.

3. Miscallenous Functions:

This sections contains C statements and functions that are copied exactly into the generated source file.

Without LEX

Lexical, Lexical1, Lexical2 ——- Lexical N can be any programming file. For convenience we didn’t mention any file extension.

With LEX

Format of LEX:

%% Specifications
%% Recognition and Actions
%% Functions or Miscellaneous Functions

Example of LEX Program:

/*** Definition section ***/
%{
#include <stdio.h>
%}
/*** Rules Section***/
digit[0-9]+
%%
/*** Recognition and Actions***/
digit{printf("%d is a number\n");}
%%
/*** C code sections or Miscellaneous Functions***/
int main()
{
    yylex(); /*** yylex recognized the input ***/
    return 0;
}

Output:

If this input is given to flex, it will be converted into a C file, lex.yy.c. This can be compiled into an executable which matches and outputs strings of integers. For example, given the input:

12

The program will print:

12 is a number

Leave a comment