Professional Documents
Culture Documents
Lex & Yacc Programs
Lex & Yacc Programs
?
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
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
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
18
19%%
20
21main()
22
{
23
24prntf("\nENTER THE SENTANCE : ");
25
26yylex();
27
28printf("SIMPLE SENTANCE");
29
30}
31
32
33
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
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
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
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
%{
#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