-
Notifications
You must be signed in to change notification settings - Fork 0
/
shunting_yard.c
114 lines (95 loc) · 2.86 KB
/
shunting_yard.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
113
114
#include "stack.h"
#include "queue.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
struct parser {
struct stack* input;
struct queue* output;
struct stack* st;
};
struct token {
char *token;
char *value;
char* associativity; //left or right
int isOperator;
int isFunction;
int isNumber;
int precedence;
};
struct parser* newParser(struct stack* inp, int size, int cap) {
struct parser* pa = (struct parser*) malloc(sizeof(struct parser));
pa->st = newStack(cap);
pa->input = newStack(size);
pa->output = newQueue(size);
pa->input = inp;
return pa;
}
int precedence(struct token o1, struct token o2) {
if(o1.precedence > o2.precedence) {
return 1;
}
else if(o1.precedence > o2.precedence) {
return -1;
}
return 0;
}
int associativity(struct token o) {
if(strcmp(o.associativity, "left") == 0) {
return 1;
}
return -1;
}
void shuntingyard(struct parser* pa) {
int numelem = sizeS(pa->input);
int currIndex = 0;
while(currIndex < numelem) {
struct token currToken = pop(pa->input);
if(currToken.isNumber) {
enqueue(pa->output, currToken);
}
else if(currToken.isFunction) {
push(pa->st, currToken);
}
else if(currToken.isOperator) { //o1
struct token topst = peek(pa->st); //o2
while(strcmp(")", topst.value) != 0 && (precedence(topst, currToken) > 0 ||
(precedence(topst, currToken) == 0 && associativity(currToken) < 0))) {
enqueue(pa->output, topst);
pop(pa->st);
}
enqueue(pa->output, currToken);
}
else if(strcmp(")", currToken.value) == 0) {
enqueue(pa->output, currToken);
}
else if(strcmp("(", currToken.value) == 0) {
struct token topst = peek(pa->st); //top operator stack (o2)
while(strcmp(")", topst.value) != 0) {
if(isEmptyS(pa->st)) {
printf("Error: Mismatched Parentheses.\n");
return;
}
enqueue(pa->output, topst);
pop(pa->st);
}
if(strcmp(")", topst.value) == 0) {
pop(pa->st);
}
if(peek(pa->st).isFunction) {
enqueue(pa->output, topst);
pop(pa->st);
}
}
currIndex++;
}
while(!isEmptyS(pa->st)) {
struct token topst = peek(pa->st);
if(strcmp(")", topst.value) == 0) {
printf("Error: Mismatched Parentheses.\n");
return;
}
enqueue(pa->output, topst);
pop(pa->st);
}
}