a) Write a LEX program to check whether input symbol is number or
character.
Program:-
%{
#include<stdio.h>
%}
%%
[a-z] printf("It's a
Character...\n");
[0-9] printf("It's a
Number...\n");
%%
int yywrap()
{
return 1;
}
int main()
{
yylex();
return 0;
}
Output:-
[root@SAR117
Desktop]#lex p1.lex
[root@SAR117
Desktop]#gcc lec.yy.c
[root@SAR117
Desktop]#./a.out
d
It's a
Character...
6
It's a
Number…
b) Write a LEX program to check whether
input is multi-digit number or string.
Program:-
%{
#include<stdio.h>
%}
%%
[a-zA-Z]+ printf("It's
a Multichar String...");
[0-9]+ printf("It's a Multidigit Number...");
%%
int yywrap()
{
return 1;
}
int main()
{
yylex();
return 0;
}
Output:-
[root@SAR117
Desktop]#lex p2.lex
[root@SAR117
Desktop]#gcc lec.yy.c
[root@SAR117
Desktop]#./a.out
hello
It's
a Multichar String...
226
It's
a Multidigit Number...
c) Write a LEX program to check whether
inputted character is vowel or not.
Program:-
%{
#include<stdio.h>
%}
%%
[a|A,e|E,i|I,O|o,u|U]+ printf("The Character is a vowel\n");
\n
[^a|A,e|E,i|I,O|o,u|U]+ printf("The Character is not a
vowel\n");
%%
int yywrap()
{
return 1;
}
int main()
{
yylex();
return 0;
}
Output:-
[root@SAR117 Desktop]#lex p3.lex
[root@SAR117 Desktop]#gcc lec.yy.c
[root@SAR117 Desktop]#./a.out
A
The
Character is a vowel
dd
The
Character is not a vowel
d) Write a LEX program to check whether a
word starts with vowel or not.
Program:-
%{
#include<stdio.h>
%}
%%
[aeiouAEIOU][a-zA-Z]+ printf("Start’s with a
vowel");
[a-zA-Z^aeiouAEIOU]+ printf("Does not start with a vowel");
%%
int yywrap()
{
return 1;
}
int main()
{
yylex();
return 0;
}
Output:-
[root@SAR117 Desktop]#lex p4.lex
[root@SAR117 Desktop]#gcc lec.yy.c
[root@SAR117 Desktop]#./a.out
Hey
Does not start with a vowel
Ink
Start’s
with a vowel
e) Write a LEX
program to count the number of characters, words and lines from a given input
file.
Program:-
%{
#include<stdio.h>
int w=0,l=0,c=0;
%}
%%
[a-z]+[ .] {
w++;
c+=yyleng;
}
[\n] l++;
. c++;
%%
int yywrap()
{
return 1;
}
int main()
{
yyin=fopen("inp.txt","r");
yylex();
printf("Characters: %d",c);
printf("Words: %d",w);
printf("Lines: %d",l);
return 0;
}
The File contains:-
Hi
How are you
I am good
Output:-
[root@SAR117 Desktop]#lex p5.lex
[root@SAR117 Desktop]#gcc lec.yy.c
[root@SAR117 Desktop]#./a.out
Characters:22
Words:7
Lines:3
No comments:
Post a Comment