-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
112 lines (69 loc) · 2.06 KB
/
stack.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
/**
* PROJECT: Implementation of imperative IFJ21 programming language compiler
* PART: Expression processing (symbol stack)
*
* AUTHOR(S): Sadovskyi Dmytro (xsadov06)
*/
#include "stack.h"
void s_init ( Stack *stack ) {
stack->top = NULL;
}
void s_push ( Stack *stack, Data_type type, pt_symbol symbol, bool isZero ) {
Stack_item *newItem = malloc( sizeof ( Stack_item ) );
if (!newItem) exit( ERR_INTERNAL );
newItem->nextItem = stack->top;
newItem->symbol = symbol;
newItem->type = type;
newItem->isZero = isZero;
stack->top = newItem;
}
void s_push_before_terminal ( Stack *stack, Data_type type, pt_symbol symbol ) {
Stack_item *tmp1 = stack->top;
Stack_item *tmp2 = NULL;
while (1) {
if (!tmp1) break;
if (tmp1->symbol != NONTERM && tmp1->symbol != STOP) {
Stack_item *newItem = malloc( sizeof( Stack_item ) );
if (!newItem) return;
newItem->symbol = symbol;
newItem->type = type;
if (!tmp2) {
newItem->nextItem = stack->top;
stack->top = newItem;
}
else {
newItem->nextItem = tmp1;
tmp2->nextItem = newItem;
}
return;
}
tmp2 = tmp1;
tmp1 = tmp1->nextItem;
}
}
void s_pop ( Stack *stack ) {
if (stack->top != NULL) {
Stack_item *toPop = stack->top;
stack->top = stack->top->nextItem;
free( toPop );
}
}
Stack_item *s_top ( Stack *stack ) {
return stack->top;
}
Data_type s_top_type ( Stack *stack ) {
return stack->top->type;
}
pt_symbol s_top_terminal_symbol ( Stack *stack ) {
Stack_item *topTerminal = stack->top;
while (topTerminal) {
if (topTerminal->symbol != NONTERM && topTerminal->symbol != STOP) break;
topTerminal = topTerminal->nextItem;
}
return topTerminal->symbol;
}
void s_dispose ( Stack *stack ) {
while (stack->top) {
s_pop( stack );
}
}