-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprecedence_stack.c
114 lines (97 loc) · 2.94 KB
/
precedence_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:
* IFJ20 - Compiler for imperative programming language IFJ20
*
* UNIVERSITY:
* Faculty of Information Technology, Brno University of Technology
*
* FILE:
* precedence_stack.c
*
* DESCRIPTION:
* Stack for precedence analisis
*
* AUTHORS:
* Kolaříková Mirka <xkolar76@stud.fit.vutbr.cz>
*/
#include "precedence_stack.h"
/* creates new stack of specific size, returns pointer */
struc_prec_stack* create_precStack(int size){
struc_prec_stack* stackPtr = (struc_prec_stack*) malloc(sizeof(struc_prec_stack));
if(stackPtr == NULL){ //malloc error
exit(99);}
stackPtr->maxsize = size;
stackPtr->top = -1; //empty stack
stackPtr->token = (struc_token*) malloc(sizeof(struc_token) * size);
if(stackPtr->token == NULL){ //malloc error
exit(99);}
return stackPtr;
}
/* safely frees stack and its data */
void free_precStack(struc_prec_stack *stackPtr){
free(stackPtr->token);
free(stackPtr);
}
/* returns actual size of stack */
int size_precStack(struc_prec_stack *stackPtr){
return stackPtr->top+1;
}
/* check if stack is empty, return code: 1 = empty, 0 = not empty */
int isEmpty_precStack(struc_prec_stack *stackPtr){
return size_precStack(stackPtr) == 0;
}
/* check if stack is full, return code: 1 = full, 0 = not full */
int isFull_precStack(struc_prec_stack *stackPtr){
return size_precStack(stackPtr) == stackPtr->maxsize;
}
/* returns pointer to token at top, or NULL if empty*/
struc_token* peek1_precStack(struc_prec_stack *stackPtr){
if(!isEmpty_precStack(stackPtr)){
return &stackPtr->token[stackPtr->top];
}else{
return NULL;
}
}
/* returns pointer to token at top-1, or NULL if empty*/
struc_token* peek2_precStack(struc_prec_stack *stackPtr){
if(size_precStack(stackPtr) < 2){
return NULL;
}else{
return &stackPtr->token[stackPtr->top-1];
}
}
/* returns pointer to token at top-2, or NULL if empty*/
struc_token* peek3_precStack(struc_prec_stack *stackPtr){
if(size_precStack(stackPtr) < 3){
return NULL;
}else{
return &stackPtr->token[stackPtr->top-2];
}
}
/* adds new token (tokenNum, tokenStr) to stack top, return code: 1 = error, 0 = succes*/
int push_precStack(struc_prec_stack *stackPtr, int num, string content){
if(isFull_precStack(stackPtr)){
return 1; //is full
}
++stackPtr->top; //increment top
stackPtr->token[stackPtr->top].tokenNum = num;
strInit(&(stackPtr->token[stackPtr->top].tokenStr));
strCopyString(&(stackPtr->token[stackPtr->top].tokenStr), &content);
return 0; //succes
}
/* pops token at the stack top */
void pop_precStack(struc_prec_stack *stackPtr){
if(!isEmpty_precStack(stackPtr)){
strFree(&(stackPtr->token[stackPtr->top].tokenStr));
--stackPtr->top;
}
}
/* prints stack at format BOTTOM [{,}] TOP */
void print_precStack(struc_prec_stack *stackPtr){
int max = stackPtr->top;
printf("BOTTOM [");
for(int i = 0;i<=max;i++){
printf("{%d,%s}",stackPtr->token[i].tokenNum, stackPtr->token[i].tokenStr.str);
}
printf("] TOP\n");
}