Ex. No: 3
Date:
3. Implementation
of Lexical Analyzer using Lex Tool
AIM:
To write a lex
program to implement the lexical analyzer
INTRODUCTION
Lex is officially
known as a "Lexical Analyzer". It’s main job is to break up an input
stream into more into meaningful units, or tokens. For example, consider
breaking a text file up into individual words. More pragmatically, Lex is a
tool for automatically generating a lexer ( also known as scanner)
starting from a lex specification.
Lex specifications:
A Lex program (the .l file ) consists
of three parts:
declarations
%%
translation rules
%%
auxiliary procedures
1. The declarations section
includes declarations of variables, manifest constants (A manifest constant is
an identifier that is declared to represent a constant. e.g. # define PIE
3.14), and regular definitions.
2. The translation rules of a
Lex program are statements of the form :
p1 {action 1}
p2 {action 2}
p3 {action 3}
… …
… …
where each p is a regular
expression and each action is a program fragment describing what
action the lexical analyzer
should take when a pattern p matches a lexeme. In Lex the actions are written
in C.
3. The third section holds whatever auxiliary procedures are needed by
the actions.
Alternatively, these
procedures can be compiled separately and loaded with the lexical
analyzer.
A Lex program has the
following form:
declarations
%%
translation rules
%%
auxiliary functions
The declarations section
includes declarations of variables, manifest constants and regular
definitions.
The translation rules each
have the form Pattern { Action }
Each pattern is a regular expression, which may use the regular definitions of the
declaration section. The actions are fragments of code, typically written in C,
although many variants of Lex using other languages have been created.
STEP BY STEP PROCEDURE FOR
RUNNING A LEX AND YACC PROGRAM
For compiling a
lex program
1. write the lex program in a file and save it as
file.l (where file is the name of the file).
2. open the terminal and navigate to the directory
where you have saved the file.l
3. type - lex
file.l
4. then type - cc
lex.yy.c
5. then type - ./a.out
or a.exe
For compiling
lex and yacc together
1. write lex program in a file file.l and yacc in a
file file.y
2. open the terminal and navigate to the directory where
you have saved the files.
3. type lex
file.l
4. type yacc
file.y
5. type cc
lex.yy.c y.tab.h
6. type ./a.out
or a.exe
ALGORITHM:
1.Start the program
2. Pass
file.c via command line arguments as Input file and include the yylex()
tool for input scanning.
3.Define the alphabets and numbers
4.Print the preprocessor, function, keyword using
yytext.lex tool
5.Print the relational, assignment and all the
operator using yytext() tool
6.Also scan and print where the loop ends and
begins.
7.Use yywrap() to enter an error.
8.Stop the program.
PROGRAM
CDP3.l (Save the following code snippets in CDP3.l)
%{
int COMMENT=0;
%}
identifier [a-zA-Z][a-zA-Z0-9]*
%%
#.* { printf("\n%s is a PREPROCESSOR
DIRECTIVE",yytext);}
int |
float |
char |
double |
while |
for |
do |
if |
break |
continue |
void |
switch |
case |
long |
struct |
const |
typedef |
return |
else |
goto {printf("\n\t%s is a
KEYWORD",yytext);}
"/*" {COMMENT = 1;}
"*/" {COMMENT = 0;}
{identifier}\(
{if(!COMMENT)printf("\n\nFUNCTION\n\t%s",yytext);}
\{ {if(!COMMENT) printf("\n BLOCK
BEGINS");}
\} {if(!COMMENT) printf("\n BLOCK ENDS");}
{identifier}(\[[0-9]*\])? {if(!COMMENT)
printf("\n %s IDENTIFIER",yytext);}
\".*\" {if(!COMMENT) printf("\n\t%s
is a STRING",yytext);}
[0-9]+ {if(!COMMENT) printf("\n\t%s is a
NUMBER",yytext);}
\)(\;)? {if(!COMMENT)
printf("\n\t");ECHO;printf("\n");}
\( ECHO;
= {if(!COMMENT)printf("\n\t%s is an ASSIGNMENT
OPERATOR",yytext);}
\<= |
\>= |
\< |
== |
\> {if(!COMMENT) printf("\n\t%s is a
RELATIONAL OPERATOR",yytext);}
%%
int main(int argc,char **argv)
{
if (argc > 1)
{
FILE *file;
file = fopen(argv[1],"r");
if(!file)
{
printf("could not open %s \n",argv[1]);
exit(0);
}
yyin = file;
}
yylex();
printf("\n\n");
return 0;
} int yywrap()
{
return 0;
}
ODDREVEN.C
#include <stdio.h>
int main()
{
int
number;
printf("Enter an integer: ");
scanf("%d", &number);
if(number
% 2 == 0)
printf("%d is even.", number);
else
printf("%d is odd.", number);
return 0;
}
═══════════=============Output
═════════════════════════
D:\Mohanraj\CS6612 COMPILER LAB\lex_yacc>flex
CDP3.l
D: \Mohanraj \CS6612 COMPILER LAB\lex_yacc>gcc
lex.yy.c
D: \Mohanraj \CS6612 COMPILER LAB\lex_yacc>a.exe
ODDREVEN.c
#include <stdio.h> is a PREPROCESSOR DIRECTIVE
int is
a KEYWORD
FUNCTION
main(
)
BLOCK BEGINS
int is
a KEYWORD
number
IDENTIFIER;
FUNCTION
printf(
"Enter an integer: " is a STRING
);
FUNCTION
scanf(
"%d" is a STRING, &
number
IDENTIFIER
);
FUNCTION
if(
number
IDENTIFIER %
2 is a
NUMBER
== is
a RELATIONAL OPERATOR
0 is a
NUMBER
)
FUNCTION
printf(
"%d is even." is a STRING,
number
IDENTIFIER
);
else
is a KEYWORD
FUNCTION
printf(
"%d is odd." is a STRING,
number
IDENTIFIER
);
return
is a KEYWORD
0 is a
NUMBER;
BLOCK ENDS
( Ctl+C
to Stop Program Execution)
RESULT:
Thus the C program to implement the lexical analyzer using LEX tool was executed and the output is verified.
But it is not eliminating the single line comments.
ReplyDeletepoda k***a
DeleteHOW THIS IS ELIMINATING THAT
Deletejzjx
ReplyDeleteith nth myr
ReplyDeletepazham ann bro
Deleteok bro u take it
Deletenee aarada
ReplyDeletenee aarada myre
Deleteninta thantha
Deletehi
ReplyDeletehi maniesh
ReplyDeletebig fan broo
Deletemaniesh p**da
DeletePAZHAM VANUMA PRAGAS PATHY
DeleteSREE HRISH OMMBURIYA
Deletemaniesh ombula pundamone
DeleteONAKU IRUTHA THANA OOMBURATHU SREE HARISH
Deletesree harish lavade kabali
Deletearee yaar
Deleteeruku bro nee vanthu ombu pragaspathy
DeletePRAGAS PATHY LAVADE KA BAAL
DeleteYETHUKU SYSTEM OFF PANA SREE HARISH PU**A
Deleteomaala hrish
Deletenaai inte mone , hari
Deletepathy thevidiya
Deleteok
ReplyDeleteok ia m pathy
ReplyDeleteda naayinte mone
ReplyDeleteninte aachan
Deleteninta thanthayaada poori mone
Deletepragaspahty copera
ReplyDeletepragaspathy polaiyadi mone
ReplyDeletepragaspathy p***a 077
ReplyDeleteNUMBER SEND PANUDA POTTA HARISH
ReplyDelete8056810079 onaka num tha polaiyadi
Deletefun chat : 9150549415
ReplyDeletefun chat : 9150549415
ReplyDelete7645327855
ReplyDelete8056810079 blowjob
ReplyDeletehorny guys
ReplyDeletecall my fnd suneesh vinayakam
ReplyDeletesorry
Deletepoda dei
Deletecse
ReplyDeletehiiiiiiiiiiiiiiiiiiiiiiiii
ReplyDeleteEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
ReplyDelete