-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtreetrim.c
78 lines (65 loc) · 1.64 KB
/
treetrim.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
71
72
73
74
75
76
77
78
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "tree.h"
#include "treetrim.h"
#include "token.h"
#include "keywords.h"
node_t*
treetrim(node_t* newroot, node_t* root)
{
newroot = NULL;
newroot = insert(newroot, root->token, root->depth);
int i;
for (i = 0; i < root->num_children; i++) {
climbandtrim(newroot, root->children[i]);
}
return newroot;
}
node_t*
climbandtrim(node_t* newroot, node_t* root)
{
if (root == NULL)
return (node_t*)NULL;
//printf("fuck\n");
// if it is essential
if (isessential(root)) {
insert(newroot, root->token, root->depth);
}
int i;
for (i = 0; i < root->num_children; i++) {
if (root->num_children != 0)
climbandtrim(newroot->children[newroot->num_children-1], root->children[i]);
}
return newroot;
}
int
isessential(node_t* node)
{
if (node->essential)
return 1;
if (strcmp(node->token->id, ".TK") == 0)
return 0;
if (strcmp(node->token->id, ":TK") == 0)
return 0;
if (strcmp(node->token->id, "(TK") == 0)
return 0;
if (strcmp(node->token->id, ")TK") == 0)
return 0;
if (iskeyword(node->token->instance))
return 0;
if (strcmp(node->token->id, "EOFTK") == 0)
return 0;
if (strcmp(node->token->id, "<>") == 0) {
int i;
for (i = 0; i < node->num_children; i++) {
if (isessential(node->children[i])) {
node->essential = 1;
return 1;
}
}
return 0;
}
node->essential = 1;
return 1;
}