-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnode.c
119 lines (110 loc) · 2.12 KB
/
node.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
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
#include "node.h"
extern int yylineno;
#define NEW(p) ((p) = malloc(sizeof *(p)))
#define NEW0(p) memset(NEW(p), 0, sizeof *(p))
/* new Node */
treeNode *newNodeOp(char* name)
{
treeNode *node;
NEW0(node);
//node->lineNo=yylineno;
strcpy(node->name,name);
node->child=NULL;
node->next=NULL;
node->father=NULL;
node->lineNo = yylineno;
return node;
}
treeNode* newNodeString(char *name,char *value)
{
treeNode *node;
NEW0(node);
//node->lineNo=yylineno;
strcpy(node->name,name);
strcpy(node->value,value);
node->child=NULL;
node->next=NULL;
node->father=NULL;
node->lineNo = yylineno;
return node;
}
treeNode *newNodeInt(char* name, int int_val)
{
treeNode *node;
NEW0(node);
//node->lineNo=yylineno;
strcpy(node->name,name);
node->child=NULL;
node->next=NULL;
node->int_val=int_val;
node->father=NULL;
node->lineNo = yylineno;
return node;
}
treeNode *newNodeError() {
treeNode *node;
NEW0(node);
strcpy(node->name,"ERROR");
node->lineNo=yylineno;
node->child=NULL;
node->next=NULL;
node->father=NULL;
return node;
}
/*give father a child*/
void addChild(treeNode* parent,treeNode* child)
{
if(child == NULL)
{
parent->child = NULL;
return;
}
if(parent != NULL && child != NULL)
{
child->next = parent->child; // add to head of children list
parent->child = child;
child->father = parent;
parent->lineNo = child->lineNo; // update the row of father node
}
}
/* print the syntax tree */
void printTree(treeNode* r,int count)
{
if(r==NULL)return;
if(r->child==NULL) //token
{
int i=0;
for(;i < count;i++) // retract
{
printf(" ");
}
//not all nodes need to print value
if(strcmp(r->name,"TYPE")==0||strcmp(r->name,"INTCONST")==0||strcmp(r->name,"ID")==0)
{
if(strcmp(r->name,"INTCONST")==0)
printf("%s: %d\n",r->name,r->int_val);
else
printf("%s: %s\n",r->name,r->value);
}
else
{
printf("%s\n",r->name);
}
}
else //non-terminal
{
int i=0;
for(;i<count;i++)
{
printf(" ");
}
printf("%s(%d)\n",r->name,r->lineNo);
treeNode *p=r->child;
//traverse the child nodes
while(p!=NULL){
printTree(p,count+1);
p=p->next;
}
}
return;
}