pratice lex programs:
1. Lex program to recognize alphabet a.
%%
[a] { printf (� alphabet a is recognized�);}
%%
O/p:
$] lex file name.l
$] cc lex.yy.c �ll
$] ./a.out
a
alphabet a is recognized
b
b
bc
bc
aa
alphabet a is recognized alphabet a is recognized
2. Lex program to recognize a*.
%%
[a]* { Printf (� alphabet a is recognized�);}
%%
O/p:
a
alphabet a is recognized
b
b
bc
bc
aa
alphabet a is recognized
3. Lex program to recognize any English alphabet.
%%
[a-z] { printf (�alphabet is recognized�);}
%%
O/p:
a
alphabet is recognized
9
9
234
234
b
alphabet is recognized
ab
alphabet is recognized alphabet is recognized
4. Lex program to recognize any string over English alphabets.
%%
[a-z]* { printf (�string is recognized�);}
%%
O/p:
a
string is recognized
9
9
234
234
bat
string is recognized
car
string is recognized
5. Lex program to recognize digit.
%%
[0-9] { printf (�digit is recognized�);}
%%
O/p:
a
a
9
digit is recognized
234
digit is recognized digit is recognized digit is recognized
bat
bat
6. Lex program to recognize integer or number.
%%
[0-9]* { printf (�integer is recognized�);}
%%
O/p:
a
a
9
integer is recognized
234
integer is recognized bat
bat
7. Lex program to recognize string and integer.
%%
[a-z]* { printf (�string is recognized�);}
[0-9]* { printf (�integer is recognized�);}
%%
O/p:
9
integer is recognized
234
integer is recognized
bat
string is recognized
8. Lex program to recognize float number.
%%
[0-9]+ [.] [0-9]+ { printf (�float is recognized�);}
%%
O/p:
23.5
float is recognized
9. Lex program to convert a floating point number to an integer.
% {
int i;
% }
%%
[0-9]+ \ . [0-9]+ {
i=0;
While (yytext[i] =�.�)
{
Printf (�%c�, yytext[i]);
i++;
}
}
%%
O/p :
23.9
23
24.6
24
10. Lex program to find whether the given string i.e. number is Armstrong or not
%{
int num,r,n1,i,sum;
%}
%%
[0-9]+ {
for(i=0;i<yyleng;i++)
{
num=num*10 +(yytext[i]-48);
}
n1=num;
sum=0;
while (num !=0)
{
r=num%10;
sum=(r*r*r) + sum;
num=num/10;
}
if (sum==n1)
printf (�\n Armstrong�);
else
printf (�\n not an Armstrong�);
}
%%
O/p:
153
Armstrong
129
Not an Armstrong
11.program to identify keyword and identifier and number?
%{
//This is first lex program
%}
letter [a-z][A-Z0-9a-z0-9]*
digit [0-9]+
%%
int|float|do|char|else|while|for|if {printf("%s is a keyword",yytext);}
{letter} {printf("%s is an identifier",yytext);}
{digit} {printf("%s is a number",yytext);}
%%
O/P:
int is a keyword
12.Program to find octal and hexadecimal numbers
%{
/*program to identify octal and hexadecimal numbers*/
%}
Oct [o][0-9]+
Hex [o][x|X][0-9A-F]+
%%
{Hex} printf("this is a hexadecimal number");
{Oct} printf("this is an octal number");
%%
main()
{
yylex();
}
int yywrap()
{
return 1;
}
13. program to implement palindrome?
%{
int i,r,m,num,n;
%}
%%
[0-9]+ { num=0;
for(i=0;i<yyleng;i++)
{
num=num*10+(yytext[i]-48);
}
n=0;
m=num;
while(num>0)
{
r=num%10;
n=(n*10)+r;
num=num/10;
}
if(m==n)
printf("\npalindrome");
else
printf("\n not a palindrome");
}
%%
main()
{
yylex();
}
int yywrap()
{
return 1;
}
14. Lex program to print given string in reverse.
%{
int i;
%}
%%
[a-z]* {
for(i=yyleng-1;i>=0;i--)
{
printf (�%c�, yytext[i]);
}
}
%%
15.program to identify keyword and identifier and number?
%{
//This is first lex program
%}
letter [a-z][A-Z0-9a-z0-9]*
digit [0-9]+
%%
int|float|do|char|else|while|for|if {printf("%s is a keyword",yytext);}
{letter} {printf("%s is an identifier",yytext);}
{digit} {printf("%s is a number",yytext);}
%%
main(int argc,char **argv)
{
if(argc>1)
yyin=fopen(argv[1],"r");
else
yyin=stdin;
yylex();
printf("\n");
}
int yywrap()
{
return 0;
}
16.Program to implement standalone scanner in LEX
%{
int COMMENT=0;
%}
id [a-z][a-z0-9]*
%%
#.* {printf("\n%s is a PREPROCESSOR
DIRECTIVE",yytext);}
int|double|char {printf("\n\t%s is a KEYWORD",yytext);}
if|then|endif {printf("\n\t%s is a KEYWORD",yytext);}
else {printf("\n\t%s is a KEYWORD",yytext);}
"/*" {COMMENT=1;}
"*/" {COMMENT=0;}
{id}\( {if(!COMMENT)printf("\n\nFUNCTION\n\t%s",yytext);}
{id}(\[[0-9]*\])? {if(!COMMENT) printf("\n\tidentifier\t%s",yytext);}
\{ {if(!COMMENT) printf("\n BLOCK BEGINS");ECHO; }
\} {if(!COMMENT)printf("\n BLOCK ends");ECHO; }
\".*\" {if(!COMMENT)printf("\n\t %s is a STRING",yytext);}
[+\-]?[0-9]+ {if(!COMMENT)printf("\n\t%s is a NUMBER",yytext);}
\( {if(!COMMENT)printf("\n\t");ECHO;printf("\t delim
openparanthesis\n");}
\) {if(!COMMENT)printf("\n\t");ECHO;printf("\t delim
closed paranthesis");}
\; {if(!COMMENT)printf("\n\t");ECHO;printf("\t delim
semicolon");}
\= {if(!COMMENT)printf("\n\t%s is an ASSIGNMENT
OPERATOR",yytext);}
\<|\> {printf("\n\t %s is relational operator",yytext);}
"+"|"-"|"*"|"/" {printf("\n %s is an operator\n",yytext);}
"\n" ;
%%
main(int argc ,char **argv)
{
if (argc > 1)
yyin = fopen(argv[1],"r");
else
yyin = stdin;
yylex ();
printf ("\n");
}
int yywrap()
{ return 0; }