Professional Documents
Culture Documents
LAB REPORT # 06
Submitted To:
Submitted By:
Malik M.Shahzaib
Reg No:
17-CS-036
Lab #06
Objectives:
Theory:
The lexical analyser is the part of the compiler that reads the source text, it may also perform
certain secondary tasks at the user interface. One such task is stripping out comments and white
space in the form of blanks, tabs and new line characters, from the source program. Another is
correlating error messages from the compiler with the source program i.e. keeping a
correspondence between errors and source line numbers.
Description:
Lexical analysis is the process of converting a sequence of characters into a sequence of tokens. A
program or function which performs lexical analysis is called a lexical analyser, lexer or scanner.
A lexer often exists as a single function which is called by a parser or another function.
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.
Procedure:
Task 01:
Input:
%{
int length = 0;
%}
%%
%%
int yywrap(){}
int main(){
yylex();
return 0;
}
Output:
Task 02:
Write a program in lex language to Count the number of characters and number of lines in the
input.
Input:
%{
#include<stdio.h>
%}
%%
\n { lines++;}
[A-Z] characters++;
[a-z] characters++;
[0-9] characters++;
. characters++;
%%
main(void)
{
yyin= fopen("myfile.txt","r");
yylex();
int yywrap()
return(1);
Output:
Conclusion:
In this lab we learned to implement codes for lexical analysis. We implemented different codes in
flex to understand it’s working and at the end of the lab session we performed different tasks to
test our concepts.
=======================================================