Experiment No. 9 3118013: Aim: Theory: Lexical Analyzer
Experiment No. 9 3118013: Aim: Theory: Lexical Analyzer
9 3118013
AIM: To study and implement experiment using Lexical analyzer tool: FLEX
THEORY:
LEXICAL ANALYZER
Lexical analysis or scanning is the process where the stream of characters making
up the source program is read from left-to-right and grouped into tokens. Tokens are
sequences of characters with a collective meaning. There are usually only a small number of
tokens for a programming language: constants (integer, double, char, string, etc.), operators
(arithmetic, relational, logical), punctuation, and reserved words.
The Lexical Analyzer is the interface between the source program and the
compiler. The lexical Analyzer reads the source program one character at a time
and can be treated as a single entity. The lexical analyzer (scanner) is the first phase
of a compiler. Its main task is to read the input characters and produce as output a
sequence of tokens that the parser uses for syntax analysis.
Source Program
Lexical Parser
Analyzer
Symbol
Table
The lexical analyzer is a subroutine of the parser i.e. scanner will operate
under the control of the parser. Upon receiving a “get next token” command from
the parser, the lexical analyzer reads input characters from it until it can identify the
next token.
Flex can only generate code for C and C++. To use the scanner code
generated by flex from other languages a language binding tool such as SWIG can
be used.
A similar lexical scanner for C++ is flex++, which is included as part of the
flex package. At the moment, flex supports generating code only for C and C++.
The generated code does not depend on any runtime or external library except for
a memory allocator (malloc or a user-supplied alternative) unless the input also
depends on it. This can be useful inembedded and similar
situations where traditional operating system or C runtime facilities may not
be available.
The flex++ classes and code require a C++ compiler to create lexical and
pattern-matching programs. The flex++ generated C++ scanner includes the header
file FlexLexer.h, which defines the interfaces of the two C++ generated classes.
How to use flex:
1. Format:
definitions
%%
rules
%% user
code
2. The definitions section: "name definition"
The rules section: "pattern The user code action"
section: "yylex() routine"
1. Try sample*.lex
2. Command Sequence:
flex sample*.lex
gcc lex.yy.c -lfl
./a.out
Code:
lex.l
%{
int total =0 ;
%}
%option noyywrap
%%
("int")|("if")|("else")|("while")|("do")|("break")|("continue")|("double")|("float")|("return")|("E
OF") {total++; fprintf(yyout,"This is Keyword: %s\n\n",yytext);}
("return")|("char")|("case")|("sizeof")|("long")|("short")|("typedef")|("switch")|("unsigned")|("v
oid")|("static")|("struct")|("goto") {total++;fprintf(yyout,"This is Keyword:%s\n",yytext);}
. {fprintf(yyout,"",yytext);}
[\t\n]+
%%
main()
{
extern FILE *yyin, *yyout;
yylex();
return 0;
}
input.txt
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char str[100],word[30];
int i,j,found,slen,wlen,index;
slen=strlen(str);
wlen=strlen(word);
index=-1;
for(i = 0; i < slen; i++){
found = 1;
if(str[ i + j ] != word[j]){
found = 0;
break;
}
}
if(str[i + j] != ' ' && str[i + j] != '\t' && str[i + j] != '\n' && str[i + j] != '\0')
{
found = 0;
}
if(found == 1){
for(j=i;j<=slen-wlen;j++){
str[j]=str[j+wlen];
}
Output.txt
his is Pre-processor directive: #include <stdio.h>
This is Delimiter: )
This is Delimiter: {
This is Keyword:char
This is Identifier: str
This is Delimiter: [
This is Delimiter: ]
This is Delimiter: ,
This is Identifier: word
This is Delimiter: [
This is Integer: 30
This is Delimiter: ]
This is Delimiter: ;
This is Identifier: i
This is Delimiter: ,
This is Identifier: j
This is Delimiter: ,
This is Delimiter: ,
This is Delimiter: ,
This is Delimiter: ,
This is Delimiter: ;
This is Delimiter: )
This is Delimiter: ;
This is Delimiter: )
This is Delimiter: ;
This is Delimiter: )
This is Delimiter: ;
This is Delimiter: )
This is Delimiter: ;
This is Delimiter: )
This is Delimiter: ;
This is Delimiter: )
This is Delimiter: ;
This is Identifier: index
This is Delimiter: ;
This is Identifier: i
This is Integer: 0
This is Delimiter: ;
This is Identifier: i
This is Delimiter: ;
This is Identifier: i
This is Delimiter: )
This is Delimiter: {
This is Integer: 1
This is Delimiter: ;
This is Identifier: j
This is Assignment Operator: =
This is Integer: 0
This is Delimiter: ;
This is Identifier: j
This is Delimiter: ;
This is Identifier: j
This is Delimiter: )
This is Delimiter: {
This is Delimiter: [
This is Identifier: i
This is Identifier: j
This is Delimiter: ]
This is Delimiter: [
This is Identifier: j
This is Delimiter: ]
This is Delimiter: )
This is Delimiter: {
This is Integer: 0
This is Delimiter: ;
This is Delimiter: ;
This is Delimiter: }
This is Delimiter: }
This is Delimiter: [
This is Identifier: i
This is Identifier: j
This is Delimiter: ]
This is Identifier: i
This is Identifier: j
This is Delimiter: ]
This is Identifier: t
This is Delimiter: [
This is Identifier: i
This is Identifier: j
This is Delimiter: ]
This is Identifier: n
This is Delimiter: [
This is Identifier: i
This is Identifier: j
This is Delimiter: ]
This is Integer: 0
This is Delimiter: )
This is Delimiter: {
This is Integer: 0
This is Delimiter: ;
This is Delimiter: }
This is Integer: 1
This is Delimiter: )
This is Delimiter: {
This is Function: for(
This is Identifier: j
This is Identifier: i
This is Delimiter: ;
This is Identifier: j
This is Delimiter: ;
This is Identifier: j
This is Delimiter: )
This is Delimiter: {
This is Delimiter: [
This is Identifier: j
This is Delimiter: ]
This is Delimiter: [
This is Identifier: j
This is Arithmatic Operator: +
This is Delimiter: ]
This is Delimiter: ;
This is Delimiter: }
This is Delimiter: }
This is Delimiter: }
This is Delimiter: ,
This is Delimiter: ,
This is Delimiter: )
This is Delimiter: ;
This is Delimiter: }
Command prompt:
C:\Users\Husain\OneDrive\Desktop\GnuWin32\Flex>flex lex.l
C:\Users\Husain\OneDrive\Desktop\GnuWin32\Flex>gcc lex.yy.c
C:\Users\Husain\OneDrive\Desktop\GnuWin32\Flex>a.exe