CD Expt 3 Implementation of A Lexical Analyzer Using Lex Tool
CD Expt 3 Implementation of A Lexical Analyzer Using Lex Tool
OBJECTIVE::
To implement the lexical analyzer using lex tool for a subset of C language.
PROGRAM LOGIC:
PROGRAM
%{
%}
identifier[a-zA-Z][a-zA-Z0-9]*
%%
int |
float |
char |
double |
while |
do |
if |
break |
continue |
void |
switch |
return |
else |
\{ {printf("\nblock begins");}
\} {printf("\nblock ends");}
\( {printf("\n");ECHO;}
\<= |
\>= |
\< |
\> |
\= |
\+ |
\- |
\/ |
\& |
% {printf("\n %s is a operator",yytext);}
.|
\n;
%%
FILE *file;
file=fopen("inp.c","r");
if(!file)
exit(0);
}
yyin=file;
yylex();
printf("\n\n");
return(0);
int yywrap()
return 1;
INPUT FILE:
#include<stdio.h>
void main()
int a,b,c;
scanf("%d%d",&a,&b)';
c=a+b;
}
OUTPUT:
void is a keyword
function main(
block begins
int is a keyword
a is an identifier
b is an identifier
c is an identifier
function printf(
function scanf(
"%d%d" is a string
& is a operator
a is an identifier
& is a operator
b is an identifier
c is an identifier
= is a operator
a is an identifier
+ is a operator
b is an identifier
function printf(
& is a operator
c is an identifier
block ends
RESULT:
Thus the program to implement the lexical analyzer using lex tool for a subset of C language was
implemented and verifiedverified.