-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
72 lines (62 loc) · 1.34 KB
/
main.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
#include "defs.h"
int main(void) {
linecounter = 1;
if (yyparse() == 0) {
fprintf(stderr, "\nparser successfully ended\n\n");
}
return(EXIT_SUCCESS);
}
Cell *cons(Cell *car, Cell *cdr) {
Cell *pointer;
pointer = (Cell *)malloc(sizeof(Cell));
pointer->kind = CONS;
pointer->head = car;
pointer->tail = cdr;
return(pointer);
}
Cell *node(char *car, Cell *cdr) {
Cell *pointer;
pointer = (Cell *)malloc(sizeof(Cell));
pointer->kind = NODE;
pointer->head = (Cell *)strdup(car);
pointer->tail = cdr;
return(pointer);
}
Cell *leaf(char *car, char *cdr) {
Cell *pointer;
pointer = (Cell *)malloc(sizeof(Cell));
pointer->kind = LEAF;
pointer->head = (Cell *)strdup(car);
pointer->tail = (Cell *)strdup(cdr);
return(pointer);
}
void tree(Cell *pointer) {
visit(pointer, 1);
printf("\n");
}
void visit(Cell *pointer, int level) {
int count;
printf("\n");
for (count = 0; count < level; count++) {
printf(" ");
}
if (pointer->kind == CONS) {
printf("(");
visit(pointer->head, level + 1);
visit(pointer->tail, level + 1);
printf(")");
}
if (pointer->kind == NODE) {
printf("(");
printf("%s ", (char *)pointer->head);
visit(pointer->tail, level + 1);
printf(")");
}
if (pointer->kind == LEAF) {
printf("(");
printf("%s ", (char *)pointer->head);
printf("%s", (char *)pointer->tail);
printf(")");
}
return;
}