Program:
identifier [a-zA-Z][a-zA-Z0-9]*
%%
#.* {printf("\n%s This is a preprocessor directive",yytext);}
int |
float |
char |
double |
while |
for |
struct |
typedef |
do |
if |
break |
continue |
void |
switch |
return |
else |
goto {printf("\n\t%s is a keyword",yytext);}
[a-zA-Z][a-zA-Z0-9]* {printf("\n%s is an identifier", yytext);}
= {printf("\n\t %s is an ASSIGNMENT OPERATOR",yytext);}
\<= |
\>= |
\< |
== |
\> {printf("\n\t%s is a RELATIONAL OPERATOR",yytext);}
%%
int main(int argc, char **argv)
{
FILE *file;
file=fopen("var.c","r");
if(!file)
{
printf("could not open the file");
exit(0);
}
yyin=file;
yylex();
printf("\n");
return(0);
}
int yywrap()
{
return(1);
}
INPUT:
//var.c
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
a=1;
b=2;
c=a+b;
printf("Sum:%d",c);
}
#include<stdio.h> This is a preprocessor directive
#include<conio.h> This is a preprocessor directive
void is a keyword
main is an identifier
int is a keyword
a is an identifier
b is an identifier
c is an identifier
a is an identifier
= is an ASSIGNMENT OPERATOR
b is an identifier
= is an ASSIGNMENT OPERATOR
c is an identifier
= is an ASSIGNMENT OPERATOR
a is an identifier
b is an identifier
printf is an identifier