0% found this document useful (0 votes)
110 views9 pages

Introduction To LEX

LEX is a tool that generates programs to perform pattern matching on text. It reads input files or standard input and generates a C source file defining a yylex() routine. LEX programs have three sections divided by "%%": declarations, rules, and user subroutines. Rules specify patterns to match and actions to perform. Running LEX compiles the program into a C file which can then be compiled and executed to perform pattern matching and call user-defined subroutines.

Uploaded by

Kushal Nehete
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
110 views9 pages

Introduction To LEX

LEX is a tool that generates programs to perform pattern matching on text. It reads input files or standard input and generates a C source file defining a yylex() routine. LEX programs have three sections divided by "%%": declarations, rules, and user subroutines. Rules specify patterns to match and actions to perform. Running LEX compiles the program into a C file which can then be compiled and executed to perform pattern matching and call user-defined subroutines.

Uploaded by

Kushal Nehete
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Introduction to LEX

LEX is a tool for generating programs that perform


pattern-matching on text. More precisely LEX reads
• the given input files
• or its standard input if no file names are given,
• for a description of a scanner to generate in form of a C
source file, lex.yy.c, which defines a routine yylex().
Format of lex program

• Input to Lex is divided into three sections with %% dividing the sections.
This is best illustrated by example. The first example is the shortest possible
lex file:
%%

• Input is copied to output one character at a time. The first %% is always


required, as there must always be a rules section. However if we don’t
specify any rules then the default action is to match everything and copy it to
output. Defaults for input and output are stdin and stdout, respectively.
Lex program to count number of vowels and consonant
%{
#include<stdio.h>
int v=0,c=0;
%}

%%
[aeiouAEIOU] { v++;}
[a-zA-Z] { c++; }
%%

main()
{
printf("ENTER INTPUT : \n");
yylex();
printf("VOWELS=%d\nCONSONANTS=%d\n",v,c);
}
How to run lex file
go to the program directory and run the following commands
1. lex filename.l (Compiling lex file)
2. cc lex.yy.c –ll (Compiling c file)
3. ./a.out (Running c code)
this will show the out put of your program.
lex Output
• The goal of lex is to generate the code for a C function named yylex(). This
function is called with no operands. It returns an int value. A value of 0 is
returned when end-of-file is reached; otherwise, yylex() returns a value
indicating what kind of token was found.
• lex also creates two important external data objects: A string named yytext.
This string contains a sequence of characters making up a single input
token. The token is read from the stream yyin, which, by default, is the
standard input (stdin). An integer variable named yyleng. This gives the
number of characters in the yytext string.
• In most lex programs, a token in yytext has an associated value that must
be calculated and passed on to the supporting program. By convention,
yacc names this data object yylval. For example, if yylex() reads a token
which is an integer, yytext contains the string of digits that made up the
integer, while yylval typically contains the actual value of the integer. By
default, yylval is declared to be an int, but there are ways to change this
default.

You might also like