-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser-common.c
560 lines (401 loc) · 8.29 KB
/
parser-common.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
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
#include"parser-common.h"
/* Reads contents of file into character buffer */
int fileread(char **ptrDest,const char *fileName,int *bytesRead)
{
int nbytes; /* Total number of bytes read from file */
int nread; /* Bytes read by one call to fread() */
int count; /* Number of times memory was allocated */
int offset; /* Offset from start of buffer */
const int BUF_STEP_SZ = 256;
char *oldbuffer;
char *buffer = NULL;
FILE *fp;
assert(buffer == NULL);
fp = fopen(fileName,"rb"); /* rb mode for compatibility with non POSIX systems */
if(!fp)
{
perror(fileName);
return 1;
}
count = 0;
nbytes = 0;
for(; ; count++)
{
if(feof(fp))
{
fclose(fp);
break;
}
oldbuffer = buffer; /* store pointer to memory allocated so far */
buffer = (char*)realloc(buffer,(count+1)*BUF_STEP_SZ); /* Increase size of buffer */
if(!buffer)
{
printf("\nError: Unable to allocate memory.");
free(oldbuffer);
fclose(fp);
return 1;
}
offset = count*BUF_STEP_SZ;
nread = fread(buffer+offset,sizeof(char),BUF_STEP_SZ,fp);
if(nread == 0)
{
if(ferror(fp))
{
perror(fileName);
fclose(fp);
free(buffer);
return 1;
}
}
nbytes += nread;
}
*bytesRead = nbytes;
*ptrDest = buffer;
return 0;
}
/* function for printing a byte array */
/* Array = a;
* Number of bytes to be printed = nbytes
*/
void printRaw(char *a,int nbytes)
{
int i ;
assert(a != NULL);
for(i=0;i<nbytes;i++)
{
printf("%c",*a);
a++;
}
}
/* Adds a new row to a 2d array of characters
* Arg1 = A pointer to a two dimensional array.
* Arg2 = New row number
*/
char **addRow(char **arrayPtr,int rowNum)
{
arrayPtr = (char**)realloc(arrayPtr,rowNum*sizeof(char*));
return arrayPtr;
}
/* Add a new string to an array of characters
* Arg1 = array;
* Arg2 = String to append;
*/
char *addSymbolToRow(char *array,const char *str)
{
int newlen;
if(array == NULL)
{
newlen = strlen(str) + 2;
array = (char*)realloc(array,newlen);
strcpy(array,str);
array = strcat(array," ");
}
else
{
newlen = (strlen(array) + strlen(str) +2);
array = (char*)realloc(array,newlen);
if(array == NULL)
{
printf("\n%s:%d Error during memory allocation",__FILE__,__LINE__);
return array;
}
//array = strcat(array,str);
array = strcat(array," ");
array = strcat(array,str);
}
return array;
}
/* Increment number of rows in an array of type struct prodList
* Arg1 = array
* Arg2 = new number of rows;
*/
struct prodList **incrementRows(struct prodList **a,int updated_row_count)
{
a = (struct prodList **)realloc(a , sizeof(struct prodList*) * updated_row_count);
return a;
}
/* Eat newlines and tabs.
* Put a trailing NULL character
*/
char *preProcess(char **a,int sz)
{
char *tokens;
/* Make the array NULL terminated */
int i;
for(i=sz-1; i>=1 ;i--)
{ /* Skip trailing white spaces */
if((*a)[i] == ';') break;
}
(*a)[++i] = '\0'; /* Overwrite first trailing white space with a NULL */
for(i=0;i<strlen(*a);i++)
{
if((*a)[i] == '\t' || (*a)[i] == '\n')
{
(*a)[i] = ' ';
}
}
tokens = strsep(a,"%");
return tokens;
}
/*
* Tests if a string is nullable
* Arg1 = A production body.
* Arg2 = Grammar table.
* Arg3 = Number of variables in the grammar table
* Arg4 = Array of all tokens.
*/
_Bool isNullable(char *body,struct prodList **table,int no_of_variables,char *tokens)
{
int i;
int lastindex = 0;
_Bool retval = true;
int j;
char *next;
if( strncmp(body,"epsilon",7) == 0) /* If body contains nothing but epsilon */
{
return true;
}
for(i=0; ;i++)
{
next = getNextSymbol(body,&lastindex);
if( next == NULL) break;
if(isToken(next,tokens)) /* If symbol is a token, body can not be nullable */
{
retval = false;
break;
}
else /* If symbol is a variable */
{
j = indexOf(next,table,no_of_variables); /* Index of this variable in the grammar table */
assert(j>=0);
if(table[j]->nullable == false)
{
retval = false;
break;
}
}
}
return retval;
}
/*
* Returns next symbol from an array holding a production body.
* A
*/
char *getNextSymbol(char *body,int *lastindex)
{
int i,j;
char *retval = NULL;
int len;
if(body[*lastindex] == '\0') /* No more symbols to extract ? Return NULL */
{
return retval;
}
for (i = *lastindex ; i<strlen(body) ; i++)
{
if(body[i] != ' ') break;
}
for(j=i+1; j<strlen(body) ;j++)
{
if(body[j] == ' ') break;
}
len = j-i+1;
retval = (char*)malloc(sizeof(char) * (len + 1));
strncpy(retval,body+i,len);
retval[len] = '\0';
(*lastindex) = j;
if(strcmp (retval,"") == 0) return NULL; /* Never return an empty string */
return retval;
}
_Bool isToken(char *sym,char *toks)
{
if( strstr(toks,sym) == NULL )
{
return false;
}
return true;
}
int indexOf(char *variable,struct prodList **table,int tablelen)
{
int i;
for(i=0;i<tablelen;i++)
{
if( strstr(table[i]->head,variable) != NULL)
{
#ifdef DEBUG
printf("\nindexOf(%s) returned %d",variable,i);
#endif
return i;
}
}
return -1;
}
/*
* Add a new element (string) to a space separated set of strings if it not already in the set.
* Arg1 = set
* Arg2 = element to add
* Arg3 = Used to indicate if the set changed as a result of this call
*/
char *addToSet(char *set, char * element, int *changed)
{
char *retval = set;
#ifdef DEBUG
printf("\nset = %s ; element = %s ; ",set,element);
#endif
if(set == NULL) /* strstr() can not handle NULL as first argument.*/
{
if(element == NULL)
{
*changed = 0;
}
else
{
retval = addSymbolToRow(set,element);
*changed = 1;
}
}
else
{
if(element == NULL)
{
*changed = 0;
}
else
{
if(strstr(set,element) == NULL)
{
retval = addSymbolToRow(set,element);
*changed = 1;
}
else
{
*changed = 0;
}
}
}
#ifdef DEBUG
printf("returning %s",retval);
#endif
return retval;
}
/* Union of two sets(strings).
* Each set has members in the form of substrings separated by white spaces.
*
* Arg1 = String1
* Arg2 = String2
*/
char *doUnion(char *set1, char *set2,int *changed)
{
char *more;
char *ret = set1;
int marker = 0;
int tmp;
if(set2 == NULL)
{
*changed = 0;
return set1;
}
for( ; ;)
{
more = getNextSymbol(set2,&marker);
if(more == NULL) break;
ret = addToSet(ret,more,&tmp);
*changed = tmp;
}
return ret;
}
char *removeFromSet(char* set,char *element,int *setchanged)
{
char *ret = set;
int changes;
int index = -1;
char *before = NULL ,*after = NULL;
int i;
//printf("\n[ Set = %s ] [element = %s ]",set,element);
/* If set is no removal is required */
if(set == NULL)
{
changes = 0;
}
else
{
/* If element to be removed is empty, do nothing */
if(element == NULL)
{
changes = 0;
}
else
{
/* Find the offset of element in set */
int j = strlen(set)-strlen(element)+1;
for(i=0 ; i < j ; i++)
{
//
if(strncmp(set+i,element,strlen(element)) == 0)
{
index = i;
break;
}
}
/* If element not found in set */
if(index == -1) changes = 0;
/* If element found in set, extract subsets before and after of set and merge them*/
/* Index is the offset at which element was found */
else
{
before = strndup(set,index);
after = strdup(set+index+strlen(element));
ret = before;
ret = strcat(ret,after);
changes = 1;
}
}
}
if(setchanged) *setchanged = changes;
//printf("Returning %-s",ret);
return ret;
}
/*
* Writes Parse Table to a file
* @PARAM: Parse Table Structure.
* @TODO: Use XML instead of proprietary format when saving to file.
*/
int save(struct parseinfo parseTable)
{
int i;
int j;
FILE *fp;
fp = fopen("parsetable.txt","w+");
if(!fp)
{
perror("parsetable.txt");
return 1;
}
fprintf(fp,"%d\n",parseTable.varcount);
for(i=0 ; i<parseTable.varcount ; i++)
{
fprintf(fp,"%s\n",parseTable.variables[i]);
}
fprintf(fp,"%d\n",parseTable.termcount);
for(i=0 ; i<parseTable.termcount ; i++)
{
fprintf(fp,"%s\n",parseTable.terminals[i]);
}
for(i=0 ; i<parseTable.varcount ; i++)
{
for(j=0 ; j<parseTable.termcount ; j++)
{
if(parseTable.actions[i][j] != NULL)
{
fprintf(fp,"%s\n",parseTable.actions[i][j]);
}
else fprintf(fp,"%s\n","(null)");
}
}
fclose(fp);
return 0;
}
char *clean(char *dirty)
{
int tmp = 0;
return(getNextSymbol(dirty,&tmp));
}