forked from SarthakShah001/Compiler2K23
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackADT.c
More file actions
120 lines (102 loc) · 2.19 KB
/
stackADT.c
File metadata and controls
120 lines (102 loc) · 2.19 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
/*
***** Group No. - 9 *****
Name : Sarthak Shah ID : 2020A7PS0092P
Name : Siddharth Khandelwal ID : 2020A7PS0098P
Name : Archaj Jain ID : 2020A7PS0072P
Name : BhanuPratap Singh Rathore ID : 2020A7PS1675P
Name : Rishi Rakesh Shrivastava ID : 2020A7PS0108P
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "parserDef.h"
#include "stackADT.h"
parse_Stack init_parseStack()
{
parse_Stack S = (parse_Stack)(malloc(sizeof(struct stack)));
stackNode n = (stackNode)malloc(sizeof(struct node));
parseTreeNode tnode = createTree();
symbol s1 = (symbol)malloc(sizeof(struct SYMBOL));
s1->is_terminal = true;
s1->t = $;
tnode->s = s1;
n->tree_ptr = tnode;
n->next = NULL;
S->size = 1;
S->top = n;
return S;
}
bool isEmpty(parse_Stack S)
{
return S->size == 0;
}
void push_on_stack(parse_Stack S, parseTreeNode x)
{
stackNode temp = (stackNode)(malloc(sizeof(struct node)));
if (temp == NULL)
{
printf("temp Malloc function failed\n");
return;
}
temp->tree_ptr = x;
temp->next = S->top;
S->top = temp;
S->size++;
return;
}
void pop(parse_Stack S)
{
if (isEmpty(S))
{
printf("Stack is empty.\n");
}
else
{
stackNode temp = S->top;
S->top = S->top->next;
temp->next = NULL;
S->size--;
free(temp);
}
return;
}
stackNode top(parse_Stack S)
{
if (isEmpty(S))
{
printf("Stack is empty.\n");
return NULL;
}
else
{
return S->top;
}
}
void push_rule(parse_Stack S, dlinkedlist dlist)
{
stackNode t = S->top;
parseTreeNode p1 = t->tree_ptr;
parseTreeNode temp = NULL;
if (p1 == NULL)
{
printf("p1 null hogya\n");
return;
}
temp = p1->child;
if (temp == NULL)
{
printf("No matching rule found\n");
return;
}
while (temp->sibling != NULL)
{
temp = temp->sibling;
}
pop(S);
while (temp != NULL)
{
push_on_stack(S, temp);
temp = temp->prevSibling;
}
return;
}