This repository was archived by the owner on Apr 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPARSER.cpp
339 lines (289 loc) · 10.5 KB
/
PARSER.cpp
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include "csvmanag.h"
#include "stack.h"
#include "errorhan.h"
#include "parsetre.h"
struct derivations{
char input[50];
CsvRow *derives;
derivations *next;
} *curr_derivation, *head_derivation, *new_derivation;
struct symbols{
char symbol[50];
derivations *start;
struct symbols *next;
} *curr_symbol, *head_symbol, *new_symbol;
void init_parse_table(){
int i = 0;
// file, delimiter, first_line_is_header?
CsvParser *csvparser = CsvParser_new("parsetbl.csv", ",", 1);
CsvRow *header;
CsvRow *row;
header = CsvParser_getHeader(csvparser);
if (header == NULL) {
printf("%s\n", CsvParser_getErrorMessage(csvparser));
}
char **headerFields = CsvParser_getFields(header);
bool head = true;
bool start_derivation = true;
//INITIAL SYMBOL TABLE ROW
row = CsvParser_getRow(csvparser);
while(row != NULL){
char **rowFields = CsvParser_getFields(row);
for (i = 0 ; i < CsvParser_getNumFields(row) ; i++) {
// printf("FIELD: %s\n", rowFields[i]);
if(i==0){
//Symbols
/*
i is zero, pointing to the symbols column
row->numOfFields is 1, pointing to the table's 2nd row which will be the initial input of the parser
*/
new_symbol = (symbols *)malloc(sizeof(symbols));
strcpy(new_symbol->symbol, rowFields[i]);
if(head){
head_symbol = new_symbol;
curr_symbol = head_symbol;
head = false;
}else{
curr_symbol->next = new_symbol;
curr_symbol = new_symbol;
}
//for every row
start_derivation = true;
}
else{
if(strcmp(rowFields[i]," ")==1){ //field is not null
CsvParser *derivation_parser = CsvParser_new_from_string(rowFields[i], " ", 0);
new_derivation = (derivations *)malloc(sizeof(derivations));
strcpy(new_derivation->input,headerFields[i]);
new_derivation->derives = CsvParser_getRow(derivation_parser);
if(start_derivation){
curr_symbol->start = new_derivation;
curr_derivation = new_derivation;
start_derivation = false;
}
else{
curr_derivation->next = new_derivation;
curr_derivation = new_derivation;
}
// int j;
// char **values = CsvParser_getFields(curr_derivation->derives);
// for (j = 0 ; j < CsvParser_getNumFields(curr_derivation->derives) ; j++) {
// printf("%s\n",values[j]);
// }
CsvParser_destroy(derivation_parser);
}
}
}
// printf("\n");
CsvParser_destroy_row(row);
//NEXT ROW
row = CsvParser_getRow(csvparser);
// getch();
}
CsvParser_destroy(csvparser);
}
void unload_parse_table(){
//start from head symbol
curr_symbol = head_symbol;
int ctr = 0;
while(curr_symbol != NULL){
ctr++;
curr_derivation = curr_symbol->start;
printf("\n\nCurr Symbol: %d %s", ctr, curr_symbol->symbol);
int der_ctr = 0;
do{
der_ctr++;
printf("\nCurr Derivation: %d %s", der_ctr, curr_derivation->input);
char **derivation_array = CsvParser_getFields(curr_derivation->derives);
printf("\nStart of Derivation: %s",derivation_array[0]);
printf("\nUnloading Derivation");
CsvParser_destroy_row(curr_derivation->derives);
printf("\nUnloaded Derivation");
printf("\nMoving to next derivation");
curr_derivation = curr_derivation->next;
printf("\nMoved to next derivation");
}while(curr_derivation != NULL);
printf("\nMoving to next Symbol");
curr_symbol = curr_symbol->next;
printf("\nMoved to next Symbol");
}
printf("\n\nUnloading Complete");
}
int main() {
init_parse_table();
//initialize lexical output file
CsvParser *lexical_parser = CsvParser_new("samsymtb.txt", "~", 0);
CsvRow *symboltbl_row;
//initialize stack
push("$");
push("SYNO");
//initialize symbol table input
symboltbl_row = CsvParser_getRow(lexical_parser);
char token[50];
char **symboltbl_rowFields;
bool root = true;
bool EOF_reached = false;
bool found_derivation = false;
do{
printf("\n\nSTACK'S TOP: %s", get_top());
if(symboltbl_row != NULL){
symboltbl_rowFields = CsvParser_getFields(symboltbl_row);
strcpy(token,symboltbl_rowFields[1]);
}else{
EOF_reached = true;
strcpy(token,"$");
}
printf("\nCURRENT TOKEN: %s\nACTION--------", token);
if(strcmp(token, "COMMENT_OPERATOR") == 0 || strcmp(token, "SINGLE_COMMENT") == 0 || strcmp(token, "MULTI_COMMENT") == 0){
//if current token is comment ignore and move to next token
CsvParser_destroy_row(symboltbl_row);
symboltbl_row = CsvParser_getRow(lexical_parser);
}
else{
if(symboltbl_row != NULL){
if((terminal_identifier(get_top())==1)){ //top of stack is symbol
//symbol table has input token
//start scanning table from the head symbol
curr_symbol = head_symbol;
found_derivation = false;
do{ //scan non-terminals or symbols
if(strcmp(get_top(),curr_symbol->symbol)==0){ //symbol found
curr_derivation = curr_symbol->start; //initialize derivation list with the current symbol's start derivation link
do{ //scan derivations
if(strcmp(curr_derivation->input,symboltbl_rowFields[1])==0){ //derivation for this input found
char popped_symbol[50]; //pop the top of the stack
strcpy(popped_symbol, pop());
printf("\nPOPPED: %s", popped_symbol);
add_line(popped_symbol);
char **derivation_array = CsvParser_getFields(curr_derivation->derives);
int j;
for (j = CsvParser_getNumFields(curr_derivation->derives) - 1 ; j >=0 ; j--) { //push from the last to first item in array of terminals
if(strcmp(derivation_array[j],"''")==0) //if it derives empty break this loop
break;
else{ //push derivations
push(derivation_array[j]);
}
}
for (j = 0 ; j < CsvParser_getNumFields(curr_derivation->derives) ; j++) { //push from the first to last item in array of terminals for the parse tree representation
concat_data(derivation_array[j]);
if(strcmp(derivation_array[j],"''")==0) //if it derives empty break this loop
break;
}
found_derivation = true;
break;
}
//move to the next derivation
curr_derivation = curr_derivation->next;
if((!found_derivation) && (curr_derivation==NULL)){
//no found derivation
add_error(symboltbl_rowFields[2],symboltbl_rowFields[0]);
//destroy current token input
CsvParser_destroy_row(symboltbl_row);
//move to next token input
symboltbl_row = CsvParser_getRow(lexical_parser);
}
}while(curr_derivation != NULL);
break;
}
//move to the next symbol
curr_symbol = curr_symbol->next;
}while(curr_symbol != NULL);
}
else{ //top of stack is terminal
if (strcmp(get_top(), symboltbl_rowFields[1]) == 0){
char popped_symbol[50]; //pop the top of the stack
strcpy(popped_symbol, pop());
// curr_treenode = addNode
printf("\nPOPPED: %s", popped_symbol);
add_line(popped_symbol);
concat_data(symboltbl_rowFields[0]);
}else{
//ERROR
add_error(symboltbl_rowFields[2],symboltbl_rowFields[0]);
}
//destroy current token input
CsvParser_destroy_row(symboltbl_row);
//move to next token input
symboltbl_row = CsvParser_getRow(lexical_parser);
}
}else{
//symbol table's inputs are already used up
if((terminal_identifier(get_top())==1)){ //top of stack is symbol
//start scanning table from the head symbol
curr_symbol = head_symbol;
found_derivation = false;
while(curr_symbol != NULL){ //scan non-terminals or symbols
if(strcmp(get_top(),curr_symbol->symbol)==0){ //non-terminal found
curr_derivation = curr_symbol->start; //initialize derivation list with the current symbol's start derivation link
while(curr_derivation != NULL){ //scan derivations
if(strcmp(curr_derivation->input,"$")==0){ //derivation for this input found
char popped_symbol[50]; //pop the top of the stack
strcpy(popped_symbol, pop());
printf("\nPOPPED: %s", popped_symbol);
printf("\nTOP: %d", stack_top);
add_line(popped_symbol);
char **derivation_array = CsvParser_getFields(curr_derivation->derives);
for (int j = CsvParser_getNumFields(curr_derivation->derives) - 1 ; j >=0 ; j--) { //push from the last to first item in array of terminals
if(strcmp(derivation_array[j],"''")==0) //if it derives empty break this loop
break;
else{ //push derivations
push(derivation_array[j]);
}
}
for (int j = 0 ; j < CsvParser_getNumFields(curr_derivation->derives) ; j++) { //push from the first to last item in array of terminals for the parse tree
concat_data(derivation_array[j]);
if(strcmp(derivation_array[j],"''")==0) //if it derives empty break this loop
break;
}
found_derivation = true;
break;
}
//move to the next derivation
curr_derivation = curr_derivation->next;
}
//break "while(curr_symbol != NULL)" because symbol was already found
break;
}
//move to the next symbol
curr_symbol = curr_symbol->next;
}//while
}
else{ //top of stack is terminal
if (strcmp(get_top(), "$") == 0){
char popped_symbol[50]; //pop the top of the stack
strcpy(popped_symbol, pop());
printf("\nPOPPED: %s", popped_symbol);
printf("\nTOP: %d", stack_top);
add_line(popped_symbol);
concat_data("$");
}else{
//ERROR
found_derivation = false;
}
}
}
}
if(!found_derivation && EOF_reached)
break;
// getch();
}while(stack_top!=0);
if(stack_top == 0){
printf("\n\nParsing Complete!");
print_tree();
// construct_tree();
}else{
print_errors();
}
// getch();
//destroy symbol table analyzer
CsvParser_destroy(lexical_parser);
//destroy parse table
// unload_parse_table();
getch();
return 0;
}