forked from Shivankit-Gaind/Compiler-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack.c
108 lines (79 loc) · 1.91 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
/****
Name: Shivankit Gaind
ID: 2015A7PS0076P
****/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Stack.h"
Stack* initialize_stack(){
Stack* stk = (Stack*)malloc(sizeof(Stack));
stk->size=0;
stk->list = (StackList*)malloc(sizeof(StackList));
stk->list->head = NULL;
return stk;
}
StackNode* create_stack_node(TreeNode* treenode){
StackNode* node = (StackNode*)malloc(sizeof(StackNode));
node->treeNodePointer = treenode;
node->next = NULL;
return node;
}
void push(Stack* stk, TreeNode* treenode){
StackNode* node = create_stack_node(treenode);
node->next = stk->list->head;
stk->list->head = node;
stk->size++;
}
StackNode* pop(Stack* stk){
if(stk->size==0){
printf("Stack is Empty\n");
return NULL;
}
else{
StackNode* temp = stk->list->head;
stk->list->head = stk->list->head->next;
stk->size--;
return temp;
}
}
StackNode* top(Stack* stk){
if(stk->size==0){
printf("Stack is Empty\n");
return NULL;
}
else{
StackNode* temp = stk->list->head;
return temp;
}
}
void push_children_on_the_stack(Stack* stk, TreeNode* treenode){
Children* children = treenode->children;
TreeNode* temp = children->head;
//DUMMY NODE
StackNode* head = create_stack_node(NULL);
StackNode* temp_stack = head;
//Don't Push EPSILON on the stack
if(temp->info->term_or_nonterm == 0 && temp->info->type.terminal == EPSILON){
return;
}
while(temp!=NULL){
//Create a Stack Node
StackNode* node = create_stack_node(temp);
temp_stack->next = node;
temp_stack = temp_stack->next;
temp = temp->next;
}
temp_stack->next = stk->list->head;
stk->list->head = head->next;
stk->size += treenode->children->no_siblings;
}
//Whether $ is on the top of the stack or not
int isTopDOLLAR(Stack* stk){
if(stk->size==1){
TreeNodeInfo* info = stk->list->head->treeNodePointer->info;
if(info->term_or_nonterm==0 && info->type.terminal==DOLLAR)
return 1;
}
return 0;
}