0% found this document useful (0 votes)
159 views5 pages

Lexical Analysis Lab Report

This lab report discusses implementing lexical analysis using FLEX. The objectives were to download and install FLEX, implement codes in FLEX, and perform tasks from the lab manual. Regular expressions were written to remove C-style comments and recognize C++ style comments. A FLEX input file was created containing regular expressions to test recognizing different comment styles in a sample code file. The output showed the tokens generated after removing the comments.

Uploaded by

malik shahzaib
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)
159 views5 pages

Lexical Analysis Lab Report

This lab report discusses implementing lexical analysis using FLEX. The objectives were to download and install FLEX, implement codes in FLEX, and perform tasks from the lab manual. Regular expressions were written to remove C-style comments and recognize C++ style comments. A FLEX input file was created containing regular expressions to test recognizing different comment styles in a sample code file. The output showed the tokens generated after removing the comments.

Uploaded by

malik shahzaib
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/ 5

COMPILER CONSTRUCTION

LAB REPORT # 08

Submitted To:

Sir Shahzad Arif

Submitted By:

Malik M.Shahzaib

Reg No:

17-CS-036
Lab #08

LEXICAL ANALYSIS USING FLEX #02

Objectives:

 To download and install flex in system.


 To implement different codes in flex.
 To perform lab tasks given in lab manual.

Software tools:
 Command Prompt
 Flex

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 ) :

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:

Write regular expressions using starting conditions to remove C-style comments e.g. /* This is a

comment */

C++ supports another style of comments i.e. starting by //.

Regular Expressions:

\/\/(.*)

\/\*(.*\n)*.*\*\/

Task 02:

Write a regular expression to recognize this kind of comment and build it into flex input file. Test
the scanner generated by flex using a C program.

Input File:

/*** // comment 1 */ \

/* comment 2 */

//This is another book

/** comment 3 continue ***/

gogo

This is a book

byebye

// C++ Comment for Test

Really bye bye


Flex Code:

%{

#include<stdio.h>

%}

%%

\/\/(.*)

\/\*(.*\n)*.*\*\/

%%

int yywrap()

{}

int main()

FILE *fp;

char filename[50];

printf("Enter file :");

scanf("%s",filename);

fp=fopen(filename,"r");

yyin=fp;

yylex();

}
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.

=======================================================

You might also like