0% found this document useful (0 votes)
62 views6 pages

Compiler Construction: Lab Report # 07

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. FLEX is used to generate lexical analyzers by taking input from a .l file and producing a yylex() function. In the procedure, a FLEX program was written to process a Pascal-like toy language by matching tokens like integers, floats, identifiers, keywords, operators, and comments, and printing unrecognized characters.

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)
62 views6 pages

Compiler Construction: Lab Report # 07

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. FLEX is used to generate lexical analyzers by taking input from a .l file and producing a yylex() function. In the procedure, a FLEX program was written to process a Pascal-like toy language by matching tokens like integers, floats, identifiers, keywords, operators, and comments, and printing unrecognized characters.

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/ 6

COMPILER CONSTRUCTION

LAB REPORT # 07

Submitted To:

Sir Shahzad Arif

Submitted By:

Malik M.Shahzaib

Reg No:

17-CS-036
Lab #06

LEXICAL ANALYSIS USING FLEX

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 a flex program to process a pascal-like toy language with the following specifications:

• Match integers and floating point constants


• Match Identifiers, starting with lower-case alphabets and allowing for integers in non-
starting locations.
• Keywords: if, then, begin, end, procedure, function
• Operators: +, -, *, /
• Skipping of white-space characters i.e. new-line, tabs and spaces
• Printing of un-recognized characters

Use the following example code to test your lexical analyser.

procedure compute

begin

area = 3.141 * radius * radius

end

function main

begin

compute

end

Input:

Exam.txt:

procedure compute

begin

area = 3.141 * radius * radius


end

function main

begin

compute

end

Flex File:

%{

#include <math.h>

int count=0;

%}

digit [0-9]

ID [a-z][a-z0-9]*

%%

if|then|begin|end|procedure|function {

printf( "A keyword: %s\n", yytext );

{digit}+ {

printf( "An integer: %s (%d)\n", yytext,

atoi( yytext ) );

{digit}+"."{digit}* {
printf( "A float: %s (%g)\n", yytext,

atof( yytext ) );

{ID} printf( "An identifier: %s\n", yytext );

"+"|"-"|"*"|"/" printf( "An operator: %s\n", yytext );

"{"[^}\n]*"}" /* eat up one-line comments */

[ \t\n]+ /* eat up whitespace */

. printf( "Unrecognized character: %s\n", yytext );

%%

int yywrap(){}

int main( )

extern FILE *yyin;

yyin = fopen("Exam.txt", "r");

yylex();

return 0;

}
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