-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.y
218 lines (176 loc) · 4.67 KB
/
parser.y
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
%{
#include "parser.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
extern FILE *yyin;
extern int yylineno;
extern char* yytext;
char namespace[16];
char filename[3][32], *buffer;
FILE *source, *header;
int indentlevel = 0;
int in_class = 0;
int yylex();
int yyerror(const char *p) {
printf("Error: %s\n", p);
printf("Near %s\n", yytext);
printf("Line: %d\n", yylineno);
}
int indent_level(FILE *output) {
for (int i = 0; i < indentlevel; i++)
fprintf(output, "\t");
}
%}
%union {
int ival;
char* str_val;
};
%token <ival> START END STARTFILE ENDFILE
%token <ival> STARTMAIN ENDMAIN CLASS ENDCLASS
%token <ival> PUBLIC PRIVATE INCLUDE NAMESPACES CONST
%token <ival> DATA FUNCTION COMMA LPAR RPAR
%token <str_val> IDENTIFIER STDHEADER USRHEADER QSTRING
%%
program: START files END;
files:
files file
| file;
file:
STARTFILE IDENTIFIER {
for (int i = 0; i < 3; i++)
strcpy(filename[i], $2);
strcat(filename[0], ".h");
header = fopen(filename[0], "w");
strcat(filename[1], ".cpp");
source = fopen(filename[1], "w");
for (int i = 0; i < strlen(filename[2]); i++)
filename[2][i] = toupper(filename[2][i]);
strcat(filename[2], "_H");
fprintf(header, "#ifndef %s\n#define %s\n\n", filename[2], filename[2]);
}
includes {
fprintf(source, "#include \"%s\"\n\n", filename[0]);
fprintf(header, "\n");
}
contents ENDFILE {
fprintf(header, "#endif\n");
fclose(source);
fclose(header);
}
| STARTMAIN {
header = fopen("main.cpp", "w");
source = header;
}
includes contents ENDMAIN {
fclose(header);
};
/* include headers */
includes:
INCLUDE includelist;
includelist:
includelist COMMA include
| include;
include: /* empty */
| STDHEADER { fprintf(header, "#include %s\n", $1); }
| QSTRING { fprintf(header, "#include %s\n", $1); };
contents: /* empty */
| contents content
| content
content:
class
| function
| data;
class:
CLASS IDENTIFIER {
strcpy(namespace, $2);
strcat(namespace, "::");
indent_level(header);
fprintf(header, "class %s {\n", $2);
in_class = 1;
indentlevel += 1;
}
domain classmembers { indentlevel -=1; }
domain classmembers { indentlevel -=1; }
ENDCLASS {
indentlevel -= 1;
indent_level(header);
fprintf(header, "};\n");
strcpy(namespace, "");
};
domain: /* empty */
| PUBLIC {
indent_level(header);
fprintf(header, "public:\n");
indentlevel += 1;
}
| PRIVATE {
indent_level(header);
fprintf(header, "private:\n");
indentlevel += 1;
}
classmembers: /* empty */
| classmembers classmember
| classmember;
classmember:
function
| data;
function:
FUNCTION CONST IDENTIFIER IDENTIFIER {
indent_level(header);
fprintf(header, "const %s %s(", $3, $4);
fprintf(source, "const %s %s%s(", $3, namespace, $4);
}
LPAR argumentslist RPAR {
fprintf(header, ");\n");
fprintf(source, ") {\n\n}\n\n");
}
| FUNCTION IDENTIFIER IDENTIFIER {
indent_level(header);
fprintf(header, "%s %s(", $2, $3);
fprintf(source, "%s %s%s(", $2, namespace, $3);
}
LPAR argumentslist RPAR {
fprintf(header, ");\n");
fprintf(source, ") {\n\n}\n\n");
};
argumentslist: /* empty */
| argumentslist COMMA {
fprintf(header, ", ");
fprintf(source, ", ");
}
argument
| argument;
argument:
CONST IDENTIFIER IDENTIFIER {
fprintf(header, "const %s", $2);
fprintf(source, "const %s %s", $2, $3);
}
| IDENTIFIER IDENTIFIER {
fprintf(header, "%s", $1);
fprintf(source, "%s %s", $1, $2);
};
data:
DATA IDENTIFIER IDENTIFIER {
if (in_class) {
indent_level(header);
fprintf(header, "%s %s;\n", $2, $3);
} else {
fprintf(source, "%s %s;\n", $2, $3);
}
};
%%
/* Main and stuff here */
int main (int argc, char** argv) {
FILE* input;
if (argc > 1) {
input = fopen(argv[1], "r");
}
if (input == NULL) {
printf("Could not open %s\n", argv[1]);
return 0;
}
yyin = input;
yyparse();
return 0;
}