-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprg6.txt
79 lines (73 loc) · 1.59 KB
/
prg6.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
.l part
%{
/* 6.a) Write a LEX program to eliminate comment lines in a C program and copy the resulting
program into a separate file. */
#include<stdio.h>
int c_count=0;
%}
%%
"/*"[^*/]*"*/" {c_count++;}
"//".* {c_count++;}
%%
int main( int argc, char *argv[])
{
if(argc!=3)
{
printf("usage %s <src file> <dest file>",argv[0]);
return 0;
}
yyin=fopen(argv[1],"r");
yyout=fopen(argv[2],"w");
yylex();
printf("Number of Comment Lines: %d\n",c_count);
return 0;
}
***********************************************************************************************************
.L
%{
/* b) Write YACC program to recognize valid identifier, operators and keywords in the given text (C
program) file. */
#include <stdio.h>
#include "y.tab.h"
extern yylval;
%}
%%
[ \t] ;
[+|-|*|/|=|<|>] {printf("operator is %s\n",yytext);return OP;}
[0-9]+ {yylval = atoi(yytext); printf("numbers is %d\n",yylval); return DIGIT;}
int|char|bool|float|void|for|do|while|if|else|return {printf("keyword is %s\n",yytext);return KEY;}
[a-zA-Z0-9]+ {printf("identifier is %s\n",yytext);return ID;}
. ;
%%
.Y part
%{
#include <stdio.h>
#include <stdlib.h>
int id=0, dig=0, key=0, op=0;
extern FILE *yyin;
%}
%token DIGIT ID KEY OP
%%
input:
DIGIT input { dig++; }
| ID input { id++; }
| KEY input { key++; }
| OP input {op++;}
| DIGIT { dig++; }
| ID { id++; }
| KEY { key++; }
| OP { op++;}
;
%%
int main(int argc, char *argv[]) {
yyin = fopen(argv[1], "r");
do {
yyparse();
} while (!feof(yyin));
printf("numbers = %d\nKeywords = %d\nIdentifiers = %d\noperators = %d\n",
dig, key,id, op);
}
int yyerror() {
printf("parse error! Message: ");
exit(0);
}