-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAST_parsing.c
More file actions
435 lines (400 loc) · 16.4 KB
/
Copy pathAST_parsing.c
File metadata and controls
435 lines (400 loc) · 16.4 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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#include <stdio.h>
#include <stdlib.h>
#include "cJSON.h"
#include "cJSON.c"
#include <string.h>
/*각 함수의 if문 카운트*/
int Func_count(char *json_string, long file_size)
{
int count = 0;
cJSON *root = cJSON_Parse(json_string);
if (root == NULL)
{
printf("JSON 파싱실패 root가 존재하지 않음");
free(json_string);
}
else
{
cJSON *ext = cJSON_GetObjectItem(root, "ext");
if (ext == NULL)
{
printf("ext가 존재하지 않음");
cJSON_Delete(root);
}
long arr_size = cJSON_GetArraySize(ext);
for (int i = 0; i < arr_size; i++)
{
cJSON *idx_JSON = cJSON_GetArrayItem(ext, i);
if (idx_JSON == NULL)
{
printf("idx_JSON이 존재하지 않음");
cJSON_Delete(root);
}
cJSON *nodetype = cJSON_GetObjectItem(idx_JSON, "_nodetype");
if (strcmp(nodetype->valuestring, "FuncDef") == 0)
{
count++;
}
}
return count;
}
cJSON_Delete(root);
};
void Function_Param_Helper(const cJSON *function)
{
const cJSON *decl = cJSON_GetObjectItem(function, "decl");
if (decl == NULL)
return;
const cJSON *type = cJSON_GetObjectItem(decl, "type");
if (type == NULL)
return;
const cJSON *args = cJSON_GetObjectItem(type, "args");
if (args == NULL)
return;
const cJSON *params = cJSON_GetObjectItem(args, "params");
if (params == NULL)
return;
int param_count = 0;
cJSON *param;
cJSON_ArrayForEach(param, params)
{
param_count++;
printf("\tParameter %d:\n", param_count);
cJSON *param_type = cJSON_GetObjectItem(param, "type");
if (param_type != NULL)
{
cJSON *identifierType = cJSON_GetObjectItem(param_type, "type");
if (identifierType != NULL)
{
cJSON *names = cJSON_GetObjectItem(identifierType, "names");
if (names != NULL)
{
cJSON *nameType = cJSON_GetArrayItem(names, 0);
if (nameType != NULL)
{
printf("\t\tType: %s\n", nameType->valuestring);
}
}
}
}
cJSON *name = cJSON_GetObjectItem(param, "name");
if (name != NULL)
{
printf("\t\tparameter Name: %s\n", name->valuestring);
}
}
}
int IF_Count_Help(cJSON *node)
{
int count_if = 0;
cJSON *block_items = cJSON_GetObjectItem(node, "block_items");
long sizeof_block_items = cJSON_GetArraySize(block_items);
for (long block_items_idx = 0; block_items_idx < sizeof_block_items; block_items_idx++)
{
cJSON *items = cJSON_GetArrayItem(block_items, block_items_idx);
cJSON *block_items_nodetype = cJSON_GetObjectItem(items, "_nodetype");
if (strcmp(block_items_nodetype->valuestring, "If") == 0)
{
count_if++;
}
if (count_if > 0)
{
cJSON *iftrue = cJSON_GetObjectItem(items, "iftrue");
cJSON *iffalse = cJSON_GetObjectItem(items, "iffalse");
if (!cJSON_IsNull(iftrue))
{
count_if += IF_Count_Help(iftrue);
}
if (!cJSON_IsNull(iffalse))
{
count_if += IF_Count_Help(iffalse);
}
}
}
return count_if;
}
int elseIF_Count_Help(cJSON *node)
{
cJSON *block_items = cJSON_GetObjectItem(node, "block_items");
long sizeof_block_items = cJSON_GetArraySize(block_items);
int count_if = 0;
int count_elseif = 0;
for (long block_items_idx = 0; block_items_idx < sizeof_block_items; block_items_idx++)
{
cJSON *items = cJSON_GetArrayItem(block_items, block_items_idx);
cJSON *block_items_nodetype = cJSON_GetObjectItem(items, "_nodetype");
if (strcmp(block_items_nodetype->valuestring, "If") == 0)
{
cJSON *iffalse = cJSON_GetObjectItem(items, "iffalse");
if (!cJSON_IsNull(iffalse))
{
count_elseif++;
count_elseif += elseIF_Count_Help(iffalse);
}
}
}
return count_elseif;
}
/*함수 이름 출력*/
void Function_name(char *json_string, long file_size, long idx)
{
cJSON *root = cJSON_Parse(json_string);
if (root == NULL)
{
printf("JSON 파싱실패 root가 존재하지 않음");
free(json_string);
}
else
{
cJSON *ext = cJSON_GetObjectItem(root, "ext");
if (ext == NULL)
{
printf("ext가 존재하지 않음");
cJSON_Delete(root);
}
long arr_size = cJSON_GetArraySize(ext);
cJSON *idx_JSON = cJSON_GetArrayItem(ext, idx);
if (idx_JSON == NULL)
{
printf("idx_JSON이 존재하지 않음");
cJSON_Delete(root);
}
cJSON *nodetype = cJSON_GetObjectItem(idx_JSON, "_nodetype");
if (strcmp(nodetype->valuestring, "FuncDef") == 0)
{
cJSON *decl = cJSON_GetObjectItem(idx_JSON, "decl");
cJSON *body = cJSON_GetObjectItem(idx_JSON, "body");
cJSON *name = cJSON_GetObjectItem(decl, "name");
printf("function name : %s\n", cJSON_Print(name));
}
}
cJSON_Delete(root);
};
/*각 함수의 if문 카운트*/
void IF_Count(char *json_string, long file_size, long idx)
{
cJSON *root = cJSON_Parse(json_string);
if (root == NULL)
{
printf("JSON 파싱실패 root가 존재하지 않음");
free(json_string);
}
else
{
cJSON *ext = cJSON_GetObjectItem(root, "ext");
if (ext == NULL)
{
printf("ext가 존재하지 않음");
cJSON_Delete(root);
}
long arr_size = cJSON_GetArraySize(ext);
cJSON *idx_JSON = cJSON_GetArrayItem(ext, idx);
if (idx_JSON == NULL)
{
printf("idx_JSON이 존재하지 않음");
cJSON_Delete(root);
}
cJSON *nodetype = cJSON_GetObjectItem(idx_JSON, "_nodetype");
if (strcmp(nodetype->valuestring, "FuncDef") == 0)
{
cJSON *decl = cJSON_GetObjectItem(idx_JSON, "decl");
cJSON *body = cJSON_GetObjectItem(idx_JSON, "body");
int count_if = IF_Count_Help(body);
int count_elseif = elseIF_Count_Help(body);
printf("\tcount if = %d\n\tcount else if = %d\n", count_if, count_elseif);
}
}
cJSON_Delete(root);
};
void Return_Type(char *json_string, long file_size, long idx)
{
cJSON *root = cJSON_Parse(json_string);
cJSON *funcExt = cJSON_GetObjectItem(root, "ext");
cJSON *funcBlock = cJSON_GetObjectItem(cJSON_GetObjectItem(cJSON_GetArrayItem(funcExt, idx), "body"), "block_items");
int line = cJSON_GetArraySize(funcBlock);
for (int k = 0; k < line; k++)
{
cJSON *funcBlockPosition = cJSON_GetArrayItem(funcBlock, k);
cJSON *funcFirstName = cJSON_GetObjectItem(funcBlockPosition, "_nodetype");
if (strcmp(funcFirstName->valuestring, "Return") == 0)
{
cJSON *funcName = cJSON_GetObjectItem(cJSON_GetObjectItem(funcBlockPosition, "expr"), "_nodetype");
if (strcmp(funcName->valuestring, "Constant") == 0) // return을 어떤 값으로 보내줄 때 그 보내는 값의 type을 출력
{
cJSON *funcType = cJSON_GetObjectItem(cJSON_GetObjectItem(funcBlockPosition, "expr"), "type");
printf("\treturn type: %s\n", funcType->valuestring);
}
else if (strcmp(funcName->valuestring, "ID") == 0) // return을 변수를 이용해 보내줄 때 그 변수명을 출력
{
cJSON *funcName = cJSON_GetObjectItem(cJSON_GetObjectItem(funcBlockPosition, "expr"), "name");
printf("\treturn name: %s\n", funcName->valuestring);
}
else if (strcmp(funcName->valuestring, "FuncCall") == 0) // return으로 함수 호출 할 때 호출한 함수 이름 출력
{
cJSON *funcFuncName = cJSON_GetObjectItem(cJSON_GetObjectItem(cJSON_GetObjectItem(funcBlockPosition, "expr"), "name"), "name");
printf("return func name: %s\n", funcFuncName->valuestring);
}
else if (strcmp(funcName->valuestring, "BinaryOp") == 0) // return이 ||와 같이 여러개로 나누어져 있을 경우(현재는 패스함)
{
continue;
}
else if (strcmp(funcName->valuestring, "UnaryOp") == 0) // return이 단항 연산자가 있을 경우 expr에 한 번 더 들어가서 판단
{
cJSON *funcUnary = cJSON_GetObjectItem(funcBlockPosition, "expr");
cJSON *funcUnaryName = cJSON_GetObjectItem(funcUnary, "_nodetype");
if (strcmp(funcUnaryName->valuestring, "Constant") == 0) // return을 어떤 값으로 보내줄 때 그 보내는 값의 type을 출력
{
cJSON *funcType = cJSON_GetObjectItem(funcUnary, "type");
printf("return type: %s\n", funcType->valuestring);
}
else if (strcmp(funcUnaryName->valuestring, "ID") == 0) // return을 변수를 이용해 보내줄 때 그 변수명을 출력
{
cJSON *funcName = cJSON_GetObjectItem(funcUnary, "name");
printf("return name: %s\n", funcName->valuestring);
}
else if (strcmp(funcUnaryName->valuestring, "FuncCall") == 0) // return으로 함수 호출 할 때 호출한 함수 이름 출력
{
cJSON *funcFuncName = cJSON_GetObjectItem(funcUnary, "name");
printf("return func name: %s\n", funcFuncName->valuestring);
}
else if (strcmp(funcUnaryName->valuestring, "BinaryOp") == 0) // return이 ||와 같이 여러개로 나누어져 있을 경우(현재는 패스함)
{
continue;
}
printf("-------this is Unary------\n");
}
else if (strcmp(funcName->valuestring, "Case") == 0)
{
cJSON *funcStmtsName = cJSON_GetObjectItem(cJSON_GetArrayItem(cJSON_GetObjectItem(cJSON_GetObjectItem(funcBlockPosition, "expr"), "stmts"), 0), "expr");
if (strcmp(funcStmtsName->valuestring, "Constant") == 0) // return을 어떤 값으로 보내줄 때 그 보내는 값의 type을 출력
{
cJSON *funcType = cJSON_GetObjectItem(cJSON_GetObjectItem(funcBlockPosition, "expr"), "type");
printf("return type: %s\n", funcType->valuestring);
}
else if (strcmp(funcStmtsName->valuestring, "ID") == 0) // return을 변수를 이용해 보내줄 때 그 변수명을 출력
{
cJSON *funcName = cJSON_GetObjectItem(cJSON_GetObjectItem(funcBlockPosition, "expr"), "name");
printf("return name: %s\n", funcName->valuestring);
}
else if (strcmp(funcStmtsName->valuestring, "FuncCall") == 0) // return으로 함수 호출 할 때 호출한 함수 이름 출력
{
cJSON *funcFuncName = cJSON_GetObjectItem(cJSON_GetObjectItem(cJSON_GetObjectItem(funcBlockPosition, "expr"), "name"), "name");
printf("return func name: %s\n", funcFuncName->valuestring);
}
else if (strcmp(funcStmtsName->valuestring, "BinaryOp") == 0) // return이 ||와 같이 여러개로 나누어져 있을 경우(현재는 패스함)
{
continue;
}
}
else if (strcmp(funcName->valuestring, "if") == 0)
{
cJSON *funcIffalse = cJSON_GetObjectItem(cJSON_GetObjectItem(cJSON_GetObjectItem(funcBlockPosition, "expr"), "iffalse"), "block_items");
cJSON *funcIftrue = cJSON_GetObjectItem(cJSON_GetObjectItem(cJSON_GetObjectItem(funcBlockPosition, "expr"), "iftrue"), "block_items");
int iffalseLine = cJSON_GetArraySize(funcIffalse);
int iftrueLine = cJSON_GetArraySize(funcIftrue);
for (int n = 0; n < iffalseLine; n++)
{
cJSON *funcIffalsePosition = cJSON_GetArrayItem(funcIffalse, n);
cJSON *funcIfName = cJSON_GetObjectItem(funcIffalsePosition, "iffalse");
if (cJSON_IsNull(funcIfName))
{
continue;
}
else
{
cJSON *funcIfBlock = cJSON_GetArrayItem(cJSON_GetObjectItem(funcIfName, "block_items"), 0); // 0이라는건 단일 if문이라는것 if문이 중첩되면 block_items의 크기를 알아와서 다시 if, iffalse, iftrue를 구분히야함
cJSON *funcIfType = cJSON_GetObjectItem(cJSON_GetObjectItem(funcIfBlock, "expr"), "type");
printf("%s\n", funcIfType->valuestring);
}
}
for (int m = 0; m < iftrueLine; m++)
{
cJSON *funcIftruePosition = cJSON_GetArrayItem(funcIftrue, m);
cJSON *funcIfName = cJSON_GetObjectItem(funcIftruePosition, "iftrue");
if (cJSON_IsNull(funcIfName))
{
continue;
}
else
{
cJSON *funcIfBlock = cJSON_GetArrayItem(cJSON_GetObjectItem(funcIfName, "block_items"), 0); // 0이라는건 단일 if문이라는것 if문이 중첩되면 block_items의 크기를 알아와서 다시 if, iffalse, iftrue를 구분히야함
cJSON *funcIfType = cJSON_GetObjectItem(cJSON_GetObjectItem(funcIfBlock, "expr"), "type");
printf("%s\n", funcIfType->valuestring);
}
}
}
}
else
{
continue;
}
}
// for(int k = 0; k < line; k++){
// cJSON* funcRetrun = cJSON_GetObjectItem(cJSON_GetArrayItem(funcBlock,k), "expr");
// if(funcRetrun != NULL){
// cJSON* funcType = cJSON_GetObjectItem(funcRetrun,"type");
// if(funcType != NULL){
// printf("%s\n", funcType->valuestring);
// }
// else{
// printf("(null)\n");
// }
// }
// }
cJSON_Delete(root);
}
int main(int argc, char *argv[])
{
/*파일 열기*/
FILE *file = fopen(argv[1], "r");
if (file == NULL)
{
printf("json파일 열기 실패");
return 0;
}
/*json 메모리 할당*/
fseek(file, 0, SEEK_END); // 스트림 위치 끝으로 이동
long file_size = ftell(file); // 스트림 현재 위치 확인
fseek(file, 0, SEEK_SET); // 스트림 위치 처음으로 이동
char *json_string = (char *)malloc(file_size + 1); // json_string에 메모리 할당
if (json_string == NULL)
{
fclose(file);
printf("메모리 할당 실패");
return 0;
}
/*읽어오고 문자열 종료설정*/
fread(json_string, 1, file_size, file); // file에서 1부터 file_size까지 읽고 json_string에 저장
json_string[file_size] = '\0'; // 마지막 글자를 종료문자로 설정
fclose(file);
cJSON *root = cJSON_Parse(json_string);
if (root == NULL)
{
printf("JSON 파싱실패 root가 존재하지 않음");
free(json_string);
return 0;
}
cJSON *ext = cJSON_GetObjectItem(root, "ext");
if (ext == NULL)
{
printf("ext가 존재하지 않음");
cJSON_Delete(root);
return 0;
}
long arr_size = cJSON_GetArraySize(ext);
cJSON *function;
long idx = 0;
int Function_count = Func_count(json_string, file_size);
printf("number of Function : %d \n", Function_count);
cJSON_ArrayForEach(function, ext)
{
printf("[*]%ld\n", idx);
Function_name(json_string, file_size, idx);
IF_Count(json_string, file_size, idx);
Function_Param_Helper(function);
Return_Type(json_string, file_size, idx);
idx++;
}
free(json_string);
return 0;
}