-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathial.c
More file actions
executable file
·477 lines (373 loc) · 13.6 KB
/
ial.c
File metadata and controls
executable file
·477 lines (373 loc) · 13.6 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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/**
* Implementace interpretu imperativního jazyka IFJ16.
*
* Jakub Fajkus
* Richard Bureš
* Andrej Hučko
* David Czernin
*/
#include <stdint.h>
#include <stdlib.h>
#include "ifj16.h"
#include "symboltable.h"
#include "debug.h"
//************** BOYER-MOORE **************
#define ALPHABET_LEN 256
void make_delta(int *delta, unsigned char *pat, int patlen) {
int i;
for (i=0; i < ALPHABET_LEN; i++) {
delta[i] = patlen; //not found
}
for (i=0; i < patlen-1; i++) {
delta[pat[i]] = patlen-1 - i;
}
}
int boyer_moore (unsigned char *string, unsigned int stringlen,unsigned char *pat, unsigned int patlen) {
int i;
int delta[ALPHABET_LEN];
make_delta(delta, pat, patlen);
// The empty pattern must be considered specially
if (patlen == 0) {
return 0;
}
i = patlen-1;
while (i < stringlen) {
int j = patlen-1;
while (j >= 0 && (string[i] == pat[j])) {
--i;
--j;
}
if (j < 0) {
return (string + i+1) - string;
}
i += delta[string[i]];
}
return -1;
}
//************** QUICK SORT **************
char* quickSortWrapper(char *s) {
int right = (int)strlen(s);
char *arr; /* our array for return as sorted string */
arr = (char*) malloc(sizeof(char)*right);
int k = 0; /* copy elements into that */
while(k <= right-1){
arr[k] = s[k];
k++;
}
quickSort(arr,0,right); /* call our quick sort */
return arr;
}
void quickSort(char *arr, int left, int right){
if(left < right){
int border = left;
for(int i = left+1; i < right; i++){
if(arr[i] < arr[left]){
swap(arr, i, ++border);
}
}
swap(arr, left, border);
quickSort(arr, left, border);
quickSort(arr, border + 1, right);
}
}
void swap(char *arr, int left, int right){
char tmp = arr[right];
arr[right] = arr[left];
arr[left] = tmp;
}
//************** SYMBOL TABLE TREE FUNCTIONS **************
extern char* actualClass;
void BSTInit (SYMBOL_TABLE_NODEPtr *RootPtr) {
*RootPtr = NULL;
}
bool BSTSearch (SYMBOL_TABLE_NODEPtr RootPtr, char* K, TREE_NODE_DATA *Content) {
if (RootPtr == NULL) {
return false;
}
//the key is the root
if (strcmp(K, RootPtr->key) == 0) {
*Content = *RootPtr->data;
return true;
} else if (strcmp(K, RootPtr->key) == -1 && RootPtr->lPtr != NULL) {
//the has lower value than the root
return BSTSearch(RootPtr->lPtr, K, Content);
} else if (RootPtr->rPtr != NULL){
//the has higher value than the root
return BSTSearch(RootPtr->rPtr, K, Content);
} else {
return false;
}
}
void BSTInsert (SYMBOL_TABLE_NODEPtr* RootPtr, char* K, TREE_NODE_DATA Content) {
if (NULL == *RootPtr) {
SYMBOL_TABLE_NODEPtr newItem = malloc(sizeof(struct SYMBOL_TABLE_NODE));
newItem->key = K;
newItem->data = (TREE_NODE_DATA*)malloc(sizeof(TREE_NODE_DATA));
newItem->data->item = Content.item;
newItem->data->type = Content.type;
newItem->lPtr = NULL;
newItem->rPtr = NULL;
(*RootPtr) = newItem;
return;
}
//the key is the root
if (K == (*RootPtr)->key) {
//aktalizacni semantika...
fprintf(stderr,"redeclaration of %s", K);
exit(3);
} else if (strcmp(K, (*RootPtr)->key) == -1) {
//the has lower value than the root
BSTInsert(&(*RootPtr)->lPtr, K, Content);
} else {
//the has higher value than the root
BSTInsert(&(*RootPtr)->rPtr, K, Content);
}
}
void ReplaceByRightmost (SYMBOL_TABLE_NODEPtr PtrReplaced, SYMBOL_TABLE_NODEPtr *RootPtr) {
if (NULL == RootPtr) {
return;
}
//if the current node has right subtree and the root node of the subtree has no right subtree - it is the rightmost
if (NULL != (*RootPtr)->rPtr && NULL == (*RootPtr)->rPtr->rPtr) {
PtrReplaced->data = (TREE_NODE_DATA*)malloc(sizeof(TREE_NODE_DATA));
PtrReplaced->data->item = (*RootPtr)->rPtr->data->item;
PtrReplaced->data->type = (*RootPtr)->rPtr->data->type;
PtrReplaced->key = (*RootPtr)->rPtr->key;
free((*RootPtr)->rPtr);
(*RootPtr)->rPtr = NULL;
//the current node has no right subtree - it it the rightmost
} else if(NULL == (*RootPtr)->rPtr) {
TREE_NODE_DATA *data = (TREE_NODE_DATA*)malloc(sizeof(TREE_NODE_DATA));
data->type = (*RootPtr)->data->type;
data->item = (*RootPtr)->data->item;
char* key = (*RootPtr)->key;
BSTDelete(&PtrReplaced, (*RootPtr)->key);
PtrReplaced->data = data;
PtrReplaced->key = key;
} else {
ReplaceByRightmost(PtrReplaced, &(*RootPtr)->rPtr);
}
}
void BSTDelete (SYMBOL_TABLE_NODEPtr *RootPtr, char* K) {
SYMBOL_TABLE_NODEPtr actualItem = *RootPtr;
SYMBOL_TABLE_NODEPtr temp;
//the key is the root
if (K == (*RootPtr)->key) {
//let the game begin
//if the item has only right subtree
if (NULL == (*RootPtr)->lPtr && NULL != (*RootPtr)->rPtr) {
temp = (*RootPtr)->rPtr;
free(*RootPtr);
*RootPtr = temp;
//if the item has only left subtree
} else if (NULL != (*RootPtr)->lPtr && NULL == (*RootPtr)->rPtr) {
temp = (*RootPtr)->lPtr;
free(*RootPtr);
*RootPtr = temp;
//the item has two subtrees
} else if(NULL != (*RootPtr)->lPtr && NULL != (*RootPtr)->rPtr){
ReplaceByRightmost((*RootPtr), &(*RootPtr)->lPtr);
//both subtrees are null
} else {
(*RootPtr) = NULL;
free(actualItem);
}
} else if (K < (*RootPtr)->key && (*RootPtr)->lPtr != NULL) {
//the has lower value than the root
BSTDelete(&(*RootPtr)->lPtr, K);
} else if ((*RootPtr)->rPtr != NULL){
//the has higher value than the root
BSTDelete(&(*RootPtr)->rPtr, K);
}
}
void BSTDispose (SYMBOL_TABLE_NODEPtr *RootPtr) {
if (NULL == *RootPtr) {
return;
}
if (NULL != (*RootPtr)->lPtr) {
BSTDispose(&(*RootPtr)->lPtr);
}
if (NULL != (*RootPtr)->rPtr) {
BSTDispose(&(*RootPtr)->rPtr);
}
if(NULL == (*RootPtr)->lPtr && NULL == (*RootPtr)->rPtr) {
free(*RootPtr);
*RootPtr = NULL;
}
}
void initializeSymbolTable(struct SYMBOL_TABLE_NODE *symbolTable) {
symbolTable = malloc(sizeof(struct SYMBOL_TABLE_NODE));
BSTInit(&symbolTable);
}
void Leftmost_Inorder(struct SYMBOL_TABLE_NODE *ptr, struct STACK_STR *Stack){
while ( ptr != NULL ) {
struct STACK_ELEMENT element;
element.type = STACK_ELEMENT_TYPE_SYMBOL_TABLE_PTR;
element.data.symbolTableNode = ptr;
stackPush(Stack, element);
ptr = ptr->lPtr;
}
}
tStack *BTInorder (struct SYMBOL_TABLE_NODE *RootPtr){
tStack *StackHelper = malloc(sizeof(tStack));
stackInit(StackHelper);
tStack *ReturnStack = malloc(sizeof(tStack));
stackInit(ReturnStack);
Leftmost_Inorder (RootPtr, StackHelper);
while ( !stackEmpty(StackHelper) ) {
//get top element
STACK_ELEMENT *elem = malloc(sizeof(STACK_ELEMENT));
stackTop(StackHelper, elem);
RootPtr = elem->data.symbolTableNode;
//pop top element
stackPop(StackHelper);
//push to return stack
stackPush(ReturnStack, *elem);
Leftmost_Inorder (RootPtr->rPtr, StackHelper);
}
return ReturnStack;
}
//************** SYMBOL TABLE INTERFACE **************
SYMBOL_TABLE_VARIABLE* getVariableFromTable(SYMBOL_TABLE_NODEPtr *symbolTable, char* name)
{
TREE_NODE_DATA *nodeData = getNodeDataFromTable(symbolTable, name);
if(nodeData == NULL) {
return NULL;
}
if (nodeData->type != TREE_NODE_VARIABLE) {
fprintf(stderr, "internal error, requested %s is not a variable", name);
exit(99);
}
return nodeData->item->variable;
}
SYMBOL_TABLE_FUNCTION* getFunctionFromTable(SYMBOL_TABLE_NODEPtr *symbolTable, char* name)
{
TREE_NODE_DATA *nodeData = getNodeDataFromTable(symbolTable, name);
if(nodeData == NULL) {
return NULL;
}
if (nodeData->type != TREE_NODE_FUNCTION) {
fprintf(stderr, "internal error, requested %s is not a function", name);
exit(99);
}
return nodeData->item->function;
}
TREE_NODE_DATA *getNodeDataFromTable(SYMBOL_TABLE_NODEPtr *symbolTable, char *name) {
if(symbolTable == NULL || *symbolTable == NULL || (*symbolTable)->key == NULL) {
return NULL;
}
TREE_NODE_DATA *nodeData = malloc(sizeof(TREE_NODE_DATA));
if (true == BSTSearch(*symbolTable, name, nodeData)) {
//found
return nodeData;
} else {
// not found
return NULL;
}
return NULL;
}
SYMBOL_TABLE_VARIABLE* getVariable(SYMBOL_TABLE_NODEPtr *localSymbolTable, SYMBOL_TABLE_NODEPtr *globalSymbolTable, char *activeClass, char* name)
{
SYMBOL_TABLE_VARIABLE *variable = NULL;
if(localSymbolTable != NULL) {
variable = getVariableFromTable(localSymbolTable, name);
//the variable was found and is local
if(variable != NULL) {
return variable;
}
}
//when searching in the global table, there are 2 situations:
//1. the searching variable is ordinary identifier(e.g. property) - in this case we want to prefix the name with class name(the class that contains the function definition!)
//2. the searching variable is fully qualified name(e.g. class.property) - in this case we want to search for the name as it is
//if it does not contain a dot
if (-1 == ifj16_find(name, ".")) {
//add class prefix to it
char *nameWithDot = stringConcat(activeClass, ".");
char *fullyQualifiedName = stringConcat(nameWithDot, name);
free(nameWithDot);
variable = getVariableFromTable(globalSymbolTable, fullyQualifiedName);
} else {
//the name is already fully qualified
variable = getVariableFromTable(globalSymbolTable, name);
}
//the variable was either found(is global) or not found at all
return variable;
}
SYMBOL_TABLE_VARIABLE* createVariable(char *name, DATA_TYPE type, bool initialized) {
SYMBOL_TABLE_VARIABLE *variable = malloc(sizeof(SYMBOL_TABLE_VARIABLE));
variable->name = name;
variable->type = type;
variable->usages = 0;
variable->initialized = initialized;
return variable;
}
TREE_NODE_DATA* createVariableData(SYMBOL_TABLE_VARIABLE *variable) {
TREE_NODE_DATA *treeData = malloc(sizeof(TREE_NODE_DATA));
treeData->type = TREE_NODE_VARIABLE;
treeData->item = malloc(sizeof(SYMBOL_TABLE_ITEM));
treeData->item->variable = variable;
return treeData;
}
SYMBOL_TABLE_VARIABLE* createAndInsertVariable(SYMBOL_TABLE_NODEPtr *symbolTable, char *name, DATA_TYPE type, bool initialized) {
//if the variable already exists
if (NULL != getVariableFromTable(symbolTable, name)) {
fprintf(stderr,"redeclaration of variable %s\n", name);
exit(3);
}
SYMBOL_TABLE_VARIABLE *variable = createVariable(name, type, initialized);
TREE_NODE_DATA *treeData = createVariableData(variable);
debugPrintf("inserting variable %s\n", variable->name);
BSTInsert(symbolTable, variable->name, *treeData);
return variable;
}
SYMBOL_TABLE_VARIABLE* createAndInsertIntVariable(SYMBOL_TABLE_NODEPtr *symbolTable, char *name, bool initialized) {
return createAndInsertVariable(symbolTable, name, TYPE_INT, initialized);
}
SYMBOL_TABLE_FUNCTION* createFunction(char *name, DATA_TYPE type, unsigned int usages, tDLList *parameters, tDLList *instructions) {
SYMBOL_TABLE_FUNCTION *function = malloc(sizeof(SYMBOL_TABLE_FUNCTION));
//initialize symbol table
function->localSymbolTable = NULL;
function->type = type;
function->name = name;
function->usages = usages;
function->parameters = parameters;
function->instructions = instructions;
return function;
}
TREE_NODE_DATA* createFunctionData(SYMBOL_TABLE_FUNCTION *function) {
TREE_NODE_DATA *treeData = malloc(sizeof(TREE_NODE_DATA));
treeData->type = TREE_NODE_FUNCTION;
treeData->item = malloc(sizeof(SYMBOL_TABLE_ITEM));
treeData->item->function = function;
return treeData;
}
SYMBOL_TABLE_FUNCTION* createAndInsertFunction(SYMBOL_TABLE_NODEPtr *symbolTable, char *name, DATA_TYPE type, unsigned int usages, tDLList *parameters, tDLList *instructions, bool hasReturn) {
//if the function already exists
debugPrintf("inserting a function %s\n", name);
if (NULL != getNodeDataFromTable(symbolTable, name)) {
fprintf(stderr, "redeclaration of function or variable %s", name);
exit(3);
}
SYMBOL_TABLE_FUNCTION *function = createFunction(name, type, usages, parameters, instructions);
if (function->parameters == NULL) {
function->parameters = malloc(sizeof(tDLList));
ListInit(function->parameters);
}
if (function->instructions == NULL) {
function->instructions = malloc(sizeof(tDLList));
ListInit(function->instructions);
}
function->hasReturn = hasReturn;
TREE_NODE_DATA *treeData = createFunctionData(function);
BSTInsert(symbolTable, function->name, *treeData);
return function;
}
LIST_ELEMENT *createFunctionParamListElement(DATA_TYPE type, char* name) {
FUNCTION_PARAMETER *param = malloc(sizeof(FUNCTION_PARAMETER));
param->type = type;
param->name = name;
LIST_ELEMENT *listElement = malloc(sizeof(LIST_ELEMENT));
listElement->type = LIST_ELEMENT_TYPE_FUNCTION_PARAMETER;
listElement->data.parameter = param;
return listElement;
}