Lab 2
Lab 2
Introduction To Flex
Lesson objective
At the end of the lesson student will able to:
be familiar with Lexical analysis using Flex
and the process how to create tokens
Flex and lexical analysis
From the area of compilers, we get a host of tools
to convert text files into programs. The first part of
that process is often called lexical analysis,
particularly for such languages as C / C++…etc.
6
Flex file format
Definition section
%%
Rule section
%%
auxiliary procedures
flex input files are structured as follows( flex
specifications)
The flex input file consists of three sections
separated by a line with just %%
%{
declarations
%}
regular definitions
%%
translation rules
%%
auxiliary procedures (user subroutines)
Definitions is structured as follows:
Declarations of ordinary variables ,constants,
%{ Include header files …
Declaration of some global variables
Declarations
%}
%{
%option directive /* This is a comment inside the
definition*/
Regular definition #include <math.h> //may need headers
#include <iostream> // for cout
%}
int main()
{
yylex();
}
13
Example
[ \t\n] { /* no action and no return */ }
if {cout<”keyword found”;}
else {cout<”keyword found”;}
[A_Za-z_][A-Za-z0-9_]+ {cout<<“”ID found”;}
[0-9]+ {cout<<”integer found”;}
“<=” {cout<<“relop found”;}
“==” {cout<<”relop found”;}
...
%%
14
option directive
1. Maintaining Line Number :
Flex allows to maintain the number of the current line in
the global variable yylineno using the following option
mechanism
%option yylineno
2. Removes the call to the routine yywrap()
%option noyywrap
- is called whenever flex reaches an end-of-file(eof)
- It is an option not include yywrap()
- indicating this is the end of the file or no more file content
16
flex Library Routines
• yylex()
– The default main() contains a call of yylex()
• yywarp()
– is called whenever flex reaches an end-of-file(eof)
– The default yywarp() always returns 1
• yymore()
– return the next token
• yyless(n)
– retain the first n characters in yytext
17
yylex()
• Most programs with flex scanners use the
scanner to return a stream of tokens that are
handled by a parser
• Each time the program needs a token, it calls
yylex(), which reads a little input and returns the
token
yywrap()
• Used to continue reading from another file
• It is called at EOF
• U can then open another file and return 0 or
• U can return 1, indicating this is the end or
• U can use an option not include it
Reading from a file
• Flex reads its input from a global pointer to a C
FILE variable called yyin
• yyin is set to STDIN by default
• So all we have to do is set that pointer to our file
handle
-FILE*myfile=fopen(“filename","r");
-If(! myfile) {…}//cout<<"Error opening file"<<endl;
// return -1
- yyin=myfile;
- yylex();
How the input is matched