This repository was archived by the owner on Apr 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.c
More file actions
152 lines (126 loc) · 3.25 KB
/
Copy pathnode.c
File metadata and controls
152 lines (126 loc) · 3.25 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
#include <stdlib.h>
#include <stdio.h>
#include "node.h"
const char* current;
/**
* peek - Get current character
* @return char
*/
char peek() {
return *current;
}
/**
* next - Get next character and updates pointer
* @return char
*/
char next() {
return *current++;
}
Node* allocate_node(const NodeType type, const char value, Node* left, Node* right) {
Node* node = malloc(sizeof(Node));
if (node) {
node->type = type;
node->value = value;
node->left = left;
node->right = right;
}
return node;
}
void free_node(Node* node) {
if (node) {
free_node(node->left);
free_node(node->right);
free(node);
}
}
Node* parse_atom() {
if (peek() == '(') {
next();
Node* node = parse_union();
if (peek() == ')') {
next();
} else {
printf("Error! Malformed regex!\n");
exit(1);
}
return node;
}
if (peek() != 0 && peek() != ')' && peek() != '|' && peek() != '*') {
return allocate_node(NODE_CHAR, next(), NULL, NULL);
}
return NULL;
}
Node* parse_repeat() {
Node* node = parse_atom();
while (1) {
if (peek() == '*') {
next();
node = allocate_node(NODE_STAR, 0, node, NULL);
} else if (peek() == '+') {
next();
node = allocate_node(NODE_PLUS, 0, node, NULL);
} else if (peek() == '?') {
next();
node = allocate_node(NODE_OPTIONAL, 0, node, NULL);
} else {
break;
}
}
return node;
}
Node* parse_concat() {
Node* left = parse_repeat();
while (peek() && peek() != '|' && peek() != ')') {
Node* right = parse_repeat();
if (!right) break;
left = allocate_node(NODE_CONCAT, 0, left, right);
}
return left;
}
Node* parse_union() {
Node* left = parse_concat();
while (peek() == '|') {
next();
Node* right = parse_concat();
left = allocate_node(NODE_UNION, 0, left, right);
}
return left;
}
Node* parse(const char* input) {
current = input;
return parse_union();
}
void print_tree_rec(Node* node, const char* prefix, const char is_last) {
if (!node) return;
printf("%s", prefix);
printf("%s", is_last ? "|_ " : "|- ");
switch (node->type) {
case NODE_CHAR:
printf("CHAR('%c')\n", node->value); break;
case NODE_CONCAT:
printf("CONCAT\n"); break;
case NODE_UNION:
printf("UNION\n"); break;
case NODE_STAR:
printf("STAR\n"); break;
case NODE_PLUS:
printf("PLUS\n"); break;
case NODE_OPTIONAL:
printf("OPTIONAL\n"); break;
default:
printf("UNKNOWN\n");
}
char new_prefix[1024];
snprintf(new_prefix, sizeof(new_prefix), "%s%s", prefix, is_last ? " " : "| ");
unsigned int child_count = 0;
if (node->left) child_count++;
if (node->right) child_count++;
if (node->left)
print_tree_rec(node->left, new_prefix, child_count == 1 && !node->right);
if (node->right)
print_tree_rec(node->right, new_prefix, 1);
}
void print_tree(Node* node) {
printf("ROOT\n");
print_tree_rec(node, "", 1);
}