Practical - 2: Aim: Introduction To Lex Tool. Lex
Practical - 2: Aim: Introduction To Lex Tool. Lex
Practical – 2
Aim: Introduction to Lex Tool.
Lex
o Lex is a program that generates lexical analyzer. It is used with YACC parser generator.
o The lexical analyser is a program that transforms an input stream into a sequence of tokens.
o It reads the input stream and produces the source code as output through implementing the lexical
analyser in the C program.
o Firstly, lexical analyser creates a program lex.1 in the Lex language. Then Lex compiler runs the lex.1
program and produces a C program lex.yy.c.
o Finally, C compiler runs the lex.yy.c program and produces an object program a.out.
o a.out is lexical analyser that transforms an input stream into a sequence of tokens.
180220131060 | 2021
1. { definitions }
2. %%
3. { rules }
4. %%
5. { user subroutines }
Program
/*lex program to count number of words*/
%{
#include<stdio.h>
#include<string.h>
int i = 0;
%}
/* Rules Section*/
%%
([a-zA-Z0-9])* {i++;} /* Rule for counting
number of words*/
int yywrap(void){}
int main()
{
// The function that starts the analysis
yylex();
return 0;
}
Output