-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.c
68 lines (67 loc) · 1.58 KB
/
tree.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
#include<stdio.h>
#include <stdlib.h>
#include<string.h>
#include"tree.h"
#include"syntax.tab.h"
Node* malloc_node(char *name,int type,void* val,int line){
Node *node = malloc(sizeof(Node));
node->name = malloc(strlen(name)+1);
strcpy(node->name,name);
node->childnum = 0;
node->type = type;
node->lineno = line;
for(int i=0;i<10;i++){
node->childlist[i]=NULL;
}
if(type==INT){
node->val.i = *(int*)val;
}
else if(type == FLOAT){
node->val.f = *(float*)val;
}
else if(type==ID||type==Type || type == RELOP){
node->val.s = malloc(strlen(val)+1);
strcpy(node->val.s,val);
}
else{
node->val.i = 0;
}
return node;
}
void *add(Node *father,Node *son){
father->childlist[father->childnum++]=son;
}
void print_space(int n){
for(int i=0;i<2*n;i++){
printf(" ");
}
}
void preorder(Node *root,int space_num){
if(root==NULL)return;
print_space(space_num);
switch (root->type)
{
case 0://gramma unit not generating empty
/* code */
printf("%s (%d)\n", root->name, root->lineno);
break;
case INT:
printf("%s: %d\n", root->name, root->val.i);
break;
case FLOAT:
printf("%s: %f\n", root->name, root->val.f);
break;
case Type:
case ID:
printf("%s: %s\n", root->name, root->val.s);
break;
default:
printf("%s\n", root->name);
break;
}
space_num++;
for(int i=0;i<root->childnum;i++){
preorder(root->childlist[i],space_num);
}
space_num--;
}