ASSIGNMENT
SUBMITTED BY
SOORYA M P
C2PSCS2116
SUBMITTED TO
MIDHUN SHAJI
Assistant Professor
Mary Matha Arts and Science College Mananthavad
1
Lex
Lex is a tool or a computer program that generates Lexical Analyzers (converts
the stream of characters into tokens). The Lex tool itself is a compiler. The Lex
compiler takes the input and transforms that input into input patterns. It is
commonly used with YACC(Yet Another Compiler Compiler). It was written
by Mike Lesk and Eric Schmidt.
Function of Lex
1. In the first step the source code which is in the Lex language having the file
name ‘File.l’ gives as input to the Lex Compiler commonly known as Lex to get
the output as lex.yy.c.
2. After that, the output lex.yy.c will be used as input to the C compiler which
gives the output in the form of an ‘a.out’ file, and finally, the output file a.out
will take the stream of character and generates tokens as output.
lex.yy.c: It is a C program.
File.l: It is a Lex source program
a.out: It is a Lexical analyzer
2
Structre of lex program
A Lex program consists of three parts and is separated by %% delimiters:-
Declarations
%%
Translation rules
%%
Auxiliary procedures
Declarations: The declarations include declarations of variables.
Transition rules: These rules consist of Pattern and Action.
Auxiliary procedures: The Auxilary section holds auxiliary functions
used in the actions.
For example:
declaration
number[0-9]
%%
translation
if {return (IF);}
%%
auxiliary function
int numberSum()
3
Compiling a Lex Program involves several steps:
1.Write the Lex Program: Create a file with a .l extension that contains your Lex
program. This program defines regular expressions and actions to recognize and
handle tokens in input text.
2.Generate the Lexical Analyzer: Use the lex command to generate a C source
file from your Lex program. For example:
lex your_program.l
This will typically generate a file named 'lex.yy.c'.
3.Compile the C Source File: Use a C compiler (e.g., gcc) to compile the
generated C source file:
gcc lex.yy.c -o lexer
4.Run the Lexical Analyzer: Execute your compiled lexer on input text to
perform lexical analysis. For example:
./lexer input.txt
5. Interpret or Process the Output: Depending on your Lex program's purpose,
you may need to further process or interpret the output produced by the lexer.
4
/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*/
"\n" {printf("%d\n", i); i = 0;}
%%
int yywrap(void){}
int main()
// The function that starts the analysis
yylex();
return 0;
5
How to execute
Type lex lexfile.l
Type gcc lex.yy.c.
Type ./a.out
6
Reference:
[1]: https://www.geeksforgeeks.org/what-is-lex-in-compiler-design/
[2]: https://www.codingninjas.com/studio/library/lex-in-compiler-design