forked from omkarparsewar789/CompilerConstructionBITSPILANI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseTree.c
More file actions
120 lines (110 loc) · 3.09 KB
/
parseTree.c
File metadata and controls
120 lines (110 loc) · 3.09 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
/*
Group No: 15
Group members:
1. Shivodit Raj Vishnoi - 2022A7PS1221P
2. Parsewar Omkar Balaji - 2022A7PS0089P
3. Samiksha Kaul - 2022A7PS1169P
4. Sohan Reddy Jalakanti - 2022A7PS1177P
5. Arnav Gupta - 2022A7PS1189P
6. Akshat Gosain - 2022A7PS0154G
*/
#include "parseTreeDef.h"
#include "lexerDef.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Create a new tree node
// TreeNode* createTreeNode(Symbol symbol, bool isleaf, tokenInfo* token) {
// TreeNode* node = (TreeNode*)malloc(sizeof(TreeNode));
// if (!node) {
// printf("Failed to allocate tree node\n");
// exit(EXIT_FAILURE); // Exit if allocation fails
// }
// node->symbol = symbol;
// node->isleaf = isleaf;
// node->token = token;
// node->parent = NULL;
// node->child = NULL;
// node->next = NULL;
// node->prev = NULL;
// return node;
// }
TreeNode* createTreeNode(Symbol symbol, bool isleaf, tokenInfo* token) {
TreeNode* node = (TreeNode*)malloc(sizeof(TreeNode));
if (!node) {
printf("Failed to allocate tree node\n");
exit(EXIT_FAILURE);
}
node->symbol = symbol;
node->isleaf = isleaf;
if (token != NULL) {
// Allocate memory for a new tokenInfo structure
node->token = (tokenInfo*)malloc(sizeof(tokenInfo));
if (!node->token) {
printf("Failed to allocate token info\n");
exit(EXIT_FAILURE);
}
// Copy the content of token into the newly allocated tokenInfo
*(node->token) = *token;
// Deep copy the lexeme if it exists
if (token->lexeme != NULL) {
node->token->lexeme = strdup(token->lexeme);
if (node->token->lexeme == NULL) {
printf("Failed to allocate lexeme copy\n");
exit(EXIT_FAILURE);
}
}
} else {
node->token = NULL;
}
node->parent = NULL;
node->child = NULL;
node->next = NULL;
node->prev = NULL;
return node;
}
// Add a child to a parent node
void addChild(TreeNode* parent, TreeNode* child) {
if (parent->child == NULL) {
parent->child = child;
} else {
TreeNode* temp = parent->child;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = child;
child->prev = temp;
}
child->parent = parent;
}
// free the treenode and its children
void freeNode(TreeNode* node) {
if (node == NULL) return;
TreeNode* child = node->child;
while (child != NULL) {
TreeNode* next = child->next;
freeNode(child);
child = next;
}
free(node);
}
// remove a node from the tree
void removeNode(TreeNode* node) {
// assuming if node is root then remove the whole tree
if (node->parent == NULL){
freeNode(node);
return;
}
TreeNode* parent = node->parent;
if (parent->child == node) {
parent->child = node->next;
} else {
node->prev->next = node->next;
if (node->next != NULL) {
node->next->prev = node->prev;
}
}
freeNode(node);
return;
}