-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab5_4.l
More file actions
76 lines (58 loc) · 1.67 KB
/
Lab5_4.l
File metadata and controls
76 lines (58 loc) · 1.67 KB
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
%{
#include <stdio.h>
int scanf_count = 0;
int printf_count = 0;
FILE *output_file;
%}
%%
"scanf" {
scanf_count++;
fprintf(output_file, "READ");
fprintf(yyout, "READ");
}
"printf" {
printf_count++;
fprintf(output_file, "WRITE");
fprintf(yyout, "WRITE");
}
. { fprintf(output_file, "%s", yytext); }
\n { fprintf(output_file, "\n"); }
%%
int yywrap() {
return 1;
}
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("Usage: %s <input_file.c> [output_file.c]\n", argv[0]);
return 1;
}
FILE *input_file = fopen(argv[1], "r");
if (!input_file) {
printf("Error: Cannot open input file %s\n", argv[1]);
return 1;
}
char output_filename[256];
if (argc >= 3) {
strcpy(output_filename, argv[2]);
} else {
strcpy(output_filename, "output.c");
}
output_file = fopen(output_filename, "w");
if (!output_file) {
printf("Error: Cannot create output file %s\n", output_filename);
fclose(input_file);
return 1;
}
yyin = input_file;
printf("Processing file: %s\n", argv[1]);
printf("Output file: %s\n\n", output_filename);
yylex();
fclose(input_file);
fclose(output_file);
printf("\n=== Results ===\n");
printf("Number of scanf statements: %d\n", scanf_count);
printf("Number of printf statements: %d\n", printf_count);
printf("Total replacements: %d\n", scanf_count + printf_count);
printf("\nReplacement complete! Check %s\n", output_filename);
return 0;
}