-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyacc_prodrule_hack.c
70 lines (53 loc) · 1.39 KB
/
yacc_prodrule_hack.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern int yylex();
extern char* yytext;
extern FILE* yyin;
/**
* scans the input file for uppercase tokens prefixed by "_PR_"
* prints found tokens to output file and stdout prints
*
* output file will use the same name as the input file: <somefile>.y ---> <somefile>_pr.h
*
* @param argc number of command line arguments
* @param argv array of command line arguments
* @return program return code
*/
int main(int argc, char *argv[]) {
if (argc == 1) {
printf("no file provided\n");
} else {
while (--argc > 0) {
++argv;
if ((yyin = fopen(*argv, "r")) == NULL) {
printf("can't open %s\n", *argv);
continue;
}
yytext = NULL;
/* create output filename */
char buf[255];
strncpy(buf, *argv, 250);
char *b = buf + strlen(buf) - 2;
strcpy(b, "_pr.h");
printf("opfile name: %s\n", buf);
/* open output file */
FILE *opfile = NULL;
if ((opfile = fopen(buf, "wx")) == NULL) {
printf("file %s already exists.\n", buf);
exit(1);
}
/* construct an enum in the output file */
fprintf(opfile, "typedef enum {\n");
while (yylex() == 42) {
printf("%s\n", yytext);
fprintf(opfile, "\t%s,\n", yytext);
}
fprintf(opfile, "} prodrule;\n");
/* close files */
fclose(opfile);
fclose(yyin);
}
}
return 0;
}