Tuesday 23 February 2016

Experiment No : 03




3. Implementation of Lexical Analyzer using Lex Tool
 


%{
/*******************************************************************
Program to obtain tokens from a c program using LEX

*******************************************************************/
int COMMENT = 0;
int cnt = 0;

%}


identifier[a-zA-Z][a-zA-Z0-9]*
%%


#.* {printf("\n%is a PREPROCESSOR DIRECTIVE", yytext);}

int |
float |
char |
double |
while |
for |
do |
if |
break |
continue |
void |
switch |
switch |
case |
long |
struct |
const |
typedef |
return |
else |
goto {printf("\n\t%s is a KEYWORD",yytext);}


"*/" {COMMENT = 1;}

"*/" {COMMENT = 0;}
cnt++;
}
{identifier}\(if(COMMENT){printf("\n\nFUNCTIONAL\n\t%s",yytext);}
\{ {if(!COMMENT) printf("\n BLOCK BEGINS");}



\} {if(!COMMENT)printf("\nBLOCK ENDS");}


{identifier}(\[0-9]*\])? {if(!COMMENT)printf("\n\t%s is an IDENTIFIER",yytext);}

\".*\" {if(!COMMENT)printf("\n\t% is a STRING",yytext);}

[0-9]+ {if(!COMMENT)printf("\n\t%is a NUMBER",yytext);}

\)(\;)? {if(!COMMENT)printf("\n\t");ECHO;print("\n";)}

\( ECHO;
= {if(!COMMENT)printf("\n\t%s is an ASSIGNMENT OPERATOR",yytext)}

\+|
\- {if(!COMMENT)printf("\n\t%s is an OPERATOR",yytext);}


\<=|
\>=|
\<|
==|
\> {if(!COMMENT)printf(\n\t%s is a RELATIONAL OPERATOR",yytext);}



|\n


%%
int main(int argc.char**argv)
{
if(argc>1)
{
FILE *file';
file fopen(argv[1]."r");
if(file)
{
printf(“\n Could not open %s”,argv[1];
exit(0);
}
yyin = file;
}
yylex();
printf(“ \n Total number of comments are %d”,cnt);
return 0;
}

int yywrap()
{
return 1;
}



Input File:

#include
#include

double area_of_circle(double r);

int main(int argc,char *argv[])
{
if(argc < 2)
{
printf(“ Usage: %s radius \n”,argv[0]);
exit(1);
}
else
{
/* This is a double line comment */
double radius = atof(argv[1]);
double area = area_of_circle(radius);
printf(“ Area of circle with radius %f = %f \n”,radius,area);
}
return 0;
}
























Output:

[root@localhost]# lex lexp.l
[root@localhost]# gcc lex.yy.c
[root@localhost]# ./a.out area.c

#include
#include
double is a KEYWORD
area is an INDENTIFIER
of is an INDENTIFIER
circle is an INDENTIFIER
(double is a KEYWORD
r is an INDENTIFIER
);

int is a KEYWORD
main is an INDENTIFIER
(int is a KEYWORD
argc is an INDENTIFIER
char is a KEYWORD
argv[] is an INDENTIFIER
)

BLOCK BEGINS
if is a KEYWORD
(argc is an INDENTIFIER
< is a RELATIONAL OPERATOR
2 is a NUMBER
)

BLOCK BEGINS
printf is an INDENTIFIER
(“ Usage: %s radius \n”,is a STRING
argv[0] is an INDENTIFIER
);

exit is an INDENTIFIER
(1 is a NUMBER
);

BLOCK ENDS
else is a KEYWORD


BLOCK BEGINS
double is a KEYWORD
radius is an INDENTIFIER
= is an ASSIGNMENT OPERATOR
atof is an INDENTIFIER
(argv[1]is an INDENTIFIER
);

double is a KEYWORD
area is an INDENTIFIER
= is an ASSIGNMENT OPERATOR
area is an INDENTIFIER
of is an INDENTIFIER
circle is an INDENTIFIER
(radius is an INDENTIFIER
);

printf is an INDENTIFIER
(“Area of circle with radius %f = %f \n”, is a STRING
radius is an INDENTIFIER
area is an INDENTIFIER
);


BLOCK ENDS
return is a KEYWORD
0 is a NUMBER

BLOCK ENDS
Total number of comments are 3{root@localhost]#

No comments:

Post a Comment