You are on page 1of 2

lexical.

lex

%{
%}
identifier [_a-zA-Z][_a-zA-Z0-9]*
%%
#.* {printf("\n %s is a preprocessor",yytext);}
void|int|float|double|char|do|while|for|switch|case|break|continue|return|goto {
printf("\n\t%s is a keyword",yytext);}
{identifier}\( {printf("\n Function %s",yytext);}
\; {printf("\n\t %s is a delimiter",yytext);}
\{ {printf("\n block begins %s",yytext);}
\} {printf("\n block ends %s",yytext);}
{identifier}(\[0-9\]*)? {printf("\n\t %s is an identifier",yytext);}
\".*\" {printf("\n\t %s is s string",yytext);}
[0-9]+ {printf("\n\t %s is a number",yytext);}
\+|\-|\*|\/|\% {printf("\n\t %s is an operator",yytext);}
= {printf("\n\t %s is a assignment operator",yytext);}
\<=|\>=|\==|\!= {printf("\n\t %s is a relationla operator",yytext);}
%%

main()
{
FILE *file;
file=fopen("ip.c","r");
yyin=file;
yylex();
}
int yywrap()
{
return 1;
}

Input:

ip.c

#include<stdio.h>
void main()
{
int a=5,b=56,c;
c=a+b;
printf("%d",c);
}

Output:

[itstaff@Telnetserv ~]$ lex lexical.lex


[itstaff@Telnetserv ~]$ cc lex.yy.c -ll
[itstaff@Telnetserv ~]$ ./a.out
#include<stdio.h> is a preprocessor
void is a keyword

You might also like