lex program to recognize identifier, keyword and number

 

 

 

%{
#include<stdio.h>
%}
%%
if|else|while|int|switch|for|char    {printf(“keyword”);}
[a-z]([a-z]|[0-9])*    {printf(“identifier”);}
[0-9]*        {printf(“number”);}
.*    {printf(“invalid”);}
%%
main()
{
yylex();
return 0;
}
int yywrap()
{
}

Sample output

else
keyword
humble
identifier
9876
number

 

Leave a Reply

Your email address will not be published. Required fields are marked *