-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrdp.c
280 lines (264 loc) · 8.73 KB
/
rdp.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
//
// rdp.c
// expr
//
// Created by sallaben on 10/4/17.
//
#include "rdp.h"
/*
* RDP struct definition
*/
struct RDP {
int index; //character index measuring how much of the input has been evaluated
char* input; //stores the full input given to the RDP as a constant
Tree tree; //stores the parse tree object for this RDP
bool epsilon; //if true, the last production matched was with the symbol epsilon (useful for creating the tree)
};
/*
* Allocate and initialize a new RDP instance
*/
RDP RDP_new(char* input) {
RDP rdp = malloc(sizeof(struct RDP)); //allocate memory for new RDP
rdp->index = -1; //set its initial index to -1, signifying that the RDP has not yet processed any input
rdp->input = input; //stores the full input expression
rdp->epsilon = false;
return rdp;
}
/*
* Processes an input using the given RDP and recursive-descent parsing,
* starting by checking whether or not the input matches G_expression()
*/
bool RDP_process(RDP rdp) {
rdp->tree = Tree_new(Node_new(NULL, "EXPRESSION")); //initialize the tree
if(G_expression(rdp, Tree_root(rdp->tree))) { //if the RDP can match an expression
if(RDP_next(rdp) == '$') { //if the next symbol is the end symbol '$'
printf("\n ** Successfully parsed! **\n"); //success
return true;
} else { //failure while reading final char
printf("\n ** Error while reading end of expression! **\n");
return false;
}
} else { //failure while parsing
printf("\n ** Error(s) while parsing expression! **\n");
return false;
}
}
/*
* "Consumes" character (increments the character index of the RDP by 1)
*/
void RDP_consume(RDP rdp) {
rdp->index++;
}
/*
* Returns the lookahead char (next character in the input string) without consuming anything
*/
char RDP_next(RDP rdp) {
return rdp->input[rdp->index + 1];
}
/*
* Matches an input character in the given RDP to a Digit
*/
bool G_digit(RDP rdp, Node node) {
char c = RDP_next(rdp);
if(c == '0' || c == '1' ||
c == '2' || c == '3' ||
c == '4' || c == '5' ||
c == '6' || c == '7' ||
c == '8' || c == '9') {
char* label = malloc(2);
label[0] = c; label[1] = '\0';
Node charNode = Node_new(node, label);
Node_add_child(node, charNode);
RDP_consume(rdp);
return true;
}
return false;
}
/*
* Matches an input character in the given RDP to a Number
*/
bool G_number(RDP rdp, Node node) {
Node digitNode = Node_new(node, "DIGIT");
Node numberPrimeNode = Node_new(node, "NUMBERPRIME");
if(G_digit(rdp, digitNode)) {
Node_add_child(node, digitNode);
if(G_number_prime(rdp, numberPrimeNode)) {
if(!rdp->epsilon) { //purely visual: don't print productions matched only by epsilon
Node_add_child(node, numberPrimeNode);
} else {
rdp->epsilon = false;
}
return true;
}
}
return false;
}
/*
* Matches an input character in the given RDP to a Number' (a product of eliminating left-recursion)
*/
bool G_number_prime(RDP rdp, Node node) {
Node numberNode = Node_new(node, "NUMBER");
if(G_number(rdp, numberNode)) {
Node_add_child(node, numberNode);
return true;
}
rdp->epsilon = true;
return true;
}
/*
* Matches an input character in the given RDP to a Factor
*/
bool G_factor(RDP rdp, Node node) {
Node numberNode = Node_new(node, "NUMBER");
Node lparenNode = Node_new(node, "(");
Node expressionNode = Node_new(node, "EXPRESSION");
Node rparenNode = Node_new(node, ")");
if(G_number(rdp, numberNode)) {
Node_add_child(node, numberNode);
return true;
} else if(RDP_next(rdp) == '(') {
Node_add_child(node, lparenNode);
RDP_consume(rdp);
if(G_expression(rdp, expressionNode)) {
Node_add_child(node, expressionNode);
if(RDP_next(rdp) == ')') {
Node_add_child(node, rparenNode);
RDP_consume(rdp);
return true;
}
}
}
return false;
}
/*
* Matches an input character in the given RDP to a Factor' (a product of eliminating left-recursion)
*/
bool G_factor_prime(RDP rdp, Node node) {
Node multiplyNode = Node_new(node, "*");
Node divideNode = Node_new(node, "/");
Node factorNode = Node_new(node, "FACTOR");
Node factorPrimeNode = Node_new(factorNode, "FACTORPRIME");
switch(RDP_next(rdp)) {
case '*':
Node_add_child(node, multiplyNode);
RDP_consume(rdp);
if(G_factor(rdp, factorNode)) {
Node_add_child(node, factorNode);
if(G_factor_prime(rdp, factorPrimeNode)) {
if(!rdp->epsilon) {
Node_add_child(node, factorPrimeNode);
} else {
rdp->epsilon = false;
}
return true;
}
} break;
case '/':
Node_add_child(node, divideNode);
RDP_consume(rdp);
if(G_factor(rdp, factorNode)) {
Node_add_child(node, factorNode);
if(G_factor_prime(rdp, factorPrimeNode)) {
if(!rdp->epsilon) {
Node_add_child(node, factorPrimeNode);
} else {
rdp->epsilon = false;
}
return true;
}
} break;
default: rdp->epsilon = true; return true;
}
return false;
}
/*
* Matches an input character in the given RDP to a Term
*/
bool G_term(RDP rdp, Node node) {
Node factorNode = Node_new(node, "FACTOR");
Node factorPrimeNode = Node_new(node, "FACTORPRIME");
if(G_factor(rdp, factorNode)) {
Node_add_child(node, factorNode);
if(G_factor_prime(rdp, factorPrimeNode)) {
if(!rdp->epsilon) {
Node_add_child(node, factorPrimeNode);
} else {
rdp->epsilon = false;
}
return true;
}
}
return false;
}
/*
* Matches an input character in the given RDP to a Term' (a product of eliminating left-recursion)
*/
bool G_term_prime(RDP rdp, Node node) {
Node plusNode = Node_new(node, "+");
Node minusNode = Node_new(node, "-");
Node termNode = Node_new(node, "TERM");
Node termPrimeNode = Node_new(termNode, "TERMPRIME");
switch(RDP_next(rdp)) {
case '+':
Node_add_child(node, plusNode);
RDP_consume(rdp);
if(G_term(rdp, termNode)) {
Node_add_child(node, termNode);
if(G_term_prime(rdp, termPrimeNode)) {
if(!rdp->epsilon) {
Node_add_child(node, termPrimeNode);
} else {
rdp->epsilon = false;
}
return true;
}
} break;
case '-':
Node_add_child(node, minusNode);
RDP_consume(rdp);
if(G_term(rdp, termNode)) {
Node_add_child(node, termNode);
if(G_term_prime(rdp, termPrimeNode)) {
if(!rdp->epsilon) {
Node_add_child(node, termPrimeNode);
} else {
rdp->epsilon = false;
}
return true;
}
} break;
default: rdp->epsilon = true; return true;
}
return false;
}
/*
* Matches an input character in the given RDP to an Expression
*/
bool G_expression(RDP rdp, Node node) {
Node termNode = Node_new(node, "TERM");
Node termPrimeNode = Node_new(node, "TERMPRIME");
if(G_term(rdp, termNode)) {
Node_add_child(node, termNode);
if(G_term_prime(rdp, termPrimeNode)) {
if(!rdp->epsilon) {
Node_add_child(node, termPrimeNode);
} else {
rdp->epsilon = false;
}
return true;
}
}
return false;
}
/*
* Returns the given RDP's tree
*/
Tree RDP_get_tree(RDP rdp) {
return rdp->tree;
}
/*
* Frees the RDP from memory
*/
void RDP_free(RDP rdp) {
free(rdp);
}