{
BvUuee
J
VIKSGEBIUA
v0
Ex No:2. IMPLEMENTATION OF LEXICAL AN
TOOL
ALYZER USING L!
DATE: (9 Hs (22
AIM:
‘To implement the lexical analyzer using lex tool for a subset of C language.
ALGORITHM:
1. Start the program,
2. Declare necessary variables and ere
3, Print the pre processor or directives, keywords by analysis of the input program.
4.In the program check whether there are arguments.
5. Declare a file and open it as read mode.
6, Read the file and if any taken in source program matches with RE that all
retumed as integer value.
7, Print the token identified usi
YYdex() function,
8. Stop the program .
PROGRAM:#.* {printf{"\n%s is a preprocessor directive" yytext);}
int|
float |
char |
double |
while |
do|
if|
break |
continue |
void |
switch |
return |
else |
goto {printi("Wn%s is a keyword" yytext);}
{identifier)\( {print{("\n function %s",yytext);}
\f {print{("\nblock begins"); =~
\} (printf("\nblock ends"); a
{identifier) (\{[0-9]*\})* {printf("Wn%s is an identifier", yytext);}
\"A\" {printf("\n %s is a string ",yytext);}
0-9]+ {printf("Wn%s is a number",yytext);
}\== (printf("\n %s is a relational operator",yytext);}
A=|
\]
‘|
VI
\&|
% {print{("\n %s is a operator" ,yytext);)
“|
\n;
w%
int main(int arge,char **argv)
{
FILE *file;
file=fopen("inp.c","r");
if({file)
{
print{("could not open the file!!!");
exit(0);
}OO
yyin=file;
yylex();
printf("\n\n");
return(0);
}
int yywrap()
{
return 1;C:\Documents and Settings\admin\Desktop>flex alex.]
C:\Documents and Settings\admin\Desktop>gec lex.yy.c
C:\Documents and Settings\admin\Desktop>a.exe
#include is a Preprocessor directive
void is a keyword
function main(
block begins
int is a keyword
ais an identifier
bis an identifier oe
c is an identifier
function pri
“enter the value for a,b" is a stringfimetion scantt
"%od%d" is a string,
& is a operator
ais an identifier
& is a operator
bis an identifier
c is an identifier
= is a operator
ais an identifier
+ is a operator
bis an identifier
function printf{
"the value of c:%d" is a string
& isa operator
c is an identifier
block ends
RESULT:
Thus the program to implement lexical analyzer using lex tool is executed and
ieenebeean,Ex No:3.IMPLEMENTATION OF ARITHMETIC CALCULATOR USING
LEX and YAC
DATE: 9/4 (2?
AIM:
‘To implement the arithmetic calculator using lex and yace for program.
ALGORITHM:
1, Start the program.
2.Perform the calculation using both the lex and yace.
3.In the lex tool, if the given expression contains numbers and letters then they are
displayed.
4.In the same way, the digits, letters and uminus are identified and displayed using
yacctool.
5.The calculation is performed and the result is displayed.
6.Stop the program.
PROGRAM:
Lex
Yt
#include"y.tab.h"
#include
%}%%
({0-9}+|({0-9]*\,[0-9]+)((eE][-+1?(0-9]+)?) {yylval.dval=atof{yytext);retun
NUMBER;}
log |
LOG {return LOG;}
In {return nLOG;}
sin |
SIN {return SINE;}
cos |
COS {return COS;}
tan|
TAN {return TANS}
mem {retum MEMS}
(;
\$ return 0;
\n|, return yytext[0}; 7
%W% a
YaceNUMBER
otoken MEM
%token LOG SINE nlOG COS TAN
lef 24"
left
Yoright
%left LOG SINE nLOG COS TAN
%enonassoc UMINUS
“atypeexpression
%%
Slart:statement’\n’
|start statement’\n®
statement: MEM'="expression (memvar=$3;}
| expression{print{(” AnsWer Sop\n",S1);)
: f
expressionspresson eapresson ($8°S1483;}
| expression “' expression {$$=$1-$3;}
| expression '* expression ($$=$1*S3:)
| expression 'f expression{
i{S3—=0)
Yyerror("divide by zero");
else
SS=$1/$3;
}
lexpression’expression ($S=pow(S1,$3);)
xpression:expression %pree UMINUS{SS=-$2;)
['Cexpression’) {S$=$2;}
LOG expression {$$=log($2V/log(10);)
InLOG expression {8$=Iog($2);}
ISINE expression ($8=sin($2*3.14/180);)
{COS expression {88=cos($2*3.14/180);}
\TAN expression {SSHtan($2*3.14/180);}
print{("Enter the expression");
yyparse();}int yyerror(char *error)
{
print{("%s\n" error);
}
OUTPUT:
[linuxpert@fosslab ~]$ vi cal
[linuxpert@fosslab ~]$ lex cal.1
[linuxpert@fosslab ~]$ yace -d cal.y
[linuxpert@fosslab ~]$ ce lex.yy.c y.tab.c -Il -Im
[linuxpert@fosslab ~J$ /a.out
Enter the expression(5+2)*(3-1)/(2)
Answer=7
RESULT:
Thug the program to implement calculator using LEX andYACC tool is
executed sudeessfully and output is verified.