-
Notifications
You must be signed in to change notification settings - Fork 5
/
in.txt
270 lines (258 loc) · 9.57 KB
/
in.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/**
* Abstract Syntax Tree building functions for parser
* of the C Programming Language (ISO/IEC 9899:2018).
*
* @authors: Denis Chernikov, Vladislav Kuleykin
*/
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "alloc_wrap.h"
#include "ast.h"
#include "string_tools.h"
AST_NODE *ast_create_node(AST_NODE_TYPE type, AST_CONTENT content, int n_children, ...)
{
AST_NODE *res = (AST_NODE *) my_malloc(sizeof(AST_NODE), "AST node");
*res = (AST_NODE) {type, content, n_children, NULL};
va_list ap;
int i = 0;
if (n_children > 0)
{
res->children = (AST_NODE **)
my_malloc(sizeof(AST_NODE *) * n_children, "AST node's children");
va_start(ap, n_children);
while (i < n_children)
{
res->children[i++] = va_arg(ap);
}
va_end(ap);
}
return res;
}
AST_NODE *ast_expand_node(AST_NODE *node, AST_NODE *to_append)
{
if (!node || !to_append)
{
return node;
}
++node->children_number;
node->children = my_realloc(node->children,
sizeof(AST_NODE *) * node->children_number, "AST node's children");
node->children[node->children_number - 1] = to_append;
return node;
}
char *ast_type_to_str(AST_NODE_TYPE type)
{
switch (type)
{
case TranslationUnit: return "TranslationUnit";
case FunctionDefinition: return "FunctionDefinition";
case DeclarationList: return "DeclarationList";
case Declaration: return "Declaration";
case DeclarationSpecifiers: return "DeclarationSpecifiers";
case InitDeclaratorList: return "InitDeclaratorList";
case InitDeclarator: return "InitDeclarator";
case StorageClassSpecifier: return "StorageClassSpecifier";
case TypeSpecifier: return "TypeSpecifier";
case StructSpecifier: return "StructSpecifier";
case UnionSpecifier: return "UnionSpecifier";
case StructDeclarationList: return "StructDeclarationList";
case StructDeclaration: return "StructDeclaration";
case SpecifierQualifierList: return "SpecifierQualifierList";
case StructDeclaratorList: return "StructDeclaratorList";
case StructDeclarator: return "StructDeclarator";
case EnumSpecifier: return "EnumSpecifier";
case EnumeratorList: return "EnumeratorList";
case Enumerator: return "Enumerator";
case AtomicTypeSpecifier: return "AtomicTypeSpecifier";
case TypeQualifier: return "TypeQualifier";
case FunctionSpecifier: return "FunctionSpecifier";
case AlignmentSpecifier: return "AlignmentSpecifier";
case Declarator: return "Declarator";
case DirectDeclarator: return "DirectDeclarator";
case DirectDeclaratorBrackets: return "DirectDeclaratorBrackets";
case DirectDeclaratorParen: return "DirectDeclaratorParen";
case Pointer: return "Pointer";
case TypeQualifierList: return "TypeQualifierList";
case ParameterList: return "ParameterList";
case ParameterDeclaration: return "ParameterDeclaration";
case IdentifierList: return "IdentifierList";
case TypeName: return "TypeName";
case AbstractDeclarator: return "AbstractDeclarator";
case DirectAbstractDeclarator: return "DirectAbstractDeclarator";
case DirectAbstractDeclaratorBrackets: return "DirectAbstractDeclaratorBrackets";
case DirectAbstractDeclaratorParen: return "DirectAbstractDeclaratorParen";
case Initializer: return "Initializer";
case InitializerList: return "InitializerList";
case InitializerListElem: return "InitializerListElem";
case DesignatorList: return "DesignatorList";
case StaticAssertDeclaration: return "StaticAssertDeclaration";
case LabeledStatement: return "LabeledStatement";
case BlockItemList: return "BlockItemList";
case SelectionStatement: return "SelectionStatement";
case IterationStatement: return "IterationStatement";
case JumpStatement: return "JumpStatement";
case Expression: return "Expression";
case AssignmentExpression: return "AssignmentExpression";
case AssignmentOperator: return "AssignmentOperator";
case ConditionalExpression: return "ConditionalExpression";
case ArithmeticalExpression: return "ArithmeticalExpression";
case CastExpression: return "CastExpression";
case UnaryExpression: return "UnaryExpression";
case UnaryOperator: return "UnaryOperator";
case PostfixExpression: return "PostfixExpression";
case ArgumentExpressionList: return "ArgumentExpressionList";
case GenericSelection: return "GenericSelection";
case GenericAssocList: return "GenericAssocList";
case GenericAssociation: return "GenericAssociation";
case Identifier: return "Identifier";
case StringLiteral: return "StringLiteral";
case IntegerConstant: return "IntegerConstant";
case FloatingConstant: return "FloatingConstant";
case CharacterConstant: return "CharacterConstant";
default: return NULL;
}
}
void ast_free(AST_NODE *root)
{
if (root == NULL) return;
if (root->type == Identifier || root->type == IntegerConstant || root->type == FloatingConstant
|| root->type == CharacterConstant || root->type == StringLiteral) {
free(root->content.value);
}
for (int i = 0; i < root->children_number; ++i)
{
ast_free(root->children[i]);
}
free(root);
}
char *ast_to_json(AST_NODE *root, int shift, char *tab, char *(*cont_to_str)(AST_NODE *))
{
char *json;
char *act_tab = repeat(shift, tab);
int res;
// If given NULL root
if (!root)
{
json = (char *) my_malloc(sizeof(char) * (shift * strlen(tab) + 5),
"JSON representation");
res = sprintf(json, "%snull", act_tab);
free(act_tab);
if (res < 0)
{
fprintf(stderr,
"FATAL ERROR! String formatting cannot be applied!\n");
free(json);
free(act_tab);
exit(-1);
}
return json;
}
// Get string representation of `type' field
char *type_str = ast_type_to_str(root->type);
// Get string representation of `node' content
char *content_str;
if (root->content.value)
{
char *tmp = (*cont_to_str)(root);
if (!tmp)
{
goto null_content;
}
content_str = wrap_by_quotes(tmp);
}
else
{
null_content:
content_str = (char *) my_malloc(sizeof(char) * 5, "JSON null");
res = sprintf(content_str, "null");
if (res < 0)
{
fprintf(stderr,
"FATAL ERROR! String formatting cannot be applied!\n");
free(act_tab);
free(content_str);
exit(-1);
}
}
// Get string representation of `children_number' field
char *children_num_str = (char *) my_malloc(7, "number representation");
sprintf(children_num_str, "%d", root->children_number);
// Get string representation of `children' array field
int i;
char **children = (char **) malloc(sizeof(char *) * root->children_number);
for (i = 0; i < root->children_number; ++i)
{
children[i] = ast_to_json(root->children[i], shift + 2, tab, cont_to_str);
}
char *children_str;
if (root->children)
{
char *arr = concat_array(children, root->children_number, ",\n");
for (i = 0; i < root->children_number; ++i) free(children[i]);
size_t size = strlen(arr) + strlen(tab) * (shift + 1) + sizeof(char) * (4 + 1);
children_str = (char *) my_malloc(size, "children array string");
res = sprintf(children_str, "[\n%s\n%s%s]", arr, act_tab, tab);
free(arr);
if (res < 0)
{
fprintf(stderr,
"FATAL ERROR! String formatting cannot be applied!\n");
free(act_tab);
free(content_str);
free(children_num_str);
free(children_str);
exit(-1);
}
free(children);
}
else
{
children_str = (char *) my_malloc(sizeof(char) * 5, "JSON null");
res = sprintf(children_str, "null");
if (res < 0)
{
fprintf(stderr,
"FATAL ERROR! String formatting cannot be applied!\n");
free(act_tab);
free(content_str);
free(children_num_str);
free(children_str);
exit(-1);
}
}
// Concatenate the resulting JSON object
size_t json_size
= strlen(type_str)
+ strlen(content_str)
+ strlen(children_num_str)
+ strlen(children_str)
+ strlen(tab) * (shift * 6 + 4)
+ sizeof(char) * (62 + 1);
json = (char *) my_malloc(json_size, "JSON representation");
res = sprintf(json,
"%s{\n"
"%s%s\"type\": \"%s\",\n"
"%s%s\"content\": %s,\n"
"%s%s\"children_number\": %s,\n"
"%s%s\"children\": %s\n"
"%s}",
act_tab,
act_tab, tab, type_str,
act_tab, tab, content_str,
act_tab, tab, children_num_str,
act_tab, tab, children_str,
act_tab);
free(act_tab);
free(content_str);
free(children_num_str);
free(children_str);
if (res < 0)
{
fprintf(stderr,
"FATAL ERROR! String formatting cannot be applied!\n");
exit(-1);
}
return json;
}