-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLineAnalysis.c
More file actions
459 lines (389 loc) · 12.2 KB
/
LineAnalysis.c
File metadata and controls
459 lines (389 loc) · 12.2 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
#include "declarationsHeader.h"
extern symPtr symTableHead;
extern struct keyWords keys[];
/*
* Testing the line for any kind of errors and adding symbols to the symbol table.
*/
void analyzeLine(int *DC) {
findSyntaxErrors();
findSymbols(DC);
findWrongMethodUse();
findRedundantInfo();
}
/*
* Finds syntax errors and reports to the user if it finds any.
*/
void findSyntaxErrors() {
bracketsAndStrError(O_BRACKETS);
bracketsAndStrError(STRING);
commaError();
searchUndefChars();
labelError();
paramsError();
definerError();
extOrEntError();
stopRtsError();
}
/*
* Generic function for testing miss match between opening and closing chars, like () and ""
*/
void bracketsAndStrError(int type) {
int j = 0, len, cnt = 0, closer;
char lineCpy[LINE_SIZE], cmd[CMD_LENGTH];
getCommand(cmd);
len = strlen(line);
strcpy(lineCpy, line);
/*No command and its a .string line.*/
if (cmd[0] == STR_END && strstr(lineCpy, GUIDING_SENTENCE_STRING) == NULL)
return;
switch (type) {
case O_BRACKETS :
closer = C_BRACKETS;
break;
case STRING :
closer = STRING;
break;
}
/*Testing for a miss match between brackets or \" char, depends on the parameter 'type'.*/
for (j = 0; j < len; j++) {
if (line[j] == type)
++cnt;
else {
if (line[j] == closer) {
if (cnt > 0) {
--cnt;
} else {
if (type == STRING)
printError(string_err);
else
printError(brack_err);
return;
}
}
}
}
if (closer == STRING && (cnt % 2) == 0)
return;
if (cnt != 0)
type == STRING ? printError(string_err) : printError(brack_err);
}
/*
* Finding a symbol label withing the line, inserting it into the symbols table
* and checking if it is already has been defined.
*/
void findSymbols(int *DC) {
int i = 0;
linePtr ptr;
char *temp, *temp2;
for (ptr = lineListHead; ptr; ptr = ptr->next, i++) {
/*Looking for the ':' char within the string.*/
if ((strchr(ptr->str, ISLABEL)) != NULL) {
temp = ptr->str;
temp2 = (strchr(temp, ISLABEL) + 1);
if (strlen(temp2) != 0) {
printErrorWithComment(illegal_sym, temp);
return;
}
*(strchr(temp, ISLABEL)) = STR_END;
if (symIsDefined(temp)) {
return;
} else if (symIsIllegal(temp)) {
return;
} else if (symIsAKey(temp)) {
return;
} else {
addToSymTable(temp, DC, TRUE);
}
}
}
}
/*
* Testing if the Symbol that was passed as argument is already defined in the symbol Table
*/
boolean symIsDefined(char *sym) {
symPtr ptr;
ptr = symTableHead;
for (; ptr != NULL; ptr = ptr->next) {
if (strcmp(ptr->symName, sym) == EQUAL && ptr->definedInFile ) {
printErrorWithComment(sym_defined_already, sym);
}
if (strcmp((ptr->symName), sym) == EQUAL)
return TRUE;
}
return FALSE;
}
/*
* Testing if the Symbol that was passed as argument is defined as a saved keyword.
*/
boolean symIsAKey(char *sym) {
int i = 0;
for (; i < NUM_OF_KEYS; ++i) {
if (strcmp(sym, (keys[i].symbol)) == 0) {
printErrorWithComment(sym_is_key, sym);
return TRUE;
}
}
return FALSE;
}
/*
* Tests if a symbol is not defined according to the syntax rules.
*/
boolean symIsIllegal(char *sym) {
char p[STRING_SIZE];
int i = 1;
const size_t symMaxLen = 30;
strcpy(p, sym);
if ((strlen(sym) > symMaxLen) || (!(isalpha(p[0])))) {
printErrorWithComment(illegal_sym, sym);
return TRUE;
}
while (p[i]) {
if ((!isalnum(p[i]))) {
printErrorWithComment(illegal_sym, sym);
return TRUE;
}
++i;
}
return FALSE;
}
/*
* Tests for wrong uses of the 4 methods defined in the instructions.
*/
void findWrongMethodUse() {
char lineCpy[LINE_SIZE], *temp, cmd[CMD_LENGTH];
strcpy(lineCpy, line);
getCommand(cmd);
/*No command in the line.*/
if (cmd[0] == STR_END)
return;
/*Checking first parameter*/
if ((temp = strstr(lineCpy, "#")) != NULL) {
if (!isdigit(temp[1]) && temp[1] != '-') {
if (strchr(temp, COMMA))
*strchr(temp, COMMA) = STR_END;
printErrorWithComment(immediate_wrong_use, temp);
}
/*Checking second parameter (if exists)*/
if ((temp = strstr(++temp, "#")) != NULL) {
if (!isdigit(temp[1]) && temp[1] != '-' && temp[1] != '+') {
if (strchr(temp, C_BRACKETS))
*strchr(temp, C_BRACKETS) = STR_END;
printErrorWithComment(immediate_wrong_use, temp);
}
}
}
}
/*
* Tests for any comma error within the file.
*/
void commaError() {
int i = 0, cntCommas = 0, cntDigist = 0;
char lineCpy[LINE_SIZE], cmd[CMD_LENGTH];
strcpy(lineCpy, line);
getCommand(cmd);
/*if the first char in cmd is null, its means we have no command in the line.*/
/*Testing for a missing comma between two parameteres.*/
if (cmd[0] != STR_END && isBinaryAction(cmd) && strchr(lineCpy, COMMA) == NULL) {
printError(missing_comma);
return;
}
/*Its a cmd but its not binary action and we have a comma without ()*/
if (cmd[0] != STR_END && !isBinaryAction(cmd) && strchr(lineCpy, COMMA) != NULL) {
if (strchr(lineCpy, O_BRACKETS) == NULL) {
printError(extra_commas);
return;
}
}
/*Counting commas for later use.*/
while (lineCpy[i++]) {
if (lineCpy[i] == COMMA)
++cntCommas;
}
/*If its not a .data sentence, there's no shouldn't be more than 1 comma*/
if (cntCommas > 1 && strstr(lineCpy, GUIDING_SENTENCE_INT) == NULL) {
printError(extra_commas);
return;
}
/*One comma with no brackets is ok.*/
if (strchr(lineCpy, O_BRACKETS) != NULL && cntCommas == 1)
return;;
/*Counting numbers for later use.*/
i = 0;
while (lineCpy[i++]) {
if (isdigit(lineCpy[i])) {
cntDigist++;
if (i > 0 &&
(isdigit(lineCpy[i - 1]) || lineCpy[i - 1] == DOT))/*Number counted twice..*/
cntDigist--;
}
}
if (strstr(lineCpy, GUIDING_SENTENCE_INT) != NULL && cntCommas > 0 &&
cntCommas + 1 < cntDigist) {
printError(missing_comma);
return;;
}
/*The right relation between commas and ints is: digit num = commas+1*/
if (strstr(lineCpy, GUIDING_SENTENCE_INT) != NULL && cntCommas > 0 &&
cntCommas + 1 != cntDigist)
printError(extra_commas);
}
/*
* Searching for 'wondering' chars within the code file.
*/
void searchUndefChars() {
char lineCpy[LINE_SIZE], temp[2];
size_t i = 0;
boolean IN_STRING = FALSE;
strcpy(lineCpy, line);
/*Searching for illegal char wondering in the code.*/
for (i = 0; lineCpy[i]; i++) {
if (isspace(lineCpy[i]))
continue;
/*Skipping chars within the string.*/
if (lineCpy[i] == '\"' && !IN_STRING ) {
IN_STRING = TRUE;
} else if (lineCpy[i] == '\"' && IN_STRING) {
IN_STRING = FALSE;
}
if (IN_STRING)
continue;
/*Searching for chars not matching to those below.*/
if ((!isalnum(lineCpy[i])) && lineCpy[i] != COMMA && lineCpy[i] != O_BRACKETS
&& lineCpy[i] != C_BRACKETS && lineCpy[i] != '.'
&& lineCpy[i] != '\"' && lineCpy[i] != '#'
&& lineCpy[i] != ISLABEL && lineCpy[i] != '-'
&& lineCpy[i] != '+' && lineCpy[i] != ISCOMMENT) {
strcpy(temp, (lineCpy + i));
removeSpaces(temp);
printErrorWithComment(undef_char, temp);
}
}
}
/*
* Tests for any unnecessary information within the line.
*/
void findRedundantInfo() {
char lineCpy[LINE_SIZE], cmd[CMD_LENGTH];
int i;
linePtr ptr, prev;
strcpy(lineCpy, line);
getCommand(cmd);
if (strstr(lineCpy, GUIDING_SENTENCE_ENT) || strstr(lineCpy, GUIDING_SENTENCE_EXTERN)) {
if (lineListLength > 2) {
for (ptr = lineListHead, i = 0; ptr; ptr = ptr->next, i++) {
if (strstr(ptr->str, GUIDING_SENTENCE_EXTERN) ||
strstr(ptr->str, GUIDING_SENTENCE_ENT)) {
if (i == 0) /*After*/
printErrorWithComment(redu_info, ((ptr->next)->next)->str);
else /*Before*/
printErrorWithComment(redu_info, prev->str);
}
prev = ptr;
}
}
}
for (ptr = lineListHead, i = 0; cmd[0] != STR_END && ptr; ptr = ptr->next, i++) {
if (strchr(ptr->str, C_BRACKETS) && i + 1 != lineListLength)
printErrorWithComment(redu_info, (ptr->next)->str);
}
}
/*
* Tests for label error.
*/
void labelError() {
char lineCpy[LINE_SIZE], cmd[CMD_LENGTH];
linePtr ptr;
size_t i = 0, j = 0;
strcpy(lineCpy, line);
getCommand(cmd);
if (lineListHead == NULL)
return;
if (strchr(lineCpy, ISLABEL)) {
strcpy(lineCpy, lineListHead->str);
/*Testing for strings before the label definition.*/
if (strchr(lineCpy, ISLABEL) == NULL) {
printError(sym_miss_place);
}
if (cmd[0] == STR_END)
return;
/*Testing for incorrect order: label->cmd->operands.*/
for (i = 0, ptr = lineListHead; i != 1 && ptr; ptr = ptr->next, i++);
for (j = 0; ptr && j < LINE_DEFINERS_NUM; j++) {
if (strstr(ptr->str, keys[j].symbol) != NULL)
return;
}
printError(order_error);
}
}
/*
* Tests for parameters error.
*/
void paramsError() {
char lineCpy[LINE_SIZE], cmd[CMD_LENGTH], *firstParam;
size_t i, lastChar, offset = 2;
linePtr ptr;
strcpy(lineCpy, line);
getCommand(cmd);
if (cmd[0] == STR_END) /*No params*/
return;
if (strchr(lineCpy, COMMA)) {
/*Missing both params.*/
for (ptr = lineListHead; ptr->next; ptr = ptr->next);
for (i = 0; i < ACTIONS_NUM - 2; i++) {
if (strstr(ptr->str, keys[i].symbol))
printError(missing_both_params);
}
/*Missing second param.*/
removeSpaces(lineCpy);
lastChar = strlen(lineCpy) - 1;
if (lineCpy[lastChar] == COMMA)
printError(missing_second_param);
/*Missing first param.*/
firstParam = strtok(lineCpy, COMMA_STR);
removeSpaces(firstParam);
if (strchr(firstParam, ISLABEL))/*Getting rid of the symbol.*/
firstParam = strchr(firstParam, ISLABEL) + 1;
for (i = 0; i < ACTIONS_NUM - offset; i++) {
if (strcmp(firstParam, keys[i].symbol) == EQUAL)
printError(missing_first_param);
}
}
}
/*
* Tests for missing difiner - .data/.string/command within the line.
*/
void definerError() {
char lineCpy[LINE_SIZE];
size_t i;
strcpy(lineCpy, line);
for (i = 0; i < LINE_DEFINERS_NUM; i++) {
if (strstr(lineCpy, keys[i].symbol))
return;
}
printError(no_definer_in_line);
}
void extOrEntError() {
char lineCpy[LINE_SIZE];
strcpy(lineCpy, line);
const int MIN_LENGTH = 2;
if (strstr(lineCpy, GUIDING_SENTENCE_EXTERN) == NULL &&
strstr(lineCpy, GUIDING_SENTENCE_ENT) == NULL) {
return;
}
if (lineListLength < MIN_LENGTH)
printError(missing_sym);
}
void stopRtsError() {
char lineCpy[LINE_SIZE];
linePtr ptr;
strcpy(lineCpy, line);
if (strstr(lineCpy, "stop") == NULL && strstr(lineCpy, "rts") == NULL)
return;
for (ptr = lineListHead; ptr; ptr = ptr->next) {
if ((strcmp(ptr->str, "stop") == EQUAL) || (strcmp(ptr->str, "rts") == EQUAL)) {
if (ptr->next == NULL)
return;
printErrorWithComment(illegal_operand, ptr->str);
}
}
}