You are on page 1of 10

Lex program to count number of vowels and consonanta

?
1
2 %{
3
4 int v=0,c=0;
5
6 %}
7
%%
8
9 [aeiouAEIOU] v++;
10
11[a-zA-Z] c++;
12
13%%
14
15main()
16
17{
18printf("ENTER INTPUT : \n");
19
20yylex();
21
22printf("VOWELS=%d\nCONSONANTS=%d\n",v,c);
23
24}
25

Lex program to count the type of numbers


?
%{
1
2 int pi=0,ni=0,pf=0,nf=0;
3
4 %}
5
6 %%
7
8 \+?[0-9]+ pi++;
9
10\+?[0-9]*\.[0-9]+ pf++;
11
\-[0-9]+ ni++;
12
13\-[0-9]*\.[0-9]+ nf++;

14
15%%
16
17main()
18
{
19
20printf("ENTER INPUT : ");
21
22yylex();
23
24printf("\nPOSITIVE INTEGER : %d",pi);
25
26printf("\nNEGATIVE INTEGER : %d",ni);
27
28printf("\nPOSITIVE FRACTION : %d",pf);
29printf("\nNEGATIVE FRACTION : %d\n",nf);
30
31}
32
33
34
35

Lex program to count the number of printf and scanf


statements
?
1 %{
2
#include "stdio.h"
3
4 int pf=0,sf=0;
5
6 %}
7
8 %%
9
10printf {
11
12pf++;
13
fprintf(yyout,"%s","writef");
14
15}
16
17scanf {
18

19sf++;
20
21fprintf(yyout,"%s","readf");
22
}
23
24%%
25
26main()
27
28{
29
30yyin=fopen("file1.l","r+");
31
32yyout=fopen("file2.l","w+");
33yylex();
34
35printf("NUMBER OF PRINTF IS %d\n",pf);
36
37printf("NUMBER OF SCANF IS %d\n",sf);
38
39}
40
41
42
43

Lex program to find simple and compound statements


?
1 %{
2
}%
3
4 %%
5
6 "and"|
7
8 "or"|
9
10"but"|
11
12"because"|
13
"nevertheless" {printf("COMPOUNT SENTANCE"); exit(0); }
14
15. ;
16
17\n return 0;

18
19%%
20
21main()
22
{
23
24prntf("\nENTER THE SENTANCE : ");
25
26yylex();
27
28printf("SIMPLE SENTANCE");
29
30}
31
32
33

Lex program to count the number of identifiers


?
1 %{
2
#include<stdio.h>
3
4 int id=0,flag=0;
5
6 %}
7
8 %%
9
10"int"|"char"|"float"|"double" { flag=1; printf("%s",yytext); }
11
12";" { flag=0;printf("%s",yytext); }
13[a-zA-Z][a-zA-z0-9]* { if(flag!=0) id++; printf("%s",yytext); }
14
15[a-zA-Z0-9]*"="[0-9]+ { id++; printf("%s",yytext); }
16
17[0] return(0);
18
19%%
20
21main()
22
{
23
24printf("\n *** output\n");
25
26yyin=fopen("f1.l","r");

27
28yylex();
29
30printf("\nNUMBER OF IDENTIFIERS = %d\n",id);
31
fclose(yyin);
32
33}
34
35
36
37
38
39int yywrap()
40
41{
42
43return(1);
44
45}
46
47
48
49

Lex program to count the number of words,characters,blank


spaces and lines
?
1 %{
2
int c=0,w=0,l=0,s=0;
3
4 %}
5
6 %%
7
8 [\n] l++;
9
10[' '\n\t] s++;
11
12[^' '\t\n]+ w++; c+=yyleng;
13
%%
14
15int main(int argc, char *argv[])
16
17{

18
19if(argc==2)
20
21{
22
yyin=fopen(argv[1],"r");
23
24yylex();
25
26printf("\nNUMBER OF SPACES = %d",s);
27
28printf("\nCHARACTER=%d",c);
29
30printf("\nLINES=%d",l);
31
32printf("\nWORD=%d\n",w);
33}
34
35else
36
37printf("ERROR");
38
39}
40
41
42
43

Lex program to count the number of comment lines


?
1 %{
2
#include<stdio.h>
3
4 int cc=0;
5
6 %}
7
8 %%
9
10"/*"[a-zA-Z0-9' '\t\n]*"*/" cc++;
11
12"//"[a-zA-Z0-9' '\t]* cc++;
13%%
14
15main()
16

17{
18
19yyin=fopen("f1.l","r");
20
yyout=fopen("f2.l","w");
21
22yylex();
23
24fclose(yyin);
25
26fclose(yyout);
27
28printf("\nTHE NUMBER OF COMMENT LINES = %d\n",cc);
29
30}
31
32
33

Lex program to check the validity of arithematic statement


?
1 %{
2
3 #include<stdio.h>
4
5 int opr=0,opd=0;
6
int n;
7
8 %}
9
10%%
11
12[\+\-\*\/] { printf("OPERATORS ARE %s\n",yytext);
13
14opr++;
15
16}
17
[a-zA-Z]+ { printf("OPERANDS ARE %s\n",yytext);
18
19opd++;
20
21}
22
23[0-9]+ { printf("OPERANDS ARE %s\n",yytext);
24
25opd++;

26
27}
28
29[a-zA-Z]+\+\-\*\/[a-zA-Z]+ { n=0; }
30
[0-9]+\+\-\*\/[0-9]+ { n=0; }
31
32%%
33
34main()
35
36{
37
38
39
40printf("\nENTER THE EXPRESSION : \n");
41
42yylex();
43printf("\nNUMBER OF OPERATORS ARE %d",opr);
44
45printf("\nNUMBER OF OPERANDS ARE %d",opd);
46
47if((n==0)&&(opd==opr+1))
48
49printf("\nVALID EXPRESSION\n");
50
51else
52
printf("\nINVALID EXPRESSION\n");
53
54
55
56}
57
58
59
60
61

Lex program to find the number of constants


?
1
2
3
4
5
6

%{
#include<stdio.h>
int cons=0;
%}

7
8 %%
9
10[0-9]+ { printf("\n%s",yytext); cons++; }
11
. ;
12
13%%
14
15main(int argc,char *argv[])
16
17{
18
19if(argc==2)
20
21{
22yyin=fopen(argv[1],"r");
23
24yylex();
25
26printf("\nNUMBER OF CONSTANTS : %d\n",cons);
27
28}
29
30else
31
printf("\nERROR");
32
33}
34
35
36
37
Q.
Write a lex program to count the number of comment lines in a given C program .Also eliminate
them and copy that program into seperate file
Answers (1)
1.
%{
#include
int comments=0;
%]
%%

"//".* {comments++;}
"/*"[a-zA-Z0-9\n]*"*/" {comments++;}
%%
main()
{
char s[10],d[10];
printf("Enter the source file and destination file\n");
scanf("%s%s",s,d);
yyin=fopen(s,"r"); /*open input file in read mode*/
yyout=fopen(d,"w"); /*open output file in write mode*/
yylex();
printf("Number of comments = %d\n",comments);
fclose(yyin);
fclose(yyout);
}
--------------------------------------- ----------------------------------------------------UNIX environment:
To compile save this file as cmt.lex
lex cmt.lex
cc yy.lex.c -ll
./a.out
Input the source file (any c program with comments)
Enter the destination file name as output.txt
To see the output:
cat output.txt ...less

You might also like