forked from omkarparsewar789/CompilerConstructionBITSPILANI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.c
More file actions
112 lines (100 loc) · 2.8 KB
/
stack.c
File metadata and controls
112 lines (100 loc) · 2.8 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
/*
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 "stackDef.h"
#include <stdlib.h>
#include <stdio.h>
bool isEmpty(Stack* stack);
StackNode* createStackNode(Symbol symbol, TreeNode* parent);
Stack* createStack();
void push(Stack* stack, Symbol symbol, TreeNode* parent);
Symbol pop(Stack* stack);
Symbol top(Stack* stack);
TreeNode* topParent(Stack* stack);
void freeStack(Stack* stack);
// Check if the stack is empty
bool isEmpty(Stack* stack) {
return stack->top == NULL;
}
StackNode* createStackNode(Symbol symbol, TreeNode* parent) {
StackNode* node = (StackNode*)malloc(sizeof(StackNode));
if (!node) {
printf("Failed to allocate stack node\n");
exit(EXIT_FAILURE); // Exit if allocation fails
}
node->symbol = symbol;
node->next = NULL;
node->parent = parent;
return node;
}
// Create a new stack
Stack* createStack() {
Stack* stack = (Stack*)malloc(sizeof(Stack));
if (!stack) {
printf("Failed to allocate stack\n");
exit(EXIT_FAILURE); // Exit if allocation fails
}
stack->top = NULL;
// pushing dollar symbol onto the stack
// VERIFY
Symbol dollar = {true, .symbol.t = DOLLAR};
push(stack, dollar, NULL); // Push the dollar symbol onto the stack
return stack;
}
// Push a symbol onto the stack
void push(Stack* stack, Symbol symbol, TreeNode* parent) {
StackNode* node = (StackNode*)malloc(sizeof(StackNode));
if (!node) {
printf("Failed to allocate stack node\n");
exit(EXIT_FAILURE); // Exit if allocation fails
}
node->symbol = symbol;
node->next = stack->top;
node->parent = parent;
stack->top = node;
}
// Pop a symbol from the stack
Symbol pop(Stack* stack) {
if (isEmpty(stack)) {
printf("Stack underflow\n");
exit(EXIT_FAILURE); // Exit if stack is empty
}
StackNode* node = stack->top;
stack->top = node->next;
Symbol symbol = node->symbol;
free(node);
return symbol;
}
// Get the top symbol from the stack
Symbol top(Stack* stack) {
if (isEmpty(stack)) {
printf("Stack is empty\n");
exit(EXIT_FAILURE); // Exit if stack is empty
}
return stack->top->symbol;
}
// Get the parent of the top symbol from the stack
TreeNode* topParent(Stack* stack) {
if (isEmpty(stack)) {
printf("Stack is empty\n");
exit(EXIT_FAILURE); // Exit if stack is empty
}
return stack->top->parent;
}
// Free the stack
void freeStack(Stack* stack) {
StackNode* node = stack->top;
while (node) {
StackNode* temp = node;
node = node->next;
free(temp);
}
free(stack);
}