-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.c
More file actions
597 lines (518 loc) · 21.5 KB
/
parser.c
File metadata and controls
597 lines (518 loc) · 21.5 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
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "parser.h"
#include "errors_handler.h"
#include "globals.h"
int parse_directive_line(char *line, ParsedLine *out, char *file_name, int line_number) {
char *dynamic_operands_pointer;
/* Locate directive in the line and move pointer to its operands */
dynamic_operands_pointer = strstr(line, out->directive_name);
if (dynamic_operands_pointer == NULL) return 0;
dynamic_operands_pointer += strlen(out->directive_name) + 1; /* +1 for the '.' */
dynamic_operands_pointer = cut_spaces_before_and_after_string(dynamic_operands_pointer);
/* Call the specific parser based on directive type */
if (strcmp(out->directive_name, DIR_STRING) == 0) {
return parse_string_directive(dynamic_operands_pointer, out, file_name, line_number);
}
else if (strcmp(out->directive_name, DIR_DATA) == 0) {
return parse_data_directive(dynamic_operands_pointer, out, file_name, line_number);
}
else if (strcmp(out->directive_name, DIR_EXTERN) == 0) {
return parse_extern_directive(dynamic_operands_pointer, out, file_name, line_number);
}
else if (strcmp(out->directive_name, DIR_ENTRY) == 0) {
return parse_entry_directive(dynamic_operands_pointer, out, file_name, line_number);
}
else if (strcmp(out->directive_name, DIR_MAT) == 0) {
return parse_mat_directive(dynamic_operands_pointer, out, file_name, line_number);
}
return 0;
}
int parse_string_directive(char *operands, ParsedLine *out, char *file_name, int line_number) {
char *string_without_quotes;
/* Check if directive has no content */
if(operands[0]=='\n'||operands[0]=='\0'){
error_log(file_name, line_number, EMPTY_STRING_DIR);
return 0;
}
/* Ensure string starts and ends with quotes */
if (operands[0] != '"' || operands[strlen(operands) - 1] != '"') {
error_log(file_name, line_number, STRING_MISSING_QUOTES);
return 0;
}
/* Copy string and remove quotes */
strncpy(out->operands[0], operands, MAX_LINE_LENGTH - 1);
string_without_quotes = strip_quotes(out->operands[0]);
/* Validate characters inside the string */
if (verify_string_has_invalid_chars(string_without_quotes)) {
error_log(file_name, line_number, INVALID_STRING);
return 0;
}
/* Store the processed string in output */
strncpy(out->operands[0], string_without_quotes, MAX_LINE_LENGTH - 1);
out->operands[0][MAX_LINE_LENGTH - 1] = '\0';
out->operand_count = 1;
return 1;
}
int parse_data_directive(char *operands, ParsedLine *out, char *file_name, int line_number) {
char *token;
int i = 0;
/* Check for missing operands */
if (operands[0] == '\0') {
error_log(file_name, line_number, MISSING_OPERANDS_DATA);
return 0;
}
/* Split operands by commas */
token = strtok(operands, COMMA);
while (token != NULL) {
token = cut_spaces_before_and_after_string(token);
/* Multiple consecutive commas */
if (token[0] == '\0') {
error_log(file_name, line_number, MULTIPLE_COMMAS);
return 0;
}
/* Validate that token is a valid number */
if (is_digit_or_char(token, 0, file_name, line_number)) {
return 0;
}
/* Store validated operand */
strncpy(out->operands[i], token, MAX_LINE_LENGTH - 1);
out->operands[i][MAX_LINE_LENGTH - 1] = '\0';
i++;
token = strtok(NULL, COMMA);
}
/* Save operand count */
out->operand_count = i;
return 1;
}
int parse_extern_directive(char *operands, ParsedLine *out, char *file_name, int line_number) {
/* Check for missing operand */
if (operands[0] == '\0') {
error_log(file_name, line_number, MISSING_OPERAND_EXTERN);
return 0;
}
/* Trim spaces around operand */
operands = cut_spaces_before_and_after_string(operands);
/* Validate label format */
if (!is_valid_label(operands, file_name, line_number)) {
return 0;
}
/* Label cannot be an opcode, directive or register */
if (identify_opcode(operands) != OPCODE_INVALID || identify_directive(operands) != -1) {
error_log(file_name, line_number, SAVED_WORD_AS_LABEL);
return 0;
} else if (identify_register(operands) != -1) {
error_log(file_name, line_number, REGISTER_NAME_AS_LABEL);
return 0;
}
/* Store valid operand */
strncpy(out->operands[0], operands, MAX_LINE_LENGTH - 1);
out->operands[0][MAX_LINE_LENGTH - 1] = '\0';
out->operand_count = 1;
return 1;
}
int parse_entry_directive(char *operands, ParsedLine *out, char *file_name, int line_number) {
/* Check for missing operand */
if (operands[0] == '\0') {
error_log(file_name, line_number, MISSING_OPERANDS_DATA);
return 0;
}
/* Trim spaces around operand */
operands = cut_spaces_before_and_after_string(operands);
/* Validate label format */
if (!is_valid_label(operands, file_name, line_number)) {
return 0;
}
/* Label cannot be an opcode, directive or register */
if (identify_opcode(operands) != OPCODE_INVALID || identify_directive(operands) != -1) {
error_log(file_name, line_number, SAVED_WORD_AS_LABEL);
return 0;
} else if (identify_register(operands) != -1) {
error_log(file_name, line_number, REGISTER_NAME_AS_LABEL);
return 0;
}
/* Store valid operand */
strncpy(out->operands[0], operands, MAX_LINE_LENGTH - 1);
out->operands[0][MAX_LINE_LENGTH - 1] = '\0';
out->operand_count = 1;
return 1;
}
int parse_mat_directive(char *operands, ParsedLine *out, char *file_name, int line_number) {
int n, m, mat_size, i;
char *mat_values, *token, *number;
/* Validate matrix dimensions format */
if (!is_valid_matrix_dim(operands, file_name, line_number)) {
return 0;
}
/* Read matrix dimensions and store size of dimension string */
if (sscanf(operands, MAT_DIM_SPACE, &n, &m, &mat_size) != 2) {
error_log(file_name, line_number, MATRIX_DIMENSION_FORMAT);
return 0;
}
/* Save dimensions as first operand */
strncpy(out->operands[0], operands, mat_size);
out->operands[0][mat_size] = '\0';
out->operand_count = 1;
/* Get values part after the dimensions */
mat_values = operands + mat_size;
mat_values = cut_spaces_before_and_after_string(mat_values);
if (mat_values[0] != '\0') {
/* Parse comma-separated values */
token = strtok(mat_values, COMMA);
while (token != NULL) {
token = cut_spaces_before_and_after_string(token);
/* Multiple commas */
if (token[0] == '\0') {
error_log(file_name, line_number, MULTIPLE_COMMAS);
return 0;
}
/* Validate that token is numeric */
number = token;
if (is_digit_or_char(number, 0, file_name, line_number)) {
return 0;
}
/* Store value */
strncpy(out->operands[out->operand_count], token, MAX_LINE_LENGTH - 1);
out->operands[out->operand_count][MAX_LINE_LENGTH - 1] = '\0';
out->operand_count++;
token = strtok(NULL, COMMA);
}
} else {
/* No values provided: fill with zeros */
for (i = 0; i < mat_size; i++) {
strncpy(out->operands[out->operand_count], "0", MAX_LINE_LENGTH - 1);
out->operands[out->operand_count][MAX_LINE_LENGTH - 1] = '\0';
out->operand_count++;
}
}
return 1;
}
int parse_instruction_operands(char *operands, ParsedLine *out, char *file_name, int line_number) {
char *token;
int i = 0;
int max_operands = expected_operands_for_each_opcode[out->opcode];
while (i < max_operands && (token = strtok((i == 0) ? operands : NULL, COMMA)) != NULL) {
token = cut_spaces_before_and_after_string(token);
if (token[0] == '\0') {
error_log(file_name, line_number, MULTIPLE_COMMAS);
return 0;
}
/* Validate operand based on instruction and position */
if (!validate_operand_for_instruction(token, out->opcode, i, file_name, line_number)) {
return 0;
}
strncpy(out->operands[i], token, MAX_LINE_LENGTH - 1);
out->operands[i][MAX_LINE_LENGTH - 1] = '\0';
i++;
}
/* Check for extra operands */
if (i == max_operands) {
token = strtok(NULL, "");
if (token != NULL) {
token = cut_spaces_before_and_after_string(token);
if (token[0] != '\0') {
error_log(file_name, line_number, MANY_OP);
return 0;
}
}
}
out->operand_count = i;
return 1;
}
int validate_operand_for_instruction(char *operand, Opcode opcode, int position, char *file_name, int line_number) {
switch (opcode) {
case OPCODE_MOV:
/* Validate MOV */
if (position == 0) { /* Source operand */
if (strchr(operand, LADDER)) {
if (is_valid_immediate(operand, file_name, line_number) == 1) {
return 0;
}
} else if (strchr(operand, OPENING_BRACKET)) {
if (verify_matrix_registers_are_valid(operand, file_name, line_number)) {
return 0;
}
} else if (strchr(operand, CLOSED_BRACKET)) {
if (verify_matrix_registers_are_valid(operand, file_name, line_number)) {
return 0;
}
} else if (identify_register(operand) == -1) {
if (!is_valid_label(operand, file_name, line_number)) {
return 0;
}
}
} else { /* Destination operand */
if (strchr(operand, LADDER)){
error_log(file_name,line_number,IMM_NOT_VALID_ARG_DEST_MOV);
return 0;
}
else if (strchr(operand, OPENING_BRACKET)) {
if (verify_matrix_registers_are_valid(operand, file_name, line_number)) {
return 0;
}
} else if (strchr(operand, CLOSED_BRACKET)) {
if (verify_matrix_registers_are_valid(operand, file_name, line_number)) {
return 0;
}
} else if (identify_register(operand) == -1) {
if (!is_valid_label(operand, file_name, line_number)) {
return 0;
}
}
}
break;
case OPCODE_CMP:
/* Validate CMP */
if (strchr(operand, LADDER)) {
if (is_valid_immediate(operand, file_name, line_number) == 1) {
return 0;
}
} else if (strchr(operand, OPENING_BRACKET)) {
if (verify_matrix_registers_are_valid(operand, file_name, line_number)) {
return 0;
}
} else if (strchr(operand, CLOSED_BRACKET)) {
if (verify_matrix_registers_are_valid(operand, file_name, line_number)) {
return 0;
}
} else if (identify_register(operand) == -1) {
if (!is_valid_label(operand, file_name, line_number)) {
return 0;
}
}
break;
case OPCODE_ADD:
case OPCODE_SUB:
/* Validate ADD/SUB */
if (position == 0) {/* Source operand */
if (strchr(operand, LADDER)) {
if (is_valid_immediate(operand, file_name, line_number) == 1) {
return 0;
}
} else if (strchr(operand, OPENING_BRACKET)) {
if (verify_matrix_registers_are_valid(operand, file_name, line_number)) {
return 0;
}
} else if (strchr(operand, CLOSED_BRACKET)) {
if (verify_matrix_registers_are_valid(operand, file_name, line_number)) {
return 0;
}
} else if (identify_register(operand) == -1) {
if (!is_valid_label(operand, file_name, line_number)) {
return 0;
}
}
} else {/* Destination operand */
if (strchr(operand, LADDER)){
error_log(file_name,line_number,IMM_NOT_VALID_ARG_DEST_ADD_SUB);
return 0;
}
else if (strchr(operand, OPENING_BRACKET)) {
if (verify_matrix_registers_are_valid(operand, file_name, line_number)) {
return 0;
}
} else if (strchr(operand, CLOSED_BRACKET)) {
if (verify_matrix_registers_are_valid(operand, file_name, line_number)) {
return 0;
}
} else if (identify_register(operand) == -1) {
if (!is_valid_label(operand, file_name, line_number)) {
return 0;
}
}
}
break;
case OPCODE_LEA:
/* Validate LEA */
if (position == 0) {/* Source operand */
if (strchr(operand, LADDER)){
error_log(file_name,line_number,IMM_NOT_VALID_ARG_SRC_LEA);
return 0;
}
else if (strchr(operand, OPENING_BRACKET)) {
if (verify_matrix_registers_are_valid(operand, file_name, line_number)) {
return 0;
}
} else if (strchr(operand, CLOSED_BRACKET)) {
if (verify_matrix_registers_are_valid(operand, file_name, line_number)) {
return 0;
}
} else if (!is_valid_label(operand, file_name, line_number)) {
return 0;
}
} else {/* Destination operand */
if (strchr(operand, LADDER)){
error_log(file_name,line_number,IMM_NOT_VALID_ARG_DEST_LEA);
return 0;
}
else if (strchr(operand, OPENING_BRACKET)) {
if (verify_matrix_registers_are_valid(operand, file_name, line_number)) {
return 0;
}
} else if (strchr(operand, CLOSED_BRACKET)) {
if (verify_matrix_registers_are_valid(operand, file_name, line_number)) {
return 0;
}
} else if (identify_register(operand) == -1) {
if (!is_valid_label(operand, file_name, line_number)) {
return 0;
}
}
}
break;
case OPCODE_CLR:
case OPCODE_NOT:
case OPCODE_INC:
case OPCODE_DEC:
case OPCODE_JMP:
case OPCODE_BNE:
case OPCODE_JSR:
case OPCODE_RED:
/* Single operand */
if (strchr(operand, LADDER)){
error_log(file_name,line_number,IMM_NOT_VALID_ARG_DEST_REST_OP);
return 0;
}
else if (strchr(operand, OPENING_BRACKET)) {
if (verify_matrix_registers_are_valid(operand, file_name, line_number)) {
return 0;
}
} else if (strchr(operand, CLOSED_BRACKET)) {
if (verify_matrix_registers_are_valid(operand, file_name, line_number)) {
return 0;
}
} else if (identify_register(operand) == -1) {
if (!is_valid_label(operand, file_name, line_number)) {
return 0;
}
}
break;
case OPCODE_PRN:
/* Validate PRN */
if (strchr(operand, LADDER)) {
if (is_valid_immediate(operand, file_name, line_number) == 1) {
return 0;
}
} else if (strchr(operand, OPENING_BRACKET)) {
if (verify_matrix_registers_are_valid(operand, file_name, line_number)) {
return 0;
}
} else if (strchr(operand, CLOSED_BRACKET)) {
if (verify_matrix_registers_are_valid(operand, file_name, line_number)) {
return 0;
}
} else if (identify_register(operand) == -1) {
if (!is_valid_label(operand, file_name, line_number)) {
return 0;
}
}
break;
default:
return 0; /* Invalid opcode */
}
return 1; /* Operand is valid */
}
int parse_line(char *line, ParsedLine *out, char *file_name, int line_number) {
char *buffer,*cleaned_buffer;
char *token, *dynamic_operands_pointer;
int expected;
/* initial output ParsedLine */
out->label[0] = '\0';
out->directive_name[0] = '\0';
out->operand_count = 0;
out->opcode = OPCODE_INVALID;
out->line_type = LINE_INVALID;
buffer = malloc_allocation(MAX_LINE_LENGTH);
/* Copy line to buffer for tokenization */
strncpy(buffer, line, MAX_LINE_LENGTH - 1);
buffer[MAX_LINE_LENGTH - 1] = '\0';
cleaned_buffer = cut_spaces_before_and_after_string(buffer);
/*check for invalid comma*/
if(comma_validation(cleaned_buffer,file_name,line_number)){
return 0;
}
token = strtok(cleaned_buffer, " \t\n");
/* Handle Label */
if (token[strlen(token) - 1] == DOUBLE_DOTS) {
token[strlen(token) - 1] = '\0'; /* Remove ':' */
/*checking label validity*/
if (!is_valid_label(token, file_name, line_number)) {
return 0;
}
if (identify_opcode(token) != OPCODE_INVALID || identify_directive(token) != -1) {
error_log(file_name, line_number, SAVED_WORD_AS_LABEL);
return 0;
} else if (identify_register(token) != -1) {
error_log(file_name, line_number, REGISTER_NAME_AS_LABEL);
return 0;
}
strncpy(out->label, token, MAX_LABEL_LEN);
out->label[MAX_LABEL_LEN] = '\0';
token = strtok(NULL, " \t\n");
if (token == NULL) {
error_log(file_name, line_number, EMPTY_AFTER_LABEL);
return 0;
}
}
/* Process directive or instruction */
if (token[0] == '.') {
/* Handle directive */
int directive_identifier = identify_directive(token);
if (directive_identifier == -1) {
error_log(file_name, line_number, ILLEGAL_DIRECTIVE);
return 0;
}
out->line_type = LINE_DIRECTIVE;
copy_directive_name(token, out->directive_name);
return parse_directive_line(line, out, file_name, line_number);
} else {
/* Handle instruction */
Opcode opcode_identifier = identify_opcode(token);
if (opcode_identifier == OPCODE_INVALID) {
error_log(file_name, line_number, INVALID_OPCODE);
return 0;
}
out->line_type = LINE_INSTRUCTION;
out->opcode = opcode_identifier;
/* Handle instructions with no operands */
if (opcode_identifier == OPCODE_STOP || opcode_identifier == OPCODE_RTS) {
token = strtok(NULL, "\n");
if (token != NULL) {
token = cut_spaces_before_and_after_string(token);
if (token[0] != '\0') {
error_log(file_name, line_number, EXTRANEOUS_TEXT_AFTER_COMMAND);
return 0;
}
}
return 1;
}
/* Process remaining line for operands */
dynamic_operands_pointer = strstr(line, token);
if (dynamic_operands_pointer == NULL) return 1;
dynamic_operands_pointer += strlen(token);
dynamic_operands_pointer = cut_spaces_before_and_after_string(dynamic_operands_pointer);
/* Parse operands */
if (dynamic_operands_pointer[0] == '\0') {
/* No operands found */
error_log(file_name,line_number,MISSING_INSTRUCTION_ARG);
out->operand_count = 0;
return 0;
} else {
if (!parse_instruction_operands(dynamic_operands_pointer, out, file_name, line_number)) {
return 0;
}
}
/* validate operand count match operand name*/
expected = expected_operands_for_each_opcode[out->opcode];
if (out->operand_count != expected) {
error_log(file_name, line_number, INVALID_OPERANDS_COUNT);
return 0;
}
/* Verify addressing modes */
if (!verify_addressing_modes_are_valid(out, file_name, line_number)) {
return 0;
}
}
return 1;
}